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
|
const { lex } = require('./lexer.js')
let symbols = {}
let newSymbol = (type, nud, lbp, led) => {
symbols[type] = { type, nud, lbp, led }
}
let lexemeToToken = lexeme => {
let token = Object.create(symbols[lexeme.type])
token.value = lexeme.value
return token
}
newSymbol('constant', function() {
return { type: 'constant', value: this.value }
})
newSymbol('(', function(parser) {
const value = parser.expression(1)
parser.match(')')
return value
})
newSymbol(')')
newSymbol('d', null, 30, (left, parser) => {
return {
type: 'd',
left: left,
right: parser.expression(29)
}
})
newSymbol('+', null, 20, (left, parser) => {
return {
type: 'add',
left: left,
right: parser.expression(20)
}
})
newSymbol('-', (parser) => {
return {
type: 'negative',
value: parser.expression(40)
}
}, 20, (left, parser) => {
return {
type: 'subtract',
left: left,
right: parser.expression(20)
}
})
newSymbol('end', null, -1)
const newParser = (tokens) => {
return {
tokens,
currentToken: 0,
token: function() { return this.tokens[this.currentToken] },
advanceToken: function() { this.currentToken++ },
match: function(token) {
if (this.token().type === token) {
this.advanceToken()
} else {
throw 'error'
}
},
expression: function(rbp) {
let symbol = this.token()
this.advanceToken()
let left = symbol.nud(this)
while (rbp < this.token().lbp) {
symbol = this.token()
this.advanceToken()
left = symbol.led(left, this)
}
return left
}
}
}
const parse = expressionString => {
const tokens = lex(expressionString).map(lexemeToToken)
tokens.push(symbols.end)
const parser = newParser(tokens)
return parser.expression(0)
}
exports.parse = parse
|