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
|
# Integer arithmetic using conventional precedence.
#
# Follows part 2 of Jack Crenshaw's "Let's build a compiler!"
# https://compilers.iecc.com/crenshaw
#
# Limitations:
# No division yet.
#
# To build:
# $ ./translate_mu apps/arith.mu
#
# Example session:
# $ ./a.elf
# press ctrl-c or ctrl-d to exit
# > 1
# 1
# > 1+1
# 2
# > 1 + 1
# 2
# > 1+2 +3
# 6
# > 1+2 *3
# 7
# > (1+2) *3
# 9
# > 1 + 3*4
# 13
# > ^D
# $
#
# Error handling is non-existent. This is just a prototype.
fn main -> _/ebx: int {
enable-keyboard-immediate-mode
var look/esi: grapheme <- copy 0 # lookahead
var n/eax: int <- copy 0 # result of each expression
print-string 0, "press ctrl-c or ctrl-d to exit\n"
# read-eval-print loop
{
# print prompt
print-string 0, "> "
# read and eval
n, look <- simplify # we explicitly thread 'look' everywhere
# if (look == 0) break
compare look, 0
break-if-=
# print
print-int32-decimal 0, n
print-string 0, "\n"
#
loop
}
enable-keyboard-type-mode
return 0
}
fn simplify -> _/eax: int, _/esi: grapheme {
# prime the pump
var look/esi: grapheme <- get-char
# do it
var result/eax: int <- copy 0
result, look <- expression look
return result, look
}
fn expression _look: grapheme -> _/eax: int, _/esi: grapheme {
var look/esi: grapheme <- copy _look
# read arg
var result/eax: int <- copy 0
result, look <- term look
$expression:loop: {
# while next non-space char in ['+', '-']
look <- skip-spaces look
{
var continue?/eax: boolean <- is-add-or-sub? look
compare continue?, 0 # false
break-if-= $expression:loop
}
# read operator
var op/ecx: byte <- copy 0
op, look <- operator look
# read next arg
var second/edx: int <- copy 0
look <- skip-spaces look
{
var tmp/eax: int <- copy 0
tmp, look <- term look
second <- copy tmp
}
# reduce
$expression:perform-op: {
{
compare op, 0x2b # '+'
break-if-!=
result <- add second
break $expression:perform-op
}
{
compare op, 0x2d # '-'
break-if-!=
result <- subtract second
break $expression:perform-op
}
}
loop
}
look <- skip-spaces look
return result, look
}
fn term _look: grapheme -> _/eax: int, _/esi: grapheme {
var look/esi: grapheme <- copy _look
# read arg
look <- skip-spaces look
var result/eax: int <- copy 0
result, look <- factor look
$term:loop: {
# while next non-space char in ['*', '/']
look <- skip-spaces look
{
var continue?/eax: boolean <- is-mul-or-div? look
compare continue?, 0 # false
break-if-= $term:loop
}
# read operator
var op/ecx: byte <- copy 0
op, look <- operator look
# read next arg
var second/edx: int <- copy 0
look <- skip-spaces look
{
var tmp/eax: int <- copy 0
tmp, look <- factor look
second <- copy tmp
}
# reduce
$term:perform-op: {
{
compare op, 0x2a # '*'
break-if-!=
result <- multiply second
break $term:perform-op
}
#? {
#? compare op, 0x2f # '/'
#? break-if-!=
#? result <- divide second # not in Mu yet
#? break $term:perform-op
#? }
}
loop
}
return result, look
}
fn factor _look: grapheme -> _/eax: int, _/esi: grapheme {
var look/esi: grapheme <- copy _look # should be a no-op
look <- skip-spaces look
# if next char is not '(', parse a number
compare look, 0x28 # '('
{
break-if-=
var result/eax: int <- copy 0
result, look <- num look
return result, look
}
# otherwise recurse
look <- get-char # '('
var result/eax: int <- copy 0
result, look <- expression look
look <- skip-spaces look
look <- get-char # ')'
return result, look
}
fn is-mul-or-div? c: grapheme -> _/eax: boolean {
compare c, 0x2a # '*'
{
break-if-!=
return 1 # true
}
compare c, 0x2f # '/'
{
break-if-!=
return 1 # true
}
return 0 # false
}
fn is-add-or-sub? c: grapheme -> _/eax: boolean {
compare c, 0x2b # '+'
{
break-if-!=
return 1 # true
}
compare c, 0x2d # '-'
{
break-if-!=
return 1 # true
}
return 0 # false
}
fn operator _look: grapheme -> _/ecx: byte, _/esi: grapheme {
var op/ecx: byte <- copy _look
var look/esi: grapheme <- get-char
return op, look
}
fn num _look: grapheme -> _/eax: int, _/esi: grapheme {
var look/esi: grapheme <- copy _look
var result/edi: int <- copy 0
{
var first-digit/eax: int <- to-decimal-digit look
result <- copy first-digit
}
{
look <- get-char
# done?
var digit?/eax: boolean <- is-decimal-digit? look
compare digit?, 0 # false
break-if-=
# result *= 10
{
var ten/eax: int <- copy 0xa
result <- multiply ten
}
# result += digit(look)
var digit/eax: int <- to-decimal-digit look
result <- add digit
loop
}
return result, look
}
fn skip-spaces _look: grapheme -> _/esi: grapheme {
var look/esi: grapheme <- copy _look # should be a no-op
{
compare look, 0x20
break-if-!=
look <- get-char
loop
}
return look
}
fn get-char -> _/esi: grapheme {
var look/eax: grapheme <- read-key-from-real-keyboard
print-grapheme-to-real-screen look
compare look, 4
{
break-if-!=
print-string 0, "^D\n"
syscall_exit
}
return look
}
|