forked from jahnvi12/Nand2Tetris-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJackAnalyzer.py
38 lines (33 loc) · 1.01 KB
/
JackAnalyzer.py
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
import os
import JackTokenizer as jt
import CompilationEngineFinal as ce
import SymbolTable as ST
import VMWriter
import FuncTable
import sys
input=sys.argv[1]
fileList=[]
if os.path.isdir(input):
if input.endswith('/'):
input=input[:-1]
os.chdir(input)
for file in os.listdir('.'):
if file.endswith('.jack'):
fileList.append(file)
elif os.path.isfile(input):
fileList=[input]
else:
raise Exception("Input should be either a file name or a directory name!")
fntable = FuncTable.FuncTable()
for file in fileList:
inpname = file.split('/')[-1].split('.')[0]
outFile = inpname + '.vm'
tokenizer=jt.JackTokenizer(file)
table = ST.SymbolTable()
vm = VMWriter.VMWriter(outFile)
print "Compiling {0}".format(inpname)
compiler=ce.CompilationEngine(tokenizer, table, vm, inpname, fntable)
compiler.CompileClass()
if not fntable.isemptyundec():
for (a, b) in fntable.undecfnlist:
raise Exception("Undefined function {0} in {1} class".format(b, a))