while.scala
changeset 69 cc3f7908b942
parent 68 a09ecb1e7384
child 70 e6868bd2942b
equal deleted inserted replaced
68:a09ecb1e7384 69:cc3f7908b942
     2 //:load matcher.scala
     2 //:load matcher.scala
     3 
     3 
     4 // some regular expressions
     4 // some regular expressions
     5 val SYM = RANGE("""ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_""")
     5 val SYM = RANGE("""ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_""")
     6 val DIGIT = RANGE("0123456789")
     6 val DIGIT = RANGE("0123456789")
     7 val ID = SEQ(PLUS(SYM)) ~ STAR(ALT(SYM, DIGIT)) 
     7 val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) 
     8 
     8 val NUM = PLUS(DIGIT)
     9 val BTAG = SEQS("<", WORD, ">") 
     9 val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "begin", "end", "true", "false") 
    10 val ETAG = SEQS("</", WORD, ">")
    10 val SEMI: Rexp = ";"
    11 
    11 val OP: Rexp = ALTS(":=", "=", "+", "-", "*")
    12 val WHITESPACE = PLUS(RANGE(" \n"))
    12 val WHITESPACE = PLUS(RANGE(" \n"))
       
    13 val RPAREN: Rexp = ")"
       
    14 val LPAREN: Rexp = "("
       
    15 val BEGIN: Rexp = "{"
       
    16 val END: Rexp = "}"
    13 
    17 
    14 // for classifying the strings that have been recognised
    18 // for classifying the strings that have been recognised
    15 abstract class Token
    19 abstract class Token
    16 case object T_WHITESPACE extends Token
    20 case object T_WHITESPACE extends Token
    17 case class T_WORD(s: String) extends Token
    21 case object T_SEMI extends Token
    18 case class T_ETAG(s: String) extends Token
    22 case object T_LPAREN extends Token
    19 case class T_BTAG(s: String) extends Token
    23 case object T_RPAREN extends Token
    20 case class T_NT(s: String, rhs: List[Token]) extends Token
    24 case object T_BEGIN extends Token
       
    25 case object T_END extends Token
       
    26 case class T_ID(s: String) extends Token
       
    27 case class T_OP(s: String) extends Token
       
    28 case class T_NUM(s: String) extends Token
       
    29 case class T_KWD(s: String) extends Token
       
    30 
    21 
    31 
    22 val lexing_rules: List[Rule[Token]] = 
    32 val lexing_rules: List[Rule[Token]] = 
    23   List((BTAG, (s) => T_BTAG(s.mkString)),
    33   List((KEYWORD, (s) => T_KWD(s.mkString)),
    24        (ETAG, (s) => T_ETAG(s.mkString)),
    34        (ID, (s) => T_ID(s.mkString)),
    25        (WORD, (s) => T_WORD(s.mkString)),
    35        (OP, (s) => T_OP(s.mkString)),
       
    36        (NUM, (s) => T_NUM(s.mkString)),
       
    37        (SEMI, (s) => T_SEMI),
       
    38        (LPAREN, (s) => T_LPAREN),
       
    39        (RPAREN, (s) => T_RPAREN),
       
    40        (BEGIN, (s) => T_BEGIN),
       
    41        (END, (s) => T_END),
    26        (WHITESPACE, (s) => T_WHITESPACE))
    42        (WHITESPACE, (s) => T_WHITESPACE))
    27 
    43 
    28 // the tokenizer
    44 // the tokenizer
    29 val T = Tokenizer(lexing_rules)
    45 val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE))
    30 
    46 
    31 // width for printing
    47 // the abstract syntax trees
    32 val WIDTH = 60
    48 abstract class Stmt
       
    49 abstract class AExp
       
    50 abstract class BExp 
       
    51 type Block = List[Stmt]
       
    52 case object Skip extends Stmt
       
    53 case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
       
    54 case class While(b: BExp, bl: Block) extends Stmt
       
    55 case class Assign(s: String, a: AExp) extends Stmt
       
    56 case class Var(s: String) extends AExp
       
    57 case class Num(i: Int) extends AExp
       
    58 case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
       
    59 case object True extends BExp
       
    60 case object False extends BExp
       
    61 case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
    33 
    62 
    34 
    63 
    35 def interpret(ts: List[Token], c: Int, ctr: List[String]) : Unit= ts match {
    64 case class TokParser(tok: Token) extends Parser[List[Token], Token] {
    36   case Nil => println(Console.RESET)
    65   def parse(ts: List[Token]) = ts match {
    37   case T_WHITESPACE::rest => print(Console.RESET + " "); interpret(rest, c + 1, ctr)
    66     case t::ts if (t == tok) => Set((t, ts)) 
    38   case T_WORD(s)::rest => {
    67     case _ => Set ()
    39     val newstr = Console.RESET + ctr.reverse.mkString + s
       
    40     if (c + s.length < WIDTH) {
       
    41       print(newstr);
       
    42       interpret(rest, c + s.length, ctr)
       
    43     }
       
    44     else {
       
    45       print("\n" + newstr)
       
    46       interpret(rest, s.length, ctr)
       
    47     } 
       
    48   }
    68   }
    49   case T_BTAG("<p>")::rest => print("\n"); interpret(rest, 0, ctr)
       
    50   case T_ETAG("</p>")::rest => print("\n"); interpret(rest, 0, ctr)
       
    51   case T_BTAG("<b>")::rest => interpret(rest, c, Console.BOLD :: ctr)
       
    52   case T_BTAG("<a>")::rest => interpret(rest, c, Console.UNDERLINED :: ctr)
       
    53   case T_BTAG("<cyan>")::rest => interpret(rest, c, Console.CYAN :: ctr)
       
    54   case T_BTAG("<red>")::rest => interpret(rest, c, Console.RED :: ctr)
       
    55   case T_BTAG("<blink>")::rest => interpret(rest, c, Console.BLINK :: ctr)
       
    56   case T_ETAG(_)::rest => interpret(rest, c, ctr.tail)
       
    57   case _::rest => interpret(rest, c, ctr)
       
    58 }
    69 }
    59  
    70 implicit def token2tparser(t: Token) = TokParser(t)
    60 interpret(T.fromFile("test.html"), 0, Nil)
    71 
       
    72 case object NumParser extends Parser[List[Token], Int] {
       
    73   def parse(ts: List[Token]) = ts match {
       
    74     case T_NUM(s)::ts => Set((s.toInt, ts)) 
       
    75     case _ => Set ()
       
    76   }
       
    77 }
       
    78 
       
    79 case object IdParser extends Parser[List[Token], String] {
       
    80   def parse(ts: List[Token]) = ts match {
       
    81     case T_ID(s)::ts => Set((s, ts)) 
       
    82     case _ => Set ()
       
    83   }
       
    84 }
       
    85 
       
    86 
       
    87 lazy val AExp: Parser[List[Token], AExp] = 
       
    88   (T ~ T_OP("+") ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } ||
       
    89   (T ~ T_OP("-") ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || T  
       
    90 lazy val T: Parser[List[Token], AExp] = 
       
    91   (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || F
       
    92 lazy val F: Parser[List[Token], AExp] = 
       
    93   (T_LPAREN ~> AExp <~ T_RPAREN) || 
       
    94   IdParser ==> ((s) => Var(s)) || 
       
    95   NumParser ==> ((i) => Num(i))
       
    96 
       
    97 lazy val BExp: Parser[List[Token], BExp] = 
       
    98   (AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } || 
       
    99   (T_KWD("true") ==> ((_) => True: BExp)) || 
       
   100   (T_KWD("false") ==> ((_) => False: BExp))
       
   101 
       
   102 lazy val Stmt: Parser[List[Token], Stmt] =
       
   103   (T_KWD("skip") ==> ((_) => Skip: Stmt)) ||
       
   104   (IdParser ~ T_OP(":=") ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } ||
       
   105   (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==>
       
   106     { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt }
       
   107 
       
   108 lazy val Stmts: Parser[List[Token], Block] =
       
   109   (Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
       
   110   (Stmt ==> ((s) => List(s) : Block))
       
   111 
       
   112 lazy val Block: Parser[List[Token], Block] =
       
   113   (T_BEGIN ~> Stmts <~ T_END) || 
       
   114   (Stmt ==> ((s) => List(s)))
       
   115 
       
   116 val p1 = "x := 5"
       
   117 val p1_toks = Tok.fromString(p1) 
       
   118 val p1_ast = Block.parse_all(p1_toks)
       
   119 println(p1_toks)
       
   120 println(p1_ast)
       
   121 
       
   122 val p2 = "5 = 6"
       
   123 val p2_toks = Tok.fromString(p2) 
       
   124 val p2_ast = BExp.parse_all(p2_toks)
       
   125 println(p2_toks)
       
   126 println(p2_ast)
       
   127 
       
   128 val p2a = "true"
       
   129 val p2a_toks = Tok.fromString(p2a) 
       
   130 val p2a_ast = BExp.parse_all(p2a_toks)
       
   131 println(p2a_toks)
       
   132 println(p2a_ast)
       
   133 
       
   134 val p3 = "if true then skip else skip"
       
   135 val p3_toks = Tok.fromString(p3) 
       
   136 val p3_ast = Stmt.parse_all(p3_toks)
       
   137 println(p3_toks)
       
   138 println(p3_ast)
       
   139 
       
   140 val p3a = "if true then x := 5 else x := 10"
       
   141 val p3a_toks = Tok.fromString(p3a) 
       
   142 val p3a_ast = Stmt.parse_all(p3a_toks)
       
   143 println(p3a_toks)
       
   144 println(p3a_ast)
       
   145 
       
   146 val p3b = "if false then x := 5 else x := 10"
       
   147 val p3b_toks = Tok.fromString(p3b) 
       
   148 val p3b_ast = Stmt.parse_all(p3b_toks)
       
   149 println(p3b_toks)
       
   150 println(p3b_ast)
       
   151 
       
   152 
       
   153 type Env = Map[String, Int]
       
   154 
       
   155 def eval_bexp(b: BExp, env: Env) : Boolean = b match {
       
   156   case True => true
       
   157   case False => false
       
   158   case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
       
   159 }
       
   160 
       
   161 def eval_aexp(a: AExp, env : Env) : Int = a match {
       
   162   case Num(i) => i
       
   163   case Var(s) => env(s)
       
   164   case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
       
   165   case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
       
   166   case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
       
   167 }
       
   168 
       
   169 def eval_stmt(s: Stmt, env: Env) : Env = s match {
       
   170   case Skip => env
       
   171   case Assign(x, a) => env + (x -> eval_aexp(a, env))
       
   172   case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) 
       
   173 }
       
   174 
       
   175 def eval_bl(bl: Block, env: Env) : Env = bl match {
       
   176   case Nil => env
       
   177   case s::bl => eval_bl(bl, eval_stmt(s, env))
       
   178 }
       
   179 
       
   180 //println(eval_stmt(p3a_ast.head, Nil.toMap))
       
   181 //println(eval_stmt(p3b_ast.head, Nil.toMap))