-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawlang.pony
265 lines (252 loc) · 6.31 KB
/
drawlang.pony
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
use "collections"
use "debug"
use "random"
use "time"
primitive Test
fun is_num(token: String): Bool =>
if token == "-" then
return false
end
for c in token.values() do
if ((c != '.') and (c != '-')) and ((c < '0') or (c > '9')) then
return false
end
end
true
primitive _StopProgram
class DrawLang
let stack: Array[F64] = Array[F64]
let return_stack: Array[USize] = Array[USize]
let marks: Map[String, USize] = Map[String, USize]
let image: SVGNode = SVG.svg()
let random: Rand = Rand(Time.millis())
fun ref next(token: String, cur: USize): (USize | _StopProgram) =>
if token == "" then
None
elseif Test.is_num(token) then
stack.push(token.f64())
// STACK
elseif token == "drop" then
try
stack.pop()?
else
Debug("drop: not enough items on stack")
end
elseif token == "swap" then
try
let x = stack.pop()?
let y = stack.pop()?
stack.>push(x).>push(y)
else
Debug("swap: not enough items on stack")
end
elseif token == "dup" then
try
let x = stack.pop()?
stack.>push(x).>push(x)
else
Debug("dup: not enough items on stack")
end
elseif token == "rot" then
try
let x = stack.pop()?
let y = stack.pop()?
let z = stack.pop()?
stack.>push(y).>push(x).>push(z)
else
Debug("rot: not enough items on stack")
end
elseif token == "pick" then
try
let i = stack.pop()?
stack.push(stack(stack.size() - i.usize() - 1)?)
else
Debug("pick: not enough items on stack")
end
elseif token == "roll" then
try
let i = stack.pop()?
stack.push(stack(stack.size() - i.usize() - 1)?)
stack.remove(stack.size() - i.usize() - 2, 1)
else
Debug("roll: not enough items on stack")
end
//
// MATH
//
elseif token == "+" then
try
stack.push(stack.pop()? + stack.pop()?)
else
Debug("+: not enough items on stack")
end
elseif token == "-" then
try
let y = stack.pop()?
let x = stack.pop()?
stack.push(x - y)
else
Debug("-: not enough items on stack")
end
elseif token == "*" then
try
stack.push(stack.pop()? * stack.pop()?)
else
Debug("*: not enough items on stack")
end
elseif token == "/" then
try
let y = stack.pop()?
let x = stack.pop()?
stack.push(x / y)
else
Debug("/: not enough items on stack")
end
elseif token == "//" then
try
let y = stack.pop()?
let x = stack.pop()?
stack.push((x.i64() / y.i64()).f64())
else
Debug("//: not enough items on stack")
end
elseif token == "%" then
try
let y = stack.pop()?
let x = stack.pop()?
stack.push(x % y)
else
Debug("%: not enough items on stack")
end
elseif token == "sin" then
try
let x = stack.pop()?
stack.push(x.sin())
else
Debug("sin: not enough items on stack")
end
elseif token == "cos" then
try
let x = stack.pop()?
stack.push(x.cos())
else
Debug("sin: not enough items on stack")
end
elseif token == "random" then
stack.push(random.real())
elseif token == ">" then
try
let y = stack.pop()?
let x = stack.pop()?
stack.push(if (x > y) then 1 else 0 end)
end
elseif token == "<" then
try
let y = stack.pop()?
let x = stack.pop()?
stack.push(if (x < y) then 1 else 0 end)
end
elseif token == "==" then
try
let y = stack.pop()?
let x = stack.pop()?
stack.push(if (x > y) then 1 else 0 end)
end
//
// BRANCHING
//
elseif (try token(0)? else ' ' end) == '}' then
let name: String = token.substring(1)
try
if 0 != stack.pop()? then
try
let ret = marks(name)?
return_stack.push(cur)
return ret
end
end
else
Debug("branch on '" + name + "': not enough arguments")
end
elseif token == "@" then
try
return return_stack.pop()? + 1
end
elseif token == "end" then
return _StopProgram
//
// SHAPES
//
elseif token == "line" then
try
let y2 = stack.pop()?
let x2 = stack.pop()?
let y1 = stack.pop()?
let x1 = stack.pop()?
image.c(SVG.line(x1, y1, x2, y2))
else
Debug("line: not enough items on stack")
end
elseif token == "polyline" then
try
let pair_count = stack.pop()?
let pairs = Array[(F64, F64)]
for _ in Range(0, pair_count.usize()) do
let y = stack.pop()?
let x = stack.pop()?
pairs.push((x, y))
end
image.c(SVG.polyline(pairs.>reverse_in_place().values()))
else
Debug("polyline: not enough items on stack")
end
elseif token == "polygon" then
try
let pair_count = stack.pop()?
let pairs = Array[(F64, F64)]
for _ in Range(0, pair_count.usize()) do
let y = stack.pop()?
let x = stack.pop()?
pairs.push((x, y))
end
image.c(SVG.polygon(pairs.>reverse_in_place().values()))
else
Debug("polygon: not enough items on stack")
end
elseif token == "circle" then
try
let r = stack.pop()?
let y = stack.pop()?
let x = stack.pop()?
image.c(SVG.circle(x, y, r))
else
Debug("circle: not enough items on stack")
end
elseif token == "?" then
Debug(stack)
end
cur + 1
fun ref run(program: String): String =>
let tokens: Array[String] = program.split()
for (i, token) in tokens.pairs() do
try
if token(0)? == '{' then
marks(token.substring(1)) = i
end
end
end
try
var next_inst: USize = 0
while true do
match next(tokens(next_inst)?, next_inst)
| let i: USize =>
next_inst = i
if next_inst >= tokens.size() then
break
end
else
break
end
end
end
image.render()