silenced some warnings
[bugg-scheme-compiler.git] / compiler.sml
blob51a0df94bc9bb993f1b5b4aa0ddd871e4a86ec22
1 (* Scheme -> C Compiler *)
3 datatype SchemeToken = LparenToken
4 | RparenToken
5 | QuoteToken
6 | DotToken
7 | VectorToken
8 | IntToken of int
9 | CharToken of char
10 | StringToken of string
11 | SymbolToken of string
12 | BoolToken of bool;
14 datatype Sexpr = Void
15 | Nil
16 | Pair of Sexpr * Sexpr
17 | Vector of Sexpr list
18 | Symbol of string
19 | String of string
20 | Number of int
21 | Bool of bool
22 | Char of char;
24 datatype Expr = Const of Sexpr
25 | Var of string
26 | VarFree of string (* free variable *)
27 | VarParam of string * int (* parameter variable *)
28 | VarBound of string * int * int (* bound variable *)
29 | If of Expr * Expr * Expr
30 | Abs of (string list) * Expr
31 | AbsOpt of (string list) * string * Expr
32 | AbsVar of string * Expr
33 | App of Expr * (Expr list)
34 | AppTP of Expr * (Expr list) (* in tail position *)
35 | Seq of Expr list
36 | Or of Expr list
37 | Set of Expr * Expr
38 | Def of Expr * Expr;
40 use "./src/sml/utils.sml"; (* Utility functions *)
41 use "./src/sml/scanner.sml"; (* Scanner *)
42 use "./src/sml/reader.sml"; (* Reader *)
43 use "./src/sml/tag-parser.sml"; (* Tag Parser *)
44 use "./src/sml/sa.sml"; (* Semantic Analysis *)
45 use "./src/sml/cg.sml"; (* Code Generation *)
47 signature CODE_GEN =
48 sig
49 val cg : Expr -> string;
50 val compileSchemeFile : string * string -> unit;
51 end;
53 fun stringToFile (filename : string, str : string) =
54 let
55 val f = TextIO.openOut filename
57 ( TextIO.output(f, str);
58 TextIO.closeOut f )
59 end;
61 fun fileToString (filename : string) =
62 let
63 val f = TextIO.openIn filename
64 fun loop s =
65 (case TextIO.input1 f of
66 NONE => s
67 | SOME c => loop (c :: s))
68 val result = String.implode (rev (loop []))
70 (TextIO.closeIn f;
71 result)
72 end;
74 structure CodeGen : CODE_GEN =
75 struct
77 fun cg (e : Expr) : string =
78 (Program.reset ();
79 (Program.gen e 0);
80 Program.emit (8,1024*10))
82 fun compile str =
83 (Program.reset ()
84 ;map (fn expr => (Program.gen (SemanticAnalysis.semanticAnalysis expr) 0))
85 (TagParser.stringToPEs str)
86 ;Program.emit (8,1024*10)
89 fun compileSchemeFile (infile:string, outfile:string) : unit =
90 stringToFile (
91 outfile,
92 (compile ((*(fileToString "./src/scm/support-code.scm")^*)
93 (fileToString infile))));
95 end; (* of struct CodeGen *)