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:
James Shubin
2024-01-06 14:13:43 -05:00
parent f92f34dc54
commit 44ee578a3a
46 changed files with 1197 additions and 186 deletions

View File

@@ -85,8 +85,14 @@
return MINUS
}
/\*/ {
// This is used as the multiplication symbol, but also
// (for now) the bare import feature, eg: `import as *`.
yylex.pos(lval) // our pos
lval.str = yylex.Text()
// sanity check... these should be the same!
if x, y := lval.str, interfaces.BareSymbol; x != y {
panic(fmt.Sprintf("MULTIPLY does not match BareSymbol (%s != %s)", x, y))
}
return MULTIPLY
}
/\// {

View File

@@ -1670,12 +1670,12 @@ func TestLexParse0(t *testing.T) {
exp: exp,
})
}
{
if ast.AllowBareImports {
exp := &ast.StmtProg{
Body: []interfaces.Stmt{
&ast.StmtImport{
Name: "foo1",
Alias: "*",
Alias: interfaces.BareSymbol,
},
},
}

View File

@@ -295,6 +295,27 @@ stmt:
Args: $4.exprs,
}
}
// `include name as foo`
// TODO: should we support: `include name as *`
| INCLUDE_IDENTIFIER dotted_identifier AS_IDENTIFIER IDENTIFIER
{
posLast(yylex, yyDollar) // our pos
$$.stmt = &ast.StmtInclude{
Name: $2.str,
Alias: $4.str,
}
}
// `include name(...) as foo`
// TODO: should we support: `include name(...) as *`
| INCLUDE_IDENTIFIER dotted_identifier OPEN_PAREN call_args CLOSE_PAREN AS_IDENTIFIER IDENTIFIER
{
posLast(yylex, yyDollar) // our pos
$$.stmt = &ast.StmtInclude{
Name: $2.str,
Args: $4.exprs,
Alias: $7.str,
}
}
// `import "name"`
| IMPORT_IDENTIFIER STRING
{