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

@@ -4,4 +4,4 @@
$x = "wow"
$x = 99 # woops, but also a change of type :P
-- OUTPUT --
# err: errSetScope: var `x` already exists in this scope
# err: errSetScope: could not generate ordering: duplicate assignment to `var:x`, have: bind(x)

View File

@@ -3,4 +3,4 @@
$x = "hello"
$x = "world" # woops
-- OUTPUT --
# err: errSetScope: var `x` already exists in this scope
# err: errSetScope: could not generate ordering: duplicate assignment to `var:x`, have: bind(x)

View File

@@ -0,0 +1,30 @@
-- main.mcl --
class c1 {
test "t1" {}
$y = "hello"
class c0 {
test "t2" {}
$x = "goodbye"
}
}
include c1 as i1 # has $y
include i1.c0 as i0 # has $x ...and $y
test $i0.x {} # ok
test $i1.y {} # ok
panic($i0.x != "goodbye")
panic($i1.y != "hello")
# the really tricky case
# XXX: works atm, but not supported for now: could not set scope: variable i0.y not in scope
# We currently re-export anything in the parent scope as available from our
# current child scope, which makes this variable visible. Unfortunately, it does
# not have the correct dependency (edge) present in the Ordering system, so it
# is flaky depending on luck of the toposort.
#test $i0.y {}
-- OUTPUT --
Vertex: test[goodbye]
Vertex: test[hello]
Vertex: test[t1]
Vertex: test[t2]

View File

@@ -0,0 +1,39 @@
-- main.mcl --
class c1($b) {
test "t1" {}
if $b {
test "t2" {}
} else {
test "t3" {}
}
class c0 {
test "t4" {}
if $b {
test "t5" {}
} else {
test "t6" {}
}
$x = if $b {
"hello"
} else {
"goodbye"
}
}
}
include c1(true) as i1
include i1.c0 as i01
include c1(false) as i2
include i2.c0 as i02
test $i01.x {}
test $i02.x {}
-- OUTPUT --
Vertex: test[hello]
Vertex: test[goodbye]
Vertex: test[t1]
Vertex: test[t2]
Vertex: test[t3]
Vertex: test[t4]
Vertex: test[t5]
Vertex: test[t6]

View File

@@ -0,0 +1,17 @@
-- main.mcl --
class c1($b) {
if $b { # scope doesn't leak up and out of `if` statement!
class inner() {
test "t1" {}
}
} else {
class inner() {
test "t2" {}
}
}
}
include c1 as i1
include i1.inner
-- OUTPUT --
# err: errSetScope: class `c1` expected 1 args but got 0

View File

@@ -0,0 +1,11 @@
-- main.mcl --
class c1() {
$x = "got: ${s}"
}
# TODO: can this be allowed?
include c1 as i1
include c1 as i1
test $i1.x {}
-- OUTPUT --
# err: errSetScope: could not generate ordering: duplicate assignment to `scoped:i1`, have: include(c1)

View File

@@ -0,0 +1,11 @@
-- main.mcl --
class c1($s) {
$x = "got: ${s}"
}
# TODO: can this be allowed?
include c1("hey") as i1
include c1("hey") as i1
test $i1.x {}
-- OUTPUT --
# err: errSetScope: could not generate ordering: duplicate assignment to `scoped:i1`, have: include(c1)

View File

@@ -0,0 +1,10 @@
-- main.mcl --
class c1($s) {
$x = "got: ${s}"
}
include c1("hey") as i1
include c1("there") as i1
test $i1.x {}
-- OUTPUT --
# err: errSetScope: could not generate ordering: duplicate assignment to `scoped:i1`, have: include(c1)

View File

@@ -0,0 +1,14 @@
-- main.mcl --
class c1 {
$x = "outside"
test "t1" {}
func f1($x) {
"hello" + $x
}
}
include c1 as i1
test i1.f1("world") {}
-- OUTPUT --
Vertex: test[helloworld]
Vertex: test[t1]

View File

@@ -0,0 +1,14 @@
-- main.mcl --
class c1 {
$x = "world"
test "t1" {}
func f1($y) {
"hello" + $x
}
}
include c1 as i1
test i1.f1("whatever") {}
-- OUTPUT --
Vertex: test[helloworld]
Vertex: test[t1]

View File

@@ -0,0 +1,13 @@
-- main.mcl --
class c1 {
test "t1" {}
func f1() {
"hello"
}
}
include c1 as i1
test i1.f1() {}
-- OUTPUT --
Vertex: test[hello]
Vertex: test[t1]

View File

@@ -0,0 +1,27 @@
-- main.mcl --
class c1($b) {
test "t1" {}
if $b {
test "t2" {}
} else {
test "t3" {}
}
func f1() {
if $b {
"hello"
} else {
"goodbye"
}
}
}
include c1(true) as i1
include c1(false) as i2
test i1.f1() {}
test i2.f1() {}
-- OUTPUT --
Vertex: test[hello]
Vertex: test[goodbye]
Vertex: test[t1]
Vertex: test[t2]
Vertex: test[t3]

View File

@@ -0,0 +1,42 @@
-- main.mcl --
class c0($b) {
test "t1" {}
if $b {
test "t2" {}
} else {
test "t3" {}
}
func f0() {
if $b {
"hello"
} else {
"goodbye"
}
}
}
class c1($b) {
test "t4" {}
if $b {
test "t5" {}
} else {
test "t6" {}
}
include c0($b) as i0
func f1() { i0.f0() }
}
include c1(true) as i1
include c1(false) as i2
test i1.f1() {}
test i2.f1() {}
test i1.i0.f0() {} # I think these might work directly too. Do we want them to?
test i2.i0.f0() {}
-- OUTPUT --
Vertex: test[goodbye]
Vertex: test[hello]
Vertex: test[t1]
Vertex: test[t2]
Vertex: test[t3]
Vertex: test[t4]
Vertex: test[t5]
Vertex: test[t6]

View File

@@ -0,0 +1,10 @@
-- main.mcl --
import "fmt" as i1
class c1($s) {
$x = fmt.printf("got: %s", $s)
}
include c1("hey") as i1
test $i1.x {}
-- OUTPUT --
# err: errSetScope: could not generate ordering: duplicate assignment to `scoped:i1`, have: import(fmt)

View File

@@ -0,0 +1,10 @@
-- main.mcl --
import "fmt"
class c1($s) {
$x = fmt.printf("got: %s", $s)
}
include c1("hey") as fmt
test $fmt.x {}
-- OUTPUT --
# err: errSetScope: could not generate ordering: duplicate assignment to `scoped:fmt`, have: import(fmt)

View File

@@ -0,0 +1,14 @@
-- main.mcl --
class c1 {
$x = "outside"
test "t1" {}
$f1 = func($x) {
"hello" + $x
}
}
include c1 as i1
test $i1.f1("world") {}
-- OUTPUT --
Vertex: test[helloworld]
Vertex: test[t1]

View File

@@ -0,0 +1,14 @@
-- main.mcl --
class c1 {
$x = "world"
test "t1" {}
$f1 = func($y) {
"hello" + $x
}
}
include c1 as i1
test $i1.f1("whatever") {}
-- OUTPUT --
Vertex: test[helloworld]
Vertex: test[t1]

View File

@@ -0,0 +1,13 @@
-- main.mcl --
class c1 {
test "t1" {}
$f1 = func() {
"hello"
}
}
include c1 as i1
test $i1.f1() {}
-- OUTPUT --
Vertex: test[hello]
Vertex: test[t1]

View File

@@ -0,0 +1,27 @@
-- main.mcl --
class c1($b) {
test "t1" {}
if $b {
test "t2" {}
} else {
test "t3" {}
}
$f1 = func() {
if $b {
"hello"
} else {
"goodbye"
}
}
}
include c1(true) as i1
include c1(false) as i2
test $i1.f1() {}
test $i2.f1() {}
-- OUTPUT --
Vertex: test[hello]
Vertex: test[goodbye]
Vertex: test[t1]
Vertex: test[t2]
Vertex: test[t3]

View File

@@ -0,0 +1,44 @@
-- main.mcl --
class c0($b) {
test "t1" {}
if $b {
test "t2" {}
} else {
test "t3" {}
}
$f0 = func() {
if $b {
"hello"
} else {
"goodbye"
}
}
#$f0 = "hey"
}
class c1($b) {
test "t4" {}
if $b {
test "t5" {}
} else {
test "t6" {}
}
include c0($b) as i0
$x = $i0.f0
}
include c1(true) as i1
include c1(false) as i2
test $i1.x() {}
test $i1.i0.f0() {}
test $i2.x() {}
test $i1.i0.f0() {} # I think these should work directly too. Do we want them to?
test $i2.i0.f0() {}
-- OUTPUT --
Vertex: test[goodbye]
Vertex: test[hello]
Vertex: test[t1]
Vertex: test[t2]
Vertex: test[t3]
Vertex: test[t4]
Vertex: test[t5]
Vertex: test[t6]

View File

@@ -0,0 +1,11 @@
-- main.mcl --
class c1 {
test "t1" {}
$x = "hello"
}
include c1 as i1
test $i1.x {}
-- OUTPUT --
Vertex: test[hello]
Vertex: test[t1]

View File

@@ -0,0 +1,25 @@
-- main.mcl --
class c1($b) {
test "t1" {}
if $b {
test "t2" {}
} else {
test "t3" {}
}
$x = if $b {
"hello"
} else {
"goodbye"
}
}
include c1(true) as i1
include c1(false) as i2
test $i1.x {}
test $i2.x {}
-- OUTPUT --
Vertex: test[hello]
Vertex: test[goodbye]
Vertex: test[t1]
Vertex: test[t2]
Vertex: test[t3]

View File

@@ -0,0 +1,40 @@
-- main.mcl --
class c0($b) {
test "t1" {}
if $b {
test "t2" {}
} else {
test "t3" {}
}
$y = if $b {
"hello"
} else {
"goodbye"
}
}
class c1($b) {
test "t4" {}
if $b {
test "t5" {}
} else {
test "t6" {}
}
include c0($b) as i0
$x = $i0.y
}
include c1(true) as i1
include c1(false) as i2
test $i1.x {}
test $i2.x {}
test $i1.i0.y {} # I think these should work directly too. Do we want them to?
test $i2.i0.y {}
-- OUTPUT --
Vertex: test[goodbye]
Vertex: test[hello]
Vertex: test[t1]
Vertex: test[t2]
Vertex: test[t3]
Vertex: test[t4]
Vertex: test[t5]
Vertex: test[t6]

View File

@@ -0,0 +1,8 @@
-- main.mcl --
$wat = "bad1"
class c1($wat) {
test $wat {}
}
include c1("hello")
-- OUTPUT --
Vertex: test[hello]

View File

@@ -0,0 +1,22 @@
-- main.mcl --
class c1() {
$x = "i am x"
}
class c2() {
include c1 as g1
#$y = "i am y"
$z = "i am y and " + $g1.x
}
include c2 as f1
test $f1.z {} # yep
#test $f1.x {} # no
test $f1.g1.x {} # yep
-- OUTPUT --
Vertex: test[i am y and i am x]
Vertex: test[i am x]

View File

@@ -0,0 +1,20 @@
-- main.mcl --
$x = "i am x" # i am top-level
class c2() {
#$y = "i am y"
$z = "i am y and " + $x
$newx = $x + " and hello"
}
include c2 as f1
test $f1.z {}
test $f1.x {} # tricky
test $f1.newx {}
-- OUTPUT --
Vertex: test[i am x]
Vertex: test[i am x and hello]
Vertex: test[i am y and i am x]

View File

@@ -0,0 +1,20 @@
-- main.mcl --
$x = "i am x" # i am top-level
class c2() {
$z = "i am y and " + $x
# Since $x is pulled in from top-level automatically, we don't allow the
# re-definition by shadowing of the same variable.
$x = $x # not allowed
#$x = $x + "wow" # allowed?
}
include c2 as f1
test $f1.z {}
test $f1.x {} # tricky
test $f1.newx {}
-- OUTPUT --
# err: errSetScope: recursive reference while setting scope: not a dag

View File

@@ -0,0 +1,26 @@
-- main.mcl --
$x1 = "i am x1" # i am top-level
$x2 = "i am x2" # i am top-level
class c2() {
$z = "i am y and " + $x1
$x1 = "hey" # shadow
}
include c2 as f1
test $f1.z {}
test $f1.x1 {}
# the really tricky case
# XXX: works atm, but not supported for now: could not set scope: variable f1.x2 not in scope
# We currently re-export anything in the parent scope as available from our
# current child scope, which makes this variable visible. Unfortunately, it does
# not have the correct dependency (edge) present in the Ordering system, so it
# is flaky depending on luck of the toposort.
#test $f1.x2 {}
-- OUTPUT --
Vertex: test[hey]
Vertex: test[i am y and hey]

View File

@@ -0,0 +1,31 @@
-- main.mcl --
$x = "i am x" # i am top-level
class c2() {
$y = "i am y and " + $x
#$x = $x # might be allowed, i don't _really_ care, but i prefer not
# We need to be able to re-define our new $x that shadows the parent $x,
# but it should be able to incorporate the parent (top-level) $x into
# the new value that this new $x here becomes.
# XXX: not supported for now: could not set scope: not a dag
# Sam suggested the RHS $x should have a special keyword to make it
# refer to the parent scope. I suggested when $x is also on the LHS,
# then this magic keyword should be implied. And only possible in the
# case for variables on the RHS that are the same var as on the LHS.
#$x = $x + " and this is shadowed" # this is important
}
include c2 as f1
test $x {}
test $f1.y {}
# the really tricky case
# XXX: not supported for now: could not set scope: not a dag
#test $f1.x {}
-- OUTPUT --
Vertex: test[i am x]
Vertex: test[i am y and i am x]

View File

@@ -0,0 +1,20 @@
-- main.mcl --
$x = "i am x" # i am top-level
class c2() {
$z = "i am y and " + $x
#$x = $x # not allowed
$tmpx = $x # this $x is actually from below, not from the parent scope!
#$x = $x + "wow" # allowed?
$x = $tmpx + "wow" # circular with itself ($x) in this scope!
}
include c2 as f1
test $f1.z {}
test $f1.x {} # tricky
test $f1.newx {}
-- OUTPUT --
# err: errSetScope: recursive reference while setting scope: not a dag

View File

@@ -0,0 +1,15 @@
-- metadata.yaml --
#files: "files/" # these are some extra files we can use (is the default)
-- main.mcl --
import "y.mcl" as g
test $g.y {} # should work
#test $g.x {} # should fail
test $g.f.x {} # should maybe work
-- x.mcl --
$x = "this is x.mcl"
-- y.mcl --
import "x.mcl" as f
$y = $f.x + " and this is y.mcl"
-- OUTPUT --
Vertex: test[this is x.mcl]
Vertex: test[this is x.mcl and this is y.mcl]

View File

@@ -0,0 +1,14 @@
-- metadata.yaml --
#files: "files/" # these are some extra files we can use (is the default)
-- main.mcl --
import "y.mcl" as g
#test $g.y {} # should work
test $g.x {} # should fail
test $g.f.x {} # should maybe work
-- x.mcl --
$x = "this is x.mcl"
-- y.mcl --
import "x.mcl" as f
$y = $f.x + " and this is y.mcl"
-- OUTPUT --
# err: errSetScope: variable g.x not in scope

View File

@@ -0,0 +1,15 @@
-- metadata.yaml --
#files: "files/" # these are some extra files we can use (is the default)
-- main.mcl --
import "y.mcl" as g
test $g.y {} # should work
#test $g.x {} # should fail
test $g.f.x {} # should maybe work
-- x.mcl --
$x = "this is x.mcl"
-- y.mcl --
import "x.mcl" as f
$y = $f.x + " and this is y.mcl"
-- OUTPUT --
Vertex: test[this is x.mcl and this is y.mcl]
Vertex: test[this is x.mcl]

View File

@@ -0,0 +1,7 @@
-- main.mcl --
# set scope ordering test
$x = "hey"
$y = $x
test $y {}
-- OUTPUT --
Vertex: test[hey]

View File

@@ -0,0 +1,7 @@
-- main.mcl --
# set scope ordering test
$y = $x
$x = "hey"
test $y {}
-- OUTPUT --
Vertex: test[hey]

View File

@@ -0,0 +1,9 @@
-- main.mcl --
# reverse order
test $i0.f0 {}
include c0 as i0
class c0() {
$f0 = "hello"
}
-- OUTPUT --
Vertex: test[hello]

View File

@@ -0,0 +1,9 @@
-- main.mcl --
# reverse order
test $i0.f0 {}
include c0("hello") as i0
class c0($s) {
$f0 = $s
}
-- OUTPUT --
Vertex: test[hello]