644
|
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
|
642
|
9 |
|
644
|
10 |
object Detokenise {
|
642
|
11 |
|
|
12 |
import java.io._
|
|
13 |
|
|
14 |
abstract class Token extends Serializable
|
|
15 |
case object T_SEMI extends Token
|
|
16 |
case object T_LPAREN extends Token
|
|
17 |
case object T_RPAREN extends Token
|
|
18 |
case class T_ID(s: String) extends Token
|
|
19 |
case class T_OP(s: String) extends Token
|
|
20 |
case class T_NUM(n: Int) extends Token
|
|
21 |
case class T_KWD(s: String) extends Token
|
|
22 |
case class T_STR(s: String) extends Token
|
|
23 |
|
|
24 |
def deserialise[T](fname: String) : T = {
|
|
25 |
val in = new ObjectInputStream(new FileInputStream(fname))
|
|
26 |
val data = in.readObject.asInstanceOf[T]
|
|
27 |
in.close
|
|
28 |
data
|
|
29 |
}
|
|
30 |
|
|
31 |
def main(args: Array[String]) = {
|
644
|
32 |
val fname = args(0)
|
|
33 |
val tks = deserialise[List[Token]](fname)
|
|
34 |
println(s"Reading back from ${fname}:\n${tks.mkString("\n")}")
|
642
|
35 |
}
|
|
36 |
|
|
37 |
|
|
38 |
} |