+/TECS

git clone https://git.julia.blue/+/TECS

P1n3appl3Fix folder naming bug in VMTranslator91f6f51

master
2.4 KiB63 linesraw
1# VM to ASM translator by Joseph Ryan
2import sys
3import os
4from codeparser import CodeParser
5from codewriter import CodeWriter
6
7if len(sys.argv) > 1:
8    files = []
9    fName = sys.argv[-1].replace('\\', '/')
10    tempName = fName.split('.')[0]
11    if fName.endswith(".vm"):
12        files.append(fName)
13        fName = tempName
14    else:
15        for i in os.listdir(fName):
16            if i.endswith(".vm"):
17                files.append(fName + '/' + i)
18        fName = tempName + '/' + tempName.split('/')[-1]
19    if "-m" not in sys.argv:
20        print "Parsing " + str(len(files)) + " file(s)"
21    cw = CodeWriter(fName)
22    if "-b" not in sys.argv:
23        cw.writeInit()
24    for f in files:
25        p = CodeParser(f)
26        cw.setFile(f)
27        if "-m" not in sys.argv:
28            print "Translating " + f.split('/')[-1]
29        while p.hasMoreCommands():
30            c = p.commandType()
31            if "-c" in sys.argv:
32                cw.f.write("\n//\t\t\t\t" + p.lines[p.currentLine] + '\n')
33            if c == "C_ARITHMETIC":
34                cw.writeArithmetic(p.arg1())
35            elif c in {"C_PUSH", "C_POP"}:
36                cw.writePushPop(c, p.arg1(), p.arg2())
37            elif c == "C_LABEL":
38                cw.writeLabel(p.arg1())
39            elif c == "C_GOTO":
40                cw.writeGoto(p.arg1())
41            elif c == "C_IF":
42                cw.writeIf(p.arg1())
43            elif c == "C_FUNCTION":
44                cw.writeFunction(p.arg1(), p.arg2())
45            elif c == "C_CALL":
46                cw.writeCall(p.arg1(), p.arg2())
47            elif c == "C_RETURN":
48                cw.writeReturn()
49            p.advance()
50    cw.close()
51    if "-l" in sys.argv:
52        lineNum = 0
53        with open(fName + ".asm", 'r') as src:
54            with open(fName + ".debug", 'w') as dest:
55                for line in src:
56                    if not line.startswith('(') and not line.startswith('//') and not line == '\n':
57                        dest.write(str(lineNum) + '\t\t')
58                        lineNum += 1
59                    dest.write(line)
60    if "-m" not in sys.argv:
61        print "Translation Complete"
62else:
63    print "usage: vmtranslator.py [options] source[.vm]\n\tsourceFile(s) may be file or directory.\noptions:\n\t-m mutes status messages\n\t-c writes vm commands as comments in .asm file\n\t-b doesn't generate bootstrap code\n\t-l creates source.debug with adjusted line numbers"