Prototyping native modules support

This commit is contained in:
Pavel Shevaev 2023-07-18 15:22:57 +03:00
parent a66e20ec4d
commit 9d80229653
2 changed files with 46 additions and 0 deletions

View File

@ -242,6 +242,31 @@ function add_twig_support(\Twig\Environment $twig)
return $sliced;
}
));
$twig->addFunction(new \Twig\TwigFunction('get_modules',
function(\mtgMetaInfo $m)
{
$modules = array();
foreach($m->getUnits() as $u)
{
$tmp = $u->object->getToken('bhl_bind');
if(!$tmp || !is_string($tmp))
continue;
$m = module_path($tmp);
if($m)
$modules[$m] = module_name($m);
}
return $modules;
}
));
$twig->addFunction(new \Twig\TwigFunction('module_scope_or',
function($o, $default_val)
{
$m = $o->getToken("bhl_bind");
if(!$m || !is_string($m))
return $default_val;
return "Module".module_name($m).".ns";
}
));
}
function Warn($msg)
@ -289,6 +314,18 @@ function ns_prefix($name)
return $prefix;
}
function module_path($path)
{
return trim($path, '"');
}
function module_name($name)
{
$name = str_replace('/', '_', $name);
$name = str_replace('.', '_', $name);
return $name;
}
function norm_name($name)
{
return str_replace('.', '__', $name);

View File

@ -28,6 +28,10 @@ static public class Types_{{u.object.name|norm_name}} {
static public class {{register_class}} {
{%- for mk,mv in get_modules(meta) ~%}
static public Module Module{{mv}};
{%- endfor ~%}
public static void Register(Types types)
{
RegisterBegin(types);
@ -36,6 +40,11 @@ public static void Register(Types types)
public static void RegisterBegin(Types types)
{
{%- for mk,mv in get_modules(meta) ~%}
Module{{mv}} = new Module(types, "{{mk}}");
types.RegisterModule(Module{{mv}});
{%- endfor ~%}
{{ macro.call_reg_units(slice_units(meta.units, 20)) }}
}