progs/while-arrays/compile_arrays2.sc
changeset 943 5365ef60707e
parent 870 739039774cee
child 949 61797848eede
equal deleted inserted replaced
942:c82a45f48bfc 943:5365ef60707e
     1 // A Small Compiler for the WHILE Language
     1 // A Small Compiler for the WHILE Language
       
     2 //
       
     3 // - there are some small peep-hole optimisations
       
     4 //   implemented
     2 //
     5 //
     3 // - this compiler contains support for "static" integer 
     6 // - this compiler contains support for "static" integer 
     4 //   arrays (they are mutable but cannot be re-sized)
     7 //   arrays (they are mutable but cannot be re-sized)
     5 //
     8 //
     6 // Call with 
     9 // Call with 
    86 type Env = Map[String, Int]
    89 type Env = Map[String, Int]
    87 
    90 
    88 // convenient string interpolations 
    91 // convenient string interpolations 
    89 // for generating instructions and labels
    92 // for generating instructions and labels
    90 
    93 
    91 implicit def sring_inters(sc: StringContext) = new {
    94 extension (sc: StringContext) {
    92     def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
    95     def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
    93     def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
    96     def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
    94 }
    97 }
       
    98 
    95 
    99 
    96 def compile_num(i: Int) = 
   100 def compile_num(i: Int) = 
    97   if (0 <= i && i <= 5) i"iconst_$i" else 
   101   if (0 <= i && i <= 5) i"iconst_$i" else 
    98   if (-128 <= i && i <= 127) i"bipush $i" else i"ldc $i"
   102   if (-128 <= i && i <= 127) i"bipush $i" else i"ldc $i"
    99 
   103 
   260 }
   264 }
   261 
   265 
   262 
   266 
   263 
   267 
   264 
   268 
   265 
       
   266 // runs with amm2 and amm3