diff --git a/examples/parser_tests.nom b/examples/parser_tests.nom index 47cb893..5cf64d3 100644 --- a/examples/parser_tests.nom +++ b/examples/parser_tests.nom @@ -1,4 +1,4 @@ -run file "lib/core.nom" +require "lib/core.nom" test: say "foo" ..yields ".." |Call [say %]: diff --git a/examples/sample_code.nom b/examples/sample_code.nom index 4aa8cde..561a3c8 100644 --- a/examples/sample_code.nom +++ b/examples/sample_code.nom @@ -3,7 +3,7 @@ start with a #.. and continue until dedent -run file "lib/core.nom" +require "lib/core.nom" say "foo" diff --git a/examples/sample_game.nom b/examples/sample_game.nom index e393747..0e4210a 100644 --- a/examples/sample_game.nom +++ b/examples/sample_game.nom @@ -1,6 +1,6 @@ -run file "lib/core.nom" -run file "lib/secrets.nom" -run file "lib/plurals.nom" +require "lib/core.nom" +require "lib/secrets.nom" +require "lib/plurals.nom" # Users: diff --git a/examples/tutorial.nom b/examples/tutorial.nom index 5f0acb3..424148c 100644 --- a/examples/tutorial.nom +++ b/examples/tutorial.nom @@ -3,7 +3,7 @@ continue until dedent # Import files like so: -run file "core.nom" +require "core.nom" # Numbers: 23 diff --git a/lib/plurals.nom b/lib/plurals.nom index f25536b..cf77f05 100644 --- a/lib/plurals.nom +++ b/lib/plurals.nom @@ -1,4 +1,5 @@ -run file "lib/secrets.nom" +require "lib/core.nom" +require "lib/secrets.nom" # Plurals with secrets: diff --git a/lib/secrets.nom b/lib/secrets.nom index da6942e..8a689bd 100644 --- a/lib/secrets.nom +++ b/lib/secrets.nom @@ -1,3 +1,5 @@ +require "lib/core.nom" + macro block: with secrets %block ..=: ".." |local secrets = {} diff --git a/nomsu.lua b/nomsu.lua index 3267fc9..7ddfd97 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -816,6 +816,13 @@ do }) return self:tree_to_value(vars.lua_code, inner_vars) end) + self:def("require %filename", function(self, vars) + if not self.loaded_files[vars.filename] then + local file = io.open(vars.filename) + self.loaded_files[vars.filename] = self:run(file:read('*a')) + end + return self.loaded_files[vars.filename] + end) return self:def("run file %filename", function(self, vars) local file = io.open(vars.filename) return self:run(file:read('*a')) @@ -835,6 +842,7 @@ do return io.write(...) end self.utils = utils + self.loaded_files = { } end, __base = _base_0, __name = "NomsuCompiler" diff --git a/nomsu.moon b/nomsu.moon index 43d21f1..03309b1 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -99,6 +99,7 @@ class NomsuCompiler @initialize_core! @write = (...)=> io.write(...) @utils = utils + @loaded_files = {} writeln:(...)=> @write(...) @@ -593,6 +594,12 @@ class NomsuCompiler inner_vars = setmetatable({}, {__index:(_,key)-> "vars[#{utils.repr(key,true)}]"}) return @tree_to_value(vars.lua_code, inner_vars) + @def "require %filename", (vars)=> + if not @loaded_files[vars.filename] + file = io.open(vars.filename) + @loaded_files[vars.filename] = @run(file\read('*a')) + return @loaded_files[vars.filename] + @def "run file %filename", (vars)=> file = io.open(vars.filename) return @run(file\read('*a'))