def get_rebindings(inverse=T.Bool): example_nodes = Var(Self.parent.cast(T.Example.list).as_array) n1 = Var(example_nodes.at(If(inverse, 1, 2))) n2 = Var(example_nodes.at(If(inverse, 2, 1))) return No(T.EnvRebindings).append_rebinding( n1.children_env, n2.children_env, )
def rebind(from_block=T.Block.entity, to_block=T.Block.entity): rbdng = Var( Entity.info.rebindings.append_rebinding( from_block.params.children_env, to_block.children_env)) e_info = Var(New(T.entity_info, md=Entity.info.md, rebindings=rbdng)) return New(Block.entity, el=Self, info=e_info)
def test_struct_iterator(): val = Var(TestStruct.new( count=2, nodes=[No(Example.entity), Entity], )) itr = Var( ArrayLiteral([val, val], element_type=TestStruct).to_iterator) return ArrayLiteral([itr, itr], element_type=T.TestStruct.iterator).to_iterator
def test_dotexpr_lhs(): """ Test various valid dotexpr's LHS. """ a = Var(ArrayLiteral([1]).find(lambda v: v == 1)) b = Var(Let(lambda b=[1, 2]: b).find(lambda v: v == 1)) c = Var(String("hello").find(lambda c: c == Char('h'))) ignore(b) ignore(c) return a
def rebind(): left_env = Var(Self.left.children_env) right_env = Var(Self.right.children_env) # Depending on whether "left_env" is rebindable, the following will # succeed or raise a PropertyError. The status of "right_env" does not # matter. r = Var(No(T.EnvRebindings).append_rebinding(left_env, right_env)) return Self.children_env.rebind_env(r).env_node
def mmz_prop(): # Both calls to unit_count are memoized, but when called for the first # time, the first one's result will be different from the second one. before = Var([Self.unit_count]) # Update context version by parsing a new unit ignore(Var(Self.fetch_example_unit)) after = Var([Self.unit_count]) return before.concat(after)
def rebind(from_node=T.FooNode.entity, to_node=T.FooNode.entity): rbdng = Var(Entity.info.rebindings.append_rebinding( from_node.children_env, to_node.children_env )) e_info = Var(New(T.entity_info, md=Entity.info.md, rebindings=rbdng, from_rebound=False)) return New(T.FooNode.entity, node=Self, info=e_info)
def arg_exprs_assocs_getter(): """ For each argument, associate its name to its default expression. """ decl = Var(Self.node_env.get_first(Self.name).cast(T.FunDecl)) return decl.args.map(lambda a: T.inner_env_assoc.new( key=a.name.symbol, value=a.arg_expr.node, metadata=No(T.env_md)))
def parent_rebindings(): e_info = Var( New(T.entity_info, md=Entity.info.md, rebindings=Entity.info.rebindings.get_parent, from_rebound=False)) return New(Block.entity, node=Self, info=e_info)
def mmz_prop(i=T.Int): # Update context version by parsing a new unit ignore(Var(Self.fetch_example_unit)) # Trigger a cache clear by calling another property # (which will call Reset_Caches). return Self.internal_mmz_prop(i)
def test_if(): # define x x = Var( # let's see... maybe 24? 24 ) # call foo return If( # no way! True, # haha If( # second False, # nope 12, # finally! Self.test_cond( # arg x ) ), # hoho 24 )
def args_assocs_getter(): """ For each argument, associate its name to the expression passed in this call. """ decl = Var(Self.node_env.get_first(Self.name).cast(T.FunDecl)) return decl.args.map(lambda i, a: T.inner_env_assoc.new( key=a.name.symbol, value=Self.args.at(i), metadata=No(T.env_md)))
def body_decl_scope(): """ Assuming this PackageBody is not in the top-level list, return the environment of its PackageDecl that it should reference. """ pkg_decl = Var(Self.lookup_decl_part) return If(pkg_decl.private_part.is_null, pkg_decl.children_env, pkg_decl.private_part.children_env)
def resolve(lhs=T.FooNode.entity, rhs=T.FooNode.entity): eq = Var( All([ NPropagate(Self.v, T.Literal.combiner, Self.lhs.v, Self.rhs.v), Bind(Self.lhs.v, lhs), Bind(Self.rhs.v, rhs), ])) return If(eq.solve, Self.v.get_value, PropertyError(T.FooNode.entity, "unreachable"))
def get_initial_env(): """ Assuming this PackageBody cannot define a named environment, return the environment in which this declaration should be registered. """ pkg_decl = Var( Self.parent.children_env.get_first( Self.name.base_name.to_symbol).cast(T.PackageDecl)) return If(pkg_decl.private_part.is_null, pkg_decl.children_env, pkg_decl.private_part.children_env)
def rebind(from_node=T.FooNode.entity, to_node=T.FooNode.entity): # Build non-null rebindings rbdng = Var(No(T.EnvRebindings).append_rebinding( from_node.children_env, to_node.children_env )) # Call rebind_env on them (this is the important part, the rest is # cosmetic). return No(T.LexicalEnv).rebind_env(rbdng).get("foo")
def test_arrays(): empty = Var(No(T.Example.array)) single = Var(ArrayLiteral([Self.cast(Example)])) complete = Var(Self.parent.cast(T.Example.list).as_array) arr = Var(empty.concat(single).concat(complete)) return arr.length # BREAK:test_arrays
def test_envs(): null = Var(No(T.LexicalEnv)) primary = Var(Self.children_env) orphan = Var(primary.env_orphan) group = Var(ArrayLiteral([primary, null]).env_group()) rebound = Var(primary.rebind_env(Self.get_rebindings(False))) arr = Var(ArrayLiteral([ null, primary, orphan, group, rebound, ])) return arr.length # BREAK:test_envs
def test_rebindings(): null = Var(No(T.EnvRebindings)) r1 = Var(Self.get_rebindings(False)) r2 = Var(Self.get_rebindings(True)) concat = Var(r1.concat_rebindings(r2)) arr = Var(ArrayLiteral([ null, r1, r2, concat, ])) return arr.length # BREAK:test_rebindings
def test_strings(): empty = Var(String("")) foo = Var(String("foo")) s_quote = Var(String("'")) d_quote = Var(String('"')) quote_mix = Var(String("\"'")) lf = Var(String("\n")) nul = Var(String("\x00")) arr = Var( ArrayLiteral([ empty, foo, s_quote, d_quote, quote_mix, lf, nul, ])) return arr.length # BREAK:test_strings
def prop(): ignore(Var(Predicate(FooNode.sophisticated_predicate, Self.var1))) return Self.as_bare_entity
def test_1(): """ Tests that block delimiter prevents a syntactic ambiguity. """ a = Var(12) return [a]
def prop(): ignore(Var(Predicate(*pred_args))) return Self.as_bare_entity
def wrapper(): _ = Var(Self.main_prop) ignore(_) return Self.as_bare_entity
def unused_block_var(): ex_var = Var(Self) # The whole point of this is to test that ex_var is properly flagged by # langkit as being unused. del ex_var return Self.items.as_bare_entity
def decl_part(): """ Return the PackageDecl node corresponding to this PackageBody. """ env = Var(Self.parent.children_env) return Self.name.resolve(env).cast(T.PackageDecl)
def prop1(): ignore(Var(Self.singleton)) return PropertyError(Bool, 'Explicit error 1')
def prop(): ignore(Var(Predicate(T.Example.predicate, Self.var1, Self.var2))) return Self.as_bare_entity
def public_prop(): struct = Var(Self.prop) return struct.a + struct.b
def parent_rebindings(): e_info = Var( New(T.entity_info, md=Entity.info.md, rebindings=Entity.info.rebindings.rebindings_parent)) return New(Block.entity, el=Self, info=e_info)