--- a/progs/parser-combinators/comb2.sc Sat Nov 11 10:08:33 2023 +0000
+++ b/progs/parser-combinators/comb2.sc Fri Nov 17 20:06:43 2023 +0000
@@ -9,15 +9,13 @@
//
// amm comb2.sc
-
// more convenience for the map parsers later on;
// it allows writing nested patterns as
// case x ~ y ~ z => ...
+
case class ~[+A, +B](x: A, y: B)
-val a = (1, "2")
-val v = new ~(1, "2")
type IsSeq[I] = I => Seq[_]
@@ -100,8 +98,6 @@
}
-
-
// the abstract syntax trees for the WHILE language
abstract class Stmt
abstract class AExp
@@ -172,10 +168,11 @@
// Examples
-Stmt.parse_all("x2:=5+3")
-Block.parse_all("{x:=5;y:=8}")
-Block.parse_all("if(false)then{x:=5}else{x:=10}")
-
+println(BExp.parse_all("5+3"))
+println(Stmt.parse_all("5==3"))
+println(Stmt.parse_all("x2:=5+3"))
+println(Block.parse_all("{x:=5;y:=8}"))
+println(Block.parse_all("if(false)then{x:=5}else{x:=10}"))
val fib = """n := 10;
minus1 := 0;
@@ -189,7 +186,8 @@
};
result := minus2""".replaceAll("\\s+", "")
-Stmts.parse_all(fib)
+println("fib testcase:")
+println(Stmts.parse_all(fib))
// an interpreter for the WHILE language
@@ -272,4 +270,3 @@
}""".replaceAll("\\s+", "")
println(eval(Stmts.parse_all(primes).head))
-