-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawlang.pony
182 lines (170 loc) · 4.17 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
use "collections"
use "debug"
use "random"
use "time"
primitive Test
fun is_num(token: String): Bool =>
for c in token.values() do
if (c != '.') and ((c < '0') or (c > '9')) then
return false
end
end
true
primitive _StopProgram
class DrawLang
let stack: Array[F64] = Array[F64]
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 Test.is_num(token) then
stack.push(token.f64())
// STACK
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
//
// 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 == "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
return marks.get_or_else(name, cur + 1)
end
else
Debug("branch on '" + name + "': not enough arguments")
end
//
// SHAPES
//
elseif token == "line" then
try
image.c(SVG.line(stack.pop()?,
stack.pop()?,
stack.pop()?,
stack.pop()?))
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("line: not enough items on stack")
end
elseif token == "circle" then
try
image.c(SVG.circle(stack.pop()?,
stack.pop()?,
stack.pop()?))
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()