-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm-writer.lisp
62 lines (52 loc) · 1.54 KB
/
vm-writer.lisp
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
(in-package #:compiler)
(defun write-push (segment index)
"Writes a VM push command."
(if segment
(concatenate 'string "push " segment " " (write-to-string index))
(concatenate 'string "push " (write-to-string index))))
(defun write-pop (segment index)
"Writes a VM pop command."
(concatenate 'string "pop " segment " " (write-to-string index)))
(defun write-arithmetic (command)
"Writes a VM arithmetic command."
(cond ((string= #\+ command)
"add")
((string= #\- command)
"sub")
((string= #\= command)
"eq")
((string= #\> command)
"gt")
((string= #\< command)
"lt")
((string= #\& command)
"and")
((string= #\| command)
"or")
((string= #\* command)
(write-call "Math.multiply" 2))
((string= #\/ command)
(write-call "Math.divide" 2))
((or (string= #\~ command) (string= "not" command))
"not")
((string= "neg" command)
"neg")
(t (error "Arithmetic command not recognized."))))
(defun write-label (label)
"Writes a VM label command."
(concatenate 'string "label " label))
(defun write-goto (label)
"Writes a VM goto command."
(concatenate 'string "goto " label))
(defun write-if (label)
"Writes a VM if-goto command."
(concatenate 'string "if-goto " label))
(defun write-call (name nargs)
"Writes a VM call command."
(concatenate 'string "call " name " " (write-to-string nargs)))
(defun write-function (name nlocals)
"Writes a VM function command."
(concatenate 'string "function " name " " (write-to-string nlocals)))
(defun write-return ()
"Writes a VM return command."
"return")