-rw-r--r-- | expr-actions.lua | 5 | ||||
-rw-r--r-- | expr-lexer.lua | 4 | ||||
-rw-r--r-- | expr-parse.lua | 4 | ||||
-rw-r--r-- | expr-print.lua | 6 |
diff --git a/expr-actions.lua b/expr-actions.lua index 8ba169a1..3e82cf1c 100644 --- a/expr-actions.lua +++ b/expr-actions.lua @@ -29,6 +29,10 @@ end local function ident_action(id) return id end +local function literal_action(name) + return {literal= name} +end + -- return true iff expr is a variable (with enums or not). -- if it is a variable returns, in addition, the var_name and the enumeration flag local function is_variable(expr) @@ -47,6 +51,7 @@ end return { infix = infix_action, ident = ident_action, + literal = literal_action, prefix = prefix_action, enum = enum_action, func_eval = func_eval_action, diff --git a/expr-lexer.lua b/expr-lexer.lua index c075d8a5..c13b1c38 100644 --- a/expr-lexer.lua +++ b/expr-lexer.lua @@ -52,6 +52,10 @@ function expr_lexer.next_token(lexer) lexer:skip('%s*') if lexer.n > len(lexer.src) then return {type= 'EOF'} end local c = lexer:char() + if c == '\'' then + local str = lexer:consume("'[^']+'") + return {type= 'literal', value = str:sub(2, -2)} + end if c == '[' then local str = lexer:consume('%b[]') return {type= 'ident', value= str:sub(2,-2)} diff --git a/expr-parse.lua b/expr-parse.lua index 85e770e9..a8e72481 100644 --- a/expr-parse.lua +++ b/expr-parse.lua @@ -28,6 +28,10 @@ local function factor(lexer, actions) else return actions.ident(id) end + elseif token.type == 'literal' then + local x = token.value + lexer:next() + return actions.literal(x) elseif token.type == 'number' then local x = token.value lexer:next() diff --git a/expr-print.lua b/expr-print.lua index ebbc6518..4c73f44d 100644 --- a/expr-print.lua +++ b/expr-print.lua @@ -38,6 +38,8 @@ ex_print = function(e) local s = e if not is_ident_simple(s) then s = format('[%s]', s) end return s, 3 + elseif e.literal then + return format('%q', e.literal) elseif e.func then local arg_str = ex_print(e.arg) return format('%s(%s)', e.func, arg_str), 3 @@ -72,6 +74,8 @@ local function eval(expr, scope, ...) return expr elseif type(expr) == 'string' then return scope.ident(expr, ...) + elseif expr.literal then + return expr.literal elseif expr.func then local arg_value = eval(expr.arg, scope, ...) if arg_value then @@ -99,6 +103,8 @@ local function ref_list_rec(expr, list) return elseif type(expr) == 'string' then list[expr] = true + elseif expr.literal then + return elseif expr.func then ref_list_rec(expr.arg, list) else |