forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluationContext.swift
More file actions
558 lines (517 loc) · 18.5 KB
/
EvaluationContext.swift
File metadata and controls
558 lines (517 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//
// EvaluationContext.swift
// ShapeScript
//
// Created by Nick Lockwood on 18/12/2018.
// Copyright © 2018 Nick Lockwood. All rights reserved.
//
import Euclid
import Foundation
#if canImport(CoreGraphics)
import CoreGraphics
#endif
#if canImport(CoreText)
import CoreText
#endif
#if canImport(SceneKit)
import SceneKit
#endif
// MARK: Implementation
public struct SourceLocation: Hashable {
public let line: Int
public let file: URL?
public init(at line: Int, in file: URL?) {
self.line = line
self.file = file
}
public func range(in source: String) -> SourceRange {
source.range(ofLine: line)
}
}
final class BreakpointManager {
private var breakpointIndex: Int = 0
func next() -> Int {
defer { breakpointIndex += 1 }
return breakpointIndex
}
}
final class EvaluationContext {
private final class ImportCache {
enum Import {
case program(Program)
case geometry(Geometry)
case value(Value)
}
var fonts: [String] {
store.values.compactMap {
switch $0 {
case let .value(.font(name)): name
default: nil
}
}
}
var store = [URL: Import]()
}
private weak var delegate: EvaluationDelegate?
private var symbols: Symbols = .root
var userSymbols: Symbols = [:]
var options: Options = [:]
private let importCache: ImportCache
private var importStack: [URL]
let isCancelled: Mesh.CancellationHandler
var source: String
var sourceIndex: String.Index?
var baseURL: URL?
var material: Material = .default
var background: MaterialProperty?
var transform = Transform.identity
var childTransform = Transform.identity
var childTypes: ValueType = .mesh
var name: String = ""
var namedObjects: [String: Geometry] = [:]
var children = [Value]() {
didSet {
for case let .mesh(geometry) in children {
geometry.gatherNamedObjects(&namedObjects)
}
}
}
var random: RandomSequence
var breakpoints: BreakpointManager
var detail: Int = 16
var smoothing: Angle?
var font: String = ""
var opacity: Double = 1
var stackDepth = 1
var isFunctionScope = false
var sourceLocation: () -> SourceLocation? {
{ [sourceIndex, source, baseURL] in
sourceIndex.map {
SourceLocation(at: source.line(at: $0), in: baseURL)
}
}
}
init(
source: String,
delegate: EvaluationDelegate?,
isCancelled: @escaping () -> Bool = { false }
) {
self.source = source
self.delegate = delegate
self.isCancelled = isCancelled
self.importCache = ImportCache()
self.importStack = []
self.random = RandomSequence(seed: 0)
self.breakpoints = BreakpointManager()
}
private init(parent: EvaluationContext) {
// preserve
self.source = parent.source
self.sourceIndex = parent.sourceIndex
self.baseURL = parent.baseURL
self.delegate = parent.delegate
self.isCancelled = parent.isCancelled
self.symbols = parent.symbols
self.userSymbols = parent.userSymbols
self.importCache = parent.importCache
self.importStack = parent.importStack
self.material = parent.material
self.childTypes = parent.childTypes
self.namedObjects = parent.namedObjects
self.random = parent.random
self.breakpoints = parent.breakpoints
self.detail = parent.detail
self.smoothing = parent.smoothing
self.font = parent.font
// root-only
self.background = parent.background
// opacity is cumulative
self.opacity = parent.material.opacity?.color?.a ?? parent.opacity
// reset
self.transform = .identity
self.childTransform = .identity
self.children = []
// stack
self.stackDepth = parent.stackDepth + 1
}
func push(_ type: BlockType) -> EvaluationContext {
let new = EvaluationContext(parent: self)
new.childTypes = type.childTypes
new.symbols = type.symbols
new.options = type.options
for (name, symbol) in type.symbols where Symbols.global[name] == nil {
if case .placeholder = symbol {
continue
}
new.userSymbols[name] = nil
}
return new
}
func pushScope(_ block: (EvaluationContext) throws -> Void) rethrows {
let oldSourceIndex = sourceIndex
let oldSymbols = userSymbols
defer {
sourceIndex = oldSourceIndex
userSymbols = oldSymbols.merging(userSymbols.filter {
if case .option = $0.value, options[$0.key] != nil {
return true
}
return false
}) { $1 }
}
try block(self)
}
func pushDefinition() -> EvaluationContext {
let new = EvaluationContext(parent: self)
new.name = name
new.namedObjects = namedObjects
new.transform = transform
new.opacity = opacity
new.childTypes = ValueType.any
new.symbols = .definition
return new
}
}
extension EvaluationContext {
static let altNames = ["colour": "color", "centre": "center", "grey": "gray"]
func symbol(for name: String) -> Symbol? {
if let symbol = userSymbols[name] ?? symbols[name] ?? Symbols.global[name] {
return symbol
}
if let name = Self.altNames[name] {
return symbol(for: name)
}
return nil
}
func define(_ name: String, as symbol: Symbol?) {
userSymbols[name] = symbol
}
var allSymbols: Symbols {
Symbols.global.merging(symbols) { $1 }.merging(userSymbols) { $1 }
}
/// Symbols that can be used in an expression (i.e. that return a value)
var expressionSymbols: [String] {
Array(allSymbols.filter {
switch $1 {
case let .function(type, _) where type.returnType == .void:
return false
case .function, .property, .block, .constant, .option, .placeholder:
return true
}
}.keys)
}
/// Symbols that can be used as a command (i.e. that accept an argument)
var commandSymbols: [String] {
Array(allSymbols.filter {
switch $1 {
case .function, .property, .block, .placeholder:
return true
case .constant, .option:
return false
}
}.keys) + Keyword.allCases.map(\.rawValue)
}
/// Return the value of the specified symbol in the current context
func value(for name: String) -> Value? {
switch symbol(for: name) {
case let .constant(value), let .option(value):
return value
case .function, .property, .block, .placeholder, nil:
return nil
}
}
}
// MARK: Debugging
extension EvaluationContext {
func debugLog(_ values: [AnyHashable]) {
delegate?.debugLog(values)
}
func hitBreakpoint() throws {
let index = breakpoints.next()
if delegate?.shouldPauseAtBreakpoint(index) == true {
throw RuntimeErrorType.breakpoint(index)
}
}
}
// MARK: External file access
extension EvaluationContext {
private func isUndownloadedUbiquitousFile(_ url: URL) -> Bool {
#if os(macOS) || os(iOS)
return (try? url.resourceValues(forKeys: [
.ubiquitousItemDownloadingStatusKey,
]).ubiquitousItemDownloadingStatus) == .notDownloaded
#else
return false
#endif
}
func resolveURL(for path: String) throws -> URL {
let path = path.trimmingCharacters(in: .whitespacesAndNewlines)
guard !path.isEmpty else {
// TODO: should this be a different error type, like "empty path not allowed"?
throw RuntimeErrorType.fileNotFound(for: path, at: nil)
}
let documentRelativePath = baseURL.map {
URL(fileURLWithPath: path, relativeTo: $0).path
} ?? path
guard let url = delegate?.resolveURL(for: documentRelativePath) else {
// TODO: should this be a different error type, like "delegate not available"?
throw RuntimeErrorType.fileNotFound(for: path, at: nil)
}
let fileManager = FileManager.default
// try? fileManager.evictUbiquitousItem(at: url) // Handy for testing
// TODO: move this logic out of EvaluationContext into delegate
// so we can more easily mock the filesystem for testing purposes
let isRemote = isUndownloadedUbiquitousFile(url)
#if targetEnvironment(simulator) || !os(iOS)
// macOS/Linux/Windows can check for existence of files even without access permission
guard isRemote || fileManager.fileExists(atPath: url.path) else {
throw RuntimeErrorType.fileNotFound(for: path, at: url)
}
#endif
let directory = url.deletingLastPathComponent()
guard isRemote || fileManager.isReadableFile(atPath: url.path) else {
throw RuntimeErrorType.fileAccessRestricted(for: path, at: directory)
}
guard isRemote || fileManager.fileExists(atPath: url.path) else {
throw RuntimeErrorType.fileNotFound(for: path, at: url)
}
#if os(macOS) || os(iOS)
// TODO: Make this interface asynchronous instead of blocking
if isRemote {
try FileManager.default.startDownloadingUbiquitousItem(at: url)
var url = url
let start = CFAbsoluteTimeGetCurrent()
let timeout: TimeInterval = 30
while isUndownloadedUbiquitousFile(url), !isCancelled() {
if CFAbsoluteTimeGetCurrent() - start > timeout {
throw RuntimeErrorType.fileTimedOut(for: path, at: url)
}
Thread.sleep(forTimeInterval: 0.1)
url.removeAllCachedResourceValues()
}
}
#endif
return url
}
var fontNames: [String] {
var options = [String]()
#if canImport(CoreGraphics) && canImport(CoreText)
options += CTFontManagerCopyAvailablePostScriptNames() as? [String] ?? []
options += CTFontManagerCopyAvailableFontFamilyNames() as? [String] ?? []
options = options.map { CGFont($0 as CFString)?.fullName as String? ?? $0 } + importCache.fonts
#endif
return Array(Set(options)).sorted()
}
func resolveFont(_ name: String) throws -> String {
let name = name
.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: "\t", with: " ")
.replacingOccurrences(of: " ", with: " ")
#if canImport(CoreGraphics) && canImport(CoreText)
guard [".otf", ".ttf", ".ttc"].contains(where: {
name.lowercased().hasSuffix($0)
}) else {
if importCache.fonts.contains(name) {
return name
}
let font = CTFontCreateWithName(name as CFString, 1, nil)
let fullName = CTFontCopyFullName(font) as String
// CTFontCreateWithName always returns a value even for an empty string
// so use a basic heuristic to make sure we got the font that we requested
let lowercasedFullName = fullName.lowercased()
let lowercasedName = name.lowercased()
guard lowercasedFullName == lowercasedName ||
lowercasedFullName.hasPrefix("\(lowercasedName) ")
else {
throw RuntimeErrorType.unknownFont(name, options: fontNames)
}
return fullName
}
let url = try resolveURL(for: name)
if case let .value(.font(fullName)) = importCache.store[url] {
return fullName
}
guard let dataProvider = CGDataProvider(url: url as CFURL) else {
throw RuntimeErrorType.fileNotFound(for: name, at: url)
}
guard let cgFont = CGFont(dataProvider), let fullName = cgFont.fullName as? String,
CGFont(fullName as CFString) != nil || CTFontManagerRegisterGraphicsFont(cgFont, nil)
else {
throw RuntimeErrorType.fileParsingError(for: name, at: url, message: "")
}
importCache.store[url] = .value(.font(fullName))
return fullName
#else
return name
#endif
}
func importGeometry(at url: URL) throws -> Geometry? {
if let geometry = try delegate?.importGeometry(for: url) {
// Allow delegate to implement alternative loading mechanism, e.g. hard-coded geometry for testing
return geometry
}
switch url.pathExtension.lowercased() {
case "stl", "stla", "obj", "off":
let mesh = try Mesh(url: url) {
switch $0 {
case let color as Color:
return Material(color: color)
#if canImport(SceneKit)
case let scnMaterial as SCNMaterial:
return Material(scnMaterial)
#endif
default:
return nil
}
}
return Geometry(
type: .mesh(mesh),
name: nil,
transform: .identity,
material: .default,
smoothing: nil,
children: [],
sourceLocation: nil
)
default:
break
}
var isDirectory: ObjCBool = false
_ = FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory)
var url = url
if isDirectory.boolValue {
let newURL = url.appendingPathComponent(url.lastPathComponent)
if FileManager.default.fileExists(atPath: newURL.path) {
url = newURL
}
}
#if canImport(SceneKit)
let scene = try SCNScene(url: url, options: [
.flattenScene: false,
.createNormalsIfAbsent: true,
.convertToYUp: true,
.preserveOriginalTopology: true,
])
return try Geometry(scene.rootNode)
#else
return nil
#endif
}
func importFile(at path: String) throws -> Value {
let url = try resolveURL(for: path)
switch url.pathExtension.lowercased() {
case "shape":
if importStack.contains(url) {
throw RuntimeErrorType.circularImport(for: url)
}
let program: Program
if case let .program(entry) = importCache.store[url] {
program = entry
} else {
let source: String
do {
source = try String(contentsOf: url)
} catch {
throw RuntimeErrorType.fileParsingError(
for: path,
at: url,
message: error.localizedDescription
)
}
do {
// TODO: async source loading?
program = try parse(source, at: url)
importCache.store[url] = .program(program)
} catch {
throw RuntimeErrorType.importError(
ProgramError(error),
for: url,
in: source
)
}
}
importStack.append(url)
defer {
importStack.removeLast()
}
do {
try program.evaluate(in: self)
return .void
} catch {
throw RuntimeErrorType.importError(
ProgramError(error),
for: url,
in: program.source
)
}
case "txt":
if case let .value(value) = importCache.store[url] {
return value
}
do {
let value = try Value.string(String(contentsOf: url))
importCache.store[url] = .value(value)
return value
} catch {
throw RuntimeErrorType.fileParsingError(
for: path,
at: url,
message: error.localizedDescription
)
}
case "json":
if case let .value(value) = importCache.store[url] {
return value
}
do {
let value = try Value(jsonData: Data(contentsOf: url))
importCache.store[url] = .value(value)
return value
} catch let error as ParserError {
let source = try String(contentsOf: url)
throw RuntimeErrorType.importError(
.parserError(error),
for: url,
in: source
)
} catch {
throw RuntimeErrorType.fileParsingError(
for: path,
at: url,
message: error.localizedDescription
)
}
default:
do {
let geometry: Geometry
if case let .geometry(entry) = importCache.store[url] {
geometry = entry
} else if let entry = try importGeometry(at: url) {
geometry = entry
importCache.store[url] = .geometry(geometry)
} else {
throw RuntimeErrorType.fileTypeMismatch(
for: path,
at: url,
expected: nil
)
}
return .mesh(geometry.with(
transform: .identity,
material: material,
smoothing: smoothing,
sourceLocation: sourceLocation
))
} catch let error as ProgramError {
guard let source = try? String(contentsOf: url) else {
throw RuntimeErrorType.fileParsingError(
for: path, at: url, message: error.message
)
}
throw RuntimeErrorType.importError(error, for: url, in: source)
} catch {
throw RuntimeErrorType.fileError(error, for: path, at: url)
}
}
}
}