progs/fun/fun_tokens.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 01 Dec 2020 05:41:12 +0000
changeset 813 059f970287d1
parent 789 f0696713177b
child 822 6b06aeb192ea
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     1
// A tokeniser for the Fun language
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     2
//==================================
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     3
//
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     4
// call with 
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     5
//
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     6
//     amm fun_tokens.sc fact.fun
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     7
//
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     8
//     amm fun_tokens.sc defs.fun
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     9
//
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    11
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
import scala.language.implicitConversions    
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
import scala.language.reflectiveCalls 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
abstract class Rexp 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
case object ZERO extends Rexp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
case object ONE extends Rexp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
case class CHAR(c: Char) extends Rexp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case class STAR(r: Rexp) extends Rexp 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case class RECD(x: String, r: Rexp) extends Rexp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
abstract class Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case object Empty extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case class Chr(c: Char) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
case class Sequ(v1: Val, v2: Val) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
case class Left(v: Val) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
case class Right(v: Val) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
case class Stars(vs: List[Val]) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
case class Rec(x: String, v: Val) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
   
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// some convenience for typing in regular expressions
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
def charlist2rexp(s : List[Char]): Rexp = s match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  case Nil => ONE
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  case c::Nil => CHAR(c)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
implicit def string2rexp(s : String) : Rexp = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  charlist2rexp(s.toList)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
implicit def RexpOps(r: Rexp) = new {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  def | (s: Rexp) = ALT(r, s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  def % = STAR(r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  def ~ (s: Rexp) = SEQ(r, s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
implicit def stringOps(s: String) = new {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  def | (r: Rexp) = ALT(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  def | (r: String) = ALT(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  def % = STAR(s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  def ~ (r: Rexp) = SEQ(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  def ~ (r: String) = SEQ(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  def $ (r: Rexp) = RECD(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
def nullable (r: Rexp) : Boolean = r match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  case ZERO => false
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  case ONE => true
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  case CHAR(_) => false
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case STAR(_) => true
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case RECD(_, r1) => nullable(r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
def der (c: Char, r: Rexp) : Rexp = r match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  case ZERO => ZERO
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  case ONE => ZERO
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  case CHAR(d) => if (c == d) ONE else ZERO
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  case SEQ(r1, r2) => 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
    else SEQ(der(c, r1), r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  case STAR(r) => SEQ(der(c, r), STAR(r))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  case RECD(_, r1) => der(c, r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// extracts a string from value
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
def flatten(v: Val) : String = v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  case Empty => ""
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  case Chr(c) => c.toString
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  case Left(v) => flatten(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  case Right(v) => flatten(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  case Stars(vs) => vs.map(flatten).mkString
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  case Rec(_, v) => flatten(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
// extracts an environment from a value;
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
// used for tokenise a string
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
def env(v: Val) : List[(String, String)] = v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  case Empty => Nil
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
  case Chr(c) => Nil
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  case Left(v) => env(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  case Right(v) => env(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  case Sequ(v1, v2) => env(v1) ::: env(v2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  case Stars(vs) => vs.flatMap(env)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  case Rec(x, v) => (x, flatten(v))::env(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
// The Injection Part of the lexer
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
def mkeps(r: Rexp) : Val = r match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  case ONE => Empty
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  case ALT(r1, r2) => 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  case STAR(r) => Stars(Nil)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  case RECD(x, r) => Rec(x, mkeps(r))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  case (CHAR(d), Empty) => Chr(c) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
  case _ => { println ("Injection error") ; sys.exit(-1) } 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
// some "rectification" functions for simplification
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
def F_ID(v: Val): Val = v
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
  case Right(v) => Right(f2(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  case Left(v) => Left(f1(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  (v:Val) => Sequ(f1(Empty), f2(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
  (v:Val) => Sequ(f1(v), f2(Empty))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
def F_RECD(f: Val => Val) = (v:Val) => v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  case Rec(x, v) => Rec(x, f(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
def F_ERROR(v: Val): Val = throw new Exception("error")
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
def simp(r: Rexp): (Rexp, Val => Val) = r match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  case ALT(r1, r2) => {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
    val (r1s, f1s) = simp(r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
    val (r2s, f2s) = simp(r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
    (r1s, r2s) match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
      case (_, ZERO) => (r1s, F_LEFT(f1s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
    }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
  case SEQ(r1, r2) => {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
    val (r1s, f1s) = simp(r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
    val (r2s, f2s) = simp(r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
    (r1s, r2s) match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
      case (ZERO, _) => (ZERO, F_ERROR)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
      case (_, ZERO) => (ZERO, F_ERROR)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
    }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
  case RECD(x, r1) => {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
    val (r1s, f1s) = simp(r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
    (RECD(x, r1s), F_RECD(f1s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
  case r => (r, F_ID)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
// lexing functions including simplification
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
  case Nil => if (nullable(r)) mkeps(r) else { println ("Lexing Error") ; sys.exit(-1) } 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
  case c::cs => {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
    val (r_simp, f_simp) = simp(der(c, r))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
// The Lexing Rules for the Fun Language
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
def PLUS(r: Rexp) = r ~ r.%
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
          "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
          "w" | "x" | "y" | "z" | "T" | "_"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
val ID = SYM ~ (SYM | DIGIT).% 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
val NUM = PLUS(DIGIT)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
val KEYWORD : Rexp = "if" | "then" | "else" | "write" | "def"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
val SEMI: Rexp = ";"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
val OP: Rexp = "=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
val WHITESPACE = PLUS(" " | "\n" | "\t")
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
val RPAREN: Rexp = ")"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
val LPAREN: Rexp = "("
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
val COMMA: Rexp = ","
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
val ALL = SYM | DIGIT | OP | " " | ":" | ";" | "\"" | "=" | "," | "(" | ")"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
val ALL2 = ALL | "\n"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
val COMMENT = ("/*" ~ ALL2.% ~ "*/") | ("//" ~ ALL.% ~ "\n")
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   210
val FUN_REGS = (("k" $ KEYWORD) | 
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
                  ("i" $ ID) | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
                  ("o" $ OP) | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
                  ("n" $ NUM) | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
                  ("s" $ SEMI) | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
                  ("c" $ COMMA) |
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
                  ("pl" $ LPAREN) |
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
                  ("pr" $ RPAREN) |
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
                  ("w" $ (WHITESPACE | COMMENT))).%
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
// The tokens for the Fun language
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
abstract class Token extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
case object T_SEMI extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
case object T_COMMA extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
case object T_LPAREN extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
case object T_RPAREN extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
case class T_ID(s: String) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
case class T_OP(s: String) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
case class T_NUM(n: Int) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
case class T_KWD(s: String) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
val token : PartialFunction[(String, String), Token] = {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
  case ("k", s) => T_KWD(s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
  case ("i", s) => T_ID(s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
  case ("o", s) => T_OP(s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
  case ("n", s) => T_NUM(s.toInt)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
  case ("s", _) => T_SEMI
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
  case ("c", _) => T_COMMA
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
  case ("pl", _) => T_LPAREN
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  case ("pr", _) => T_RPAREN
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   246
def tokenise(s: String) : List[Token] = {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   247
  val tks = lexing_simp(FUN_REGS, s).collect(token)
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   248
  if (tks.length != 0) tks
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   249
  else { println (s"Tokenise Error") ; sys.exit(-1) }     
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   250
}
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   252
import ammonite.ops._
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   254
@doc("Tokenising a file.")
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   255
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   256
def main(fname: String) = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   257
  println(tokenise(os.read(os.pwd / fname)))
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
}