diff -r c82a45f48bfc -r 5365ef60707e progs/while-arrays/compile_bfc.sc --- a/progs/while-arrays/compile_bfc.sc Fri Oct 13 23:49:34 2023 +0100 +++ b/progs/while-arrays/compile_bfc.sc Sat Oct 21 09:09:09 2023 +0100 @@ -92,33 +92,37 @@ // Grammar Rules for WHILE with arrays //===================================== +import $ivy.`com.lihaoyi::fastparse:3.0.2` import fastparse._ import MultiLineWhitespace._ -def lowercase [_ : P] = P( CharIn("a-z") ) -def uppercase[_ : P] = P( CharIn("A-Z") ) -def letter[_ : P] = P( lowercase | uppercase ) -def digit [_ : P] = P( CharIn("0-9") ) +def string[A: P]: P[String] = P(CharIn("a-zA-Z0-9").rep(1).!) -def Number[_ : P]: P[Int] = P( digit.rep(1) ).!.map(_.toInt) -def Ident[_ : P]: P[String] = P( letter ~ (letter | digit | "_").rep ).! +/* +def lowercase [$ : P] = P( CharIn("a-z") ) +def uppercase[$ : P] = P( CharIn("A-Z") ) +def letter[$ : P] = P( lowercase | uppercase ) +def digit [$ : P] = P( CharIn("0-9") ) + +def Number[$ : P]: P[Int] = P( digit.rep(1) ).!.map(_.toInt) +def Ident[$ : P]: P[String] = P( letter ~ (letter | digit | "_").rep ).! // arithmetic expressions -def AExp[_ : P]: P[AExp] = +def AExp[$ : P]: P[AExp] = P( P(Te ~ "+" ~ AExp).map{ case (l, r) => Aop("+", l, r)} | P(Te ~ "-" ~ AExp).map{ case (l, r) => Aop("-", l, r)} | Te ) -def Te[_ : P]: P[AExp] = +def Te[$ : P]: P[AExp] = P( P(Fa ~ "*" ~ Te).map{ case (l, r) => Aop("*", l, r)} | Fa ) -def Fa[_ : P]: P[AExp] = +def Fa[$ : P]: P[AExp] = P( "(" ~ AExp ~ ")" | P (Ident ~ "[" ~ AExp ~ "]").map{Ref.tupled} | P(Number).map{Num} | P(Ident).map{Var} ) // boolean expressions -def BExp[_ : P]: P[BExp] = +def BExp[$ : P]: P[BExp] = P( P(AExp ~ "=" ~ AExp).map{ case (x, z) => Bop("=", x, z)} | P(AExp ~ "!=" ~ AExp).map{ case (x, z) => Bop("!=", x, z)} | P(AExp ~ "<" ~ AExp).map{ case (x, z) => Bop("<", x, z)} @@ -128,7 +132,7 @@ | "(" ~ BExp ~ ")" ) // statements and blocks -def Stmt[_ : P]: P[Stmt] = +def Stmt[$ : P]: P[Stmt] = P( P("skip").map( _ => Skip) | P(Ident ~ ":=" ~ AExp).map{Assign.tupled} | P(Ident ~ "[" ~ AExp ~ "]" ~ ":=" ~ AExp).map{AssignA.tupled} @@ -137,11 +141,11 @@ | P("new(" ~ Ident ~ "[" ~ Number ~ "])").map{ArrayDef.tupled} | P("write(" ~ Ident ~ ")").map{Write} ) -def Stmts[_ : P]: P[Block] = +def Stmts[$ : P]: P[Block] = P( P(Stmt ~ ";" ~ Stmts).map{ case (x, z) => x :: z } | P(Stmt).map{s => List(s)} ) -def Block[_ : P]: P[Block] = +def Block[$ : P]: P[Block] = P( "{" ~ Stmts ~ "}" | P(Stmt).map(s => List(s)) ) @@ -320,6 +324,4 @@ - - -// runs with amm2 and amm3 +*/