Skip to content

Commit 3326b17

Browse files
add ( parsing to exprs
1 parent a27d084 commit 3326b17

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lib/expr.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ infix('[', 80, function(left) {
6262
this.type = 'binary'
6363
return this
6464
})
65+
infix('(', 80, function(left) {
66+
this.children = [left]
67+
this.type = 'call'
68+
69+
if(token.data !== ')') while(1) {
70+
this.children.push(expression(0))
71+
if(token.data !== ',') break
72+
advance(',')
73+
}
74+
advance(')')
75+
return this
76+
})
6577

6678
prefix('-')
6779
prefix('+')
@@ -90,21 +102,23 @@ assignment('^=')
90102
assignment('>>=')
91103
assignment('<<=')
92104

93-
94-
95105
module.exports = function(incoming_state, incoming_tokens) {
96106
state = incoming_state
97107
tokens = incoming_tokens
98108
idx = 0
109+
var result
99110

100111
if(!tokens.length) return
101112

102113
advance()
103-
var result = expression(0)
104-
114+
result = expression(0)
105115
result.parent = state[0]
106116
emit(result)
107117

118+
if(idx < tokens.length) throw new Error('did not use all tokens')
119+
120+
result.parent.children = [result]
121+
108122
function emit(node) {
109123
state.unshift(node, false)
110124
for(var i = 0, len = node.children.length; i < len; ++i) {
@@ -118,8 +132,10 @@ module.exports = function(incoming_state, incoming_tokens) {
118132
function symbol(id, binding_power) {
119133
var sym = symbol_table[id]
120134
binding_power = binding_power || 0
121-
if(sym && binding_power > sym.lbp) {
122-
sym.lbp = binding_power
135+
if(sym) {
136+
if(binding_power > sym.lbp) {
137+
sym.lbp = binding_power
138+
}
123139
} else {
124140
sym = Object.create(original_symbol)
125141
sym.id = id
@@ -183,7 +199,7 @@ function assignment(id) {
183199
return infixr(id, 10, function(left) {
184200
this.children = [left, expression(9)]
185201
this.assignment = true
186-
this.type = 'type'
202+
this.type = 'assign'
187203
return this
188204
})
189205
}
@@ -195,7 +211,7 @@ function advance(id) {
195211
, output
196212

197213
if(id && token.data !== id) {
198-
return state.unexpected('expected `'+ id + '`')
214+
return state.unexpected('expected `'+ id + '`, got `'+token.data+'`')
199215
}
200216

201217
if(idx >= tokens.length) {
@@ -233,7 +249,7 @@ function advance(id) {
233249
output = Object.create(output)
234250
output.token = next
235251
output.type = type
236-
output.value = value
252+
if(!output.data) output.data = value
237253

238254
return token = output
239255
}

0 commit comments

Comments
 (0)