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 |
|
827
|
44 |
@arg(doc = "Tokens for fib and loops programs.")
|
725
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 |
|
729
|
56 |
}
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
// Primes program
|
|
62 |
//================
|
|
63 |
|
|
64 |
/*
|
|
65 |
end := 100;
|
|
66 |
n := 2;
|
|
67 |
while (n < end) do {
|
|
68 |
f := 2;
|
|
69 |
tmp := 0;
|
|
70 |
while ((f < n / 2 + 1) && (tmp == 0)) do {
|
|
71 |
if ((n / f) * f == n) then { tmp := 1 } else { skip };
|
|
72 |
f := f + 1
|
|
73 |
};
|
|
74 |
if (tmp == 0) then { write(n) } else { skip };
|
|
75 |
n := n + 1
|
|
76 |
}
|
|
77 |
*/
|
|
78 |
|
|
79 |
// Factors program
|
|
80 |
//=================
|
|
81 |
|
|
82 |
/*
|
|
83 |
write "Input n please";
|
|
84 |
read n;
|
|
85 |
write "The factors of n are";
|
|
86 |
f := 2;
|
|
87 |
while n != 1 do {
|
|
88 |
while (n / f) * f == n do {
|
|
89 |
write f;
|
|
90 |
n := n / f
|
|
91 |
};
|
|
92 |
f := f + 1
|
|
93 |
}
|
827
|
94 |
*/
|
|
95 |
|
|
96 |
|
|
97 |
// runs with amm2 and amm3
|