imports: use name imports by module name
`use` only accepted a path expression, so `use math` read `math` as a global and failed. Now the operand may be a bare module name. - compiler: `use NAME` emits the name as a constant (a path expression still works, e.g. `use "std/io/io.cat"` or `use (expr)`). - stdlib: `use_resolve` maps a name to a path, trying, in order: <name>, <name>.cat, std/<name>/<name>.cat, <name>/<name>.cat, std/<name>.cat. A path (contains `.cat` or `/`) is used as-is. - stdlib: a missing module now raises (per project rule: no silent failure) -- "cannot import module 'X'" -- instead of doing nothing. - tests/import.cat: `use math`, `use io` and a path string (109 total, all green).
This commit is contained in:
1 parent
cc893ff677
commit
83629d0e1e
4 files changed
+81
-8
No files matched your search
@@ -0,0 +1,16 @@
|
||||
-- tests/import.cat -- `use name` imports by module name.
|
||||
--
|
||||
-- The operand is a bare module name (`math`) or a path string. A name
|
||||
-- resolves (in order) to: <name>, <name>.cat, std/<name>/<name>.cat,
|
||||
-- <name>/<name>.cat, std/<name>.cat. A path is used as-is.
|
||||
|
||||
use math
|
||||
print("double 21 =", double(21))
|
||||
print("square 7 =", square(7))
|
||||
|
||||
use io
|
||||
print("join:", join({"a", "b", "c"}, "-"))
|
||||
|
||||
-- a path string still works (used as-is)
|
||||
use "std/math/math.cat"
|
||||
print("abs(-5) =", abs(-5))
|
||||
@@ -174,6 +174,14 @@ check "repr-list" "list: [a, b, _repr_]" "`echo "$out" | sed -
|
||||
out=`printf 'x = 50\nx\nx + 5\n' | $BIN | tr '>' '\n' | grep -oE '[0-9]+'`
|
||||
check "repl-echo" "50" "`echo "$out" | sed -n 1p`"
|
||||
check "repl-state" "55" "`echo "$out" | sed -n 2p`"
|
||||
# ---- imports: `use name` ---------------------------------------------
|
||||
|
||||
out=`$BIN tests/import.cat`
|
||||
check "import-name" "double 21 = 42" "`echo "$out" | sed -n 1p`"
|
||||
check "import-sq" "square 7 = 49" "`echo "$out" | sed -n 2p`"
|
||||
check "import-io" "join: a-b-c" "`echo "$out" | sed -n 3p`"
|
||||
check "import-path" "abs(-5) = 5" "`echo "$out" | sed -n 4p`"
|
||||
|
||||
# ---- the `module` global ---------------------------------------------
|
||||
|
||||
out=`$BIN tests/module.cat`
|
||||
|
||||
Reference in new issue
Block a user