-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpret.go
More file actions
485 lines (406 loc) · 11.9 KB
/
Copy pathinterpret.go
File metadata and controls
485 lines (406 loc) · 11.9 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package main
import (
"fmt"
"log"
"os"
)
//Call is interface for a function call
type Call struct {
Name string
Inputs [][]Token
}
//LocalVariable : map of local variable relative stack index
var LocalVariable map[string]int
//FunctionParamMap : Keeps track of how many parameter a function takes
var FunctionParamMap map[string]int
//FunctionCallStack : Keeps track of function calls
var FunctionCallStack CallStack
//EndStack helps determine what "}" will be ending. ex) conditional, function, loop
var EndStack StringStack
//Edit
//IfStack stores blocks of statements. Should be empty at the end of compilation if all blocks were closed properly
var IfStack StringStack
//Edit
//IfEndStack stores the type of block for each block. ex) block under function is typed "Function", block under if conditional it is typed "If"
var IfEndStack StringStack
//BlockCounter stores how many statement blocks there are. (forloop and if)
var BlockCounter int
//Stack index should be different each block so implement stack!
var stackindex int
//LocalVariableCountStack is a stack that holds number of local variables in a block
var LocalVariableCountStack IntStack
//VariableStack holds declared variables to assist deleting local variables in blocks
var VariableStack StringStack
var paramCount int //Keeps track of how many parameters are in a function
//BytesToInt : Convert int ascii value to int
func BytesToInt(bytes []byte) int {
//0 is 0x30
zero := 48
res := 0
for _, c := range bytes {
res = res*10 + (int(c) - zero)
}
return res
}
//NOTE: Fix Negatives
//Arithmetic : given Arithmetic tree, calculates integer value
func Arithmetic(tree *Tree) {
val := (*tree).Value[0]
if (*tree).Type != "Operator" {
if (*tree).Type == "Variable" {
//Look up stack index for the variable
offset, exist := LocalVariable[string((*tree).Value)]
if !exist {
err := fmt.Sprintf("Variable %s is not declared!", (*tree).Value)
log.Fatal(err)
}
index := (len(LocalVariable)+1)*8 - offset
code := fmt.Sprintf("movq %d(%%rbp), %%rax\n", index)
ASMWrite(code)
} else if (*tree).Type == "FunctionCall" {
var call Call
FunctionCallStack, call = FunctionCallStack.Poll()
FunctionCall(call)
} else {
s := fmt.Sprintf("movq $%s, %%rax\n", string((*tree).Value))
ASMWrite(s)
}
//Plus
} else if val == 43 {
Arithmetic(tree.Left)
ASMWrite("pushq %rax\n")
Arithmetic(tree.Right)
ASMWrite("popq %rcx\n")
ASMWrite("addq %rcx, %rax\n")
//Minus
} else if val == 45 {
Arithmetic(tree.Left)
ASMWrite("pushq %rax\n")
Arithmetic(tree.Right)
ASMWrite("popq %rcx\n")
ASMWrite("subq %rax, %rcx\n")
ASMWrite("movq %rcx, %rax\n")
//Mult
} else if val == 42 {
Arithmetic(tree.Left)
ASMWrite("pushq %rax\n")
Arithmetic(tree.Right)
ASMWrite("popq %rcx\n")
ASMWrite("mulq %rcx\n")
//Div
} else if val == 47 {
Arithmetic(tree.Left)
ASMWrite("pushq %rax\n")
Arithmetic(tree.Right)
ASMWrite("movq %rax, %rcx\n")
ASMWrite("popq %rax\n")
ASMWrite("xor %rdx, %rdx\n")
ASMWrite("divq %rcx\n")
} else {
}
}
//Declaration : traverse tree for variable declaration
func Declaration(tree *Tree, vartype string) {
if tree == nil {
return
}
termtype := (*tree).Type
switch termtype {
case "Declaration":
Declaration(tree.Right, vartype)
break
//Should check if it's function call or declaration
case "Function":
code := fmt.Sprintf("%s:\n", string((*tree).Value))
ASMWrite(code)
Declaration(tree.Right, vartype)
break
case "Assignment":
Declaration(tree.Left, vartype)
Declaration(tree.Right, vartype)
ASMWrite("pushq %rbp\n")
ASMWrite("movq %rsp, %rbp\n")
break
case "Variable":
LocalVariable[string((*tree).Value)] = stackindex
stackindex = stackindex + 8
break
case "Int":
Arithmetic(tree)
//move rbp to top of the stack again
ASMWrite("popq %rbp\n")
ASMWrite("pushq %rax\n")
break
case "Operator":
Arithmetic(tree)
//move rbp to top of the stack again
ASMWrite("popq %rbp\n")
ASMWrite("pushq %rax\n")
break
}
}
//VariableDeclaration : a helper for declaring variable
func VariableDeclaration(tokens []Token) {
variableName := string(tokens[1].Value)
newTokenList := make([]Token, 0)
//Currently just in type so just do arithmetic
for i := 3; i < len(tokens); i++ {
//If function call seen
if tokens[i].Type == "Function" {
j := i
for j < len(tokens) && string(tokens[j].Value) != ")" {
j++
}
//Make a function call
AddFunctionCallToStack(tokens[i : j+1])
tokens[i].Type = "FunctionCall"
newTokenList = append(newTokenList, tokens[i])
i = j
} else {
newTokenList = append(newTokenList, tokens[i])
}
}
TokenProcess(newTokenList)
//Increment variable counts
if LocalVariableCountStack.isEmpty() {
LocalVariableCountStack = LocalVariableCountStack.Push(1)
} else {
var count int
//Increment number of local variables in current block
LocalVariableCountStack, count = LocalVariableCountStack.Pop()
LocalVariableCountStack = LocalVariableCountStack.Push(count + 1)
}
log.Println(LocalVariableCountStack)
//Push declared variable onto the variablestack
VariableStack = VariableStack.Push(variableName)
LocalVariable[variableName] = stackindex
stackindex = stackindex + 8
ASMWrite("popq %rbp\n")
ASMWrite("pushq %rax\n")
ASMWrite("pushq %rbp\n")
ASMWrite("movq %rsp, %rbp\n")
}
//FunctionDeclaration : Writes assembly for function declaration statement
func FunctionDeclaration(tokens []Token) {
code := fmt.Sprintf("%s:\n", string(tokens[1].Value))
ASMWrite(code)
ASMWrite("pushq %rbp\n")
ASMWrite("movq %rsp, %rbp\n")
// Dec Func ( **variables** ) {
i := 3
for i < len(tokens) {
if tokens[i].Type == "Comma" {
i++
continue
}
if tokens[i].Type != "Declaration" || tokens[i+1].Type != "Variable" {
log.Fatalf("Unexpected %s: Interpret error", string(tokens[i].Value))
}
LocalVariable[string(tokens[i+1].Value)] = stackindex - 8
stackindex = stackindex + 8
paramCount++ //Increase number of parameter
i = i + 2
}
blockInit()
//Register # of parameters for the function
FunctionParamMap[string(tokens[1].Value)] = paramCount
}
//FunctionCall : Provides assembly for functioncall statement
func FunctionCall(functionCall Call) {
function := functionCall.Name
inputs := functionCall.Inputs
params := 0
for _, input := range inputs {
TokenProcess(input)
//Push the function argument to rax after processing current arg
ASMWrite("pushq %rax\n")
params++
}
ASMWrite(fmt.Sprintf("callq %s\n", function))
ASMWrite(fmt.Sprintf("addq $%d, %%rsp\n", params*8))
}
//AddFunctionCallToStack : adds function call to call stack
func AddFunctionCallToStack(tokens []Token) Call {
callInputs := make([][]Token, 0)
i := 2
j := 2
//Iterate through the input for the function call
for i < len(tokens) && string(tokens[i].Value) != ")" {
for j < len(tokens) && tokens[j].Type != "Comma" && string(tokens[j].Value) != ")" {
j++
}
//One input in the function call
input := tokens[i:j]
callInputs = append(callInputs, input)
j++
i = j
}
call := Call{
Name: string(tokens[0].Value),
Inputs: callInputs,
}
FunctionCallStack = FunctionCallStack.Push(call)
return call
}
//FunctionReturn is used to return value for function
func FunctionReturn(tokens []Token) {
newTokenList := make([]Token, 0)
//Currently just in type so just do arithmetic
for i := 0; i < len(tokens); i++ {
//If function call seen
if tokens[i].Type == "Function" {
j := i
for j < len(tokens) && string(tokens[j].Value) != ")" {
j++
}
//Make a function call
AddFunctionCallToStack(tokens[i : j+1])
tokens[i].Type = "FunctionCall"
newTokenList = append(newTokenList, tokens[i])
i = j
} else {
newTokenList = append(newTokenList, tokens[i])
}
}
TokenProcess(newTokenList[1:])
//Pop local variable, restore rsp to return address
FunctionEndRspReset()
}
func isCondOp(token Token) bool {
word := string(token.Value)
return word == "or" || word == "and" || word == ">" || word == "<" || word == "==" || word == "!="
}
//takes conditional statement and generates assembly
func conditionalHelper(tokens []Token) {
//Left and right pointer to parse portion of conditional statement
left := 0
right := 0
//general conditional statement grammar :
// {expression} {conditional operator} {expression}
// Need to check that expressions on both sides are the same type
for i, token := range tokens {
//If current token is conditional operator
if isCondOp(token) {
right = i + 1
for right < len(tokens) && !isCondOp(tokens[right]) {
right++
}
//segment of conditional expression
lhs := tokens[left:i]
rhs := tokens[i+1 : right]
op := tokens[i]
conditionalExpGen(lhs, rhs, op)
left = right
}
}
}
func conditionalExpGen(lhs []Token, rhs []Token, op Token) {
TokenProcess(lhs)
ASMWrite("movq %rax, %rbx\n")
TokenProcess(rhs)
ASMWrite("cmpq %rax, %rbx\n")
//If the condition in if statement is not met, it will jump to the "jump" block
jump := fmt.Sprintf("ifblock_%d", BlockCounter)
//do everything within the if block
// Here
////////
switch string(op.Value) {
case ">":
code := fmt.Sprintf("jle %s\n", jump)
ASMWrite(code)
case "<":
code := fmt.Sprintf("jge %s\n", jump)
ASMWrite(code)
case "==":
code := fmt.Sprintf("jne %s\n", jump)
ASMWrite(code)
case "!=":
code := fmt.Sprintf("je %s\n", jump)
ASMWrite(code)
}
//Push the jump address to the if stack
IfStack = IfStack.Push(jump)
}
//IfStatement : When the keyword IF is detected
func IfStatement(tokens []Token) {
if string(tokens[len(tokens)-1].Value) != "=>" {
log.Fatal("Expected => in if statement")
} else {
conditional := tokens[1 : len(tokens)-1]
conditionalHelper(conditional)
}
blockInit()
//Increment block counter to avoid conflict
BlockCounter++
}
//ElseStatement : When you encounter else statement
func ElseStatement(f *os.File) {
var address string
IfStack, address = IfStack.Pop()
code := fmt.Sprintf("%s:\n", address)
ASMWrite(code)
}
//IfEnd for ending an if conditional
func IfEnd() {
var address string
IfStack, address = IfStack.Pop()
code := fmt.Sprintf("%s:\n", address)
ASMWrite(code)
//If block has been executed jump to the end of the if statement
// ifEnd := fmt.Sprintf("ifEnd_%d", BlockCounter)
// code = fmt.Sprintf("jmp %s\n", ifEnd)
// ASMWrite(code)
// IfEndStack = IfEndStack.Push(ifEnd)
// BlockCounter++
}
//RedefineVariable to redefine already declared variable. Note: Could be used for declaring too
func RedefineVariable(tokens []Token) {
variableName := string(tokens[0].Value)
newTokenList := make([]Token, 0)
//Currently just in type so just do arithmetic
for i := 2; i < len(tokens); i++ {
//If function call seen
if tokens[i].Type == "Function" {
j := i
for j < len(tokens) && string(tokens[j].Value) != ")" {
j++
}
//Make a function call
AddFunctionCallToStack(tokens[i : j+1])
tokens[i].Type = "FunctionCall"
newTokenList = append(newTokenList, tokens[i])
i = j
} else {
newTokenList = append(newTokenList, tokens[i])
}
}
TokenProcess(newTokenList)
offset := LocalVariable[variableName]
index := (len(LocalVariable)+1)*8 - offset
code := fmt.Sprintf("movq %%rax, %d(%%rbp)\n", index)
ASMWrite(code)
}
//Initialize local variable count for current block
func blockInit() {
LocalVariableCountStack = LocalVariableCountStack.Push(0)
}
//When block ends, all the local variables declared inside needs to be deleted
func BlockEnd() {
var count int
LocalVariableCountStack, count = LocalVariableCountStack.Pop()
//Pop count amount of local variables
PopLocalVariables(count)
//If there was at least one local variable at the current block
if count != 0 {
log.Println(LocalVariable)
for count != 0 {
var variable string
VariableStack, variable = VariableStack.Pop()
stackindex = stackindex - 8
//Delete current variable from the map to make sure the variable cannot be referenced again
delete(LocalVariable, variable)
count--
}
log.Println(LocalVariable)
}
}