Skip to content

Commit

Permalink
Put the "inheritModule" function into the Helpers module
Browse files Browse the repository at this point in the history
  • Loading branch information
bytexenon committed Jun 15, 2024
1 parent 6c66317 commit 31824c3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.
18 changes: 7 additions & 11 deletions src/Evaluator/Evaluator.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
--[[
Name: Evaluator.lua
Author: ByteXenon [Luna Gilbert]
Date: 2024-05-21
Date: 2024-06-14
--]]

--* Dependencies *--
local Helpers = require("Helpers/Helpers")

--* Imports *--
local inheritModule = Helpers.inheritModule

local unpack = (unpack or table.unpack)
local insert = table.insert

Expand Down Expand Up @@ -159,17 +164,8 @@ function Evaluator:new(expression, variables, operatorFunctions, functions)
EvaluatorInstance.operatorFunctions = operatorFunctions or DEFAULT_OPERATOR_FUNCTIONS
EvaluatorInstance.functions = functions or {}

local function inheritModule(moduleName, moduleTable)
for index, value in pairs(moduleTable) do
if EvaluatorInstance[index] then
return error("Conflicting names in " .. moduleName .. " and EvaluatorInstance: " .. index)
end
EvaluatorInstance[index] = value
end
end

-- Main
inheritModule("EvaluatorMethods", EvaluatorMethods)
inheritModule("EvaluatorInstance", EvaluatorInstance, "EvaluatorMethods", EvaluatorMethods)

return EvaluatorInstance
end
Expand Down
15 changes: 15 additions & 0 deletions src/Helpers/Helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,19 @@ function Helpers.makeTrie(table)
return trieTable
end

--- Inherit a module into another module.
-- @param <String> parentModuleName The name of the parent module.
-- @param <Table> parentModule The table of the parent module.
-- @param <String> moduleName The name of the module to inherit.
-- @param <Table> moduleTable The table of the module to inherit.
function Helpers.inheritModule(parentModuleName, parentModule, moduleName, moduleTable)
for index, value in pairs(moduleTable) do
if parentModule[index] then
local errorMessage = ("Conflicting names in %s and %s: %s"):format(parentModuleName, moduleName, index)
return error(errorMessage)
end
parentModule[index] = value
end
end

return Helpers
12 changes: 2 additions & 10 deletions src/Lexer/Lexer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local TokenFactory = require("Lexer/TokenFactory")
local makeTrie = Helpers.makeTrie
local stringToTable = Helpers.stringToTable
local createPatternLookupTable = Helpers.createPatternLookupTable
local inheritModule = Helpers.inheritModule

local concat = table.concat
local insert = table.insert
Expand Down Expand Up @@ -338,17 +339,8 @@ function Lexer:new(expression, operators, charPos)
LexerInstance.operatorsTrie = DEFAULT_OPERATORS_TRIE
end

local function inheritModule(moduleName, moduleTable)
for index, value in pairs(moduleTable) do
if LexerInstance[index] then
return error("Conflicting names in " .. moduleName .. " and LexerInstance: " .. index)
end
LexerInstance[index] = value
end
end

-- Main
inheritModule("LexerMethods", LexerMethods)
inheritModule("LexerInstance", LexerInstance, "LexerMethods" , LexerMethods)

return LexerInstance
end
Expand Down
21 changes: 8 additions & 13 deletions src/MathParser.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--[[
Name: MathParser.lua
Author: ByteXenon [Luna Gilbert]
Date: 2024-04-25
Date: 2024-06-14
--]]

local scriptPath, requirePath, localPath, oldPath
Expand All @@ -15,9 +15,13 @@ if not LUAXEN_PACKER then
end

--* Dependencies *--
local Helpers = require("Helpers/Helpers")
local Evaluator = require("Evaluator/Evaluator")
local Lexer = require("Lexer/Lexer")
local Parser = require("Parser/Parser")
local Lexer = require("Lexer/Lexer")
local Parser = require("Parser/Parser")

--* Imports *--
local inheritModule = Helpers.inheritModule

--* MathParserMethods *--
local MathParserMethods = {}
Expand Down Expand Up @@ -157,17 +161,8 @@ function MathParser:new(operatorPrecedenceLevels, variables, operatorFunctions,
MathParserInstance.Parser = Parser:new(nil, operatorPrecedenceLevels)
MathParserInstance.Evaluator = Evaluator:new(nil, variables, operatorFunctions, functions)

local function inheritModule(moduleName, moduleTable)
for index, value in pairs(moduleTable) do
if MathParserInstance[index] then
return error("Conflicting names in " .. moduleName .. " and MathParserInstance: " .. index)
end
MathParserInstance[index] = value
end
end

-- Main
inheritModule("MathParserMethods", MathParserMethods)
inheritModule("MathParserInstance", MathParserInstance, "MathParserMethods", MathParserMethods)

return MathParserInstance
end
Expand Down
15 changes: 4 additions & 11 deletions src/Parser/Parser.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--[[
Name: Parser.lua
Author: ByteXenon [Luna Gilbert]
Date: 2024-05-21
Date: 2024-06-14
--]]

--* Dependencies *--
Expand All @@ -10,6 +10,8 @@ local NodeFactory = require("Parser/NodeFactory")

--* Imports *--
local stringToTable = Helpers.stringToTable
local inheritModule = Helpers.inheritModule

local insert = table.insert
local concat = table.concat
local max = math.max
Expand Down Expand Up @@ -292,17 +294,8 @@ function Parser:new(tokens, operatorPrecedenceLevels, tokenIndex, expression)
ParserInstance.operatorPrecedenceLevels = operatorPrecedenceLevels or DEFAULT_OPERATOR_PRECEDENCE_LEVELS
ParserInstance.charStream = (type(expression) == "string" and stringToTable(expression)) or expression

local function inheritModule(moduleName, moduleTable)
for index, value in pairs(moduleTable) do
if ParserInstance[index] then
return error("Conflicting names in " .. moduleName .. " and ParserInstance: " .. index)
end
ParserInstance[index] = value
end
end

-- Main
inheritModule("ParserMethods", ParserMethods)
inheritModule("ParserInstance", ParserInstance, "ParserMethods", ParserMethods)

return ParserInstance
end
Expand Down

0 comments on commit 31824c3

Please sign in to comment.