diff options
| author | Rose Hogenson <rhogenson@posteo.net> | 2023-02-11 09:40:25 -0800 |
|---|---|---|
| committer | Rose Hogenson <rhogenson@posteo.net> | 2023-02-11 09:40:25 -0800 |
| commit | ddb69212f03a82e980144594d809a072b50e7f19 (patch) | |
| tree | feffd8402d40c8ee0c3bb441355db956ef4cc6f9 /compiler.sml | |
| parent | Write a first draft of the compiler. (diff) | |
| download | sml-ddb69212f03a82e980144594d809a072b50e7f19.tar.zst | |
Finish the compiler.
Just joking. But we did manage to compile a program from SML
to bytecode.
Diffstat (limited to 'compiler.sml')
| -rw-r--r-- | compiler.sml | 37 |
1 files changed, 37 insertions, 0 deletions
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 <outfile>] <filename>" + 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 |
