summaryrefslogtreecommitdiffstats
path: root/Compiler.sml
blob: 0ca0ee576c0dbe14fbd57fcda0655b98fb11b739 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
structure Compiler =
struct
  fun compile (prog : Syntax.expr) : Word8Vector.vector =
    let
      val _ = print ("ast:\n" ^ ShowSyntax.exprToString prog ^ "\n")
      val elab = Elab.elaborate prog
      val _ = print ("lambda lang:\n" ^ ShowSyntax.lexpToString elab ^ "\n")
      val cps =
        CPS.toCPS elab
          (fn _ => Syntax.CPrimop (Syntax.PExit, [Syntax.VInt 0], [], []))
      val _ = print ("cps1:\n" ^ ShowSyntax.cexpToString cps ^ "\n")
      val cps' = CPS.convertClosures cps
      val _ = print ("cps:\n" ^ ShowSyntax.cexpToString cps' ^ "\n")
      val asm = CodeGen.toASM cps'
      val _ = print ("bytecode:\n" ^ String.concatWith "\n" (map ShowSyntax.opcodeToString asm) ^ "\n")
    in
      Linker.link asm
    end

  fun main (args : string list) : unit =
    let
      val opts = { o = ref "a.out" }
      val flags =
        [ ("o", Opts.StringOpt (fn arg => #o opts := arg)) ]
      val filename =
        case Opts.getOpt flags args 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