1 // A parser and evaluator for teh while language |
1 // A parser and evaluator for teh while language |
2 // |
2 // |
3 //:load matcher.scala |
3 import matcher._ |
4 //:load parser3.scala |
4 import parser._ |
|
5 |
5 |
6 |
6 // some regular expressions |
7 // some regular expressions |
7 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
8 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
8 val DIGIT = RANGE("0123456789") |
9 val DIGIT = RANGE("0123456789") |
9 val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) |
10 val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) |
28 case class T_ID(s: String) extends Token |
29 case class T_ID(s: String) extends Token |
29 case class T_OP(s: String) extends Token |
30 case class T_OP(s: String) extends Token |
30 case class T_NUM(s: String) extends Token |
31 case class T_NUM(s: String) extends Token |
31 case class T_KWD(s: String) extends Token |
32 case class T_KWD(s: String) extends Token |
32 |
33 |
33 val lexing_rules: List[Rule[Token]] = |
34 val lexing_rules: List[(Rexp, List[Char] => Token)] = |
34 List((KEYWORD, (s) => T_KWD(s.mkString)), |
35 List((KEYWORD, (s) => T_KWD(s.mkString)), |
35 (ID, (s) => T_ID(s.mkString)), |
36 (ID, (s) => T_ID(s.mkString)), |
36 (OP, (s) => T_OP(s.mkString)), |
37 (OP, (s) => T_OP(s.mkString)), |
37 (NUM, (s) => T_NUM(s.mkString)), |
38 (NUM, (s) => T_NUM(s.mkString)), |
38 (SEMI, (s) => T_SEMI), |
39 (SEMI, (s) => T_SEMI), |