-
Notifications
You must be signed in to change notification settings - Fork 9
/
ante.swift
executable file
·276 lines (242 loc) · 8.62 KB
/
ante.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
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
#!/usr/bin/env xcrun swift
//
// Copyright (c) 2014-2015 Michael Dvorkin
// Ante is an esoteric programming language where all you've got is a deck of cards.
//
// This is Ante implementation in Swift.
import Darwin // Posix exit()
import Foundation
typealias BigInt = Int64 // Pending arbitrary arithmetic BigInt implementation.
extension Int {
// Returns a character from its integer ordinal.
var chr: Character {
return Character(UnicodeScalar(self))
}
}
extension Character {
// Returns integer ordinal of the character.
var ord: UInt {
for s in String(self).unicodeScalars {
return UInt(s.value)
}
return 0
}
}
extension String {
subscript (i: Int) -> String {
return String(Array(self)[i])
}
var size: Int {
return countElements(self)
}
func split() -> Array<String> {
return self.componentsSeparatedByString("\n")
}
func strip() -> String {
return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
func repeat(n: Int) -> String {
return stringByPaddingToLength(n * self.size, withString: self, startingAtIndex: 0)
}
func scan(pattern: String) -> [[String]] {
var captures: [[String]] = []
let regex = NSRegularExpression(pattern: pattern, options: nil, error: nil)
regex!.enumerateMatchesInString(self, options: nil, range: NSMakeRange(0, self.size)) {
(match: NSTextCheckingResult!, _, _) in
var group: [String] = []
for i in 1..<match!.numberOfRanges {
group.append((self as NSString).substringWithRange(match!.rangeAtIndex(i)))
}
captures.append(group)
}
return captures
}
func gsub(pattern: String, substitute: String) -> String {
return self.stringByReplacingOccurrencesOfString(pattern, withString: substitute, options: .RegularExpressionSearch)
}
}
let ACE = Character("A").ord
let KING = Character("K").ord
let QUEEN = Character("Q").ord
let JACK = Character("J").ord
let DIAMONDS = Character("♦").ord
let HEARTS = Character("♥").ord
let SPADES = Character("♠").ord
let CLUBS = Character("♣").ord
struct Card {
var rank: UInt
var suit: UInt
}
// Suppose you went back to Ada Lovelace and asked her the difference between
// a script and a program. She'd probably look at you funny, then say something
// like: Well, a script is what you give the actors, but a program is what you
// give the audience. That Ada was one sharp lady... -- Larry Wall
class Ante {
var pc: Int = 0 // Program counter (index within ante.code)
var line: UInt = 0 // Current line number.
var code: [Card] = [] // Array of cards.
var vars: Dictionary<UInt, BigInt> = [:] // Four registers hashed by suit.
var labels: Dictionary<UInt, Int> = [:] // Labels for ante.pc to jump to.
var buffer: [UInt8] = [] // Buffer to collect UTF-8 character bytes.
init() {
self.vars[DIAMONDS] = 0
self.vars[HEARTS] = 0
self.vars[SPADES] = 0
self.vars[CLUBS] = 0
}
func run(fileName: String) {
var error: NSError?
let program = NSString(contentsOfFile: fileName, encoding: NSUTF8StringEncoding, error: &error)
if error != nil {
self.exception("\(error!.localizedDescription)")
}
parse(program!)
while self.pc < self.code.count {
let card = self.code[self.pc]
self.pc++
switch card.rank {
case 0:
newline(card)
case 10:
dump(card, asCharacter: false)
case JACK:
dump(card, asCharacter: true)
case QUEEN:
continue
case KING:
jump(card)
default:
assign(card)
}
}
}
func parse(program: String) {
let lines = program.split().map {
($0 as String).gsub("#.*$", substitute: "").strip()
}
// Parse lines to turn them into array of cards.
for (i, line) in enumerate(lines) {
// Line number cards have zero rank.
self.code.append(Card(rank: 0, suit: i + 1))
for caps in line.scan("(10|[2-9JQKA])([♦♥♠♣])") {
let (rank, suit) = (Character(caps[0][0]), Character(caps[1][0]))
switch rank {
case "1":
code.append(Card(rank: 10, suit: suit.ord))
case "2"..."9":
code.append(Card(rank: rank.ord - 48, suit: suit.ord))
default:
code.append(Card(rank: rank.ord, suit: suit.ord))
}
}
}
// Extra pass to extract labels.
var pc = 0
while pc < self.code.count - 1 {
let card = self.code[pc++]
if card.rank == QUEEN {
var queen = card.suit
while pc < self.code.count && self.code[pc].rank == QUEEN && self.code[pc].suit == card.suit {
queen += card.suit
pc++
}
self.labels[queen] = pc
}
}
}
func newline(card: Card) {
self.line = card.suit
}
func dump(card: Card, asCharacter: Bool) {
let value = self.vars[card.suit]!
if asCharacter {
if value >= 0 && value <= 255 {
// Collect the bytes till we have full UTF-8 character.
// Once the character is built dump it and reset the buffer.
self.buffer.append(UInt8(value))
let utf8 = NSString(bytes: self.buffer, length: self.buffer.count, encoding: NSUTF8StringEncoding)
if utf8 != nil {
print(utf8!)
self.buffer = []
}
} else {
exception("character code \(value) is not in 0..255 range")
}
} else {
print(value)
}
}
func jump(card: Card) {
var suit = card.suit
while self.pc < self.code.count && self.code[self.pc].rank == KING && self.code[self.pc].suit == card.suit {
suit += card.suit
self.pc++
}
if self.vars[card.suit] != 0 {
if self.labels[suit] != nil {
self.pc = self.labels[suit]!
} else {
let label = "Q\(Int(card.suit).chr)".repeat(Int(suit / card.suit))
exception("can't find \(label) to go to")
}
}
}
func assign(card: Card) {
let operands = remaining(card)
return expression(operands)
}
func remaining(card: Card) -> [Card] {
var operands = [card]
while self.pc < self.code.count {
let card = self.code[self.pc]
if card.rank == 0 || card.rank == KING || card.rank == QUEEN || card.rank == JACK {
break
}
operands.append(card)
self.pc++
}
return operands
}
func expression(operands: [Card]) {
var initial = BigInt(operands[0].rank)
let target = operands[0].suit
if initial == BigInt(ACE) {
initial = self.vars[target]!
}
for i in 1..<operands.count {
var rank = BigInt(operands[i].rank)
let suit = operands[i].suit
if rank == BigInt(ACE) {
rank = self.vars[suit]!
}
// Pending arbitrary arithmetic BigInt implementation we use no
// overflow operators to be able to trap exceptions gracefully.
switch suit {
case DIAMONDS:
initial = initial &+ rank
case HEARTS:
initial = initial &* rank
case SPADES:
initial = initial &- rank
case CLUBS:
if rank != 0 {
initial = initial &/ rank
} else {
exception("division by zero")
}
default:
continue
}
}
self.vars[target] = initial
}
func exception(message: String) {
println("Ante exception: \(message) on line \(self.line) (pc:\(self.pc))")
exit(0)
}
}
if Process.arguments.count == 2 {
Ante().run(Process.arguments[1])
} else {
println("usage: ante.swift filename.ante");
}