1
0
Fork 0

Added a bunch of constant evaluations

This commit is contained in:
Jon Michael Aanes 2018-03-23 12:26:45 +01:00
parent 6c7c914da2
commit 9bfcd3beb3
3 changed files with 28 additions and 5 deletions

View File

@ -1,6 +1,4 @@
require 'errors' 'shunt' . enable_strict_globals ()
---- Algorithm
local DEFAULT_ASSOC = 'left'

View File

@ -251,6 +251,26 @@ local function for_each_node_in_ast (ast, func)
return func(ast)
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)
assert(type(ast) == 'table')
assert(type(info) == 'table')
@ -260,16 +280,20 @@ local function populate_ast_with_semantics (ast, info)
node.exp, node.token = node.token, nil
end
end)
return for_each_node_in_ast(ast, function(node)
if node.exp == 'IDENTIFIER' then
node.value, node.scope, node.function_local = get_variable(node.text, info)
elseif CONSTANT_VALUE_TOKEN[node.exp] then
node.value = CONSTANT_VALUE_TOKEN[node.exp](node.text)
elseif node.exp == 'OP' and node.binop == 'DOT' then
assert(node[1].value and node[2].value)
node.value = node[1].value[ node[2].value ] --TODO
node.is_constant = true
elseif node.exp == 'OP' and CONSTANT_OP[node.binop] and node[1].value and node[2] and node[2].value then
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
--------------------------------------------------------------------------------

View File

@ -5,3 +5,4 @@ local TEST_SUITE = require "TestSuite" 'assert-gooder'
TEST_SUITE:addModules 'test/test_*.lua'
TEST_SUITE:enableStrictGlobal()
TEST_SUITE:runTests()