Attic/detokenise.scala
changeset 742 b5b5583a3a08
parent 695 484b74bc057e
equal deleted inserted replaced
741:e66bd5c563eb 742:b5b5583a3a08
       
     1 // Detokenising the ouput of Tokeniser
       
     2 //=====================================
       
     3 //
       
     4 // call with 
       
     5 //
       
     6 //     scala detokenise.scala fib.tks
       
     7 //
       
     8 //     scala detokenise.scala loops.tks
       
     9 
       
    10 object Detokenise {
       
    11 
       
    12 import java.io._
       
    13 import scala.util._ 
       
    14 
       
    15 abstract class Token extends Serializable 
       
    16 case object T_SEMI extends Token
       
    17 case object T_LPAREN extends Token
       
    18 case object T_RPAREN extends Token
       
    19 case class T_ID(s: String) extends Token
       
    20 case class T_OP(s: String) extends Token
       
    21 case class T_NUM(n: Int) extends Token
       
    22 case class T_KWD(s: String) extends Token
       
    23 case class T_STR(s: String) extends Token
       
    24 
       
    25 def deserialise[T](fname: String) : Try[T] = {
       
    26   //import scala.util.Using
       
    27   //Using(new ObjectInputStream(new FileInputStream(fname))) {
       
    28   //  in => in.readObject.asInstanceOf[T]
       
    29   //}
       
    30   Try(new ObjectInputStream(new FileInputStream(fname))).get {
       
    31     in => in.readObject.asInstanceOf[T]
       
    32   }
       
    33 }
       
    34 
       
    35 def main(args: Array[String]) = {
       
    36   val fname = args(0)
       
    37   val tks = deserialise[List[Token]](fname).getOrElse(Nil)
       
    38   println(s"Reading back from ${fname}:\n${tks.mkString(", ")}")  
       
    39 }
       
    40 
       
    41 
       
    42 }