1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-11-14 14:39:04 -08:00

add support for variables in expressions

This commit is contained in:
Connor Olding 2016-11-27 21:17:42 -08:00
parent 64ef102183
commit ab493693a9

View file

@ -4,6 +4,9 @@ local path = string.gsub(..., "[^.]+$", "")
local Base = require(path.."Base")
local Expression = Base:extend()
function Expression:init(variables)
self.variables = variables or {}
end
Expression.precedence = {
-- python-ish precedence
@ -80,6 +83,7 @@ Expression.binary_ops = {
local operators = {}
local operators_maxlen = 0
do
-- reorder operators so we can match the longest strings first
for k, v in pairs(Expression.precedence) do
if operators[#k] == nil then
operators[#k] = {}
@ -169,6 +173,13 @@ function Expression:lex1(str, tokens)
elseif consider_operator() then
insert(tokens, {type='operator', value=considered})
consume(#considered)
elseif consider('%w+') then
local num = self.variables[considered]
if num == nil then
return 'undefined variable "'..considered..'"'
end
insert(tokens, {type='number', value=num})
consume(#considered)
else
local chr = rest:sub(1, 1)
return "unexpected character '"..chr.."'"..here