Gradually adding support for all features

This commit is contained in:
Pavel Shevaev 2022-12-08 16:05:26 +03:00
parent f0f5ef4462
commit 6835969037
2 changed files with 60 additions and 1 deletions

View File

@ -102,6 +102,12 @@ function _add_twig_support(\Twig\Environment $twig)
return buf2var($name, $fname, $type, $buf, $tokens, $is_ptr);
}
));
$twig->addFunction(new \Twig\TwigFunction('var2buf',
function($name, $fname, $type, $buf, $tokens, $is_ptr)
{
return var2buf($name, $fname, $type, $buf, $tokens, $is_ptr);
}
));
$twig->addFilter(new \Twig\TwigFilter('go_type',
function($type, $tokens)
{
@ -247,6 +253,44 @@ function buf2var($name, $fname, \mtgType $type, $buf, array $tokens = array(), $
return $str;
}
function var2buf($name, $fname, \mtgType $type, $buf, array $tokens = array(), $is_ptr = false)
{
$str = '';
if($type instanceof \mtgBuiltinType)
{
$str .= _write_op("{$buf}.Write".builtin_type_prefix($type)."($fname, \"$name\")")."\n";
}
else if($type instanceof \mtgMetaEnum)
{
$str .= _write_op("{$buf}.WriteI32(int32($fname), \"$name\")");
}
else if($type instanceof \mtgMetaStruct)
{
if(array_key_exists('virtual', $tokens))
$str .= _write_op("meta.WriteStructGeneric($buf, ".($is_ptr?"":"&")."$fname, \"$name\")");
else
$str .= _write_op("meta.WriteStruct($buf, ".($is_ptr?"":"&")."$fname, \"$name\")");
}
else if($type instanceof \mtgArrType)
{
$str .= "{$buf}.BeginContainer(\"{$name}\")\n";
$str .= " for _, v := range({$fname}) {\n";
$str .= " ".var2buf("", "v", $type->getValue(), $buf, $tokens, true)."\n";
$str .= " }\n";
$str .= " "._write_op("{$buf}.EndContainer()")."\n";
}
else
throw new Exception("Unknown type '$type'");
return $str;
}
function _write_op($op)
{
return "if err := $op; err != nil { return err }";
}
function _read_op(array $tokens, $op)
{
return "if err := $op; err != nil { return " . (array_key_exists("optional", $tokens) ? "/*optional*/nil" : "err"). " }";

View File

@ -86,9 +86,12 @@ func (self *{{o.name}}) Write(writer meta.Writer) error {
return meta.WriteStruct(writer, self, "")
}
func (self *{{o.name}}) WriteFields(writer meta.Writer) error {
%write_buffer%
{{_self.struct_write_fields(o)}}
return nil
}
%ext_methods%
%analytics_methods%
@ -178,6 +181,18 @@ fieldsMask meta.FieldsMask //@bitfields support
{% endmacro %}
{% macro struct_write_fields(o) %}
{%- if o.parent ~%}
if err := self.{{o.parent.name}}.WriteFields(writer); err != nil { return err }
{%- endif ~%}
{%- for f in o.fields ~%}
{{var2buf(f.name, 'self.' ~ f.name|ucfirst, f.type, "writer", f.tokens, has_token(f, 'virtual'))}}
{%- endfor -%}
{% endmacro %}
{% macro decl_enum(o) %}
//==============================