progs/detokenise.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 02 Oct 2019 02:09:48 +0100
changeset 644 b4f5714485e1
parent 642 064afa8fc1d9
child 645 30943d5491b6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     1
// Detokenising the ouput of Tokeniser
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     2
//=====================================
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     3
//
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     4
// call with 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     5
//
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     6
//     scala detokenise.scala fib.tks
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     7
//
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     8
//     scala detokenise.scala loops.tks
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
    10
object Detokenise {
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
import java.io._
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
abstract class Token extends Serializable 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
case object T_SEMI extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
case object T_LPAREN extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
case object T_RPAREN extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
case class T_ID(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
case class T_OP(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case class T_NUM(n: Int) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case class T_KWD(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case class T_STR(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def deserialise[T](fname: String) : T = {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  val in = new ObjectInputStream(new FileInputStream(fname))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  val data = in.readObject.asInstanceOf[T]
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  in.close
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  data
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
def main(args: Array[String]) = {
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
    32
  val fname = args(0)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
    33
  val tks = deserialise[List[Token]](fname)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
    34
  println(s"Reading back from ${fname}:\n${tks.mkString("\n")}")  
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
}