-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathContents.swift
38 lines (31 loc) · 818 Bytes
/
Contents.swift
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
//
// Sources / References:
//
// Joe Groff
// - https://twitter.com/jckarter/status/1093180314526965760
// - https://gist.github.com/jckarter/f56f6244aef46bfe25ccf34d2750412b#file-basic-swift-L15
//
infix operator ⎵
var program: [Int: () -> ()] = [:]
func ⎵(lineNumber: Int, code: @autoclosure @escaping () -> ()) {
program[lineNumber] = code
}
var currentLine = 0
var run: Void {
if program.isEmpty { return }
let lines = program.keys.sorted()
currentLine = lines.first!
while true {
let line = currentLine
currentLine += 1
program[line]!()
guard let nextLine = lines.first(where: { $0 >= currentLine }) else { return }
currentLine = nextLine
}
}
func goto(_ line: Int) {
currentLine = line
}
10⎵print("hello world")
20⎵goto(10)
run