progs/token.scala
changeset 542 37a3db7cd655
parent 529 5c28e4134ee1
child 549 352d15782d35
--- a/progs/token.scala	Sun Jan 01 01:55:12 2017 +0000
+++ b/progs/token.scala	Tue Dec 05 13:49:47 2017 +0000
@@ -8,7 +8,7 @@
 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
 case class STAR(r: Rexp) extends Rexp 
-case class RECD(x: String, r: Rexp) extends Rexp
+case class RECD[A](f: String => A, r: Rexp) extends Rexp
 
 abstract class Val
 case object Empty extends Val
@@ -17,7 +17,7 @@
 case class Left(v: Val) extends Val
 case class Right(v: Val) extends Val
 case class Stars(vs: List[Val]) extends Val
-case class Rec(x: String, v: Val) extends Val
+case class Rec[A](f: String => A, v: Val) extends Val
    
 // some convenience for typing in regular expressions
 def charlist2rexp(s : List[Char]): Rexp = s match {
@@ -79,20 +79,20 @@
   case Chr(c) => c.toString
   case Left(v) => flatten(v)
   case Right(v) => flatten(v)
-  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
+  case Sequ(v1, v2) => flatten(v1) ++ flatten(v2)
   case Stars(vs) => vs.map(flatten).mkString
   case Rec(_, v) => flatten(v)
 }
 
 // extracts an environment from a value
-def env(v: Val) : List[(String, String)] = v match {
+def env[A](v: Val) : List[A] = v match {
   case Empty => Nil
   case Chr(c) => Nil
   case Left(v) => env(v)
   case Right(v) => env(v)
   case Sequ(v1, v2) => env(v1) ::: env(v2)
   case Stars(vs) => vs.flatMap(env)
-  case Rec(x, v) => (x, flatten(v))::env(v)
+  case Rec(f, v) => ((f:String => A)(flatten(v)))::env(v)
 }
 
 // injection part
@@ -211,7 +211,7 @@
 val STRING: Rexp = "\"" ~ SYM.% ~ "\""
 
 
-val WHILE_REGS = (("k" $ KEYWORD) | 
+val WHILE_REGS = (((s) => Token $ KEYWORD) | 
                   ("i" $ ID) | 
                   ("o" $ OP) | 
                   ("n" $ NUM) | 
@@ -221,6 +221,20 @@
                   ("b" $ (BEGIN | END)) | 
                   ("w" $ WHITESPACE)).%
 
+
+
+
+// extracts an environment from a value
+def env(v: Val) : List[Token] = v match {
+  case Empty => Nil
+  case Chr(c) => Nil
+  case Left(v) => env(v)
+  case Right(v) => env(v)
+  case Sequ(v1, v2) => env(v1) ::: env(v2)
+  case Stars(vs) => vs.flatMap(env)
+  case Rec(f, v) => (f(flatten(v)))::env(v)
+}
+
 //   Testing
 //============
 
@@ -282,4 +296,8 @@
   time(lexing_simp(WHILE_REGS, prog2 * i))
 }
 
+abstract class Token
+case class KeyToken(s: String) extends Token
+case class IdToken(s: String) extends Token
 
+list[(STRING, STRING)]=> List(TOKEN)