Advertisement
tinyevil

Untitled

Jul 6th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. namespace foo{
  2.  
  3. struct Foo{ field x:Bar; }
  4. // is equivalent to
  5. invoke (c) => { /*1*/
  6. let Foo = c->create_struct("Foo");
  7. Foo->set_definition((c) => { /*2*/
  8. let x = c->add_field("x");
  9. x->set_type((c) => /*3*/ c->resolve_type("Bar"));
  10. });
  11. }
  12.  
  13.  
  14. function bar(x:i32):Foo{ return c(); }
  15. // is equivalent to
  16. invoke (c) => { /*4*/
  17. let bar = c->create_function("bar");
  18. bar->set_signature((c) => { /*5*/
  19. c->add_parameter("x", c->get_i32_type());
  20. c->set_return_type(c->resolve_type("Foo"));
  21. });
  22. bar->set_definition((c) => { /*6*/
  23. c->create_return_statement(c->create_call(c->resolve_expr("c"), {}));
  24. });
  25. }
  26.  
  27.  
  28. }
  29.  
  30. ------------
  31. Someone wants to look inside foo in search of a "Foo" symbol.
  32. Compiler must construct the foo namespace, so it queues two computations: 1 and 4
  33. Function 1 is invoked. It creates a Foo symbol, sets a lambda for it, and returns
  34. Function 4 is invoked. It creates a bar symbol, sets lambdas for it, and returns
  35. Lookup returns the struct "Foo" symbol created in the (1)
  36. Then the user wants to access a field 'x' of the 'Foo' type.
  37. It requires to look inside the definition, so (2) is queued.
  38. Function 2 is invoked. It creates an 'x' field, and sets a lambda for its type.
  39. User resolves the 'x' field, and now wants to know its type.
  40. To find the type, compiler must compute (3).
  41. This requires a lookup for 'Bar' in the foo namespace. As the namespace already constructed,
  42. compiler can answer this query right away with "not found". Then it sequentially performs
  43. the same lookup in all parent namespaces, and all imported namespaces, constructing those
  44. namespaces in the process.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement