progs/fun_tokens.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 14 Oct 2019 00:02:24 +0100
changeset 655 3d04ee04966d
parent 645 30943d5491b6
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
//
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     6
//     scala fun_tokens.scala fact.fun
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     7
//
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     8
//     scala fun_tokens.scala defs.fun
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     9
//
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    10
// this will generate a .tks file that can be deserialised back
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    11
// into a list of tokens
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    12
// you can add -Xno-patmat-analysis in order to get rid of the
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    13
// match-not-exhaustive warning
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
object Fun_Tokens {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
import scala.language.implicitConversions    
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
import scala.language.reflectiveCalls 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
abstract class Rexp 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case object ZERO extends Rexp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case object ONE extends Rexp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case class CHAR(c: Char) extends Rexp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case class STAR(r: Rexp) extends Rexp 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case class RECD(x: String, r: Rexp) extends Rexp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
abstract class Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
case object Empty extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
case class Chr(c: Char) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
case class Sequ(v1: Val, v2: Val) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
case class Left(v: Val) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
case class Right(v: Val) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
case class Stars(vs: List[Val]) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
case class Rec(x: String, v: Val) extends Val
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
   
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
// some convenience for typing in regular expressions
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
def charlist2rexp(s : List[Char]): Rexp = s match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  case Nil => ONE
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  case c::Nil => CHAR(c)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
implicit def string2rexp(s : String) : Rexp = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  charlist2rexp(s.toList)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
implicit def RexpOps(r: Rexp) = new {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  def | (s: Rexp) = ALT(r, s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  def % = STAR(r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  def ~ (s: Rexp) = SEQ(r, s)
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
implicit def stringOps(s: String) = new {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  def | (r: Rexp) = ALT(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  def | (r: String) = ALT(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  def % = STAR(s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  def ~ (r: Rexp) = SEQ(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  def ~ (r: String) = SEQ(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  def $ (r: Rexp) = RECD(s, r)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
def nullable (r: Rexp) : Boolean = r match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  case ZERO => false
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case ONE => true
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case CHAR(_) => false
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  case STAR(_) => true
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  case RECD(_, r1) => nullable(r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
def der (c: Char, r: Rexp) : Rexp = r match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  case ZERO => ZERO
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  case ONE => ZERO
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  case CHAR(d) => if (c == d) ONE else ZERO
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  case SEQ(r1, r2) => 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
    else SEQ(der(c, r1), r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  case STAR(r) => SEQ(der(c, r), STAR(r))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  case RECD(_, r1) => der(c, r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
// extracts a string from value
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
def flatten(v: Val) : String = v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  case Empty => ""
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  case Chr(c) => c.toString
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  case Left(v) => flatten(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  case Right(v) => flatten(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  case Stars(vs) => vs.map(flatten).mkString
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  case Rec(_, v) => flatten(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
// extracts an environment from a value;
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
// used for tokenise a string
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
def env(v: Val) : List[(String, String)] = v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  case Empty => Nil
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  case Chr(c) => Nil
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  case Left(v) => env(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  case Right(v) => env(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  case Sequ(v1, v2) => env(v1) ::: env(v2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  case Stars(vs) => vs.flatMap(env)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  case Rec(x, v) => (x, flatten(v))::env(v)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
// The Injection Part of the lexer
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 mkeps(r: Rexp) : Val = r match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  case ONE => Empty
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  case ALT(r1, r2) => 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  case STAR(r) => Stars(Nil)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  case RECD(x, r) => Rec(x, mkeps(r))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  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
   123
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  case (CHAR(d), Empty) => Chr(c) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
  case _ => { println ("Injection error") ; sys.exit(-1) } 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
// some "rectification" functions for simplification
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
def F_ID(v: Val): Val = v
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  case Right(v) => Right(f2(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  case Left(v) => Left(f1(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
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 F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  (v:Val) => Sequ(f1(Empty), f2(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
  (v:Val) => Sequ(f1(v), f2(Empty))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
def F_RECD(f: Val => Val) = (v:Val) => v match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
  case Rec(x, v) => Rec(x, f(v))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
def F_ERROR(v: Val): Val = throw new Exception("error")
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
def simp(r: Rexp): (Rexp, Val => Val) = r match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
  case ALT(r1, r2) => {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
    val (r1s, f1s) = simp(r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
    val (r2s, f2s) = simp(r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
    (r1s, r2s) match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
      case (_, ZERO) => (r1s, F_LEFT(f1s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
    }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  case SEQ(r1, r2) => {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
    val (r1s, f1s) = simp(r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
    val (r2s, f2s) = simp(r2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
    (r1s, r2s) match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
      case (ZERO, _) => (ZERO, F_ERROR)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
      case (_, ZERO) => (ZERO, F_ERROR)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
    }
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 RECD(x, r1) => {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
    val (r1s, f1s) = simp(r1)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
    (RECD(x, r1s), F_RECD(f1s))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
  case r => (r, F_ID)
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
// lexing functions including simplification
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
  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
   183
  case c::cs => {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
    val (r_simp, f_simp) = simp(der(c, r))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
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
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
// The Lexing Rules for the Fun Language
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
def PLUS(r: Rexp) = r ~ r.%
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
          "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
          "w" | "x" | "y" | "z" | "T" | "_"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
val ID = SYM ~ (SYM | DIGIT).% 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
val NUM = PLUS(DIGIT)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
val KEYWORD : Rexp = "if" | "then" | "else" | "write" | "def"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
val SEMI: Rexp = ";"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
val OP: Rexp = "=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
val WHITESPACE = PLUS(" " | "\n" | "\t")
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
val RPAREN: Rexp = ")"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
val LPAREN: Rexp = "("
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
val COMMA: Rexp = ","
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
val ALL = SYM | DIGIT | OP | " " | ":" | ";" | "\"" | "=" | "," | "(" | ")"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
val ALL2 = ALL | "\n"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
val COMMENT = ("/*" ~ ALL2.% ~ "*/") | ("//" ~ ALL.% ~ "\n")
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   214
val FUN_REGS = (("k" $ KEYWORD) | 
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
                  ("i" $ ID) | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
                  ("o" $ OP) | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
                  ("n" $ NUM) | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
                  ("s" $ SEMI) | 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
                  ("c" $ COMMA) |
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
                  ("pl" $ LPAREN) |
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
                  ("pr" $ RPAREN) |
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
                  ("w" $ (WHITESPACE | COMMENT))).%
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
// The tokens for the Fun language
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
import java.io._
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
abstract class Token extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
case object T_SEMI extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
case object T_COMMA extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
case object T_LPAREN extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
case object T_RPAREN extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
case class T_ID(s: String) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
case class T_OP(s: String) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
case class T_NUM(n: Int) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
case class T_KWD(s: String) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
val token : PartialFunction[(String, String), Token] = {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
  case ("k", s) => T_KWD(s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  case ("i", s) => T_ID(s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
  case ("o", s) => T_OP(s)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
  case ("n", s) => T_NUM(s.toInt)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
  case ("s", _) => T_SEMI
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
  case ("c", _) => T_COMMA
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
  case ("pl", _) => T_LPAREN
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
  case ("pr", _) => T_RPAREN
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   252
def tokenise(s: String) : List[Token] = {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   253
  val tks = lexing_simp(FUN_REGS, s).collect(token)
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   254
  if (tks.length != 0) tks
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   255
  else { println (s"Tokenise Error") ; sys.exit(-1) }     
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   256
}
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
def serialise[T](fname: String, data: T) = {
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   259
  import scala.util.Using
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   260
  Using(new ObjectOutputStream(new FileOutputStream(fname))) {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   261
    out => out.writeObject(data)
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   262
  }
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   265
def main(args: Array[String]) : Unit = {
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
  val fname = args(0)
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   267
  val tname = fname.stripSuffix(".fun") ++ ".tks"
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
  val file = io.Source.fromFile(fname).mkString
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   269
  serialise(tname, tokenise(file))
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
   273
}