Franck Pommereau

cttc.py first version, not tested yet

# clex.py: modified by Franck Pommereau (2018)
# ---------------------------------------------------------------
# clex.py
#
......
# cparse.py: modified by Franck Pommereau (2018)
# ---------------------------------------------------------------
# cparse.py
#
......@@ -755,7 +757,16 @@ def p_empty(t):
pass
def p_error(t):
print("[%s:%s] syntax error" % (t.lineno, t.lexpos))
print("[%s] syntax error:" % t.lineno)
start = t.lexer.lexdata.rfind("\n", 0, t.lexpos)
if start == -1 :
start = 0
else :
start += 1
stop = t.lexer.lexdata.find("\n", t.lexpos)
head = " " * len("[%s" % t.lineno)
print(" >" + head + t.lexer.lexdata[start:stop])
print(" >" + head + (" " * (t.lexpos - start)) + "^")
raise ParseError()
yacc.yacc(debug=1)
......
This diff is collapsed. Click to expand it.
# cvisitors.py: modified by Franck Pommereau (2018)
# ---------------------------------------------------------------
# cvisitors.py
#
......
# cx86.py: modified by Franck Pommereau (2018)
# ---------------------------------------------------------------
# cx86.py
#
......@@ -177,7 +179,7 @@ class x86Registers:
"""Emits code that pops the callee-save registers used by
the stack machine off the process' stack."""
for reg in self.callee_save_regs_used:
for reg in reversed(self.callee_save_regs_used):
self.o(" popl %s" % reg,
"Restore callee-save register")
......
import argparse, sys, os.path
import ply.yacc as yacc
from . import cparse, cvisitors, cx86
cttc = cx86
from . import cparse, cvisitors, cx86, cttc
ARCH = {"x86" : cx86,
"ttc" : cttc}
......@@ -95,7 +94,7 @@ def main (args=None) :
ast_file = open(ast_path, "w")
else :
ast_file = None
code = "\n".join(open(src).readlines())
code = "\n".join(l.rstrip() for l in open(src).readlines())
ret = Compiler(ARCH[args.arch], src).compile(code, ast_file)
if ast_file is not None :
ast_file.close()
......
int main (int argc, char **argv) {
int a;
int b;
int c;
int d;
a = 1;
b = 1+a;
c = 1+a+b;
d = 1+a+b+c;
a = a * (1*a + 2*b + 3*c + 4*d) - b * (2*a + 3*b + 4*c + 4*d);
}