-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.v
330 lines (299 loc) · 6.57 KB
/
main.v
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import readline
import term
/* $if !x64 {
$compile_error('Program is only compatible with x86_64 CPUs')
} */
enum Op { eof uneg ident assign obr cbr num add sub mul div mod }
struct Lexer {
line string
mut:
pos int
tok Op
tok_lit string
peek Op
peek_lit string
}
fn (mut l Lexer) get() !(Op, string) {
for l.pos < l.line.len {
mut ch := l.line[l.pos]
l.pos++
if ch.is_space() { continue }
mut word := ''
op := match ch {
`+` { Op.add }
`-` { Op.sub }
`*` { Op.mul }
`/` { Op.div }
`%` { Op.mod }
`=` { Op.assign }
`(` { Op.obr }
`)` { Op.cbr }
else {
mut isnum := true
l.pos--
start := l.pos
ch = l.line[l.pos]
if !((ch >= `a` && ch <= `z`) || (ch >= `A` && ch <= `Z`) || (ch >= `0` && ch <= `9`) || ch == `_`) {
return error("Syntax Error: unknown character `${ch.ascii_str()}`")
}
for l.pos < l.line.len {
ch = l.line[l.pos]
if (ch >= `a` && ch <= `z`) || (ch >= `A` && ch <= `Z`) || (ch >= `0` && ch <= `9`) || ch == `_` {
l.pos++
if isnum && !(ch >= `0` && ch <= `9`) {
isnum = false
}
continue
}
break
}
word = l.line[start..l.pos]
if isnum {
Op.num
} else {
Op.ident
}
}
}
return op, word
}
return Op.eof, ''
}
fn (mut l Lexer) next() !Op {
l.tok, l.tok_lit = l.peek, l.peek_lit
l.peek, l.peek_lit = l.get()!
return l.tok
}
struct Expr {
lhs &Expr = unsafe { nil }
rhs &Expr = unsafe { nil }
op Op
val string
}
fn expr(mut l Lexer, min_bp int) !&Expr {
l.next()!
mut lhs := match l.tok {
.num, .ident {
&Expr{op: l.tok, val: l.tok_lit}
}
.obr {
o_lhs := expr(mut l, 0)!
if l.next()! != .cbr {
return error("Syntax Error: expected closing brace")
}
o_lhs
}
else {
match l.tok {
.sub {
r_bp := 7
&Expr{
rhs: expr(mut l, r_bp)!,
op: .uneg
}
}
else {
return error("Syntax Error: expected identifier or number")
}
}
}
}
for {
if l.peek in [.eof, .obr, .cbr] {
break
}
l_bp, r_bp := match l.peek {
.assign {
if lhs.op != .ident {
return error("Syntax Error: cannot assign to a value literal")
}
2, 1
}
.add, .sub { 3, 4 }
.mul, .div, .mod { 5, 6 }
else {
return error("Syntax Error: expected operator after value literal")
}
}
if l_bp < min_bp {
break
}
l.next()!
op := l.tok
lhs = &Expr{
lhs: lhs,
rhs: expr(mut l, r_bp)!,
op: op
}
}
return lhs
}
fn march(node &Expr, dep int) {
if isnil(node) {
return
}
if dep != 0 {
c := dep - 1
print("${` `.repeat(c * 3)}└─ ")
}
match node.op {
.num, .ident {
println("`${node.val}`")
}
else {
println(node.op)
}
}
march(node.lhs, dep + 1)
march(node.rhs, dep + 1)
}
struct Box[T] {
v T
}
type Symtable = map[string]&Box[i64]
fn gen(node &Expr, mut prg JitProgram, mut symtable Symtable)! {
match node.op {
.num {
prg.mov64_rax(node.val.i64())
return
}
.assign {
gen(node.rhs, mut prg, mut symtable)!
if node.lhs.val !in symtable {
symtable[node.lhs.val] = &Box[i64]{}
}
prg.mov64_rcx(voidptr(&symtable[node.lhs.val].v))
prg.comments.last().comment = 'mov rcx, &${node.lhs.val}'
prg.comment('mov [rcx], rax')
prg.code << [u8(0x48), 0x89, 0x01]
return
}
.ident {
if node.val !in symtable {
return error("Gen: identifier `${node.val}` not defined")
}
prg.mov64_rax(voidptr(&symtable[node.val].v))
prg.comments.last().comment = 'mov rax, &${node.val}'
prg.comment("mov rax, [rax]")
prg.code << [u8(0x48), 0x8B, 0x00]
return
}
else {}
}
gen(node.rhs, mut prg, mut symtable)!
if node.op == .uneg {
prg.comment('neg rax')
prg.code << [u8(0x48), 0xF7, 0xD8]
return
}
prg.comment('push rax')
prg.code << 0x50
gen(node.lhs, mut prg, mut symtable)!
prg.comment('pop rcx')
prg.code << 0x59
match node.op {
.add {
prg.comment('add rax, rcx')
prg.code << [u8(0x48), 0x01, 0xC8]
}
.sub {
prg.comment('sub rax, rcx')
prg.code << [u8(0x48), 0x29, 0xC8]
}
.mul {
prg.comment('imul rax, rcx')
prg.code << [u8(0x48), 0x0F, 0xAF, 0xC1]
}
.div, .mod {
prg.comment('cqo')
prg.code << [u8(0x48), 0x99]
prg.comment('idiv rcx')
prg.code << [u8(0x48), 0xF7, 0xF9]
if node.op == .mod {
prg.comment('mov rax, rdx')
prg.code << [u8(0x48), 0x89, 0xD0]
}
}
else {
panic("unreachable")
}
}
}
fn eval(node &Expr, mut symtable map[string]i64) i64 {
return match node.op {
.ident { symtable[node.val] }
.num { node.val.i64() }
.uneg { -eval(node.rhs, mut symtable) }
.add { eval(node.lhs, mut symtable) + eval(node.rhs, mut symtable) }
.sub { eval(node.lhs, mut symtable) - eval(node.rhs, mut symtable) }
.mul { eval(node.lhs, mut symtable) * eval(node.rhs, mut symtable) }
.div { eval(node.lhs, mut symtable) / eval(node.rhs, mut symtable) }
.mod { eval(node.lhs, mut symtable) % eval(node.rhs, mut symtable) }
.assign {
symtable[node.lhs.val] = eval(node.rhs, mut symtable)
symtable[node.lhs.val]
}
else { panic("unreachable") }
}
}
fn main() {
mut r := readline.Readline{}
mut prg := create_program()
mut symtable := map[string]&Box[i64]
mut showast := false
println("jitcalc Copyright (C) 2022 l-m.dev")
println(term.magenta("Type `help` for more information.\n"))
for {
line := r.read_line(">>> ") or {
break
}
match line.trim_space() {
'clear' { term.clear() continue }
'reset' { symtable.clear() continue }
'ast' { showast = !showast continue }
'exit' { break }
'help' {
println(term.magenta(" clear | Clear the screen."))
println(term.magenta(" reset | Undeclare all variables."))
println(term.magenta(" ast | Toggle printing AST representation."))
println(term.magenta(" help | Show this message."))
println(term.magenta(" exit | Goodbye!"))
continue
}
else {}
}
mut l := Lexer {
line: line
}
l.next()!
if l.peek == .eof {
continue
}
root := expr(mut l, 0) or {
println(term.fail_message(err.str()))
continue
}
if l.peek != .eof {
println(term.fail_message("Syntax Error: trailing tokens after expression"))
continue
}
gen(root, mut prg, mut symtable) or {
println(term.fail_message(err.str()))
prg.reset()
continue
}
if showast {
march(root, 0)
println(term.cyan('------------------------------------------------------------'))
}
prg.ret()
prg.hexdump()
fnptr := prg.finalise()
value := fnptr()
if root.op != .assign {
println("${term.bold(term.green(value.str()))}")
}
prg.reset()
}
println("goodbye!")
}