Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patching by preprocessors #149

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion loaders/actions.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
actions_loader = { }
actions_loader = {
preprocessors = {}
}

require("loaders.actions.mod")

Expand Down
3 changes: 3 additions & 0 deletions loaders/actions/add_preprocessor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function actions_loader.add_preprocessor(fn)
table.insert(actions_loader.preprocessors, fn)
end
10 changes: 4 additions & 6 deletions loaders/actions/custom_require.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ function actions_loader.custom_require(name)
local file = io.open(filename, "rb")
if file then
local header = actions_loader.extract_header(modulepath)

actions_loader.write_events(created_file, header)
actions_loader.set_priority(created_file, header)
actions_loader.write_input_parameters(created_file, header)
actions_loader.write_function(created_file, header, modulepath)
actions_loader.write_return(created_file, header)

for _, fn in ipairs(actions_loader.preprocessors) do
fn(created_file, header, modulepath)
end

created_file:close()
-- Compile and return the module
Expand Down
1 change: 1 addition & 0 deletions loaders/actions/mod.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require("loaders.actions.custom_require")
require("loaders.actions.add_preprocessor")
require("loaders.actions.extract_header")
require("loaders.actions.write_events")
require("loaders.actions.set_priority")
Expand Down
4 changes: 3 additions & 1 deletion loaders/actions/set_priority.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function actions_loader.set_priority (created_file, header)
function actions_loader.set_priority (created_file, header, modulepath)
created_file:write("\nlocal priority = " .. header.priority .. " \n\n")
end

actions_loader.add_preprocessor(actions_loader.set_priority)
4 changes: 3 additions & 1 deletion loaders/actions/write_events.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function actions_loader.write_events (created_file, header)
function actions_loader.write_events (created_file, header, modulepath)
created_file:write("local event = { \"" .. header.event[1] .. "\"") -- put values from yaml in lua form
for _, yaml_event in ipairs(header.event) do
if yaml_event ~= header.event[1] then
Expand All @@ -7,3 +7,5 @@ function actions_loader.write_events (created_file, header)
end
created_file:write(" }")
end

actions_loader.add_preprocessor(actions_loader.write_events)
2 changes: 2 additions & 0 deletions loaders/actions/write_function.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ function actions_loader.write_function (created_file, header, modulepath)
end
created_file:write("\nend")
end

actions_loader.add_preprocessor(actions_loader.write_function)
4 changes: 3 additions & 1 deletion loaders/actions/write_input_parameters.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function actions_loader.write_input_parameters (created_file, header)
function actions_loader.write_input_parameters (created_file, header, modulepath)
if header.input_parameters[1] then
created_file:write("local input_parameters = { " .. "\"" .. header.input_parameters[1] .. "\"")
for k, v in pairs(header.input_parameters) do
Expand All @@ -10,3 +10,5 @@ function actions_loader.write_input_parameters (created_file, header)
created_file:write("}\n")
end
end

actions_loader.add_preprocessor(actions_loader.write_input_parameters)
4 changes: 3 additions & 1 deletion loaders/actions/write_return.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function actions_loader.write_return (created_file, header)
function actions_loader.write_return (created_file, header, modulepath)
created_file:write("\n\nreturn{\n\tevent = event,\n\taction = action,\n\tpriority = priority,\n\tinput_parameters = input_parameters\n}") -- ending return
end

actions_loader.add_preprocessor(actions_loader.write_return)
4 changes: 3 additions & 1 deletion loaders/rules.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
rules_loader = { }
rules_loader = {
preprocessors = {}
}

require("loaders.rules.mod")

Expand Down
3 changes: 3 additions & 0 deletions loaders/rules/add_preprocessor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function rules_loader.add_preprocessor(fn)
table.insert(rules_loader.preprocessors, fn)
end
16 changes: 10 additions & 6 deletions loaders/rules/custom_require.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ function rules_loader.custom_require(name)
local header = rules_loader.extract_header(modulepath)
local priority = header.priority or 1

rules_loader.write_priority(created_file, header, priority)
rules_loader.write_events_table(created_file, header)
rules_loader.write_input_parameter(created_file, header)
rules_loader.write_rule_function(created_file, header, modulename, priority, modulepath)
rules_loader.write_get_events_parameters(created_file, header)
rules_loader.write_return(created_file, header)
-- rules_loader.write_priority(created_file, header, modulename, priority, modulepath)
-- rules_loader.write_events_table(created_file, header, modulename, priority, modulepath)
-- rules_loader.write_input_parameter(created_file, header, modulename, priority, modulepath)
-- rules_loader.write_rule_function(created_file, header, modulename, priority, modulepath)
-- rules_loader.write_get_events_parameters(created_file, header, modulename, priority, modulepath)
-- rules_loader.write_return(created_file, header, modulename, priority, modulepath)

for _, fn in ipairs(rules_loader.preprocessors) do
fn(created_file, header, modulename, priority, modulepath)
end

created_file:close()
local to_compile = io.open("module.lua", "rb")
Expand Down
1 change: 1 addition & 0 deletions loaders/rules/mod.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require("loaders.rules.custom_require")
require("loaders.rules.add_preprocessor")
require("loaders.rules.extract_header")
require("loaders.rules.write_events_table")
require("loaders.rules.write_priority")
Expand Down
4 changes: 3 additions & 1 deletion loaders/rules/write_events_table.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function rules_loader.write_events_table (created_file, header)
function rules_loader.write_events_table (created_file, header, modulename, priority, modulepath)
created_file:write("\nlocal events_table = { " .. "\"" .. header.events_table[1] .. "\"")
for k, v in pairs(header.events_table) do
if k ~= 1 then
Expand All @@ -7,3 +7,5 @@ function rules_loader.write_events_table (created_file, header)
end
created_file:write("}")
end

rules_loader.add_preprocessor(rules_loader.write_events_table)
4 changes: 3 additions & 1 deletion loaders/rules/write_get_events_parameters.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function rules_loader.write_get_events_parameters (created_file, header)
function rules_loader.write_get_events_parameters (created_file, header, modulename, priority, modulepath)
created_file:write("\nlocal function get_events_parameters(events_actions)")
created_file:write("\n\tfor k, v in pairs(events_table) do")
created_file:write("\n\t\tfor k1, v1 in pairs(events_actions[v]) do")
Expand All @@ -11,3 +11,5 @@ function rules_loader.write_get_events_parameters (created_file, header)
created_file:write("\n\tend")
created_file:write("\nend")
end

rules_loader.add_preprocessor(rules_loader.write_get_events_parameters)
4 changes: 3 additions & 1 deletion loaders/rules/write_input_parameter.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function rules_loader.write_input_parameter (created_file, header)
function rules_loader.write_input_parameter (created_file, header, modulename, priority, modulepath)
created_file:write("\nlocal input_parameter = \"" .. header.input_parameter .. "\"")
end

rules_loader.add_preprocessor(rules_loader.write_input_parameter)
4 changes: 3 additions & 1 deletion loaders/rules/write_priority.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function rules_loader.write_priority (created_file, header, priority)
function rules_loader.write_priority (created_file, header, modulename, priority, modulepath)
if priority > 100 then priority = 100 end

created_file:write("local priority = " .. priority)
end

rules_loader.add_preprocessor(rules_loader.write_priority)
4 changes: 3 additions & 1 deletion loaders/rules/write_return.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function rules_loader.write_return (created_file, header)
function rules_loader.write_return (created_file, header, modulename, priority, modulepath)
created_file:write("\nreturn{\n\trule = rule,\n\tpriority = priority,\n\tget_events_parameters = get_events_parameters\n}")
end

rules_loader.add_preprocessor(rules_loader.write_return)
2 changes: 2 additions & 0 deletions loaders/rules/write_rule_function.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ function rules_loader.write_rule_function (created_file, header, modulename, pri
created_file:write("\n\tend")
created_file:write("\nend\n") -- bottom rule function wrapper
end

rules_loader.add_preprocessor(rules_loader.write_rule_function)