--
-- decoy.module
--
-- SPDX-FileCopyrightText: Copyright (c) 2023-2024 Chris Pressey, Cat's Eye Technologies.
-- This work is distributed under a 2-clause BSD license. For more information, see:
-- SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Decoy
local Module = {}
function Module:register_symbol(name, value)
self.symbols[name] = value
end
function Module:lookup_symbol(name)
return self.symbols[name]
end
function Module:get_module_name()
local module_name = self:lookup_symbol("module")
if module_name ~= nil then
module_name = module_name:text()
end
return module_name
end
function Module:foreach_symbol(f)
for name, value in pairs(self.symbols) do
f(name, value)
end
end
--
-- Extract symbols defined in the native program text
-- and set up externs for them in the module.
--
function Module:populate_from_native(program_text)
local line
for line in string.gmatch(program_text, "[^\r\n]+") do
local definedum = string.match(line, "#_# define (.*)")
if definedum ~= nil then
self:register_symbol(definedum, Symbol.new(definedum))
end
end
end
Module.is_class_of = function(obj)
local mt = getmetatable(obj)
return mt and mt.__index == Module
end
Module.new = function(type, content)
local self = {
type = type,
content = content,
symbols = {},
}
setmetatable(self, {__index = Module})
return self
end
return Module