--- a/progs/token2.scala Mon Dec 07 17:50:05 2015 +0000
+++ b/progs/token2.scala Sat Dec 19 21:11:23 2015 +0000
@@ -225,7 +225,7 @@
// Lexing Rules for a Small While Language
-val SYM = CRANGE("abcdefghijklmnopqrstuvwxyz")
+val SYM = CRANGE("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
val DIGIT = CRANGE("0123456789")
val ID = SYM ~ (SYM | DIGIT).%
val NUM = PLUS(DIGIT)
@@ -252,7 +252,7 @@
// filters out all white spaces
def tokenise(r: Rexp, s: String) =
- env(lexing_simp(r, s)).filterNot { (s) => s._1 == "w"}.mkString("\n")
+ env(lexing_simp(r, s)).filterNot { (s) => s._1 == "w"}
// Testing
//============
@@ -293,8 +293,13 @@
// Big Test
//==========
+def escape(raw: String): String = {
+ import scala.reflect.runtime.universe._
+ Literal(Constant(raw)).toString
+}
+
val prog2 = """
-write "fib";
+write "Fib";
read n;
minus1 := 0;
minus2 := 1;
@@ -304,16 +309,155 @@
minus1 := temp;
n := n - 1
};
-write "result";
+write "Result";
write minus2
"""
+val prog3 = """
+start := 1000;
+x := start;
+y := start;
+z := start;
+while 0 < x do {
+ while 0 < y do {
+ while 0 < z do {
+ z := z - 1
+ };
+ z := start;
+ y := y - 1
+ };
+ y := start;
+ x := x - 1
+}
+"""
+
+
println("Tokens")
-println(tokenise(WHILE_REGS, prog2))
+println(tokenise(WHILE_REGS, prog2).mkString("\n"))
+
+val fib_tokens = tokenise(WHILE_REGS, prog2)
+fib_tokens.map{case (s1, s2) => (escape(s1), escape(s2))}.mkString(",\n")
+
+val test_tokens = tokenise(WHILE_REGS, prog3)
+test_tokens.map{case (s1, s2) => (escape(s1), escape(s2))}.mkString(",\n")
+
+
+/*
for (i <- 1 to 120 by 10) {
print(i.toString + ": ")
time(lexing_simp(WHILE_REGS, prog2 * i))
}
+*/
+val toks_fib =
+ List(("k","write"),
+ ("str","Fib"),
+ ("s",";"),
+ ("k","read"),
+ ("i","n"),
+ ("s",";"),
+ ("i","minus1"),
+ ("o",":="),
+ ("n","0"),
+ ("s",";"),
+ ("i","minus2"),
+ ("o",":="),
+ ("n","1"),
+ ("s",";"),
+ ("k","while"),
+ ("i","n"),
+ ("o",">"),
+ ("n","0"),
+ ("k","do"),
+ ("b","{"),
+ ("i","temp"),
+ ("o",":="),
+ ("i","minus2"),
+ ("s",";"),
+ ("i","minus2"),
+ ("o",":="),
+ ("i","minus1"),
+ ("o","+"),
+ ("i","minus2"),
+ ("s",";"),
+ ("i","minus1"),
+ ("o",":="),
+ ("i","temp"),
+ ("s",";"),
+ ("i","n"),
+ ("o",":="),
+ ("i","n"),
+ ("o","-"),
+ ("n","1"),
+ ("b","}"),
+ ("s",";"),
+ ("k","write"),
+ ("str","Result"),
+ ("s",";"),
+ ("k","write"),
+ ("i","minus2"))
+val toks_test =
+ List(("i","start"),
+ ("o",":="),
+ ("n","1000"),
+ ("s",";"),
+ ("i","x"),
+ ("o",":="),
+ ("i","start"),
+ ("s",";"),
+ ("i","y"),
+ ("o",":="),
+ ("i","start"),
+ ("s",";"),
+ ("i","z"),
+ ("o",":="),
+ ("i","start"),
+ ("s",";"),
+ ("k","while"),
+ ("n","0"),
+ ("o","<"),
+ ("i","x"),
+ ("k","do"),
+ ("b","{"),
+ ("k","while"),
+ ("n","0"),
+ ("o","<"),
+ ("i","y"),
+ ("k","do"),
+ ("b","{"),
+ ("k","while"),
+ ("n","0"),
+ ("o","<"),
+ ("i","z"),
+ ("k","do"),
+ ("b","{"),
+ ("i","z"),
+ ("o",":="),
+ ("i","z"),
+ ("o","-"),
+ ("n","1"),
+ ("b","}"),
+ ("s",";"),
+ ("i","z"),
+ ("o",":="),
+ ("i","start"),
+ ("s",";"),
+ ("i","y"),
+ ("o",":="),
+ ("i","y"),
+ ("o","-"),
+ ("n","1"),
+ ("b","}"),
+ ("s",";"),
+ ("i","y"),
+ ("o",":="),
+ ("i","start"),
+ ("s",";"),
+ ("i","x"),
+ ("o",":="),
+ ("i","x"),
+ ("o","-"),
+ ("n","1"),
+ ("b","}"))