(281 lines)
1 -- This file defines the environment in which Nomsu code runs, including some2 -- basic bootstrapping functionality.3 {:NomsuCode, :LuaCode, :Source} = require "code_obj"4 {:List, :Dict} = require 'containers'5 Text = require 'text'6 SyntaxTree = require "syntax_tree"7 Files = require "files"8 Errhand = require "error_handling"9 C = require "colors"10 make_parser = require("parser")11 pretty_error = require("pretty_errors")13 make_tree = (tree, userdata)->14 tree.source = Source(userdata.filename, tree.start, tree.stop)15 tree.start, tree.stop = nil, nil16 tree = SyntaxTree(tree)17 return tree19 Parsers = {}20 for version=1,NOMSU_VERSION[1]21 local peg_file22 if package.nomsupath23 pegpath = package.nomsupath\gsub("lib/%?%.nom", "?.peg")\gsub("lib/%?%.lua", "?.peg")24 if path = package.searchpath("nomsu.#{version}", pegpath, "/")25 peg_file = io.open(path)26 else27 peg_file = io.open("nomsu.#{version}.peg")28 assert peg_file, "No PEG file found for Nomsu version #{version}"29 peg_contents = peg_file\read('*a')30 peg_file\close!31 Parsers[version] = make_parser(peg_contents, make_tree)33 {:tree_to_nomsu, :tree_to_inline_nomsu} = require "nomsu_decompiler"34 {:compile, :fail_at} = require('nomsu_compiler')35 _module_imports = {}36 _importer_mt = {__index: (k)=> _module_imports[@][k]}37 Importer = (t, imports)->38 _module_imports[t] = imports or {}39 t._IMPORTS = _module_imports[t]40 return setmetatable(t, _importer_mt)42 _1_as_text = (x)->43 if x == true then return "yes"44 if x == false then return "no"45 return tostring(x)47 nomsu_pairs = =>48 mt = getmetatable(@)49 if mt and mt.__next50 return mt.__next, @, nil51 else52 return next, @, nil53 inext = if _VERSION == "Lua 5.2" or _VERSION == "Lua 5.1"54 inext = (i)=>55 value = @[i+1]56 return i+1, value if value != nil57 else ipairs{}58 nomsu_ipairs = =>59 mt = getmetatable(@)60 if mt and mt.__inext61 return mt.__inext, @, 062 else63 return inext, @, 065 local nomsu_environment66 nomsu_environment = Importer{67 -- Lua stuff:68 :next, :inext, unpack: unpack or table.unpack, :setmetatable, :rawequal, :getmetatable, :pcall,69 yield:coroutine.yield, resume:coroutine.resume, coroutine_status_of:coroutine.status,70 coroutine_wrap:coroutine.wrap, coroutine_from: coroutine.create,71 :error, :package, :os, :require, :tonumber, :tostring, :string, :xpcall,72 :print, :loadfile, :rawset, :_VERSION, :collectgarbage, :rawget, :rawlen,73 :table, :assert, :dofile, :loadstring, lua_type_of:type, :select, :math, :io, :load,74 pairs:nomsu_pairs, :ipairs, _ipairs:nomsu_ipairs, :jit,75 :_VERSION, LUA_VERSION: (jit and jit.version or _VERSION),76 LUA_API: _VERSION, Bit: (jit or _VERSION == "Lua 5.2") and require('bitops') or nil77 -- Nomsu types:78 a_List:List, a_Dict:Dict, Text:Text,79 -- Utilities and misc.80 lpeg:lpeg, re:re, Files:Files,81 :SyntaxTree, TESTS: Dict({}), globals: Dict({}),82 :LuaCode, :NomsuCode, :Source83 LuaCode_from: ((src, ...)-> LuaCode\from(src, ...)),84 NomsuCode_from: ((src, ...)-> NomsuCode\from(src, ...)),85 enhance_error: Errhand.enhance_error86 SOURCE_MAP: {},87 getfenv:getfenv,89 -- Nomsu functions:90 _1_as_nomsu:tree_to_nomsu, _1_as_inline_nomsu:tree_to_inline_nomsu,91 compile: compile, at_1_fail:fail_at, :_1_as_text, :_1_as_an_iterable,92 exit:os.exit, quit:os.exit,94 _1_parsed: (nomsu_code, syntax_version)->95 if type(nomsu_code) == 'string'96 filename = Files.spoof(nomsu_code)97 nomsu_code = NomsuCode\from(Source(filename, 1, #nomsu_code), nomsu_code)98 source = nomsu_code.source99 nomsu_code = tostring(nomsu_code)100 version = nomsu_code\match("^#![^\n]*nomsu[ ]+-V[ ]*([0-9.]+)")101 if syntax_version102 syntax_version = tonumber(syntax_version\match("^[0-9]+"))103 syntax_version or= version and tonumber(version\match("^[0-9]+")) or NOMSU_VERSION[1]104 parse = Parsers[syntax_version]105 assert parse, "No parser found for Nomsu syntax version #{syntax_version}"106 tree = parse(nomsu_code, source.filename)107 if tree.shebang108 shebang_version = tree.shebang\match("nomsu %-V[ ]*([%d.]+)")109 if shebang_version and shebang_version != ""110 tree.version or= List[tonumber(v) for v in shebang_version\gmatch("%d+")]111 --if tree.version and tree.version < NOMSU_VERSION\up_to(#tree.version) and not package.nomsuloaded["compatibility"]112 -- export loading_compat113 -- unless loading_compat114 -- loading_compat = true115 -- nomsu_environment\export "compatibility"116 -- loading_compat = false117 return tree119 Module: (package_name)=>120 -- This *must* be the first local121 local path122 if package_name\match("%.nom$") or package_name\match("%.lua")123 path = package_name124 else125 path, err = package.searchpath(package_name, package.nomsupath, "/")126 if not path then error(err)127 path = path\gsub("^%./", "")129 if ret = package.nomsuloaded[package_name] or package.nomsuloaded[path]130 return ret132 -- Traverse up the callstack to look for import loops133 -- This is more reliable than keeping a list of running files, since134 -- that gets messed up when errors occur.135 currently_running = {}136 for i=2,999137 info = debug.getinfo(i, 'f')138 break unless info.func139 if info.func == @Module140 n, upper_path = debug.getlocal(i, 3) -- 3 means "path"141 table.insert(currently_running, upper_path)142 assert(n == "path")143 if upper_path == path144 --circle = "\n "..table.concat(currently_running, "\n..imports ")145 circle = table.concat(currently_running, "', which imports '")146 err_i = 2147 info = debug.getinfo(err_i)148 while info and (info.func == @Module or info.func == @use or info.func == @export)149 err_i += 1150 info = debug.getinfo(err_i)151 fail_at (info or debug.getinfo(2)), "Circular import: File '#{path}' imports '"..circle.."'"152 mod = @new_environment!153 mod.MODULE_NAME = package_name154 code = Files.read(path)155 if path\match("%.lua$")156 code = LuaCode\from(Source(path, 1, #code), code)157 else158 code = NomsuCode\from(Source(path, 1, #code), code)159 ret = mod\run(code)160 if ret != nil161 mod = ret162 package.nomsuloaded[package_name] = mod163 package.nomsuloaded[path] = mod164 return mod166 use: (package_name)=>167 mod = @Module(package_name)168 imports = assert _module_imports[@]169 for k,v in pairs(mod)170 imports[k] = v171 cr_imports = assert _module_imports[@COMPILE_RULES]172 if mod.COMPILE_RULES173 for k,v in pairs(mod.COMPILE_RULES)174 cr_imports[k] = v175 return mod177 export: (package_name)=>178 mod = @Module(package_name)179 imports = assert _module_imports[@]180 for k,v in pairs(_module_imports[mod])181 if rawget(imports, k) == nil182 imports[k] = v183 for k,v in pairs(mod)184 if rawget(@, k) == nil185 @[k] = v186 cr_imports = assert _module_imports[@COMPILE_RULES]187 if mod.COMPILE_RULES188 for k,v in pairs(_module_imports[mod.COMPILE_RULES])189 if rawget(cr_imports, k) == nil190 cr_imports[k] = v191 for k,v in pairs(mod.COMPILE_RULES)192 if rawget(@COMPILE_RULES, k) == nil193 @COMPILE_RULES[k] = v194 return mod196 run: (to_run)=>197 if not to_run198 error("Need both something to run and an environment")199 if type(to_run) == 'string'200 filename = Files.spoof(to_run)201 to_run = NomsuCode\from(Source(filename, 1, #to_run), to_run)202 return @run(to_run)203 elseif NomsuCode\is_instance(to_run)204 tree = @._1_parsed(to_run)205 return nil if tree == nil206 return @run(tree)207 elseif SyntaxTree\is_instance(to_run)208 filename = to_run.source.filename\gsub("\n.*", "...")209 version = to_run.version210 if to_run.type != "FileChunks"211 to_run = {to_run}212 -- Each chunk's compilation is affected by the code in the previous chunks213 -- (typically), so each chunk needs to compile and run before the next one214 -- compiles.215 ret = nil216 for chunk_no, chunk in ipairs to_run217 chunk.version = version218 lua = @compile(chunk)219 lua\declare_locals!220 lua\prepend "-- File: #{filename} chunk ##{chunk_no}\n"221 ret = @run(lua)222 return ret223 elseif LuaCode\is_instance(to_run)224 source = to_run.source225 lua_string = to_run\text!226 -- For some reason, Lua doesn't strip shebangs from Lua files227 lua_string = lua_string\gsub("^#![^\n]*\n","")228 -- If you replace tostring(source) with "nil", source mapping won't happen229 run_lua_fn, err = load(lua_string, tostring(source), "t", @)230 if not run_lua_fn231 lines =[("%3d|%s")\format(i,line) for i, line in ipairs lua_string\lines!]232 line_numbered_lua = table.concat(lines, "\n")233 error("Failed to compile generated code:\n#{C("bright blue", line_numbered_lua)}\n\n#{err}", 0)234 source_key = tostring(source)235 unless @SOURCE_MAP[source_key] or @OPTIMIZATION >= 2236 map = {}237 file = Files.read(source.filename)238 if not file and NOMSU_PREFIX239 file = Files.read("#{NOMSU_PREFIX}/share/nomsu/#{table.concat NOMSU_VERSION, "."}/#{source.filename}")240 if not file241 error "Failed to find file: #{source.filename}"242 nomsu_str = file\sub(source.start, source.stop)243 lua_line = 1244 nomsu_line = Files.get_line_number(file, source.start)245 map_sources = (s)->246 if type(s) == 'string'247 for nl in s\gmatch("\n")248 map[lua_line] or= nomsu_line249 lua_line += 1250 else251 if s.source and s.source.filename == source.filename252 nomsu_line = Files.get_line_number(file, s.source.start)253 for b in *s.bits do map_sources(b)254 map_sources(to_run)255 map[lua_line] or= nomsu_line256 map[0] = 0257 -- Mapping from lua line number to nomsu line numbers258 @SOURCE_MAP[source_key] = map259 return run_lua_fn!260 else261 error("Attempt to run unknown thing: ".._1_as_lua(to_run))263 new_environment: ->264 env = Importer({}, {k,v for k,v in pairs(nomsu_environment)})265 env._ENV = env266 env._G = env267 env.TESTS = Dict{}268 env.COMPILE_RULES = Importer({}, {k,v for k,v in pairs(nomsu_environment.COMPILE_RULES)})269 return env270 }272 nomsu_environment._ENV = nomsu_environment273 nomsu_environment._G = nomsu_environment274 nomsu_environment.COMPILE_RULES = Importer(require('bootstrap'), nil)275 nomsu_environment.MODULE_NAME = "nomsu"277 -- Hacky use of globals:278 export SOURCE_MAP279 SOURCE_MAP = nomsu_environment.SOURCE_MAP281 return nomsu_environment