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