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:
parent
64ef102183
commit
ab493693a9
1 changed files with 11 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue