Added a bunch of constant evaluations
This commit is contained in:
parent
6c7c914da2
commit
9bfcd3beb3
|
@ -1,6 +1,4 @@
|
||||||
|
|
||||||
require 'errors' 'shunt' . enable_strict_globals ()
|
|
||||||
|
|
||||||
---- Algorithm
|
---- Algorithm
|
||||||
|
|
||||||
local DEFAULT_ASSOC = 'left'
|
local DEFAULT_ASSOC = 'left'
|
||||||
|
|
|
@ -251,6 +251,26 @@ local function for_each_node_in_ast (ast, func)
|
||||||
return func(ast)
|
return func(ast)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local CONSTANT_OP = {}
|
||||||
|
|
||||||
|
function CONSTANT_OP.DOT (node) return node[1].value[ node[2].value ] end -- TODO
|
||||||
|
function CONSTANT_OP.AND (node) return node[1].value and node[2].value end
|
||||||
|
function CONSTANT_OP.OR (node) return node[1].value or node[2].value end
|
||||||
|
function CONSTANT_OP.PLUS (node) return node[1].value + node[2].value end
|
||||||
|
function CONSTANT_OP.MINUS (node) return node[1].value - node[2].value end
|
||||||
|
function CONSTANT_OP.TIMES (node) return node[1].value * node[2].value end
|
||||||
|
function CONSTANT_OP.DIVIDE (node) return node[1].value / node[2].value end
|
||||||
|
function CONSTANT_OP.MODULO (node) return node[1].value % node[2].value end
|
||||||
|
function CONSTANT_OP.CARET (node) return node[1].value ^ node[2].value end
|
||||||
|
function CONSTANT_OP.HASHTAG (node) return #node[1].value end
|
||||||
|
function CONSTANT_OP.EQ (node) return node[1].value == node[2].value end
|
||||||
|
function CONSTANT_OP.NEQ (node) return node[1].value ~= node[2].value end
|
||||||
|
function CONSTANT_OP.LEQ (node) return node[1].value <= node[2].value end
|
||||||
|
function CONSTANT_OP.GEQ (node) return node[1].value >= node[2].value end
|
||||||
|
function CONSTANT_OP.LE (node) return node[1].value < node[2].value end
|
||||||
|
function CONSTANT_OP.GT (node) return node[1].value > node[2].value end
|
||||||
|
function CONSTANT_OP.CONCAT (node) return node[1].value .. node[2].value end
|
||||||
|
|
||||||
local function populate_ast_with_semantics (ast, info)
|
local function populate_ast_with_semantics (ast, info)
|
||||||
assert(type(ast) == 'table')
|
assert(type(ast) == 'table')
|
||||||
assert(type(info) == 'table')
|
assert(type(info) == 'table')
|
||||||
|
@ -260,16 +280,20 @@ local function populate_ast_with_semantics (ast, info)
|
||||||
node.exp, node.token = node.token, nil
|
node.exp, node.token = node.token, nil
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return for_each_node_in_ast(ast, function(node)
|
return for_each_node_in_ast(ast, function(node)
|
||||||
if node.exp == 'IDENTIFIER' then
|
if node.exp == 'IDENTIFIER' then
|
||||||
node.value, node.scope, node.function_local = get_variable(node.text, info)
|
node.value, node.scope, node.function_local = get_variable(node.text, info)
|
||||||
elseif CONSTANT_VALUE_TOKEN[node.exp] then
|
elseif CONSTANT_VALUE_TOKEN[node.exp] then
|
||||||
node.value = CONSTANT_VALUE_TOKEN[node.exp](node.text)
|
node.value = CONSTANT_VALUE_TOKEN[node.exp](node.text)
|
||||||
elseif node.exp == 'OP' and node.binop == 'DOT' then
|
node.is_constant = true
|
||||||
assert(node[1].value and node[2].value)
|
elseif node.exp == 'OP' and CONSTANT_OP[node.binop] and node[1].value and node[2] and node[2].value then
|
||||||
node.value = node[1].value[ node[2].value ] --TODO
|
assert(node[1].value and (not node[2] or node[2].value))
|
||||||
|
node.value = CONSTANT_OP[node.binop](node)
|
||||||
|
node.is_constant = node[1].is_constant and (not node[2] or node[2].is_constant)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
|
@ -5,3 +5,4 @@ local TEST_SUITE = require "TestSuite" 'assert-gooder'
|
||||||
TEST_SUITE:addModules 'test/test_*.lua'
|
TEST_SUITE:addModules 'test/test_*.lua'
|
||||||
TEST_SUITE:enableStrictGlobal()
|
TEST_SUITE:enableStrictGlobal()
|
||||||
TEST_SUITE:runTests()
|
TEST_SUITE:runTests()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user