solutions/cw5/fun_parser.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 25 Oct 2024 18:54:08 +0100
changeset 971 51e00f223792
parent 920 7af2eea19646
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     1
// Author: Zhuo Ying Jiang Li
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     2
// Starting code by Dr Christian Urban
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     3
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     4
// parser: convert sequence of tokens to AST
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     5
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
//
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     7
// Use this command to print parsed AST:
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     8
// amm fun_parser.sc <name>.fun
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    11
import $file.fun_tokens, fun_tokens._
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    13
// more convenience for the map parsers later on;
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    14
// it allows writing nested patterns as
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    15
// case x ~ y ~ z => ...
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    16
case class ~[+A, +B](x: A, y: B)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    18
// parser combinators
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    19
abstract class Parser[I, T](using is: I => Seq[_])  {
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    20
  def parse(in: I): Set[(T, I)]  
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    21
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    22
  def parse_all(in: I) : Set[T] =
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    23
    for ((hd, tl) <- parse(in); 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    24
        if is(tl).isEmpty) yield hd
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    27
// alternative parser
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    28
class AltParser[I, T](p: => Parser[I, T], 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    29
                      q: => Parser[I, T])(using I => Seq[_]) extends Parser[I, T] {
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    30
  def parse(in: I) = p.parse(in) ++ q.parse(in)   
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    31
}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    33
// sequence parser
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    34
class SeqParser[I, T, S](p: => Parser[I, T], 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    35
                         q: => Parser[I, S])(using I => Seq[_]) extends Parser[I, ~[T, S]] {
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    36
  def parse(in: I) = 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    37
    for ((hd1, tl1) <- p.parse(in); 
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    38
         (hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    41
// map parser
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    42
class MapParser[I, T, S](p: => Parser[I, T], 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    43
                         f: T => S)(using I => Seq[_]) extends Parser[I, S] {
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    44
  def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    47
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    48
// more convenient syntax for parser combinators
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    49
extension [I, T](p: Parser[I, T])(using I => Seq[_]) {
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    50
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    52
  def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
    55
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    56
// -------------------------------------------------
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    57
// atomic parsers
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    59
// atomic parser for types
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    60
case class TypeParser(ty: Set[String]) extends Parser[Tokens, String] {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    61
  def parse(tokens: Tokens) = tokens match {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    62
    case Nil => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    63
    case tk::tkns if tk._1 == "type" && ty.contains(tk._2) => Set((tk._2, tkns))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
    case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    68
// atomic parser for global ids
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    69
case object GlobalIdParser extends Parser[Tokens, String] {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    70
  def parse(tokens: Tokens) = tokens match {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    71
    case Nil => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    72
    case tk::tkns if tk._1 == "global" => Set((tk._2, tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    73
    case _ => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    74
  }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    77
// atomic parser for ids
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    78
case object IdParser extends Parser[Tokens, String] {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    79
  def parse(tokens: Tokens) = tokens match {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    80
    case Nil => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    81
    case tk::tkns if tk._1 == "id" => Set((tk._2, tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    82
    case _ => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    83
  }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    86
// atomic parser for doubles (I use Float because that's what is used in the AST structures given in CW5)
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    87
case object DoubleParser extends Parser[Tokens, Float] {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    88
  def parse(tokens: Tokens) = tokens match {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    89
    case Nil => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    90
    case tk::tkns if tk._1 == "double" => Set((tk._2.toFloat, tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    91
    case _ => Set()
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    95
// atomic parser for integers
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    96
case object IntParser extends Parser[Tokens, Int] {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    97
  def parse(tokens: Tokens) = tokens match {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    98
    case Nil => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    99
    case tk::tkns if tk._1 == "int" => Set((tk._2.toInt, tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   100
    case _ => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   101
  }
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   102
}
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   103
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   104
// atomic parser for operators
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   105
case class OpParser(ops: Set[String]) extends Parser[Tokens, String] {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   106
  def parse(tokens: Tokens) = tokens match {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   107
    case Nil => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   108
    case tk::tkns if tk._1 == "op" && ops.contains(tk._2) => Set((tk._2, tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   109
    case _ => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   110
  }
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   111
}
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   112
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   113
// atomic parser for character
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   114
case object CharParser extends Parser[Tokens, Char] {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   115
  def parse(tokens: Tokens) = tokens match {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   116
    case Nil => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   117
    case tk::tkns if tk._1 == "ch" => {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   118
      val stripped = tk._2.slice(1, tk._2.length-1)  // strip off single quotes
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   119
      stripped match {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   120
        case "\\n" => Set(('\n', tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   121
        case "\\t" => Set(('\t', tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   122
        case "\\r" => Set(('\r', tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   123
        case c => Set((c(0), tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   124
      }
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   125
    }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
    case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   130
// parser for list of arguments
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   131
def ListParser[I, T, S](p: => Parser[I, T], 
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   132
                        q: => Parser[I, S])(implicit ev: I => Seq[_]): Parser[I, List[T]] = {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   133
  (p ~ q ~ ListParser(p, q)).map{ case x ~ _ ~ z => x :: z : List[T] } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   134
  (p.map((s) => List(s)))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   135
}
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   136
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   137
// I may want to write string interpolations for:
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   138
// keywords, semicolon, colon, comma, parentheses
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   139
case class StrParser(s: String) extends Parser[Tokens, String] {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   140
  def parse(tokens: Tokens) = tokens match {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   141
    case Nil => Set()
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   142
    case tk::tkns if tk._2 == s => Set((s, tkns))
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   143
    case _ => Set()
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
   147
extension (sc: StringContext) {
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 903
diff changeset
   148
   def p(args: Any*) = StrParser(sc.s(args:_*))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   152
// the AST datastructures for the FUN language
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   153
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   154
abstract class Exp
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   155
abstract class BExp
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   156
abstract class Decl
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
case class Def(name: String, args: List[(String, String)], ty: String, body: Exp) extends Decl
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
case class Main(e: Exp) extends Decl
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
case class Const(name: String, v: Int) extends Decl
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   161
case class FConst(name: String, x: Float) extends Decl
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
case class Call(name: String, args: List[Exp]) extends Exp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
case class Var(s: String) extends Exp
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   166
case class Num(i: Int) extends Exp  // integer numbers
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   167
case class FNum(i: Float) extends Exp  // float numbers
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   168
case class ChConst(c: Int) extends Exp  // character constants
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   170
case class Sequence(e1: Exp, e2: Exp) extends Exp  // expressions separated by semicolons
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   171
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   175
lazy val Exps: Parser[Tokens, Exp] =
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   176
  (Exp ~ p";" ~ Exps).map[Exp]{ case x ~ _ ~ z => Sequence(x, z) } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   177
  Exp
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   178
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   179
lazy val Exp: Parser[Tokens, Exp] =
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   180
  (p"if" ~ BExp ~ p"then" ~ Exp ~ p"else" ~ Exp).map[Exp]{ case _ ~ x ~ _ ~ y ~ _ ~ z => If(x, y, z) } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   181
  M
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   182
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   183
lazy val M: Parser[Tokens, Exp] = 
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   184
  (T ~ OpParser(Set("+", "-")) ~ M).map[Exp]{ case x ~ y ~ z => Aop(y, x, z) } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   185
  T
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   186
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   187
lazy val T: Parser[Tokens, Exp] = 
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   188
  (U ~ OpParser(Set("*", "/", "%")) ~ T).map[Exp]{ case x ~ y ~ z => Aop(y, x, z) } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   189
  U
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   190
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   191
// includes negative factor
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   192
// a + - b CAN be recognised
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   193
// - - - b CAN be recognised
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   194
lazy val U: Parser[Tokens, Exp] =
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   195
  (OpParser(Set("-")) ~ U).map[Exp]{ case _ ~ y => Aop("*", Num(-1), y) } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   196
  (OpParser(Set("+")) ~ U).map[Exp]{ case _ ~ y => y } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   197
  F
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   198
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   199
lazy val F: Parser[Tokens, Exp] = 
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   200
  (p"(" ~ Exp ~ p")").map[Exp]{ case _ ~ y ~ _ => y } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   201
  (p"skip").map(_ => Call("skip", Nil)) ||  // hardcoded
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   202
  (p"skip" ~ p"(" ~ p")").map(_ => Call("skip", Nil)) ||  // hardcoded
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   203
  (IdParser ~ p"(" ~ ListParser(Exp, p",") ~ p")").map[Exp]{ case id ~ _ ~ args ~ _ => Call(id, args) } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   204
  (IdParser ~ p"(" ~ p")").map[Exp]{ case id ~ _ ~ _ => Call(id, Nil) } ||  // NOTE: empty args are also accepted!
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   205
  (IdParser || GlobalIdParser).map(x => Var(x)) ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   206
  IntParser.map(x => Num(x)) ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   207
  DoubleParser.map(x => FNum(x)) ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   208
  CharParser.map(x => ChConst(x.toInt)) ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   209
  (p"{" ~ Exps ~ p"}").map[Exp]{ case _ ~ x ~ _ => x }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   211
lazy val BExp: Parser[Tokens, BExp] = 
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   212
  (Exp ~ OpParser(Set("==", "!=", "<", ">", "<=", ">=")) ~ Exp).map[BExp]{ case x ~ y ~ z => Bop(y, x, z) } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   213
  (p"(" ~ BExp ~ p")").map[BExp]{ case _ ~ y ~ _ => y }
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   214
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   215
lazy val TypedIdParser: Parser[Tokens, (String, String)] =
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   216
  (IdParser ~ p":" ~ TypeParser(Set("Int", "Double"))).map{ case n ~ _ ~ t => (n, t) }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   218
lazy val Defn: Parser[Tokens, Decl] =
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   219
  (p"def" ~ IdParser ~ p"(" ~ ListParser(TypedIdParser, p",") ~ p")" ~ p":" ~ TypeParser(Set("Int", "Double", "Void")) ~ OpParser(Set("=")) ~ Exp).map[Decl]{
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   220
    case _ ~ y ~ _ ~ w ~ _ ~ _ ~ t ~ _ ~ b => Def(y, w, t, b)
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   221
  } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   222
  (p"def" ~ IdParser ~ p"(" ~ p")" ~ p":" ~ TypeParser(Set("Int", "Double", "Void")) ~ OpParser(Set("=")) ~ Exp).map[Decl]{
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   223
    case _ ~ y ~ _ ~ _ ~ _ ~ t ~ _ ~ b => Def(y, Nil, t, b)
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   224
  }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   226
lazy val Constp: Parser[Tokens, Decl] = 
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   227
  (p"val" ~ GlobalIdParser ~ p":" ~ TypeParser(Set("Int")) ~ OpParser(Set("=")) ~ IntParser).map[Decl]{  // IntParser? Not Exp? For this AST, impossible to define Exp
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   228
    case _ ~ id ~ _ ~ _ ~ _ ~ n => Const(id, n)
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   229
  } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   230
  (p"val" ~ GlobalIdParser ~ p":" ~ TypeParser(Set("Int")) ~ OpParser(Set("=")) ~ OpParser(Set("-")) ~ IntParser).map[Decl]{  // IntParser? Not Exp? For this AST, impossible to define Exp
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   231
    case _ ~ id ~ _ ~ _ ~ _ ~ _ ~ n => Const(id, -n)
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   232
  }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   234
// Int can be converted to Double but not viceversa
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   235
lazy val FConstp: Parser[Tokens, Decl] =
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   236
  (p"val" ~ GlobalIdParser ~ p":" ~ TypeParser(Set("Double")) ~ OpParser(Set("=")) ~ (DoubleParser || IntParser.map[Float](i => i.toFloat))).map[Decl]{
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   237
    case _ ~ id ~ _ ~ _ ~ _ ~ n => FConst(id, n)
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   238
  } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   239
  (p"val" ~ GlobalIdParser ~ p":" ~ TypeParser(Set("Double")) ~ OpParser(Set("=")) ~ OpParser(Set("-")) ~ (DoubleParser || IntParser.map[Float](i => i.toFloat))).map[Decl]{
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   240
    case _ ~ id ~ _ ~ _ ~ _ ~ _ ~ n => FConst(id, -n)
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   241
  }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   243
// Prog consists of global const declarations, f(x) defs, and exp in ANY order
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   244
// restricted to main body at the bottom
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   245
lazy val Prog: Parser[Tokens, List[Decl]] = 
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   246
  (Defn ~ p";" ~ Prog).map[List[Decl]]{ case x ~ _ ~ z => x :: z } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   247
  (Constp ~ p";" ~ Prog).map[List[Decl]]{ case x ~ _ ~ z => x :: z } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   248
  (FConstp ~ p";" ~ Prog).map[List[Decl]]{ case x ~ _ ~ z => x :: z } ||
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   249
  Exp.map[List[Decl]](s => List(Main(s)))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   252
def parse_tks(tokens: Tokens) = Prog.parse_all(tokens)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   254
import scala.io.Source._
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
903
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   256
@main
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   257
def parse(filename: String) = {
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   258
  val fun_code = fromFile(filename).getLines.mkString("\n")
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   259
  // print the AST list to screen
2f86ebda3629 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   260
  println(parse_tks(tokenise(fun_code)))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
}