preliminary variant syntax

This commit is contained in:
jgcodes2020 2024-12-14 10:33:46 -05:00
parent 778a979714
commit f894301571
4 changed files with 58 additions and 3 deletions

View file

@ -371,6 +371,42 @@ class IdentLiteral(AstNode):
else:
return None
class VariantValue(AstNode):
grammar = [
"variant",
"<",
UseQuoted("type"),
">",
"(",
UseQuoted("value"),
")"
]
@property
def var_type(self) -> str:
return self.tokens["type"]
@property
def var_value(self) -> str:
return self.tokens["value"]
@validate()
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
if (
isinstance(expected_type, gir.IntType)
or isinstance(expected_type, gir.UIntType)
or isinstance(expected_type, gir.FloatType)
or isinstance(expected_type, gir.FloatType)
):
raise CompileError(f"Cannot convert variant to number")
elif isinstance(expected_type, gir.StringType):
raise CompileError("Cannot convert variant to string")
if isinstance(expected_type, gir.Boxed) and expected_type.full_name == "GLib.Variant":
pass
else:
raise CompileError(f"Cannot convert variant into {expected_type.full_name}")
pass
class Literal(AstNode):
grammar = AnyOf(