progs/glue.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 09 Oct 2022 13:39:34 +0100
changeset 882 5fcad75ade92
parent 863 d59bcff69998
child 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
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
import ammonite.ops._
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
def parse_tks(tks : List[Token]) = {
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
  Stmts.parse_all(tks).head
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
}
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
def compile_to_file(bl: Block, class_name: String) : Unit = {
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  println(s"Start of compilation")
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
  write.over(pwd / s"$class_name.j", compile(bl, class_name))  
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
  println(s"generated $class_name.j file")
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call()
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  println(s"generated $class_name.class file ")
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
}
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
@main
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
def main(fname: String) = {
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
    val path = os.pwd / fname
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
    val class_name = fname.stripSuffix("." ++ path.ext)
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
    val tks = tokenise(os.read(path))
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
    val ast = parse_tks(tks)
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
    compile_to_file(ast, class_name)
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
}
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
@main
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
def run(fname: String) = {
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
    val path = os.pwd / fname
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
    val class_name = fname.stripSuffix("." ++ path.ext)
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
    val tks = tokenise(os.read(path))
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
    val ast = parse_tks(tks)
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
    compile_to_file(ast, class_name)
863
d59bcff69998 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 861
diff changeset
    49
    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
    50
    println(s"done.")
f5709803272b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
}
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