summaryrefslogtreecommitdiffstats
path: root/compiler.sml
blob: 599d85a7cb02ed47708e32e2ab2e86dd188d9a3c (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
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