From 4039c774404fffec583dc64a3c95c1c08e9c7deb Mon Sep 17 00:00:00 2001 From: pimjager Date: Fri, 6 May 2016 17:33:46 +0200 Subject: [PATCH] lala codegen --- examples/codeGen.spl | 6 ++++++ gen.icl | 46 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 examples/codeGen.spl diff --git a/examples/codeGen.spl b/examples/codeGen.spl new file mode 100644 index 0000000..a9b650d --- /dev/null +++ b/examples/codeGen.spl @@ -0,0 +1,6 @@ +main() { + var x = 4; + var y = 2; + var z = x + y; + return z; +} \ No newline at end of file diff --git a/gen.icl b/gen.icl index 0d5eb12..eb96982 100644 --- a/gen.icl +++ b/gen.icl @@ -1,7 +1,49 @@ implementation module gen -import AST + import StdMisc +import StdList +import StdOverloaded +import StdString + +from Data.Func import $ +from Text import class Text(join), instance Text String +from Data.List import intersperse + +import AST + +//Instruction is an instruction, with possible arguments and a possible comment +//Or is a label +:: Instr = Instr String [Arg] String + | Label String +:: Arg = L String | Lit Int +:: SSMProgram :== [Instr] gen :: AST -> String -gen _ = "dummy" \ No newline at end of file +gen _ = toString [Label "Test" + ,Instr "ldc" [Lit 1] "Eerste instructie" + ,Instr "ldc" [Lit 2] "Tweede instructie"] + + +class g a :: a -> SSMProgram + +instance g Expr where + g _ = undef + + + +class print a :: a -> [String] + +instance print Instr where + print (Label l) = [l, ":", "\n"] + print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"] + +instance print [Arg] where + print args = (map toString args) + +instance toString Arg where + toString (L l) = l + toString (Lit int) = toString int + +instance toString SSMProgram where + toString p = join " " $ map (\i-> join " " $ print i) p \ No newline at end of file -- 2.20.1