--- a/progs/compile_arr.scala Tue Nov 27 01:15:54 2018 +0000
+++ b/progs/compile_arr.scala Tue Nov 27 07:53:57 2018 +0000
@@ -1,5 +1,5 @@
// A Small Compiler for the WHILE Language
-// (it does not use a parser and lexer)
+// (stub for including arrays)
// the abstract syntax trees
abstract class Stmt
@@ -12,8 +12,7 @@
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
case class While(b: BExp, bl: Block) extends Stmt
case class Assign(s: String, a: AExp) extends Stmt
-case class Write(s: String) extends Stmt
-case class Read(s: String) extends Stmt
+case class Write(s: String) extends Stmt // writes out a variable
// arithmetic expressions
case class Var(s: String) extends AExp
@@ -47,40 +46,6 @@
return
.end method
-.method public static read()I
- .limit locals 10
- .limit stack 10
-
- ldc 0
- istore 1 ; this will hold our final integer
-Label1:
- getstatic java/lang/System/in Ljava/io/InputStream;
- invokevirtual java/io/InputStream/read()I
- istore 2
- iload 2
- ldc 10 ; the newline delimiter
- isub
- ifeq Label2
- iload 2
- ldc 32 ; the space delimiter
- isub
- ifeq Label2
-
- iload 2
- ldc 48 ; we have our digit in ASCII, have to subtract it from 48
- isub
- ldc 10
- iload 1
- imul
- iadd
- istore 1
- goto Label1
-Label2:
- ;when we come here we have our integer computed in local variable 1
- iload 1
- ireturn
-.end method
-
.method public static main([Ljava/lang/String;)V
.limit locals 200
.limit stack 200
@@ -171,12 +136,6 @@
case Write(x) =>
(List("iload " + env(x) + "\n" +
"invokestatic XXX/XXX/write(I)V\n"), env)
- case Read(x) => {
- val index = if (env.isDefinedAt(x)) env(x) else
- env.keys.size.toString
- (List("invokestatic XXX/XXX/read()I\n" +
- "istore " + index + "\n"), env + (x -> index))
- }
}
// compilation of a block (i.e. list of instructions)
@@ -259,3 +218,15 @@
compile_run(fib_test, "fib")
+
+val arr_test =
+ List(Array("a", 10),
+ Array("b", 2),
+ AssignA("a", Num(0), Num(10)),
+ Assign("x", Ref("a", Num(0))),
+ Write("x"),
+ AssignA("b", Num(1), Num(5)),
+ Assign("x", Ref("b", Num(1))),
+ Write("x"))
+
+//compile_run(arr_test, "a")
\ No newline at end of file