725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// This produces more meaningful tokens and
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
// also filters out comments and whitespaces
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
// call with
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
// amm token.sc
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
// load the lexer
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
import $file.lexer
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
import lexer._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
// The tokens for the WHILE language
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
abstract class Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
case object T_SEMI extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
case object T_LPAREN extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
case object T_RPAREN extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
case class T_ID(s: String) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case class T_OP(s: String) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case class T_NUM(n: Int) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
case class T_KWD(s: String) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
case class T_STR(s: String) extends Token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
val token : PartialFunction[(String, String), Token] = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
case ("s", _) => T_SEMI
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
case ("p", "{") => T_LPAREN
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
case ("p", "}") => T_RPAREN
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
case ("i", s) => T_ID(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
case ("o", s) => T_OP(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
case ("n", s) => T_NUM(s.toInt)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
case ("k", s) => T_KWD(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
case ("str", s) => T_STR(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
// by using collect we filter out all unwanted tokens
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
def tokenise(s: String) : List[Token] =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
lexing_simp(WHILE_REGS, s).collect(token)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
@doc("Tokens for fib and loops programs.")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
def main() = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
println("Fib program")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
println(tokenise(prog2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
println("Loops program")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
println(tokenise(prog3))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
for (i <- 0 to 20 by 5) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
println(f"$i%2.0f: ${time(tokenise(prog3 * i))._2}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
} |