author | Christian Urban <christian.urban@kcl.ac.uk> |
Wed, 06 Oct 2021 20:43:35 +0100 | |
changeset 842 | b53ac1bb5f43 |
parent 814 | 1fbaa5f05516 |
child 869 | 81ee93b87258 |
permissions | -rw-r--r-- |
624 | 1 |
// A Small Compiler for a Simple Functional Language |
813 | 2 |
// (includes 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 |
// |
813 | 6 |
// amm fun.sc test |
7 |
// |
|
8 |
// or |
|
9 |
// |
|
789 | 10 |
// amm fun.sc main defs.fun |
11 |
// amm fun.sc main fact.fun |
|
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
12 |
// |
813 | 13 |
// or |
14 |
// |
|
15 |
// amm fun.sc run defs.fun |
|
16 |
// amm fun.sc run fact.fun |
|
789 | 17 |
// |
813 | 18 |
// the first prints out the JVM instructions for two |
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
19 |
// factorial functions |
813 | 20 |
// |
21 |
// the next compile/run fun files |
|
22 |
// |
|
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
23 |
|
789 | 24 |
import $file.fun_tokens, fun_tokens._ |
25 |
import $file.fun_parser, fun_parser._ |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
// calculating the maximal needed stack size |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
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
|
30 |
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
|
31 |
case If(a, e1, e2) => |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
32 |
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
|
33 |
case Write(e) => max_stack_exp(e) + 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
case Var(_) => 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
case Num(_) => 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
789 | 37 |
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
|
38 |
} |
789 | 39 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
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
|
41 |
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
|
42 |
} |
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 |
// compiler - built-in functions |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
// 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
|
46 |
// |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
val library = """ |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
.class public XXX.XXX |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
.super java/lang/Object |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
.method public static write(I)V |
814 | 53 |
.limit locals 1 |
54 |
.limit stack 2 |
|
55 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
|
56 |
iload 0 |
|
57 |
invokevirtual java/io/PrintStream/println(I)V |
|
58 |
return |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
.end method |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
""" |
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 |
// for generating new labels |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
var counter = -1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
def Fresh(x: String) = { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
counter += 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
x ++ "_" ++ counter.toString() |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
|
628 | 71 |
// convenient string interpolations for |
72 |
// generating instructions, labels etc |
|
624 | 73 |
import scala.language.implicitConversions |
74 |
import scala.language.reflectiveCalls |
|
75 |
||
639 | 76 |
// convenience for code-generation (string interpolations) |
624 | 77 |
implicit def sring_inters(sc: StringContext) = new { |
639 | 78 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" // instructions |
79 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" // labels |
|
80 |
def m(args: Any*): String = sc.s(args:_*) ++ "\n" // methods |
|
624 | 81 |
} |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
|
628 | 83 |
// variable / index environments |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
type Env = Map[String, Int] |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
|
813 | 86 |
def compile_op(op: String) = op match { |
87 |
case "+" => i"iadd" |
|
88 |
case "-" => i"isub" |
|
89 |
case "*" => i"imul" |
|
90 |
case "/" => i"idiv" |
|
91 |
case "%" => i"irem" |
|
92 |
} |
|
93 |
||
94 |
||
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
// compile expressions |
813 | 96 |
def compile_exp(a: Exp, env: Env) : String = a match { |
624 | 97 |
case Num(i) => i"ldc $i" |
98 |
case Var(s) => i"iload ${env(s)}" |
|
813 | 99 |
case Aop(op, a1, a2) => |
100 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ compile_op(op) |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
case If(b, a1, a2) => { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
val if_else = Fresh("If_else") |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
val if_end = Fresh("If_end") |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
compile_bexp(b, env, if_else) ++ |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
compile_exp(a1, env) ++ |
624 | 106 |
i"goto $if_end" ++ |
107 |
l"$if_else" ++ |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
compile_exp(a2, env) ++ |
624 | 109 |
l"$if_end" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
} |
624 | 111 |
case Call(name, args) => { |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
val is = "I" * args.length |
624 | 113 |
args.map(a => compile_exp(a, env)).mkString ++ |
114 |
i"invokestatic XXX/XXX/$name($is)I" |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
} |
789 | 116 |
case Sequence(a1, a2) => { |
624 | 117 |
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
|
118 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
case Write(a1) => { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
compile_exp(a1, env) ++ |
624 | 121 |
i"dup" ++ |
122 |
i"invokestatic XXX/XXX/write(I)V" |
|
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 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
// compile boolean expressions |
624 | 127 |
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
|
128 |
case Bop("==", a1, a2) => |
624 | 129 |
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
|
130 |
case Bop("!=", a1, a2) => |
624 | 131 |
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
|
132 |
case Bop("<", a1, a2) => |
624 | 133 |
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
|
134 |
case Bop("<=", a1, a2) => |
624 | 135 |
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
|
136 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
|
639 | 138 |
// compile functions and declarations |
624 | 139 |
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
|
140 |
case Def(name, args, a) => { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
val env = args.zipWithIndex.toMap |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
142 |
val is = "I" * args.length |
624 | 143 |
m".method public static $name($is)I" ++ |
814 | 144 |
m".limit locals ${args.length}" ++ |
624 | 145 |
m".limit stack ${1 + max_stack_exp(a)}" ++ |
146 |
l"${name}_Start" ++ |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
147 |
compile_exp(a, env) ++ |
624 | 148 |
i"ireturn" ++ |
149 |
m".end method\n" |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
150 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
151 |
case Main(a) => { |
624 | 152 |
m".method public static main([Ljava/lang/String;)V" ++ |
813 | 153 |
m".limit locals 1" ++ |
154 |
m".limit stack ${1 + max_stack_exp(a)}" ++ |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
155 |
compile_exp(a, Map()) ++ |
624 | 156 |
i"invokestatic XXX/XXX/write(I)V" ++ |
157 |
i"return" ++ |
|
158 |
m".end method\n" |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
159 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
|
625 | 162 |
// the main compilation function |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
def compile(prog: List[Decl], class_name: String) : String = { |
624 | 164 |
val instructions = prog.map(compile_decl).mkString |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
(library + instructions).replaceAllLiterally("XXX", class_name) |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
} |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
168 |
|
813 | 169 |
import ammonite.ops._ |
170 |
||
171 |
def compile_to_file(prog: List[Decl], class_name: String) : Unit = |
|
172 |
write.over(pwd / s"$class_name.j", compile(prog, class_name)) |
|
173 |
||
174 |
def compile_and_run(prog: List[Decl], class_name: String) : Unit = { |
|
175 |
println(s"Start of compilation") |
|
176 |
compile_to_file(prog, class_name) |
|
177 |
println(s"generated $class_name.j file") |
|
178 |
os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call() |
|
179 |
println(s"generated $class_name.class file") |
|
180 |
println(s"Run program") |
|
181 |
os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit) |
|
182 |
println(s"done.") |
|
183 |
} |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
184 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
185 |
|
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
186 |
// An example program (two versions of factorial) |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
187 |
// |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
188 |
// def fact(n) = |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
189 |
// if n == 0 then 1 else n * fact(n - 1); |
639 | 190 |
// |
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
191 |
// def facT(n, acc) = |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
192 |
// if n == 0 then acc else facT(n - 1, n * acc); |
639 | 193 |
// |
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
194 |
// fact(10) ; facT(10, 1) |
639 | 195 |
// |
196 |
||
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
197 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
198 |
val test_prog = |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
199 |
List(Def("fact", List("n"), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
200 |
If(Bop("==",Var("n"),Num(0)), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
201 |
Num(1), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
202 |
Aop("*",Var("n"), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
203 |
Call("fact",List(Aop("-",Var("n"),Num(1))))))), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
204 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
205 |
Def("facT",List("n", "acc"), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
206 |
If(Bop("==",Var("n"),Num(0)), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
207 |
Var("acc"), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
208 |
Call("facT",List(Aop("-",Var("n"),Num(1)), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
209 |
Aop("*",Var("n"),Var("acc")))))), |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
210 |
|
789 | 211 |
Main(Sequence(Write(Call("fact",List(Num(10)))), |
813 | 212 |
Write(Call("facT",List(Num(10), Num(1))))))) |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
213 |
|
813 | 214 |
// prints out the JVM instructions for the factorial example |
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
215 |
@main |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
216 |
def test() = |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
734
diff
changeset
|
217 |
println(compile(test_prog, "fact")) |
789 | 218 |
|
219 |
||
220 |
@main |
|
221 |
def main(fname: String) = { |
|
222 |
val path = os.pwd / fname |
|
223 |
val class_name = fname.stripSuffix("." ++ path.ext) |
|
224 |
val tks = tokenise(os.read(path)) |
|
225 |
val ast = parse_tks(tks) |
|
226 |
println(compile(ast, class_name)) |
|
227 |
} |
|
813 | 228 |
|
229 |
@main |
|
230 |
def run(fname: String) = { |
|
231 |
val path = os.pwd / fname |
|
232 |
val class_name = fname.stripSuffix("." ++ path.ext) |
|
233 |
val tks = tokenise(os.read(path)) |
|
234 |
val ast = parse_tks(tks) |
|
235 |
compile_and_run(ast, class_name) |
|
236 |
} |