def is_builtin_call(): """ Return whether this is a call to a built-in property. """ return (Self.name.text == String("print")) | \ (Self.name.text == String("debug")) | \ (Self.name.text == String("list"))
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").concat(String(" world"))) ignore(b) ignore(c) return a
def quantifier_name(): """ Return the selector's quantifier name. If the name hasn't been explicitly specified, the default quantifier name is 'all'. """ return If(Self.quantifier.is_null, String("all"), Self.quantifier.text)
class PrivatePart(DeclarativePart): env_spec = EnvSpec( add_env(names=[ Self.parent.suffixed_full_name(String('__privatepart')).to_symbol ], transitive_parent=True), )
def binding_name(): """ Return the binding name associated with this selector call, if any. """ return If(Self.binding.is_null, String(""), Self.binding.text)
def new_env_names(): """ Return the names for the environment that this package creates. """ # Always register the "regular" name for this package result = Self.full_name # If there is a private part, that's all we need. Otherwise, register # this environment with the name of the private part as well, so that # package bodies can assume there is always a private part. return If( result == String(""), No(T.Symbol.array), If( Self.private_part.is_null, [result, result.concat(String('.__privatepart'))], [result], ).map(lambda s: s.to_symbol))
def min_depth_expr(): """ If the 'min_depth' arg is set and 'depth" is not set, return the expression for 'min_depth'. If 'depth' is set return its expression. If neither 'depth' or 'min_depth' is set, return a null expression. """ return If(Self.depth_expr.is_null, Self.expr_for_arg(String('min_depth')), Self.depth_expr)
def scope_name(): """ Absolute name of the scope that this name defines, assuming that ``prefix`` is the implicit prefix for that name. """ return Self.match( lambda id=Identifier: id.text, lambda dn=DottedName: dn.prefix.scope_name.concat(String('.')). concat(dn.suffix.text), )
def full_name(): """ Assuming this node can define a named environment (see ``can_have_name``), return its fully qualified namem. """ return Self.name_parent.then( lambda np: np.full_name.concat(String('.')).concat( Self.self_name.cast(T.Identifier).base_name), default_val=Self.self_name.scope_name, )
class PackageBody(FooNode): name = Field(type=T.Name) decls = Field(type=T.DeclarativePart) @langkit_property(return_type=T.Name) def self_name(): return Self.name @langkit_property(return_type=T.LexicalEnv) 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) env_spec = EnvSpec( # The initial environment for package bodies is the private part of the # correspoding package specs (or the public part if there is no private # part). set_initial_env_by_name( Self.suffixed_full_name(String('__privatepart')).to_symbol, Self.get_initial_env), add_to_env_kv('__nextpart', Self), add_env(names=[Self.suffixed_full_name(String('__body')).to_symbol]), ) @langkit_property(return_type=T.PackageDecl.entity, public=True) 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 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 root2(a=T.String, b=T.String): return String("<").concat( Self.super(String("[").concat(a).concat(String("]")), b=String("{").concat(b).concat(String("}")))).concat( String(">"))
def suffixed_full_name(suffix=T.String): return Self.full_name.then( lambda n: n.concat(String('.')).concat(suffix) )
def filtermap_elt_idx(): return Self.nb_list_entities.filtermap(lambda i, n: Self.create(i, n), lambda n: n.text == String("2"))
def filter_idx(): return Self.nb_list_entities.filter(lambda i, n: (n.text == String("2")) | (i == 0))
def filter_no_idx(): return Self.nb_list_entities.filter(lambda n: n.text == String("2"))
def newline(): return String("hello\nworld")
def extend(s=T.String): return s.concat(String("foo"))
class PackageBody(FooNode): name = Field(type=T.Name) decls = Field(type=T.DeclarativePart) @langkit_property(return_type=T.Name) def self_name(): return Self.name @langkit_property(return_type=T.LexicalEnv) 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 ) @langkit_property(return_type=T.PackageDecl) def lookup_decl_part(): """ Assuming this PackageBody is not in the top-level list, return the the corresponding package declaration. """ return ( Self.parent.children_env.get_first(Self.name.base_name.to_symbol) .cast(T.PackageDecl).node ) env_spec = EnvSpec( # The initial environment for package bodies is the private part of the # corresponding package specs (or the public part if there is no # private part). set_initial_env_by_name( If( Self.is_toplevel, Self.suffixed_full_name(String('__privatepart')).to_symbol, No(T.Symbol), ), Self.parent.children_env, ), add_to_env_by_name( '__nextpart', Self, If(Self.can_have_name, Self.suffixed_full_name(String('__privatepart')).to_symbol, No(T.Symbol)), Self.body_decl_scope, ), add_env(names=[Self.suffixed_full_name(String('__body')).to_symbol]), reference( Self.cast(FooNode).singleton, through=T.PackageBody.body_decl_scope, cond=Not(Self.is_toplevel), kind=RefKind.prioritary, ), ) @langkit_property(return_type=T.PackageDecl.entity, public=True) 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 get(): return New(T.KV, key=String("So"), value=String("What"))
def filtermap_all_idx(): return Self.nb_list_entities.filtermap( lambda i, n: Self.create(i, n), lambda i, n: (n.text == String("2")) | (i == 0))
def root2(a=T.String, b=T.String): return a.concat(String(" + ")).concat(b)
def fqn(): return Self.match(lambda p=T.Prefix: p.prefix.fqn.concat( String(".").concat(p.suffix.fqn)), lambda i=T.Id: i.text)
def test_prop(numbers=T.Int.array, c=T.String): return If(c == String("one"), numbers.at(0), numbers.at(1))
def binding_name(): return String("")
def to_public(p=T.PrivatePoint): return Point.new(label=String("from private"), x=p.x, y=p.y)
def depth_expr(): """ Return the expression associated to the 'expr' argument, if any. """ return Self.expr_for_arg(String('depth'))
def fqn(): return Self.match(lambda dn=T.DottedName: dn.prefix.fqn.concat( String(".").concat(dn.suffix.fqn)), lambda id=T.Identifier: id.text)