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