Skip to content

Commit 31824c3

Browse files
committed
Put the "inheritModule" function into the Helpers module
1 parent 6c66317 commit 31824c3

File tree

5 files changed

+36
-45
lines changed

5 files changed

+36
-45
lines changed

src/Evaluator/Evaluator.lua

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
--[[
22
Name: Evaluator.lua
33
Author: ByteXenon [Luna Gilbert]
4-
Date: 2024-05-21
4+
Date: 2024-06-14
55
--]]
66

7+
--* Dependencies *--
8+
local Helpers = require("Helpers/Helpers")
9+
710
--* Imports *--
11+
local inheritModule = Helpers.inheritModule
12+
813
local unpack = (unpack or table.unpack)
914
local insert = table.insert
1015

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

162-
local function inheritModule(moduleName, moduleTable)
163-
for index, value in pairs(moduleTable) do
164-
if EvaluatorInstance[index] then
165-
return error("Conflicting names in " .. moduleName .. " and EvaluatorInstance: " .. index)
166-
end
167-
EvaluatorInstance[index] = value
168-
end
169-
end
170-
171167
-- Main
172-
inheritModule("EvaluatorMethods", EvaluatorMethods)
168+
inheritModule("EvaluatorInstance", EvaluatorInstance, "EvaluatorMethods", EvaluatorMethods)
173169

174170
return EvaluatorInstance
175171
end

src/Helpers/Helpers.lua

+15
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,19 @@ function Helpers.makeTrie(table)
6969
return trieTable
7070
end
7171

72+
--- Inherit a module into another module.
73+
-- @param <String> parentModuleName The name of the parent module.
74+
-- @param <Table> parentModule The table of the parent module.
75+
-- @param <String> moduleName The name of the module to inherit.
76+
-- @param <Table> moduleTable The table of the module to inherit.
77+
function Helpers.inheritModule(parentModuleName, parentModule, moduleName, moduleTable)
78+
for index, value in pairs(moduleTable) do
79+
if parentModule[index] then
80+
local errorMessage = ("Conflicting names in %s and %s: %s"):format(parentModuleName, moduleName, index)
81+
return error(errorMessage)
82+
end
83+
parentModule[index] = value
84+
end
85+
end
86+
7287
return Helpers

src/Lexer/Lexer.lua

+2-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local TokenFactory = require("Lexer/TokenFactory")
1212
local makeTrie = Helpers.makeTrie
1313
local stringToTable = Helpers.stringToTable
1414
local createPatternLookupTable = Helpers.createPatternLookupTable
15+
local inheritModule = Helpers.inheritModule
1516

1617
local concat = table.concat
1718
local insert = table.insert
@@ -338,17 +339,8 @@ function Lexer:new(expression, operators, charPos)
338339
LexerInstance.operatorsTrie = DEFAULT_OPERATORS_TRIE
339340
end
340341

341-
local function inheritModule(moduleName, moduleTable)
342-
for index, value in pairs(moduleTable) do
343-
if LexerInstance[index] then
344-
return error("Conflicting names in " .. moduleName .. " and LexerInstance: " .. index)
345-
end
346-
LexerInstance[index] = value
347-
end
348-
end
349-
350342
-- Main
351-
inheritModule("LexerMethods", LexerMethods)
343+
inheritModule("LexerInstance", LexerInstance, "LexerMethods" , LexerMethods)
352344

353345
return LexerInstance
354346
end

src/MathParser.lua

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--[[
22
Name: MathParser.lua
33
Author: ByteXenon [Luna Gilbert]
4-
Date: 2024-04-25
4+
Date: 2024-06-14
55
--]]
66

77
local scriptPath, requirePath, localPath, oldPath
@@ -15,9 +15,13 @@ if not LUAXEN_PACKER then
1515
end
1616

1717
--* Dependencies *--
18+
local Helpers = require("Helpers/Helpers")
1819
local Evaluator = require("Evaluator/Evaluator")
19-
local Lexer = require("Lexer/Lexer")
20-
local Parser = require("Parser/Parser")
20+
local Lexer = require("Lexer/Lexer")
21+
local Parser = require("Parser/Parser")
22+
23+
--* Imports *--
24+
local inheritModule = Helpers.inheritModule
2125

2226
--* MathParserMethods *--
2327
local MathParserMethods = {}
@@ -157,17 +161,8 @@ function MathParser:new(operatorPrecedenceLevels, variables, operatorFunctions,
157161
MathParserInstance.Parser = Parser:new(nil, operatorPrecedenceLevels)
158162
MathParserInstance.Evaluator = Evaluator:new(nil, variables, operatorFunctions, functions)
159163

160-
local function inheritModule(moduleName, moduleTable)
161-
for index, value in pairs(moduleTable) do
162-
if MathParserInstance[index] then
163-
return error("Conflicting names in " .. moduleName .. " and MathParserInstance: " .. index)
164-
end
165-
MathParserInstance[index] = value
166-
end
167-
end
168-
169164
-- Main
170-
inheritModule("MathParserMethods", MathParserMethods)
165+
inheritModule("MathParserInstance", MathParserInstance, "MathParserMethods", MathParserMethods)
171166

172167
return MathParserInstance
173168
end

src/Parser/Parser.lua

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--[[
22
Name: Parser.lua
33
Author: ByteXenon [Luna Gilbert]
4-
Date: 2024-05-21
4+
Date: 2024-06-14
55
--]]
66

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

1111
--* Imports *--
1212
local stringToTable = Helpers.stringToTable
13+
local inheritModule = Helpers.inheritModule
14+
1315
local insert = table.insert
1416
local concat = table.concat
1517
local max = math.max
@@ -292,17 +294,8 @@ function Parser:new(tokens, operatorPrecedenceLevels, tokenIndex, expression)
292294
ParserInstance.operatorPrecedenceLevels = operatorPrecedenceLevels or DEFAULT_OPERATOR_PRECEDENCE_LEVELS
293295
ParserInstance.charStream = (type(expression) == "string" and stringToTable(expression)) or expression
294296

295-
local function inheritModule(moduleName, moduleTable)
296-
for index, value in pairs(moduleTable) do
297-
if ParserInstance[index] then
298-
return error("Conflicting names in " .. moduleName .. " and ParserInstance: " .. index)
299-
end
300-
ParserInstance[index] = value
301-
end
302-
end
303-
304297
-- Main
305-
inheritModule("ParserMethods", ParserMethods)
298+
inheritModule("ParserInstance", ParserInstance, "ParserMethods", ParserMethods)
306299

307300
return ParserInstance
308301
end

0 commit comments

Comments
 (0)