From ddb69212f03a82e980144594d809a072b50e7f19 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 11 Feb 2023 09:40:25 -0800 Subject: Finish the compiler. Just joking. But we did manage to compile a program from SML to bytecode. --- compiler.sml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 compiler.sml (limited to 'compiler.sml') diff --git a/compiler.sml b/compiler.sml new file mode 100644 index 0000000..599d85a --- /dev/null +++ b/compiler.sml @@ -0,0 +1,37 @@ +structure Compiler = +struct + fun compile (prog : Syntax.expr) : Word8Vector.vector = + let + val elab = Elab.elaborate prog + val cps = + CPS.convertClosures + (CPS.toCPS elab + (fn _ => Syntax.CPrimop (Syntax.PExit, [Syntax.VInt 0], [], []))) + val _ = print ("cps:\n" ^ Syntax.cexpToString cps ^ "\n") + val asm = CodeGen.toASM cps + val _ = print ("bytecode:\n" ^ String.concatWith "\n" (map Syntax.opcodeToString asm) ^ "\n") + in + Linker.link asm + end + + fun main () = + let + val opts = { o = ref "a.out" } + val flags = + [ ("o", Opts.StringOpt (fn arg => #o opts := arg)) ] + val filename = + case Opts.getOpt flags of + [arg] => arg + | _ => raise Fail "usage: sml [-o ] " + val ast = + case Parser.parse filename of + Result.Left e => + (print e ; + OS.Process.exit OS.Process.failure) + | Result.Right x => x + val bytecode = compile ast + val outFile = BinIO.openOut (!(#o opts)) + in + BinIO.output (outFile, bytecode) + end +end -- cgit v1.3.1