feat: allow zero or multiple imported names in import statement

Make the `for` clause in import statements optional, and support comma-separated lists of imported names when present. This removes the previous restriction of exactly one imported variable per import statement.

- Refactor `import()` in compiler to handle optional `for` clause with loop over comma-separated names
- Update existing tests to use comma-separated imports and omit `for` clause where appropriate
- Add new tests: `inside_block`, `name_collision`, `no_variable`
This commit is contained in:
Bob Nystrom
2015-02-17 15:32:33 +00:00
parent d96fc0a381
commit 0d3eae5269
11 changed files with 44 additions and 30 deletions
@@ -1,6 +1,4 @@
// TODO: Use comma-separated list.
import "module" for Module
import "module" for Other
import "module" for Module, Other
IO.print(Module) // expect: before
+1 -1
View File
@@ -1,4 +1,4 @@
import "a" for A
import "a"
// Shared module should only run once:
// expect: start a
@@ -1,5 +1,4 @@
// TODO: Allow omitting "for" clause.
import "module" for Module
import "module"
// expect: Bool
// expect: Class
// expect: Fiber
@@ -1,6 +1,4 @@
// nontest
var Module = "from module"
IO.print(Bool)
IO.print(Class)
IO.print(Fiber)
@@ -0,0 +1,10 @@
var Module = "outer"
if (true) {
import "module" for Module
// expect: ran module
IO.print(Module) // expect: from module
}
IO.print(Module) // expect: outer
+3
View File
@@ -0,0 +1,3 @@
// nontest
var Module = "from module"
IO.print("ran module")
@@ -1,9 +1,4 @@
// TODO: Comma-separated list.
import "module" for Module1
import "module" for Module2
import "module" for Module3
import "module" for Module4
import "module" for Module5
import "module" for Module1, Module2, Module3, Module4, Module5
// Only execute module body once:
// expect: ran module
+2
View File
@@ -0,0 +1,2 @@
var Collides
import "module" for Collides // expect error
+2
View File
@@ -0,0 +1,2 @@
// nontest
IO.print("ran module")
+2
View File
@@ -0,0 +1,2 @@
import "module"
// expect: ran module