author | Christian Urban <christian.urban@kcl.ac.uk> |
Sun, 25 Oct 2020 01:25:01 +0000 | |
changeset 789 | f0696713177b |
parent 735 | fc2e3609d5e5 |
child 813 | 059f970287d1 |
permissions | -rw-r--r-- |
624 | 1 |
// A Small Compiler for a Simple Functional Language |
628 | 2 |
// (it does not include a parser and lexer) |
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
3 |
// |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
4 |
// call with |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
5 |
// |
789 | 6 |
// amm fun.sc main defs.fun |
7 |
// |
|
8 |
// amm fun.sc main fact.fun |
|
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
9 |
// |
789 | 10 |
// or |
11 |
// amm fun.sc test |
|
12 |
// |
|
13 |
// the latter will print out the JVM instructions for two |
|
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
14 |
// factorial functions |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
15 |
|
789 | 16 |
import $file.fun_tokens, fun_tokens._ |
17 |
import $file.fun_parser, fun_parser._ |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
// calculating the maximal needed stack size |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
def max_stack_exp(e: Exp): Int = e match { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
case Call(_, args) => args.map(max_stack_exp).sum |
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
23 |
case If(a, e1, e2) => |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
24 |
max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
case Write(e) => max_stack_exp(e) + 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
case Var(_) => 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
case Num(_) => 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
789 | 29 |
case Sequence(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
} |
789 | 31 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
def max_stack_bexp(e: BExp): Int = e match { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
// compiler - built-in functions |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
// |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
val library = """ |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
.class public XXX.XXX |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
.super java/lang/Object |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
.method public static write(I)V |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
.limit locals 5 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
.limit stack 5 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
iload 0 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
swap |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
invokevirtual java/io/PrintStream/println(I)V |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
return |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
.end method |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
""" |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
// for generating new labels |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
var counter = -1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
def Fresh(x: String) = { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
counter += 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
x ++ "_" ++ counter.toString() |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
|
628 | 64 |
// convenient string interpolations for |
65 |
// generating instructions, labels etc |
|
624 | 66 |
import scala.language.implicitConversions |
67 |
import scala.language.reflectiveCalls |
|
68 |
||
639 | 69 |
// convenience for code-generation (string interpolations) |
624 | 70 |
implicit def sring_inters(sc: StringContext) = new { |
639 | 71 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" // instructions |
72 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" // labels |
|
73 |
def m(args: Any*): String = sc.s(args:_*) ++ "\n" // methods |
|
624 | 74 |
} |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
|
628 | 76 |
// variable / index environments |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
type Env = Map[String, Int] |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
// compile expressions |
624 | 80 |
def compile_exp(a: Exp, env : Env) : String = a match { |
81 |
case Num(i) => i"ldc $i" |
|
82 |
case Var(s) => i"iload ${env(s)}" |
|
83 |
case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"iadd" |
|
84 |
case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"isub" |
|
85 |
case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"imul" |
|
86 |
case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"idiv" |
|
87 |
case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"irem" |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
case If(b, a1, a2) => { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
val if_else = Fresh("If_else") |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
val if_end = Fresh("If_end") |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
compile_bexp(b, env, if_else) ++ |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
compile_exp(a1, env) ++ |
624 | 93 |
i"goto $if_end" ++ |
94 |
l"$if_else" ++ |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
compile_exp(a2, env) ++ |
624 | 96 |
l"$if_end" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
} |
624 | 98 |
case Call(name, args) => { |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
val is = "I" * args.length |
624 | 100 |
args.map(a => compile_exp(a, env)).mkString ++ |
101 |
i"invokestatic XXX/XXX/$name($is)I" |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
} |
789 | 103 |
case Sequence(a1, a2) => { |
624 | 104 |
compile_exp(a1, env) ++ i"pop" ++ compile_exp(a2, env) |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
case Write(a1) => { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
compile_exp(a1, env) ++ |
624 | 108 |
i"dup" ++ |
109 |
i"invokestatic XXX/XXX/write(I)V" |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
// compile boolean expressions |
624 | 114 |
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
case Bop("==", a1, a2) => |
624 | 116 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpne $jmp" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
case Bop("!=", a1, a2) => |
624 | 118 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpeq $jmp" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
case Bop("<", a1, a2) => |
624 | 120 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpge $jmp" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
case Bop("<=", a1, a2) => |
624 | 122 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpgt $jmp" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
|
639 | 125 |
// compile functions and declarations |
624 | 126 |
def compile_decl(d: Decl) : String = d match { |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
case Def(name, args, a) => { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
val env = args.zipWithIndex.toMap |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
val is = "I" * args.length |
624 | 130 |
m".method public static $name($is)I" ++ |
131 |
m".limit locals ${args.length.toString}" ++ |
|
132 |
m".limit stack ${1 + max_stack_exp(a)}" ++ |
|
133 |
l"${name}_Start" ++ |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
compile_exp(a, env) ++ |
624 | 135 |
i"ireturn" ++ |
136 |
m".end method\n" |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
138 |
case Main(a) => { |
624 | 139 |
m".method public static main([Ljava/lang/String;)V" ++ |
140 |
m".limit locals 200" ++ |
|
141 |
m".limit stack 200" ++ |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
142 |
compile_exp(a, Map()) ++ |
624 | 143 |
i"invokestatic XXX/XXX/write(I)V" ++ |
144 |
i"return" ++ |
|
145 |
m".end method\n" |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
146 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
147 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
148 |
|
625 | 149 |
// the main compilation function |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
150 |
def compile(prog: List[Decl], class_name: String) : String = { |
624 | 151 |
val instructions = prog.map(compile_decl).mkString |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
152 |
(library + instructions).replaceAllLiterally("XXX", class_name) |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
153 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
154 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
155 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
156 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
157 |
|
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
158 |
// An example program (two versions of factorial) |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
159 |
// |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
160 |
// def fact(n) = |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
161 |
// if n == 0 then 1 else n * fact(n - 1); |
639 | 162 |
// |
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
163 |
// def facT(n, acc) = |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
164 |
// if n == 0 then acc else facT(n - 1, n * acc); |
639 | 165 |
// |
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
166 |
// fact(10) ; facT(10, 1) |
639 | 167 |
// |
168 |
||
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
169 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
170 |
val test_prog = |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
List(Def("fact", List("n"), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
If(Bop("==",Var("n"),Num(0)), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
173 |
Num(1), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
174 |
Aop("*",Var("n"), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
Call("fact",List(Aop("-",Var("n"),Num(1))))))), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
177 |
Def("facT",List("n", "acc"), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
178 |
If(Bop("==",Var("n"),Num(0)), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
179 |
Var("acc"), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
180 |
Call("facT",List(Aop("-",Var("n"),Num(1)), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
181 |
Aop("*",Var("n"),Var("acc")))))), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
182 |
|
789 | 183 |
Main(Sequence(Write(Call("fact",List(Num(10)))), |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
184 |
Write(Call("facT",List(Num(10), Num(1))))))) |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
185 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
186 |
// prints out the JVM instructions |
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
187 |
@main |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
188 |
def test() = |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
189 |
println(compile(test_prog, "fact")) |
789 | 190 |
|
191 |
||
192 |
@main |
|
193 |
def main(fname: String) = { |
|
194 |
val path = os.pwd / fname |
|
195 |
val class_name = fname.stripSuffix("." ++ path.ext) |
|
196 |
val tks = tokenise(os.read(path)) |
|
197 |
val ast = parse_tks(tks) |
|
198 |
println(compile(ast, class_name)) |
|
199 |
} |