Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace foo{
- struct Foo{ field x:Bar; }
- // is equivalent to
- invoke (c) => { /*1*/
- let Foo = c->create_struct("Foo");
- Foo->set_definition((c) => { /*2*/
- let x = c->add_field("x");
- x->set_type((c) => /*3*/ c->resolve_type("Bar"));
- });
- }
- function bar(x:i32):Foo{ return c(); }
- // is equivalent to
- invoke (c) => { /*4*/
- let bar = c->create_function("bar");
- bar->set_signature((c) => { /*5*/
- c->add_parameter("x", c->get_i32_type());
- c->set_return_type(c->resolve_type("Foo"));
- });
- bar->set_definition((c) => { /*6*/
- c->create_return_statement(c->create_call(c->resolve_expr("c"), {}));
- });
- }
- }
- ------------
- Someone wants to look inside foo in search of a "Foo" symbol.
- Compiler must construct the foo namespace, so it queues two computations: 1 and 4
- Function 1 is invoked. It creates a Foo symbol, sets a lambda for it, and returns
- Function 4 is invoked. It creates a bar symbol, sets lambdas for it, and returns
- Lookup returns the struct "Foo" symbol created in the (1)
- Then the user wants to access a field 'x' of the 'Foo' type.
- It requires to look inside the definition, so (2) is queued.
- Function 2 is invoked. It creates an 'x' field, and sets a lambda for its type.
- User resolves the 'x' field, and now wants to know its type.
- To find the type, compiler must compute (3).
- This requires a lookup for 'Bar' in the foo namespace. As the namespace already constructed,
- compiler can answer this query right away with "not found". Then it sequentially performs
- the same lookup in all parent namespaces, and all imported namespaces, constructing those
- namespaces in the process.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement