861
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// Glue code for CW4
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
//===================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
// generates j-files and runs class-files
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
// needs
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
// tokenise(s: String) : List[Token]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
// Stmts - parser consuming Tokens
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
// compile(bl: Block, class_name: String) : String
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
import ammonite.ops._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
def parse_tks(tks : List[Token]) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
Stmts.parse_all(tks).head
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
def compile_to_file(bl: Block, class_name: String) : Unit = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
println(s"Start of compilation")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
write.over(pwd / s"$class_name.j", compile(bl, class_name))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
println(s"generated $class_name.j file")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
println(s"generated $class_name.class file ")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
def main(fname: String) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
val path = os.pwd / fname
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
val class_name = fname.stripSuffix("." ++ path.ext)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
val tks = tokenise(os.read(path))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
val ast = parse_tks(tks)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
compile_to_file(ast, class_name)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
def run(fname: String) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
val path = os.pwd / fname
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
val class_name = fname.stripSuffix("." ++ path.ext)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
val tks = tokenise(os.read(path))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
val ast = parse_tks(tks)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
compile_to_file(ast, class_name)
|
863
|
49 |
os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit, stdin = os.Inherit)
|
861
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
println(s"done.")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
|