Added require instead of run file.

This commit is contained in:
Bruce Hill 2017-09-19 00:35:37 -07:00
parent 3b10a60826
commit b1b36fead5
8 changed files with 25 additions and 7 deletions

View File

@ -1,4 +1,4 @@
run file "lib/core.nom"
require "lib/core.nom"
test: say "foo" ..yields ".."
|Call [say %]:

View File

@ -3,7 +3,7 @@
start with a #.. and
continue until dedent
run file "lib/core.nom"
require "lib/core.nom"
say "foo"

View File

@ -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:

View File

@ -3,7 +3,7 @@
continue until dedent
# Import files like so:
run file "core.nom"
require "core.nom"
# Numbers:
23

View File

@ -1,4 +1,5 @@
run file "lib/secrets.nom"
require "lib/core.nom"
require "lib/secrets.nom"
# Plurals
with secrets:

View File

@ -1,3 +1,5 @@
require "lib/core.nom"
macro block: with secrets %block
..=: ".."
|local secrets = {}

View File

@ -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"

View File

@ -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'))