lang: parser, ast, interfaces: Implement include as
This adds support for `include as <identifier>` type statements which in addition to pulling in any defined resources, it also makes the contents of the scope of the class available to the scope of the include statement, but prefixed by the identifier specified. This makes passing data between scopes much more powerful, and it also allows classes to return useful classes for subsequent use. This also improves the SetScope procedure and adds to the Ordering stage. It's unclear if the current Ordering stage can handle all code, or if there exist corner-cases which are valid code, but which would produce a wrong or imprecise topological sort. Some extraneous scoping bugs still exist, which expose certain variables that we should not depend on in future code. Co-authored-by: Samuel Gélineau <gelisam@gmail.com>
This commit is contained in:
@@ -414,6 +414,57 @@ parameters, then the same class can even be called with different signatures.
|
||||
Whether the output is useful and whether there is a unique type unification
|
||||
solution is dependent on your code.
|
||||
|
||||
Classes can be included under a new scoped prefix by using the `as` field and an
|
||||
identifier. When used in this manner, the captured scope of the class at its
|
||||
definition site are made available in the scope of the include. Variables,
|
||||
functions, and child classes are all exported.
|
||||
|
||||
Variables are available in the include scope:
|
||||
|
||||
```mcl
|
||||
import "fmt"
|
||||
|
||||
class c1 {
|
||||
test "t1" {} # gets pulled out
|
||||
$x = "hello" # gets exported
|
||||
}
|
||||
include c1 as i1
|
||||
|
||||
test "print0" {
|
||||
anotherstr => fmt.printf("%s", $i1.x), # hello
|
||||
onlyshow => ["AnotherStr",], # displays nicer
|
||||
}
|
||||
```
|
||||
|
||||
Classes are also available in the new scope:
|
||||
|
||||
```mcl
|
||||
import "fmt"
|
||||
class c1 {
|
||||
test "t1" {} # gets pulled out
|
||||
$x = "hello" # gets exported
|
||||
|
||||
class c0 {
|
||||
test "t2" {}
|
||||
$x = "goodbye"
|
||||
}
|
||||
}
|
||||
include c1 as i1
|
||||
include i1.c0 as i0
|
||||
|
||||
test "print0" {
|
||||
anotherstr => fmt.printf("%s", $i1.x), # hello
|
||||
onlyshow => ["AnotherStr",], # displays nicer
|
||||
}
|
||||
test "print1" {
|
||||
anotherstr => fmt.printf("%s", $i0.x), # goodbye
|
||||
onlyshow => ["AnotherStr",], # displays nicer
|
||||
}
|
||||
```
|
||||
|
||||
Of course classes can be parameterized too, and those variables specified during
|
||||
the `include`.
|
||||
|
||||
#### Import
|
||||
|
||||
The `import` statement imports a scope into the specified namespace. A scope can
|
||||
|
||||
Reference in New Issue
Block a user