-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
216 lines (200 loc) · 5.28 KB
/
Copy pathparser.js
File metadata and controls
216 lines (200 loc) · 5.28 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
let parser = {
tokens: [],
current: 0,
}
function initParser(tokens) {
parser.tokens = tokens
parser.current = 0
}
function peekTok() {
if (parser.current >= parser.tokens.length) {
return null
}
return parser.tokens[parser.current].type
}
function eat(type) {
if (peekTok() === type) {
return parser.tokens[parser.current++]
}
console.error("Expected " + type + " but got " + peekTok())
parser.current++ // Skip the token
}
// optional(id) followed by list of ("," id)
function idList() {
let list = []
if (peekTok() === TokenType.Word) {
list.push(eat(TokenType.Word).text)
while (peekTok() === TokenType.Comma) {
eat(TokenType.Comma)
list.push(eat(TokenType.Word).text)
}
}
return list
}
// optional(expr) followed by list of ("," expr)
function exprList() {
let list = []
if (peekTok() !== TokenType.RightParen) {
list.push(expr())
while (peekTok() === TokenType.Comma) {
eat(TokenType.Comma)
list.push(expr())
}
}
return list
}
// var id = expr
function varDecl() {
eat(TokenType.Var)
let id = eat(TokenType.Word).text
eat(TokenType.Equal)
let value = expr()
return NewVar(id, value)
}
// fn id (idList) { stmtList }
function fnDecl() {
eat(TokenType.Fn)
let id = eat(TokenType.Word).text
eat(TokenType.LeftParen)
let params = idList()
eat(TokenType.RightParen)
eat(TokenType.LeftBrace)
let body = []
while (peekTok() !== TokenType.RightBrace) {
body.push(stmt())
}
eat(TokenType.RightBrace)
return NewFn(id, params, body)
}
// return expr
function returnStmt() {
eat(TokenType.Return)
let value = expr()
return NewReturn(value)
}
// if expr {stmtList} else {stmtList}
function ifStmt() {
eat(TokenType.If)
let condition = expr()
eat(TokenType.LeftBrace)
let thenBranch = []
while (peekTok() !== TokenType.RightBrace) {
thenBranch.push(stmt())
}
eat(TokenType.RightBrace)
let elseBranch = []
if (peekTok() === TokenType.Else) {
eat(TokenType.Else)
eat(TokenType.LeftBrace)
while (peekTok() !== TokenType.RightBrace) {
elseBranch.push(stmt())
}
eat(TokenType.RightBrace)
}
return NewIf(condition, thenBranch, elseBranch)
}
// while expr {stmtList}
function whileStmt() {
eat(TokenType.While)
let condition = expr()
eat(TokenType.LeftBrace)
let body = []
while (peekTok() !== TokenType.RightBrace) {
body.push(stmt())
}
eat(TokenType.RightBrace)
return NewWhile(condition, body)
}
// stmt = varDecl | fnDecl | returnStmt | ifStmt | whileStmt | expr
function stmt() {
switch (peekTok()) {
case TokenType.Var:
return varDecl()
case TokenType.Fn:
return fnDecl()
case TokenType.Return:
return returnStmt()
case TokenType.If:
return ifStmt()
case TokenType.While:
return whileStmt()
default:
return expr()
}
}
// simple = id | number | string | true | false | fn(idList) {stmtList} | (expr)
function simple() {
let token = eat(peekTok())
switch (token.type) {
case TokenType.Word:
return NewVariable(token.text)
case TokenType.Number:
return NewNumber(token.value)
case TokenType.String:
return NewString(token.text)
case TokenType.True:
return NewBool(true)
case TokenType.False:
return NewBool(false)
case TokenType.Fn:
eat(TokenType.LeftParen)
let params = idList()
eat(TokenType.RightParen)
eat(TokenType.LeftBrace)
let body = []
while (peekTok() !== TokenType.RightBrace) {
body.push(stmt())
}
eat(TokenType.RightBrace)
return NewLambda(params, body)
case TokenType.LeftParen:
let result = expr()
eat(TokenType.RightParen)
return result
default:
console.error("Expected expression but got " + token.type)
lexer.current++ // Skip the token
return null
}
}
// call = simple (exprList)
function call() {
let expr = simple()
if (peekTok() === TokenType.LeftParen) {
eat(TokenType.LeftParen)
let args = exprList()
eat(TokenType.RightParen)
return NewCall(expr, args)
}
return expr
}
function isOp(token) {
return token === TokenType.Plus ||
token === TokenType.Minus ||
token === TokenType.Times ||
token === TokenType.Divide ||
token === TokenType.LessThan ||
token === TokenType.GreaterThan ||
token === TokenType.Equality ||
token === TokenType.Equal ||
token === TokenType.And ||
token === TokenType.Or
}
// expr = call ?(op expr)
function expr() {
let left = call()
if (isOp(peekTok())) {
let op = eat(peekTok()).text
let right = expr()
return NewBinOp(left, right, op)
}
return left
}
// program = list of stmt
function program() {
let list = []
while (peekTok() !== TokenType.Eof) {
list.push(stmt())
}
return list
}