progs/fun/fun.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:59:32 +0000
changeset 976 e9eac62928f5
parent 975 ae5c03560d4d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
     1
// A Small Compiler for a Simple Functional Language
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     2
// (includes a parser and lexer)
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
     3
//
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
     4
// call with
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
     5
//
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     6
//    amm fun.sc test
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     7
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     8
//  or
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     9
//
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
    10
//    amm fun.sc main defs.fun
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
    11
//    amm fun.sc main fact.fun
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
    12
//
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    13
//  or
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    14
// 
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    15
//    amm fun.sc run defs.fun
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    16
//    amm fun.sc run fact.fun   
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
    17
//
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    18
// the first prints out the JVM instructions for two
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
    19
// factorial functions
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    20
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    21
// the next compile/run fun files
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    22
//
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
    23
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
    24
import $file.fun_tokens, fun_tokens._
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
    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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
    31
  case If(a, e1, e2) => 
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
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
    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
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
    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
1fbaa5f05516 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    53
    .limit locals 1 
1fbaa5f05516 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    54
    .limit stack 2 
1fbaa5f05516 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    55
    getstatic java/lang/System/out Ljava/io/PrintStream; 
1fbaa5f05516 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    56
    iload 0
1fbaa5f05516 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    57
    invokevirtual java/io/PrintStream/println(I)V
1fbaa5f05516 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    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
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    71
// convenient string interpolations for
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    72
// generating instructions, labels etc
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    73
import scala.language.implicitConversions
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    74
import scala.language.reflectiveCalls
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    75
639
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    76
// convenience for code-generation (string interpolations)
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    77
extension (sc: StringContext) {
639
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    78
  def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"  // instructions
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    79
  def l(args: Any*): String = sc.s(args:_*) ++ ":\n"          // labels
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    80
  def m(args: Any*): String = sc.s(args:_*) ++ "\n"           // methods
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    81
}
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    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
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    86
def compile_op(op: String) = op match {
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    87
  case "+" => i"iadd"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    88
  case "-" => i"isub"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    89
  case "*" => i"imul"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    90
  case "/" => i"idiv"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    91
  case "%" => i"irem"
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    92
}
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    93
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    94
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
// compile expressions
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    96
def compile_exp(a: Exp, env: Env) : String = a match {
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    97
  case Num(i) => i"ldc $i"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    98
  case Var(s) => i"iload ${env(s)}"
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    99
  case Aop(op, a1, a2) => 
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   106
    i"goto $if_end" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   109
    l"$if_end"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
  }
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   113
    args.map(a => compile_exp(a, env)).mkString ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   116
  case Sequence(a1, a2) => {
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   121
    i"dup" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   138
// compile functions and declarations
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   143
    m".method public static $name($is)I" ++
814
1fbaa5f05516 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   144
    m".limit locals ${args.length}" ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   145
    m".limit stack ${1 + max_stack_exp(a)}" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   148
    i"ireturn" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   152
    m".method public static main([Ljava/lang/String;)V" ++
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   153
    m".limit locals 1" ++
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   156
    i"invokestatic XXX/XXX/write(I)V" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   157
    i"return" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   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
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   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
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   164
  val instructions = prog.map(compile_decl).mkString
975
Christian Urban <christian.urban@kcl.ac.uk>
parents: 955
diff changeset
   165
  (library + instructions).replace("XXX", class_name)
323
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
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 814
diff changeset
   169
// pre-2.5.0 ammonite 
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 814
diff changeset
   170
// import ammonite.ops._
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 814
diff changeset
   171
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 814
diff changeset
   172
// post 2.5.0 ammonite
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 814
diff changeset
   173
import os._
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   174
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   175
def compile_to_file(prog: List[Decl], class_name: String) : Unit = 
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   176
  write.over(pwd / s"$class_name.j", compile(prog, class_name))  
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   177
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   178
def compile_and_run(prog: List[Decl], class_name: String) : Unit = {
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   179
  println(s"Start of compilation")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   180
  compile_to_file(prog, class_name)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   181
  println(s"generated $class_name.j file")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   182
  os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call()
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   183
  println(s"generated $class_name.class file")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   184
  println(s"Run program")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   185
  os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   186
  println(s"done.")
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   187
}
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   190
// An example program (two versions of factorial)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   191
//
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   192
// def fact(n) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   193
//    if n == 0 then 1 else n * fact(n - 1);
639
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   194
//
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   195
// def facT(n, acc) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   196
//    if n == 0 then acc else facT(n - 1, n * acc);
639
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   197
// 
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   198
// fact(10) ; facT(10, 1)
639
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   199
// 
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   200
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
val test_prog = 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
  List(Def("fact", List("n"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
         If(Bop("==",Var("n"),Num(0)),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
            Num(1),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
            Aop("*",Var("n"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
                    Call("fact",List(Aop("-",Var("n"),Num(1))))))),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
       Def("facT",List("n", "acc"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
         If(Bop("==",Var("n"),Num(0)),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
            Var("acc"),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
            Call("facT",List(Aop("-",Var("n"),Num(1)), 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
                             Aop("*",Var("n"),Var("acc")))))),
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   215
       Main(Sequence(Write(Call("fact",List(Num(10)))),
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   216
                     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
   217
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   218
// prints out the JVM instructions for the factorial example
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   219
@main
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   220
def test() = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 734
diff changeset
   221
  println(compile(test_prog, "fact"))
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   222
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   223
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   224
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   225
def main(fname: String) = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   226
    val path = os.pwd / fname
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   227
    val class_name = fname.stripSuffix("." ++ path.ext)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   228
    val tks = tokenise(os.read(path))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   229
    val ast = parse_tks(tks)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   230
    println(compile(ast, class_name))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
   231
}
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   232
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   233
@main
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   234
def run(fname: String) = {
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   235
    val path = os.pwd / fname
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   236
    val class_name = fname.stripSuffix("." ++ path.ext)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   237
    val tks = tokenise(os.read(path))
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   238
    val ast = parse_tks(tks)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   239
    compile_and_run(ast, class_name)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   240
}