From 513c721198b2256235a95c98d161ab1bb51e6671 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 2 Feb 2018 13:59:58 -0800 Subject: Updated 'use %' to support directories, and cleaned up action_metadata a tiny bit. --- README.md | 2 +- nomsu.lua | 29 +++++++++++++++++++++-------- nomsu.moon | 25 +++++++++++++++++-------- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d2a7d14..f6e698e 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ revolving around natural language rule-making and self modification. ## Dependencies -Nomsu's only dependencies are [Lua 5.2 or later](https://www.lua.org/) (tested with version 5.2.4) (or [Luajit](http://luajit.org/) (tested with version 2.1.0)) and [LPEG](http://www.inf.puc-rio.br/~roberto/lpeg/) (`luarocks install lpeg`). Nomsu's compiler was written in [Moonscript](http://moonscript.org/), but all of the .moon files have been compiled into lua for convenience, so Moonscript is not a dependency. +Nomsu's only dependencies are [Lua 5.2 or later](https://www.lua.org/) (tested with version 5.2.4) (or [Luajit](http://luajit.org/) (tested with version 2.1.0)), [LPEG](http://www.inf.puc-rio.br/~roberto/lpeg/) (`luarocks install lpeg`), and [LuaFileSystem](https://keplerproject.github.io/luafilesystem/) (`luarocks install luafilesystem`). Nomsu's compiler was written in [Moonscript](http://moonscript.org/), but all of the .moon files have been compiled into lua for convenience, so Moonscript is not a dependency. ## Usage diff --git a/nomsu.lua b/nomsu.lua index 95cd8dc..a6d75fe 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -1,3 +1,4 @@ +local lfs = require('lfs') local re = require('re') local lpeg = require('lpeg') local utils = require('utils') @@ -232,7 +233,7 @@ do arg_orders[stub] = arg_positions end end - self.action_metadata[fn] = { + self.environment.ACTION_METADATA[fn] = { fn = fn, source = source, aliases = stubs, @@ -243,7 +244,7 @@ do end, define_compile_action = function(self, signature, source, fn, src) self:define_action(signature, source, fn) - self.action_metadata[fn].compile_time = true + self.environment.ACTION_METADATA[fn].compile_time = true end, serialize_defs = function(self, scope, after) if scope == nil then @@ -406,10 +407,23 @@ do end, require_file = function(self, filename) local loaded = self.environment.LOADED - if not loaded[filename] then - loaded[filename] = self:run_file(filename) or true + local file_attributes = lfs.attributes(filename) + if file_attributes.mode == "directory" then + for short_filename in lfs.dir(filename) do + local full_filename = filename .. '/' .. short_filename + local attr = lfs.attributes(full_filename) + if attr.mode ~= "directory" and short_filename:match(".*%.nom") then + if not loaded[full_filename] then + loaded[full_filename] = self:run_file(full_filename) or true + end + end + end + else + if not loaded[filename] then + loaded[filename] = self:run_file(filename) or true + end + return loaded[filename] end - return loaded[filename] end, run_lua = function(self, lua_code) local run_lua_fn, err = load(lua_code, nil, nil, self.environment) @@ -1420,10 +1434,9 @@ do return error("Attempt to run undefined action: " .. tostring(key), 0) end }) - self.action_metadata = setmetatable({ }, { + self.environment.ACTION_METADATA = setmetatable({ }, { __mode = "k" }) - self.environment.ACTION_METADATA = self.action_metadata self.environment.LOADED = { } return self:initialize_core() end, @@ -1590,7 +1603,7 @@ if arg and debug.getinfo(2).func ~= require then end local line = nil do - local metadata = nomsu.action_metadata[calling_fn.func] + local metadata = nomsu.environment.ACTION_METADATA[calling_fn.func] if metadata then local filename, start, stop = metadata.source:match("([^:]*):([0-9]*),([0-9]*)") if filename then diff --git a/nomsu.moon b/nomsu.moon index 639315c..20372bb 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -10,6 +10,7 @@ -- nomsu:run(your_nomsu_code) -- Or from the command line: -- lua nomsu.lua [input_file [output_file or -]] +lfs = require 'lfs' re = require 're' lpeg = require 'lpeg' utils = require 'utils' @@ -170,8 +171,7 @@ class NomsuCompiler @environment.ACTION = setmetatable({}, {__index:(key)=> error("Attempt to run undefined action: #{key}", 0) }) - @action_metadata = setmetatable({}, {__mode:"k"}) - @environment.ACTION_METADATA = @action_metadata + @environment.ACTION_METADATA = setmetatable({}, {__mode:"k"}) @environment.LOADED = {} @initialize_core! @@ -205,14 +205,14 @@ class NomsuCompiler error("Mismatch in args between lua function's #{repr fn_arg_positions} and stub's #{repr args} for #{repr stub}", 0) arg_orders[stub] = arg_positions - @action_metadata[fn] = { + @environment.ACTION_METADATA[fn] = { :fn, :source, aliases:stubs, :arg_orders, arg_positions:fn_arg_positions, def_number:@@def_number, } define_compile_action: (signature, source, fn, src)=> @define_action(signature, source, fn) - @action_metadata[fn].compile_time = true + @environment.ACTION_METADATA[fn].compile_time = true serialize_defs: (scope=nil, after=nil)=> -- TODO: repair @@ -316,9 +316,18 @@ class NomsuCompiler require_file: (filename)=> loaded = @environment.LOADED - if not loaded[filename] - loaded[filename] = @run_file(filename) or true - return loaded[filename] + file_attributes = lfs.attributes(filename) + if file_attributes.mode == "directory" + for short_filename in lfs.dir(filename) + full_filename = filename..'/'..short_filename + attr = lfs.attributes(full_filename) + if attr.mode ~= "directory" and short_filename\match(".*%.nom") + if not loaded[full_filename] + loaded[full_filename] = @run_file(full_filename) or true + else + if not loaded[filename] + loaded[filename] = @run_file(filename) or true + return loaded[filename] run_lua: (lua_code)=> run_lua_fn, err = load(lua_code, nil, nil, @environment) @@ -1018,7 +1027,7 @@ if arg and debug.getinfo(2).func != require name = calling_fn.name if name == "run_lua_fn" then continue line = nil - if metadata = nomsu.action_metadata[calling_fn.func] + if metadata = nomsu.environment.ACTION_METADATA[calling_fn.func] filename, start, stop = metadata.source\match("([^:]*):([0-9]*),([0-9]*)") if filename file = io.open(filename)\read("*a") -- cgit v1.2.3