feat: add import keyword token and parser support with updated test syntax

Add TOKEN_IMPORT to the token enum and wire it into the keyword parser in readName(). Refactor string literal parsing by extracting stringConstant() helper that returns the constant index, allowing the new import parser to reuse it. Update all module test files to replace the old .import_() method calls with the new import "module" for Var syntax, and add new error test cases for missing string and missing for clause after import.
This commit is contained in:
Bob Nystrom
2015-02-15 00:46:00 +00:00
parent 91efd0d0cc
commit ea958deeef
16 changed files with 74 additions and 27 deletions
@@ -1,5 +1,6 @@
var Module = "module".import_("Module")
var Other = "module".import_("Other")
// TODO: Use comma-separated list.
import "module" for Module
import "module" for Other
IO.print(Module) // expect: before
@@ -10,5 +11,3 @@ IO.print(Module) // expect: before
// But it does change there.
Other.show // expect: after
// TODO: Cyclic import.
+1 -1
View File
@@ -3,7 +3,7 @@ IO.print("start a")
var A = "a value"
IO.print("a defined ", A)
var B = "b".import_("B")
import "b" for B
IO.print("a imported ", B)
IO.print("end a")
+1 -1
View File
@@ -3,7 +3,7 @@ IO.print("start b")
var B = "b value"
IO.print("b defined ", B)
var A = "a".import_("A")
import "a" for A
IO.print("b imported ", A)
IO.print("end b")
+1 -1
View File
@@ -1,4 +1,4 @@
var A = "a".import_("A")
import "a" for A
// Shared module should only run once:
// expect: start a
@@ -1,4 +1,5 @@
var Module = "module".import_("Module")
// TODO: Allow omitting "for" clause.
import "module" for Module
// expect: Bool
// expect: Class
// expect: Fiber
+1
View File
@@ -0,0 +1 @@
import "module" NoString // expect error
@@ -0,0 +1 @@
import for NoString // expect error
@@ -1,8 +1,9 @@
var Module1 = "module".import_("Module1")
var Module2 = "module".import_("Module2")
var Module3 = "module".import_("Module3")
var Module4 = "module".import_("Module4")
var Module5 = "module".import_("Module5")
// TODO: Comma-separated list.
import "module" for Module1
import "module" for Module2
import "module" for Module3
import "module" for Module4
import "module" for Module5
// Only execute module body once:
// expect: ran module
+1 -1
View File
@@ -1,5 +1,5 @@
// nontest
IO.print("a")
var Shared = "shared".import_("Shared")
import "shared" for Shared
var A = "a " + Shared
IO.print("a done")
+1 -1
View File
@@ -1,5 +1,5 @@
// nontest
IO.print("b")
var Shared = "shared".import_("Shared")
import "shared" for Shared
var B = "b " + Shared
IO.print("b done")
+2 -2
View File
@@ -1,5 +1,5 @@
var A = "a".import_("A")
var B = "b".import_("B")
import "a" for A
import "b" for B
// Shared module should only run once:
// expect: a
+1 -1
View File
@@ -1,4 +1,4 @@
var Module = "module".import_("Module")
import "module" for Module
// expect: ran module
IO.print(Module) // expect: from module
+1
View File
@@ -0,0 +1 @@
import "does_not_exist" for DoesNotExist // expect runtime error: Could not find module 'does_not_exist'.
@@ -1 +0,0 @@
var DoesNotExist = "does_not_exist".import_("DoesNotExist") // expect runtime error: Could not find module 'does_not_exist'.
@@ -1,3 +1,3 @@
// Should execute the module:
// expect: ran module
var DoesNotExist = "module".import_("DoesNotExist") // expect runtime error: Could not find a variable named 'DoesNotExist' in module 'module'.
import "module" for DoesNotExist // expect runtime error: Could not find a variable named 'DoesNotExist' in module 'module'.