equal
deleted
inserted
replaced
7 // |
7 // |
8 // call with |
8 // call with |
9 // |
9 // |
10 // amm comb2.sc |
10 // amm comb2.sc |
11 |
11 |
12 |
|
13 // more convenience for the map parsers later on; |
12 // more convenience for the map parsers later on; |
14 // it allows writing nested patterns as |
13 // it allows writing nested patterns as |
15 // case x ~ y ~ z => ... |
14 // case x ~ y ~ z => ... |
16 |
15 |
|
16 |
17 case class ~[+A, +B](x: A, y: B) |
17 case class ~[+A, +B](x: A, y: B) |
18 |
18 |
19 val a = (1, "2") |
|
20 val v = new ~(1, "2") |
|
21 |
19 |
22 type IsSeq[I] = I => Seq[_] |
20 type IsSeq[I] = I => Seq[_] |
23 |
21 |
24 abstract class Parser[I, T](using is: I => Seq[_]) { |
22 abstract class Parser[I, T](using is: I => Seq[_]) { |
25 def parse(in: I): Set[(T, I)] |
23 def parse(in: I): Set[(T, I)] |
96 extension [I : IsSeq, T](p: Parser[I, T]) { |
94 extension [I : IsSeq, T](p: Parser[I, T]) { |
97 def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q) |
95 def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q) |
98 def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
96 def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
99 def map[S](f: => T => S) = new MapParser[I, T, S](p, f) |
97 def map[S](f: => T => S) = new MapParser[I, T, S](p, f) |
100 } |
98 } |
101 |
|
102 |
|
103 |
99 |
104 |
100 |
105 // the abstract syntax trees for the WHILE language |
101 // the abstract syntax trees for the WHILE language |
106 abstract class Stmt |
102 abstract class Stmt |
107 abstract class AExp |
103 abstract class AExp |
170 ((p"{" ~ Stmts ~ p"}").map{ case _ ~ y ~ _ => y } || |
166 ((p"{" ~ Stmts ~ p"}").map{ case _ ~ y ~ _ => y } || |
171 (Stmt.map(s => List(s)))) |
167 (Stmt.map(s => List(s)))) |
172 |
168 |
173 |
169 |
174 // Examples |
170 // Examples |
175 Stmt.parse_all("x2:=5+3") |
171 println(BExp.parse_all("5+3")) |
176 Block.parse_all("{x:=5;y:=8}") |
172 println(Stmt.parse_all("5==3")) |
177 Block.parse_all("if(false)then{x:=5}else{x:=10}") |
173 println(Stmt.parse_all("x2:=5+3")) |
178 |
174 println(Block.parse_all("{x:=5;y:=8}")) |
|
175 println(Block.parse_all("if(false)then{x:=5}else{x:=10}")) |
179 |
176 |
180 val fib = """n := 10; |
177 val fib = """n := 10; |
181 minus1 := 0; |
178 minus1 := 0; |
182 minus2 := 1; |
179 minus2 := 1; |
183 temp := 0; |
180 temp := 0; |
187 minus1 := temp; |
184 minus1 := temp; |
188 n := n - 1 |
185 n := n - 1 |
189 }; |
186 }; |
190 result := minus2""".replaceAll("\\s+", "") |
187 result := minus2""".replaceAll("\\s+", "") |
191 |
188 |
192 Stmts.parse_all(fib) |
189 println("fib testcase:") |
|
190 println(Stmts.parse_all(fib)) |
193 |
191 |
194 |
192 |
195 // an interpreter for the WHILE language |
193 // an interpreter for the WHILE language |
196 type Env = Map[String, Int] |
194 type Env = Map[String, Int] |
197 |
195 |
270 if (tmp == 0) then { write(n) } else { skip }; |
268 if (tmp == 0) then { write(n) } else { skip }; |
271 n := n + 1 |
269 n := n + 1 |
272 }""".replaceAll("\\s+", "") |
270 }""".replaceAll("\\s+", "") |
273 |
271 |
274 println(eval(Stmts.parse_all(primes).head)) |
272 println(eval(Stmts.parse_all(primes).head)) |
275 |
|