progs/fun/fun_parser.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 17 Nov 2023 20:06:43 +0000
changeset 955 47acfd7f9096
parent 870 739039774cee
child 961 c0600f8b6427
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 parser 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_parser.sc fact.fun
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
     7
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     8
//     amm fun_parser.sc defs.fun
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     9
//
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    10
// this will generate a parse-tree from a list
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    11
// of tokens
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
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    16
import $file.fun_tokens, fun_tokens._ 
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
// Parser combinators
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
//    type parameter I needs to be of Seq-type
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
//
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    22
type IsSeq[I] = I => Seq[_]
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    23
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    24
/*
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    25
abstract class Parser[I, T](using is: I => Seq[_])  {
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    26
  def parse(in: I): Set[(T, I)]  
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    27
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    28
  def parse_all(in: I) : Set[T] =
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    29
    for ((hd, tl) <- parse(in); 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    30
        if is(tl).isEmpty) yield hd
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    31
}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    32
*/
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    33
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    34
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    35
abstract class Parser[I, T](using is: I => Seq[_]) {
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  def parse(ts: I): Set[(T, I)]
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    38
  def parse_single(ts: I) : T = 
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    39
    parse(ts).partition(p => is(p._2).isEmpty) match {
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    40
      case (good, _) if !good.isEmpty => good.head._1
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    41
      case (_, err) => { println (s"Parse Error\n${err.minBy(p => is(p._2).length)}") ; sys.exit(-1) }
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    42
    }
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// convenience for writing grammar rules
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
case class ~[+A, +B](_1: A, _2: B)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    48
// parser combinators
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    49
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    50
// alternative parser
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    51
class AltParser[I : IsSeq, T](p: => Parser[I, T], 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    52
                              q: => Parser[I, T]) extends Parser[I, T] {
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    53
  def parse(in: I) = p.parse(in) ++ q.parse(in)   
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    56
// sequence parser
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    57
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    58
                                 q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    59
  def parse(in: I) = 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    60
    for ((hd1, tl1) <- p.parse(in); 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    61
         (hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2)
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    64
// map parser
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    65
class MapParser[I : IsSeq, T, S](p: => Parser[I, T], 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    66
                                f: T => S) extends Parser[I, S] {
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    67
  def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    70
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    71
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    72
// more convenient syntax for parser combinators
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    73
extension [I : IsSeq, T](p: Parser[I, T]) {
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    74
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    76
  def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    79
def ListParser[I, T, S](p: => Parser[I, T], q: => Parser[I, S])(using is: I => Seq[_]): Parser[I, List[T]] = {
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    80
  (p ~ q ~ ListParser(p, q)).map{ case (x:T) ~ (y:S) ~ (z:List[T]) => x :: z } ||
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    81
  (p.map[List[T]]{s => List(s)})
644
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
case class TokParser(tok: Token) extends Parser[List[Token], Token] {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  def parse(ts: List[Token]) = ts match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
    case t::ts if (t == tok) => Set((t, ts)) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
    case _ => Set ()
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    91
implicit def token2tparser(t: Token) : Parser[List[Token], Token] = TokParser(t)
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    92
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    94
extension (t: Token) {
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q)
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    96
  def map[S] (f: => Token => S) = new MapParser[List[Token], Token, S](t, f)
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
case object NumParser extends Parser[List[Token], Int] {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  def parse(ts: List[Token]) = ts match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
    case T_NUM(n)::ts => Set((n, ts)) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
    case _ => Set ()
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
case object IdParser extends Parser[List[Token], String] {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  def parse(ts: List[Token]) = ts match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
    case T_ID(s)::ts => Set((s, ts)) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
    case _ => Set ()
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
}
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
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
// Abstract syntax trees for the Fun language
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
abstract class Exp extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
abstract class BExp extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
abstract class Decl extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
case class Def(name: String, args: List[String], body: Exp) extends Decl
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
case class Main(e: Exp) extends Decl
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
case class Call(name: String, args: List[Exp]) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
case class Write(e: Exp) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
case class Var(s: String) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
case class Num(i: Int) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
case class Sequence(e1: Exp, e2: Exp) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
// Grammar Rules for the Fun language
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
// arithmetic expressions
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
lazy val Exp: Parser[List[Token], Exp] = 
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   139
  (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp).map{ case _ ~ x ~ _ ~ y ~ _ ~ z => If(x, y, z): Exp } ||
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   140
  (M ~ T_SEMI ~ Exp).map{ case x ~ _ ~ y => Sequence(x, y): Exp } || M
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
lazy val M: Parser[List[Token], Exp] =
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   142
  (T_KWD("write") ~ L).map{ case _ ~ y => Write(y): Exp } || L
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
lazy val L: Parser[List[Token], Exp] = 
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   144
  (T ~ T_OP("+") ~ Exp).map{ case x ~ _ ~ z => Aop("+", x, z): Exp } ||
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   145
  (T ~ T_OP("-") ~ Exp).map{ case x ~ _ ~ z => Aop("-", x, z): Exp } || T  
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
lazy val T: Parser[List[Token], Exp] = 
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   147
  (F ~ T_OP("*") ~ T).map{ case x ~ _ ~ z => Aop("*", x, z): Exp } || 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   148
  (F ~ T_OP("/") ~ T).map{ case x ~ _ ~ z => Aop("/", x, z): Exp } || 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   149
  (F ~ T_OP("%") ~ T).map{ case x ~ _ ~ z => Aop("%", x, z): Exp } || F
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
lazy val F: Parser[List[Token], Exp] = 
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   151
  (IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN).map
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
    { case x ~ _ ~ z ~ _ => Call(x, z): Exp } ||
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   153
  (T_LPAREN ~ Exp ~ T_RPAREN).map{ case _ ~ y ~ _ => y: Exp } || 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   154
  IdParser.map{ case x => Var(x): Exp } || 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   155
  NumParser.map{ case x => Num(x): Exp }
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
// boolean expressions
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
lazy val BExp: Parser[List[Token], BExp] = 
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   159
  (Exp ~ T_OP("==") ~ Exp).map{ case x ~ _ ~ z => Bop("==", x, z): BExp } || 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   160
  (Exp ~ T_OP("!=") ~ Exp).map{ case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   161
  (Exp ~ T_OP("<") ~ Exp) .map{ case x ~ _ ~ z => Bop("<",  x, z): BExp } || 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   162
  (Exp ~ T_OP(">") ~ Exp) .map{ case x ~ _ ~ z => Bop("<",  z, x): BExp } || 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   163
  (Exp ~ T_OP("<=") ~ Exp).map{ case x ~ _ ~ z => Bop("<=", x, z): BExp } || 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   164
  (Exp ~ T_OP("=>") ~ Exp).map{ case x ~ _ ~ z => Bop("<=", z, x): BExp }  
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
lazy val Defn: Parser[List[Token], Decl] =
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   167
   (T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(IdParser, T_COMMA) ~ T_RPAREN ~ T_OP("=") ~ Exp).map{ case _ ~ y ~ _ ~ w ~ _ ~ _ ~ r => Def(y, w, r): Decl }
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
lazy val Prog: Parser[List[Token], List[Decl]] =
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   170
  (Defn ~ T_SEMI ~ Prog).map{ case x ~ _ ~ z => x :: z : List[Decl] } ||
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   171
  (Exp.map((s) => List(Main(s)) : List[Decl]))
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
// Reading tokens and Writing parse trees
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 822
diff changeset
   177
// pre-2.5.0 ammonite 
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 822
diff changeset
   178
// import ammonite.ops._
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 822
diff changeset
   179
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 822
diff changeset
   180
// post 2.5.0 ammonite
870
739039774cee ivy import not needed
Christian Urban <christian.urban@kcl.ac.uk>
parents: 869
diff changeset
   181
// import os._
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   182
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   183
def parse_tks(tks: List[Token]) : List[Decl] = 
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   184
  Prog.parse_single(tks)
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
822
6b06aeb192ea updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   186
//@doc("Parses a file.")
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   187
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   188
def main(fname: String) : Unit = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   189
  val tks = tokenise(os.read(os.pwd / fname))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   190
  println(parse_tks(tks))
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193