From 9bfcd3beb35d1b0a94b958800764b95f4f0f6356 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 23 Mar 2018 12:26:45 +0100 Subject: [PATCH] Added a bunch of constant evaluations --- Parser.lua | 2 -- assert-gooder.lua | 30 +++++++++++++++++++++++++++--- test/tests.lua | 1 + 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Parser.lua b/Parser.lua index aad3da8..b80761d 100644 --- a/Parser.lua +++ b/Parser.lua @@ -1,6 +1,4 @@ -require 'errors' 'shunt' . enable_strict_globals () - ---- Algorithm local DEFAULT_ASSOC = 'left' diff --git a/assert-gooder.lua b/assert-gooder.lua index 46af4f4..f874795 100644 --- a/assert-gooder.lua +++ b/assert-gooder.lua @@ -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 -------------------------------------------------------------------------------- diff --git a/test/tests.lua b/test/tests.lua index 17bce2c..38d494c 100644 --- a/test/tests.lua +++ b/test/tests.lua @@ -5,3 +5,4 @@ local TEST_SUITE = require "TestSuite" 'assert-gooder' TEST_SUITE:addModules 'test/test_*.lua' TEST_SUITE:enableStrictGlobal() TEST_SUITE:runTests() +