-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.lua
192 lines (171 loc) · 4.95 KB
/
expr.lua
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
local comb = require('lithium.comb')
local Parser, pattern, maybe_ws, literal = comb.Parser, comb.pattern, comb.maybe_ws, comb.literal
local table = require('lithium.table')
local map, unpack = table.map, table.unpack
local expr = {}
expr.plus = literal('+')
expr.minus = literal('-')
expr.multiply = literal('*')
expr.divide = literal('/')
expr.modulo = literal('%')
expr.power = literal('^')
expr.dot = literal('.')
expr.binary_op = expr.plus + expr.minus + expr.multiply + expr.divide + expr.modulo + expr.power + expr.dot
expr.integer = comb.digits / tonumber
expr.hex = pattern('0[xX][0-9a-fA-F]+') / function(result) return tonumber(result.match) end
expr.octal = pattern('0[oO]([0-7]+)') / function(result) return tonumber(result.captures[1], 8) end
expr.binary = pattern('0[bB][01]+') / function(result) return tonumber(result.match) end
expr.decimal = (pattern('%d*%.%d+') + pattern('%d+%.%d*')):index('match') / tonumber
expr.scientific = comb.sequence({
(expr.decimal + expr.integer) / tostring,
pattern('[eE][%-%+]?%d+'):index('match')
}) / table.concat / tonumber
expr.number = expr.scientific + expr.decimal + expr.hex + expr.octal + expr.binary + expr.integer
expr.number = expr.number:tag('number')
expr.identifier = comb.identifier:tag('identifier')
local function maybe_call(parser)
local transform = parser.transform
local params = expr.expr:delimited(pattern('%s*,%s*')):maybe():default({}):surround(pattern('%s*%(%s*'), pattern('%s*%)%s*'))
return Parser(function(state)
local newState, err = transform(state)
if not (newState) then
return nil, err
end
state = newState
newState, err = params(state)
if newState then
newState.result = {
tag = 'call',
func = state.result,
params = newState.result
}
state = newState
end
return state
end)
end
expr.expr = comb.proxy(function()
return expr.binary_expr
end)
expr.expr = maybe_call(expr.expr)
expr.parens = expr.expr:surround(pattern('%s*%(%s*'), pattern('%s*%)%s*'))
local negated_number = pattern('%-%s*') * expr.number
negated_number = negated_number / function(result)
result.value = result.value * -1
return result
end
expr.atom = negated_number + expr.number + expr.identifier + expr.parens
expr.atom = maybe_call(expr.atom)
local wss = function(t)
return map(t, function(v)
return v:wss()
end)
end
expr.binary_expr = comb.binary(expr.atom, {
wss({
expr.plus,
expr.minus
}),
wss({
expr.multiply,
expr.divide,
expr.modulo
}),
wss({
expr.power
}),
wss({
expr.dot
})
})
local function tag_binary(result)
if result.operator then
result.tag = 'binary'
tag_binary(result.left)
tag_binary(result.right)
end
return result
end
expr.binary_expr = expr.binary_expr / tag_binary
function expr.compile(str)
local state = {
data = str,
index = 1
}
local err
state, err = expr.expr:surround(maybe_ws, maybe_ws * comb.eof)(state)
if not (state) then
return nil, err
end
return state.result
end
function expr.evalNode(node, env)
local result
if 'number' == node.tag then
result = node.value
elseif 'binary' == node.tag then
if node.operator == '.' then
local left = expr.evalNode(node.left, env)
if not ('table' == type(left)) then
return nil, 'trying to index a non-namespace value'
end
if not (node.right.tag == 'identifier') then
return nil, 'right side of namespace operator must be an identifier'
end
local value = left[node.right.value]
if value == nil then
return nil, "variable '" .. tostring(node.right.value) .. "' does not exist in namespace"
end
result = value
else
local left = expr.evalNode(node.left, env)
local right = expr.evalNode(node.right, env)
if not ('number' == type(left) and 'number' == type(right)) then
return nil, 'attempted to operate on non-number value'
end
if '+' == node.operator then
result = left + right
elseif '-' == node.operator then
result = left - right
elseif '*' == node.operator then
result = left * right
elseif '/' == node.operator then
result = left / right
elseif '%' == node.operator then
result = left % right
elseif '^' == node.operator then
result = left ^ right
else
return nil, "unknown operator '" .. tostring(node.operator) .. "'"
end
end
elseif 'identifier' == node.tag then
local value = env[node.value]
if value == nil then
return nil, "variable '" .. tostring(node.value) .. "' does not exist in namespace"
end
result = value
elseif 'call' == node.tag then
local func = expr.evalNode(node.func, env)
if not ('function' == type(func)) then
return nil, 'tried to call a non-function value'
end
local params = {
n = #node.params
}
for i = 1, params.n do
params[i] = expr.evalNode(node.params[i], env)
end
result = func(unpack(params, 1, params.n))
end
return result
end
function expr.eval(str, env)
if env == nil then
env = require('lithium.math')
end
local ast, err = expr.compile(str)
if ast == nil then return nil, err end
return expr.evalNode(ast, env)
end
return expr