-
Notifications
You must be signed in to change notification settings - Fork 0
/
ice9
executable file
·83 lines (65 loc) · 2.63 KB
/
ice9
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
#! /usr/bin/env python
import lib.yacc as yacc
import lib.pyice_parser
import sys
import os
from optparse import OptionParser
from lib.pyice_parser import ParseError
from lib.pyice_lexer import LexError
from lib.nodes import TypeError
from lib.symbol_table import ProcInsertionError, SymbolLookupError, SymbolInsertionError
class Compiler:
def _parse(self, code):
try:
self.program = yacc.parse(code)
# Finished compiling, make sure all functions have definitions
for func in lib.pyice_parser.functions.values():
if not func.stmts:
raise TypeError(-1, "function declared but not implemented")
except ParseError, p:
print >> sys.stderr, "line %d: syntax error near %s" % (p.lineno, p.token)
sys.exit(100)
except LexError, l:
print >> sys.stderr, "line %d: illegal character %s" % (l.lineno, l.token)
sys.exit(100)
except TypeError, t:
print >> sys.stderr, str(t)
sys.exit(100)
except ProcInsertionError, e:
print >> sys.stderr, str(e)
sys.exit(100)
except SymbolLookupError, e:
print >> sys.stderr, str(e)
sys.exit(100)
except SymbolInsertionError, e:
print >> sys.stderr, str(e)
sys.exit(100)
def compile_file(self, inputs, filename, options):
out = open(filename, "w")
self._parse(inputs)
path, extension = os.path.splitext(filename)
classname = os.path.split(path)[1]
if options.ast:
out.write(self.program)
else:
out.write(self.program.jasmin(classname, lib.pyice_parser.var_decs, lib.pyice_parser.functions))
out.close()
if options.comp:
curr_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
path = os.path.join(curr_dir, 'jasmin', 'jasmin.jar')
print filename
cmd = "java -jar %s %s" % (path, filename)
os.system(cmd)
def start_compiler():
parser = OptionParser("usage: %prog [options] filename")
parser.add_option("-a", "--ast",
help="output the AST", action="store_true", dest="ast")
parser.add_option("-c", "--compile",
help="compiles the output jasmin code with the bundled jasmin assembler", action="store_true", dest="comp")
(options, args) = parser.parse_args()
files = args
if len(sys.argv) < 2:
parser.error("Needs at least one argument")
Compiler().compile_file(sys.stdin.read(), args[0], options)
if __name__ == '__main__':
start_compiler()