-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.py
More file actions
32 lines (30 loc) · 1017 Bytes
/
assembler.py
File metadata and controls
32 lines (30 loc) · 1017 Bytes
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
from bytecodes import Bytecode, label
def assemble(assembly):
labels = {}
for index, instr in enumerate(assembly):
line = instr[1]
instr = instr[0]
if instr[0] == Bytecode.LABEL:
labels[instr[1]] = index
assembly[index] = (Bytecode.LABEL,), line
for index, instr in enumerate(assembly):
line = instr[1]
instr = instr[0]
if (instr[0] == Bytecode.GOTO or
instr[0] == Bytecode.BRANCH):
assembly[index] = (instr[0], labels[instr[1]]), line
if instr[0] == Bytecode.LAMBDA:
assembly[index] = (instr[0], (assemble(instr[1][0]), instr[1][1])), line
return assembly
if __name__ == '__main__':
import tokenizer
import parser_
import compiler
while True:
src = raw_input('assembler> ')
tokens = tokenizer.tokenize(src)
ast = parser_.parse(tokens)
asm = compiler.compile_(ast)
bc = assemble(asm)
for b in bc:
print(b)