Franck Pommereau

cttc.py first version, not tested yet

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