class Example(BaseExample): fld_2 = NullField() fld_1 = Field() @langkit_property() def to_public(p=T.PrivatePoint): return Point.new(label=String("from private"), x=p.x, y=p.y) @langkit_property(public=True) def prop(p=T.Point): return Self.to_public(PrivatePoint.new(x=p.x, y=p.y)) @langkit_property(public=True) def result(): return T.NodeResult.new(n=Self, e=Entity) # Test for primitive types id_bool = Property(lambda id=T.Bool: id, public=True) id_int = Property(lambda id=T.Int: id, public=True) id_bigint = Property(lambda id=T.BigInt: id, public=True) id_char = Property(lambda id=T.Character: id, public=True) id_token = Property(lambda id=T.Token: id, public=True) id_sym = Property(lambda id=T.Symbol: id, public=True) id_unit = Property(lambda id=T.AnalysisUnit: id, public=True) id_root_node = Property(lambda id=T.FooNode: id, public=True) id_name = Property(lambda id=T.Name: id, public=True) # Test for enums id_unit_kind = Property(lambda id=T.AnalysisUnitKind: id, public=True) # Test for arrays id_node_array = Property(lambda id=T.FooNode.entity.array: id, public=True) id_expr_array = Property(lambda id=T.Expr.entity.array: id, public=True) id_bigint_array = Property(lambda id=T.BigInt.array: id, public=True) id_unit_array = Property(lambda id=T.AnalysisUnit.array: id, public=True) # Test for iterators create_bigint_iterator = Property( ArrayLiteral([BigIntLiteral(1), BigIntLiteral(2), BigIntLiteral(3)]) .to_iterator, public=True ) id_bigint_iterator = Property(lambda id=T.BigInt.iterator: id, public=True) # Test for default values id_dflt_bool = Property(lambda id=(T.Bool, True): id, public=True) id_dflt_int = Property(lambda id=(T.Int, 42): id, public=True) id_dflt_char = Property( lambda id=(T.Character, CharacterLiteral('\x00')): id, public=True) id_dflt_root_node = Property(lambda id=(T.FooNode, No(T.FooNode)): id, public=True)
def check(): return Self.children.all( # This condition is always False, so we have a loop early exit, # which used to leave the result of "c.get_bigint" (big integers # are ref-counted) allocated when exitting the scope: the finalizer # for that scope was not called in that case. lambda c: c.get_bigint() == BigIntLiteral(0))
def get_struct(): return New(MyStruct, entity_field=Self, array_field=ArrayLiteral([ Self.cast(T.FooNode).as_bare_entity, Self.parent.as_bare_entity]), bigint_field=BigIntLiteral(10**100))
from utils import emit_and_print_errors def run(name, expr): print('== {} =='.format(name)) class KV(Struct): key = UserField(type=T.String) val = UserField(type=T.BigInt) class FooNode(ASTNode): @langkit_property(public=True) def increment(kv=KV): return expr(kv) class Example(FooNode): token_node = True emit_and_print_errors(lkt_file='foo.lkt') print('') big_1 = BigIntLiteral(1) run('Not a struct', lambda kv: big_1.update(val=kv)) run('Invalid field', lambda kv: kv.update(nosuchfield=big_1)) run('Invalid type', lambda kv: kv.update(key=big_1)) print('Done')
def evaluate(): return BigIntLiteral(If(Self.left.evaluate < Self.right.evaluate, 1, 0))
def increment(kv=KV): return kv.update(value=kv.value + BigIntLiteral(1))
def get_bigint(): return BigIntLiteral(1)
def bigint_array(): return [BigIntLiteral(1), BigIntLiteral(2), BigIntLiteral(3)]