Using consistent code style

This commit is contained in:
Pavel Shevaev 2023-08-10 12:52:45 +03:00
parent 1534bdf481
commit 6d46356fc2
1 changed files with 96 additions and 143 deletions

View File

@ -2,16 +2,6 @@
namespace metagen_go;
use Exception;
use mtgMetaInfo;
use mtgMetaInfoUnit;
use mtgMetaEnum;
use mtgMetaStruct;
use mtgMetaField;
use mtgMetaRPC;
use mtgType;
use mtgTypeRef;
use mtgBuiltinType;
use mtgArrType;
use Twig\{TwigTest, TwigFilter, TwigFunction};
function get_twig(array $inc_path = [])
@ -26,9 +16,9 @@ function get_twig(array $inc_path = [])
]);
$twig->addExtension(new \Twig\Extension\DebugExtension());
addTwigTests($twig);
addTwigFilters($twig);
addTwigFunctions($twig);
add_twig_tests($twig);
add_twig_filters($twig);
add_twig_functions($twig);
return $twig;
}
@ -56,67 +46,67 @@ function supported_tokens()
];
}
function addTwigTests(\Twig\Environment $twig)
function add_twig_tests(\Twig\Environment $twig)
{
$twig->addTest(new TwigTest(
'metaenum',
fn(mtgMetaInfoUnit $u): bool => $u->object instanceof mtgMetaEnum,
fn(\mtgMetaInfoUnit $u): bool => $u->object instanceof \mtgMetaEnum,
));
$twig->addTest(new TwigTest(
'metastruct',
fn(mtgMetaInfoUnit $u): bool => $u->object instanceof mtgMetaStruct,
fn(\mtgMetaInfoUnit $u): bool => $u->object instanceof \mtgMetaStruct,
));
$twig->addTest(new TwigTest(
'metarpc',
fn(mtgMetaInfoUnit $u): bool => $u->object instanceof mtgMetaRPC,
fn(\mtgMetaInfoUnit $u): bool => $u->object instanceof \mtgMetaRPC,
));
$twig->addTest(new TwigTest(
'enum',
fn(mtgType $t): bool => $t instanceof mtgMetaEnum,
fn(\mtgType $t): bool => $t instanceof \mtgMetaEnum,
));
$twig->addTest(new TwigTest(
'struct',
fn(mtgType $t): bool => $t instanceof mtgMetaStruct,
fn(\mtgType $t): bool => $t instanceof \mtgMetaStruct,
));
$twig->addTest(new TwigTest(
'array',
fn(mtgType $t): bool => $t instanceof mtgArrType,
fn(\mtgType $t): bool => $t instanceof \mtgArrType,
));
$twig->addTest(new TwigTest(
'builtin',
fn(mtgType $t): bool => $t instanceof mtgBuiltinType,
fn(\mtgType $t): bool => $t instanceof \mtgBuiltinType,
));
$twig->addTest(new TwigTest(
'numeric',
fn(mtgType $t): bool => $t instanceof mtgBuiltinType && $t->isNumeric(),
fn(\mtgType $t): bool => $t instanceof \mtgBuiltinType && $t->isNumeric(),
));
$twig->addTest(new TwigTest(
'int',
fn(mtgType $t): bool => $t instanceof mtgBuiltinType && $t->isInt(),
fn(\mtgType $t): bool => $t instanceof \mtgBuiltinType && $t->isInt(),
));
$twig->addTest(new TwigTest(
'uint',
fn(mtgType $t): bool => $t instanceof mtgBuiltinType && $t->isUint(),
fn(\mtgType $t): bool => $t instanceof \mtgBuiltinType && $t->isUint(),
));
$twig->addTest(new TwigTest(
'bool',
fn(mtgType $t): bool => $t instanceof mtgBuiltinType && $t->isBool(),
fn(\mtgType $t): bool => $t instanceof \mtgBuiltinType && $t->isBool(),
));
$twig->addTest(new TwigTest(
'float',
fn(mtgType $t): bool => $t instanceof mtgBuiltinType && $t->isFloat(),
fn(\mtgType $t): bool => $t instanceof \mtgBuiltinType && $t->isFloat(),
));
$twig->addTest(new TwigTest(
'double',
fn(mtgType $t): bool => $t instanceof mtgBuiltinType && $t->isDouble(),
fn(\mtgType $t): bool => $t instanceof \mtgBuiltinType && $t->isDouble(),
));
$twig->addTest(new TwigTest(
'string',
fn(mtgType $t): bool => $t instanceof mtgBuiltinType && $t->isString(),
fn(\mtgType $t): bool => $t instanceof \mtgBuiltinType && $t->isString(),
));
$twig->addTest(new TwigTest(
'blob',
fn(mtgType $t): bool => $t instanceof mtgBuiltinType && $t->isBlob(),
fn(\mtgType $t): bool => $t instanceof \mtgBuiltinType && $t->isBlob(),
));
$twig->addTest(new TwigTest(
@ -125,11 +115,11 @@ function addTwigTests(\Twig\Environment $twig)
));
}
function addTwigFilters(\Twig\Environment $twig)
function add_twig_filters(\Twig\Environment $twig)
{
$twig->addFilter(new TwigFilter(
'go_type',
fn(mtgType $t): string => goType($t),
fn(\mtgType $t): string => go_type($t),
));
$twig->addFilter(new TwigFilter(
'ucfirst',
@ -160,7 +150,7 @@ function addTwigFilters(\Twig\Environment $twig)
));
$twig->addFilter(new TwigFilter(
'alias',
function(mtgMetaField $field): string {
function(\mtgMetaField $field): string {
if ($field->hasToken('alias')) {
return $field->getToken('alias');
}
@ -169,19 +159,19 @@ function addTwigFilters(\Twig\Environment $twig)
));
$twig->addFilter(new TwigFilter(
'fname',
fn(mtgMetaField $field): string => snake2camel($field->getName())
fn(\mtgMetaField $field): string => snake2camel($field->getName())
));
$twig->addFilter(new TwigFilter(
'varname',
fn(mtgMetaField $field): string => lcfirst(snake2camel($field->getName()))
fn(\mtgMetaField $field): string => lcfirst(snake2camel($field->getName()))
));
$twig->addFilter(new TwigFilter(
'builtin_type_suffix',
fn (mtgBuiltinType $type): string => builtinTypeMethodSuffix($type),
fn (\mtgBuiltinType $type): string => builtin_type_method_suffix($type),
));
$twig->addFilter(new TwigFilter(
'default_val',
fn(mtgMetaField $field) => defaultValue($field)
fn(\mtgMetaField $field) => default_value($field)
));
$twig->addFilter(new TwigFilter(
@ -190,7 +180,7 @@ function addTwigFilters(\Twig\Environment $twig)
));
}
function addTwigFunctions(\Twig\Environment $twig)
function add_twig_functions(\Twig\Environment $twig)
{
$twig->addFunction(new TwigFunction(
'Error',
@ -204,7 +194,7 @@ function addTwigFunctions(\Twig\Environment $twig)
));
$twig->addFunction(new TwigFunction(
'has_token_in_parent',
fn(mtgMetaStruct $s, string $token): bool => $s->hasTokenInParent($token),
fn(\mtgMetaStruct $s, string $token): bool => $s->hasTokenInParent($token),
));
$twig->addFunction(new TwigFunction(
'token',
@ -216,7 +206,7 @@ function addTwigFunctions(\Twig\Environment $twig)
));
$twig->addFunction(new TwigFunction(
'get_all_fields',
fn(mtgMetaStruct $s): array => \mtg_get_all_fields($s),
fn(\mtgMetaStruct $s): array => \mtg_get_all_fields($s),
));
$twig->addFunction(new TwigFunction(
'arr_fill',
@ -224,7 +214,7 @@ function addTwigFunctions(\Twig\Environment $twig)
));
$twig->addFunction(new TwigFunction(
'table_pkey',
function(mtgMetaStruct $struct): array {
function(\mtgMetaStruct $struct): array {
$pkey = explode(',', $struct->getToken('table_pkey'));
$fields = [];
foreach ($pkey as $fieldName) {
@ -235,19 +225,19 @@ function addTwigFunctions(\Twig\Environment $twig)
));
$twig->addFunction(new TwigFunction(
'table_fields',
function(mtgMetaStruct $struct): array {
function(\mtgMetaStruct $struct): array {
$pkey = explode(',', $struct->getToken('table_pkey'));
return array_filter(
$struct->getFields(),
fn(mtgMetaField $field): bool => !in_array($field->getName(), $pkey),
fn(\mtgMetaField $field): bool => !in_array($field->getName(), $pkey),
);
}
));
$twig->addFunction(new TwigFunction(
'meta_field',
function (string $str) use ($twig): mtgMetaField {
function (string $str) use ($twig): \mtgMetaField {
$meta = $twig->getGlobals()['meta'];
return parseMetaField($str, $meta);
return parse_meta_field($str, $meta);
}
));
@ -276,115 +266,78 @@ function snake2camel(string $str): string
return str_replace('_', '', ucwords($str, '_'));
}
function goType(mtgType $type, array $tokens = []): string
function go_type(\mtgType $type, array $tokens = []): string
{
if ($type instanceof mtgMetaEnum) {
if($type instanceof \mtgMetaEnum)
return $type->getName();
} else if ($type instanceof mtgMetaStruct) {
else if($type instanceof \mtgMetaStruct)
return $type->getName();
} else if ($type instanceof mtgBuiltinType) {
if ($type->isFloat()) {
else if($type instanceof \mtgBuiltinType)
{
if($type->isFloat())
return 'float32';
} else if ($type->isDouble()) {
else if($type->isDouble())
return 'float64';
} else if ($type->isBlob()) {
else if($type->isBlob())
return '[]byte';
} else {
else
return $type->getName();
}
} else if ($type instanceof mtgArrType) {
$native = goType($type->getValue());
}
else if($type instanceof \mtgArrType)
{
$native = go_type($type->getValue());
return "[]$native";
} else {
}
else
throw new Exception("Unknown type '$type'");
}
}
function builtinTypeMethodSuffix(mtgBuiltinType $type): string
function builtin_type_method_suffix(\mtgBuiltinType $type): string
{
if ($type->isBlob()) {
if($type->isBlob())
return 'Bytes';
}
return ucfirst(goType($type));
return ucfirst(go_type($type));
}
function defaultValue(mtgMetaField $field)
function default_value(\mtgMetaField $field)
{
$type = $field->getType();
$tokens = $field->getTokens();
$default = $field->getToken('default');
if ($type instanceof mtgBuiltinType) {
if ($type->isNumeric()) {
if($type instanceof \mtgBuiltinType)
{
if($type->isNumeric())
return $default ?? 0;
} else if ($type->isString()) {
else if($type->isString())
return $default ?? '""';
} else if($type->isBool()) {
else if($type->isBool())
return $default ?? 'false';
} else if($type->isBlob()) {
if ($default == 'null') {
$default = 'nil';
}
return $default ?? 'nil';
} else {
throw new Exception("Unknown type '$type'");
}
} else if($type instanceof mtgMetaEnum) {
return $default ? goType($type, $tokens)."_".trim($tokens['default'], '"') : 0;
} else {
throw new Exception("Unknown type '$type'");
}
}
function parseMetaField(string $str, mtgMetaInfo $meta): mtgMetaField
{
list($name, $type) = explode('|', $str);
return new mtgMetaField($name, new mtgTypeRef(new mtgBuiltinType($type)));
}
function go_type(\mtgType $type, array $tokens = array())
{
if($type instanceof \mtgArrType)
{
$vtype = $type->getValue();
$native = go_type($vtype);
$str = "[]";
if(array_key_exists("virtual", $tokens))
$str .= "I";
else
$str .= $vtype instanceof \mtgMetaStruct ? "*" : "";
$str .= $native;
return $str;
}
else if($type instanceof \mtgBuiltinType)
{
if($type->isFloat())
return "float32";
else if($type->isDouble())
return "float64";
else if($type->isBlob())
return "[]byte";
{
if($default == 'null')
$default = 'nil';
return $default ?? 'nil';
}
else
return $type->getName();
}
throw new Exception("Unknown type '$type'");
}
else if($type instanceof \mtgMetaEnum)
return $type->getName();
else if($type instanceof \mtgMetaStruct)
{
if(array_key_exists("virtual", $tokens))
return "I{$type->getName()}";
else
return $type->getName();
}
return $default ? go_type($type, $tokens)."_".trim($tokens['default'], '"') : 0;
else
throw new Exception("Unknown type '$type'");
}
function builtin_type_prefix(mtgBuiltinType $type)
function parse_meta_field(string $str, \mtgMetaInfo $meta): \mtgMetaField
{
list($name, $type) = explode('|', $str);
return new \mtgMetaField($name, new \mtgTypeRef(new \mtgBuiltinType($type)));
}
function builtin_type_prefix(\mtgBuiltinType $type)
{
switch($type->getName())
{
@ -421,11 +374,11 @@ function builtin_type_prefix(mtgBuiltinType $type)
}
}
function field_reset($name, mtgType $type, array $tokens)
function field_reset($name, \mtgType $type, array $tokens)
{
$str = '';
if($type instanceof mtgBuiltinType)
if($type instanceof \mtgBuiltinType)
{
if($type->isNumeric())
{
@ -458,23 +411,23 @@ function field_reset($name, mtgType $type, array $tokens)
else
throw new Exception("Unknown type '$type'");
}
else if($type instanceof mtgArrType)
else if($type instanceof \mtgArrType)
{
$str = "if self.$name == nil { \nself.$name = make(".goType($type, $tokens).",0) \n}\n ";
$str = "if self.$name == nil { \nself.$name = make(".go_type($type, $tokens).",0) \n}\n ";
$str .= "self.$name = self.{$name}[0:0]";
}
else if($type instanceof mtgMetaEnum)
else if($type instanceof \mtgMetaEnum)
{
if(array_key_exists('default', $tokens))
$str = "self.$name = ".goType($type, $tokens)."_".trim($tokens['default'], '"');
$str = "self.$name = ".go_type($type, $tokens)."_".trim($tokens['default'], '"');
else
$str = "self.$name = 0";
}
else if($type instanceof mtgMetaStruct)
else if($type instanceof \mtgMetaStruct)
{
$is_virtual = array_key_exists("virtual", $tokens);
if($is_virtual)
$str .= "self.$name = New".ltrim(goType($type, $tokens),'I')."() ";
$str .= "self.$name = New".ltrim(go_type($type, $tokens),'I')."() ";
else
$str .= "self.$name.Reset()";
}
@ -483,37 +436,37 @@ function field_reset($name, mtgType $type, array $tokens)
return $str;
}
function buf2var($name, $fname, mtgType $type, $buf, array $tokens = array(), $is_ptr = false)
function buf2var($name, $fname, \mtgType $type, $buf, array $tokens = array(), $is_ptr = false)
{
$str = '';
if($type instanceof mtgBuiltinType)
if($type instanceof \mtgBuiltinType)
{
$str .= _read_op($tokens, $buf.'.Read'.builtin_type_prefix($type).'(&'.$fname.', "'.$name.'")');
}
else if($type instanceof mtgMetaEnum)
else if($type instanceof \mtgMetaEnum)
{
$str .= _read_op($tokens, "{$buf}.ReadI32((*int32)(&{$fname}), \"$name\")");
$str .= "\n if !{$fname}.IsValid() { return errors.Errorf(\"Bad enum value %d for $name\", $fname) }";
}
else if($type instanceof mtgMetaStruct)
else if($type instanceof \mtgMetaStruct)
{
if(array_key_exists('virtual', $tokens))
$str .= "if v, err := meta.ReadStructGeneric($buf, CreateById, \"{$name}\"); err != nil { return err } else { {$fname} = v.(I{$type}) }";
else
$str .= _read_op($tokens, "meta.ReadStruct($buf, ".($is_ptr?"":"&")."$fname, \"$name\")");
}
else if($type instanceof mtgArrType)
else if($type instanceof \mtgArrType)
{
$is_virtual = array_key_exists("virtual", $tokens);
$native_type = goType($type->getValue(), $tokens);
$native_type = go_type($type->getValue(), $tokens);
$offset = "\n ";
$str .= "/*[]{$name}*/";
$str .= $offset . _read_op($tokens, "{$buf}.BeginContainer(\"$name\")");
$str .= $offset . "_{$name}_size, err := {$buf}.GetContainerSize()";
$str .= $offset . "if err != nil { return err }";
$need_new = !$is_virtual && $type->getValue() instanceof mtgMetaStruct;
$need_new = !$is_virtual && $type->getValue() instanceof \mtgMetaStruct;
$str .= $offset . "for ; _{$name}_size > 0; _{$name}_size-- {";
$offset = "\n ";
@ -533,26 +486,26 @@ function buf2var($name, $fname, mtgType $type, $buf, array $tokens = array(), $i
return $str;
}
function var2buf($name, $fname, mtgType $type, $buf, array $tokens = array(), $is_ptr = false)
function var2buf($name, $fname, \mtgType $type, $buf, array $tokens = array(), $is_ptr = false)
{
$str = '';
if($type instanceof mtgBuiltinType)
if($type instanceof \mtgBuiltinType)
{
$str .= _write_op("{$buf}.Write".builtin_type_prefix($type)."($fname, \"$name\")")."\n";
}
else if($type instanceof mtgMetaEnum)
else if($type instanceof \mtgMetaEnum)
{
$str .= _write_op("{$buf}.WriteI32(int32($fname), \"$name\")");
}
else if($type instanceof mtgMetaStruct)
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, &$fname, \"$name\")");
}
else if($type instanceof mtgArrType)
else if($type instanceof \mtgArrType)
{
$str .= "{$buf}.BeginContainer(\"{$name}\")\n";
$str .= " for _, v := range({$fname}) {\n";