summaryrefslogtreecommitdiffstats
path: root/compiler.sml
diff options
context:
space:
mode:
Diffstat (limited to 'compiler.sml')
-rw-r--r--compiler.sml37
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