-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.scala
161 lines (142 loc) · 4.75 KB
/
Parser.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package parser
import scala.reflect._
import lexer._
private def tryParseFunctionCall(state: ParserState, current: Expression) = {
var result = current
while state.currentToken() == LP() do
ensurePopToken[LP](state)
result = FunctionCall(result, parseExpression(state))
ensurePopToken[RP](state)
result
}
private def parsePrefix(state: ParserState): Expression = state.currentToken() match {
case Number(n) => {
state.moveNext()
Literal(n)
}
case True() => {
state.moveNext()
Literal(true)
}
case False() => {
state.moveNext()
Literal(false)
}
case Null() => {
state.moveNext()
Literal(null)
}
case Id(i) => {
if state.peekToken() == Arrow() then
ensurePopToken[Id](state)
ensurePopToken[Arrow](state)
FunctionDefinition(i, parseExpression(state), Set.empty)
else
state.moveNext()
Reference(i)
}
case If() => {
ensurePopToken[If](state)
val cond = parseExpression(state)
ensurePopToken[Then](state)
val ifTrue = parseExpression(state)
ensurePopToken[Else](state)
val ifFalse = parseExpression(state)
IfThenElse(cond, ifTrue, ifFalse)
}
case LB() => {
ensurePopToken[LB](state)
var statements = List[AST]()
while(state.currentToken() != RB())
statements = statements.appended(parseStatement(state))
ensurePopToken[RB](state)
CodeBlock(statements)
}
case LP() => {
ensurePopToken[LP](state)
val t = parseExpression(state)
ensurePopToken[RP](state)
t
}
case Not() => {
ensurePopToken[Not](state)
UnaryOperator("!", parseExpression(state))
}
case Minus() => {
ensurePopToken[Minus](state)
UnaryOperator("-", parseExpression(state))
}
case t: Token => throw new Exception("Invalid prefix expression: " + t.getClass().getName())
}
private def infixConstructor(token:Token): Option[(left: Expression, rigth: Expression) => Expression] = token match {
case Plus() => Some((left, rigth) => BinaryOperator(left, "+", rigth))
case Minus() => Some((left, rigth) => BinaryOperator(left, "-", rigth))
case Multiply() => Some((left, rigth) => BinaryOperator(left, "*", rigth))
case Divide() => Some((left, rigth) => BinaryOperator(left, "/", rigth))
case And() => Some((left, rigth) => BinaryOperator(left, "&", rigth))
case Or() => Some((left, rigth) => BinaryOperator(left, "|", rigth))
case Smaller() => Some((left, rigth) => BinaryOperator(left, "<", rigth))
case Larger() => Some((left, rigth) => BinaryOperator(left, ">", rigth))
case Equals() => Some((left, rigth) => BinaryOperator(left, "=", rigth))
case NotEquals() => Some((left, rigth) => BinaryOperator(left, "!=", rigth))
case _ => None
}
private def parseInfix(state: ParserState, left: Expression): Option[Expression] = infixConstructor(state.currentToken())
.map(f => {
val precedence = state.currentPrecedence()
state.moveNext()
val right = parseExpression(state, precedence)
f(left, right)
})
def parseExpression(state: ParserState, precedence: Int = 0): Expression = {
var left = parsePrefix(state)
while(state.currentToken() != EOF() && precedence < state.currentPrecedence()) {
parseInfix(state, left) match {
case None => return tryParseFunctionCall(state, left)
case Some(newLeft) => left = newLeft
}
}
tryParseFunctionCall(state, left)
}
def parseStatement(state: ParserState): AST = {
val statement = state.currentToken() match {
case Let() => {
ensurePopToken[Let](state)
val Id(name) = ensurePopToken[Id](state)
ensurePopToken[Assign](state)
Declaration(name, parseExpression(state))
}
case Print() => {
ensurePopToken[Print](state)
PrintExpression(parseExpression(state))
}
case While() => {
ensurePopToken[While](state)
val cond = parseExpression(state)
ensurePopToken[Do](state)
val body = parseExpression(state)
WhileLoop(cond, body)
}
case Id(n) if state.peekToken() == Assign() => {
ensurePopToken[Id](state)
ensurePopToken[Assign](state)
AssignStatement(n, parseExpression(state))
}
case _ => ExpressionStatement(parseExpression(state))
}
ensurePopToken[SemiColon](state)
statement
}
def parse(state: ParserState): List[AST] = {
var statements = List[AST]()
while(state.currentToken() != EOF())
statements = statements.appended(parseStatement(state))
statements
}
private def ensurePopToken[T <: Token](state: ParserState)(implicit tag: ClassTag[T]): T = {
val token = state.currentToken()
state.moveNext()
if token.getClass.getName == tag.runtimeClass.getName
then token.asInstanceOf[T]
else throw new Error("Expected " + tag.runtimeClass.getName + ", got " + token.getClass.getName)
}