# HG changeset patch # User Christian Urban # Date 1512481787 0 # Node ID 37a3db7cd655a8e49868059c0df4dcd02ccd2a69 # Parent 885cf83ebce35a73bab60a1d43c467ae9e38767e updated diff -r 885cf83ebce3 -r 37a3db7cd655 pics/bridge-limits.png Binary file pics/bridge-limits.png has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/code.jpg Binary file pics/code.jpg has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/ken-thompson.jpg Binary file pics/ken-thompson.jpg has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/marspath1.png Binary file pics/marspath1.png has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/marspath3.png Binary file pics/marspath3.png has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/marsrover.png Binary file pics/marsrover.png has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/p1.pdf Binary file pics/p1.pdf has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/p2.jpg Binary file pics/p2.jpg has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/p3.jpg Binary file pics/p3.jpg has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/p4.jpg Binary file pics/p4.jpg has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/state.png Binary file pics/state.png has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/state2.png Binary file pics/state2.png has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/state3.jpg Binary file pics/state3.jpg has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/state4.jpg Binary file pics/state4.jpg has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/state5.png Binary file pics/state5.png has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/state6.png Binary file pics/state6.png has changed diff -r 885cf83ebce3 -r 37a3db7cd655 pics/towerbridge.jpg Binary file pics/towerbridge.jpg has changed diff -r 885cf83ebce3 -r 37a3db7cd655 progs/token.scala --- 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) diff -r 885cf83ebce3 -r 37a3db7cd655 slides/slides10.pdf Binary file slides/slides10.pdf has changed