Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local files = {
- [ "Annex/build" ] = "\
- local args = { ... }\
- \
- local path = _G._ANNEX_PATH or shell.getRunningProgram():gsub( \"/build\", \"\" )\
- local env = setmetatable( { _ANNEX_PATH = path }, { __index = _ENV } )\
- local preprocessor\
- \
- local help = [[HELP TEXT]]\
- \
- local paths = {}\
- local main = \"main\"\
- local penv = {}\
- local outputs = {}\
- \
- local mode = \"path\"\
- \
- local h = fs.open( path .. \"/preprocessor.lua\", \"r\" )\
- if h then\
- \9local content = h.readAll()\
- \9h.close()\
- \
- \9local f, err = load( content, \"preprocessor\", nil, env )\
- \9if f then\
- \9\9preprocessor = f()\
- \9else\
- \9\9error( err, 0 )\
- \9end\
- else\
- \9return error( \"Cannot find file 'preprocessor'\", 0 )\
- end\
- \
- env.preprocessor = preprocessor\
- \
- local prev\
- for i = 1, #args do\
- \9prev = mode ~= \"main\" and mode or prev\
- \9if args[i] == \"help\" or args[i] == \"-h\" or args[i] == \"-help\" then\
- \9\9print( help )\
- \9elseif args[i] == \"-m\" then\
- \9\9mode = \"main\"\
- \9elseif args[i] == \"-d\" then\
- \9\9mode = \"path\"\
- \9elseif args[i] == \"-o\" then\
- \9\9mode = \"output\"\
- \9elseif args[i] == \"-f\" then\
- \9\9mode = \"flag\"\
- \9elseif mode == \"main\" then\
- \9\9main = args[i]:gsub( \"%.\", \"/\" )\
- \9\9mode = prev\
- \9elseif mode == \"path\" then\
- \9\9paths[#paths + 1] = args[i]:gsub( \"%.\", \"/\" )\
- \9elseif mode == \"output\" then\
- \9\9outputs[#outputs + 1] = args[i]\
- \9elseif mode == \"flag\" then\
- \9\9penv[args[i]] = true\
- \9end\
- end\
- \
- outputs[1] = outputs[1] or \"?/output\"\
- \
- if #paths == 0 then\
- \9return error( \"Expected one or more build paths\", 0 )\
- end\
- \
- local p = preprocessor()\
- p.include_paths = paths\
- p.env = penv\
- p.active_include = main .. \".lua\"\
- \
- paths[#paths + 1] = \"\"\
- paths[#paths + 1] = \"Annex/lib\"\
- \
- for i = 1, #paths do\
- \9local h = fs.open( paths[i] .. \"/\" .. main .. \".lua\", \"r\" )\
- \9if h then\
- \9\9local content = h.readAll()\
- \9\9h.close()\
- \
- \9\9p:push( content )\
- \
- \9\9local r = p:build()\
- \
- \9\9for i = 1, #outputs do\
- \9\9\9local file = outputs[i]:gsub( \"%?\", paths[1] ):gsub( \"%.\", \"/\" ) .. \".lua\"\
- \9\9\9local h = fs.open( file, \"w\" )\
- \9\9\9if h then\
- \9\9\9\9h.write( r )\
- \9\9\9\9h.close()\
- \9\9\9else\
- \9\9\9\9return error( \"Failed to write to output file '\" .. outputs[i] .. \"'\", 0 )\
- \9\9\9end\
- \9\9end\
- \
- \9\9return\
- \9end\
- end\
- \
- return error( \"Failed to find main file (\" .. main .. \".lua) in paths given\", 0 )",
- [ "Annex/module/conditional.lua" ] = "\
- local open = { [\"if\"] = true, [\"ifn\"] = true, [\"ifdef\"] = true, [\"ifndef\"] = true }\
- local stop = { [\"elif\"] = true, [\"elifn\"] = true, [\"elifdef\"] = true, [\"elifndef\"] = true, [\"else\"] = true, [\"endif\"] = true }\
- \
- local function checker( self, mode, data )\
- \9if mode == 0 then\
- \9\9return self.env[data]\
- \9elseif mode == 1 then\
- \9\9return not self.env[data]\
- \9elseif mode == 2 then\
- \9\9return self.env[data] ~= nil\
- \9elseif mode == 3 then\
- \9\9return self.env[data] == nil\
- \9end\
- end\
- \
- local function getnext( self )\
- \9local l, d, i = 0, self:fetch()\
- \9while d do\
- \9\9self:write \"\"\
- \9\9if open[i] then\
- \9\9\9l = l + 1\
- \9\9elseif stop[i] and l == 0 then\
- \9\9\9return i, d\
- \9\9elseif i == \"endif\" then\
- \9\9\9l = l - 1\
- \9\9end\
- \9\9d, i = self:fetch()\
- \9end\
- end\
- \
- local function skip_to_next( self )\
- \9local i, d = getnext( self )\
- \9return i == \"elif\" and 0 or i == \"elifn\" and 1 or i == \"elifdef\" and 2 or i == \"elifndef\" and 3 or i == \"else\" and 4 or i == \"endif\" and 5, d\
- end\
- \
- local function skip_to_end( self )\
- \9local i = getnext( self )\
- \9while i do\
- \9\9if i == \"endif\" then return end\
- \9\9i = getnext( self )\
- \9end\
- \9return error( \"Expected '@endif'\", 0 )\
- end\
- \
- local function execute( self )\
- \9local d, i = self:fetch()\
- \9while d do\
- \9\9if stop[i] then\
- \9\9\9self:write \"\"\
- \9\9\9return i ~= \"endif\" and skip_to_end( self )\
- \9\9elseif i then\
- \9\9\9self:execute( i, d )\
- \9\9else\
- \9\9\9self:write( d )\
- \9\9end\
- \9\9d, i = self:fetch()\
- \9end\
- \9return error( \"Expected '@endif'\", 0 )\
- end\
- \
- local function block( self, mode, data )\
- \9self:write \"\"\
- \9if checker( self, mode, data ) then -- successful check\
- \9\9return execute( self ) -- execute and return\
- \9else\
- \9\9while true do\
- \9\9\9local mode, data = skip_to_next( self ) -- gets next valid instruction\
- \9\9\9if mode == 4 then -- else statement\
- \9\9\9\9return execute( self ) -- execute and return\
- \9\9\9elseif mode == 5 then -- got to an end with no successful branch\
- \9\9\9\9return\
- \9\9\9elseif not mode then -- no valid instruction\
- \9\9\9\9return error( \"Expected '@endif'\", 0 ) -- therefore missing an endif\
- \9\9\9else -- got an instruction, check\
- \9\9\9\9if checker( self, mode, data ) then -- successful check\
- \9\9\9\9\9return execute( self ) -- execute and return\
- \9\9\9\9end\
- \9\9\9end\
- \9\9end\
- \9end\
- end\
- \
- local module = {}\
- \
- module[\"if\"] = function( self, data )\
- \9return block( self, 0, data )\
- end\
- \
- function module:ifn( data )\
- \9return block( self, 1, data )\
- end\
- \
- function module:ifdef( data )\
- \9return block( self, 2, data )\
- end\
- \
- function module:ifndef( data )\
- \9return block( self, 3, data )\
- end\
- \
- function module:elif( data )\
- \9return error( \"Unexpected '@elif' with no initial '@if'\", 0 )\
- end\
- \
- function module:elifdef( data )\
- \9return error( \"Unexpected '@elifn' with no initial '@if'\", 0 )\
- end\
- \
- function module:elifn( data )\
- \9return error( \"Unexpected '@elifdef' with no initial '@if'\", 0 )\
- end\
- \
- function module:elifndef( data )\
- \9return error( \"Unexpected '@elifndef' with no initial '@if'\", 0 )\
- end\
- \
- module[\"else\"] = function( self, data )\
- \9return error( \"Unexpected '@else' with no initial '@if'\", 0 )\
- end\
- \
- function module:endif( data )\
- \9return error( \"Unexpected '@endif' with no initial '@if'\", 0 )\
- end\
- \
- return module",
- [ "Annex/module/include.lua" ] = "\
- local state = ...\
- \
- local module = {}\
- \
- local header = \"local __f,__err=load(\"\
- \
- state.included = {}\
- \
- local function isFile( file )\
- \9return fs.exists( file ) and not fs.isDir( file )\
- end\
- \
- local function includefile( self, file )\
- \9if not self.included[file] then\
- \9\9local h = fs.open( file, \"r\" )\
- \9\9local content = h.readAll()\
- \9\9h.close()\
- \
- \9\9self:push( content )\
- \9\9self.active_include = file\
- \9\9self:write( self:build():gsub( \"%s+$\", \"\" ) )\
- \9end\
- end\
- \
- local function requirefile( self, file, name, lib )\
- \9if not self.included[file] then\
- \9\9local h = fs.open( file, \"r\" )\
- \9\9local content = h.readAll()\
- \9\9h.close()\
- \
- \9\9self:push( content )\
- \9\9self.active_include = file\
- \9\9local str = header .. (\"%q\"):format( self:build():gsub( \"%s+$\", \"\" ) ) .. \",\" .. (\"%q\"):format( name ) .. \",nil,_ENV)if not __f then error(__err,0)end\"\
- \9\9self:write( str .. ( lib and \" local \" .. name .. \"=__f()\" or \" __f()\" ) )\
- \9end\
- end\
- \
- function module:include( data )\
- \9local file = data:gsub( \"%.\", \"/\" )\
- \9for i = 1, #self.include_paths do\
- \9\9if isFile( self.include_paths[i] .. \"/\" .. file .. \".lua\" ) then\
- \9\9\9return includefile( self, self.include_paths[i] .. \"/\" .. file .. \".lua\" )\
- \9\9elseif isFile( self.include_paths[i] .. \"/\" .. file .. \"/\" .. fs.getName( file ) .. \".lua\" ) then\
- \9\9\9return includefile( self, self.include_paths[i] .. \"/\" .. file .. \"/\" .. fs.getName( file ) .. \".lua\" )\
- \9\9elseif isFile( self.include_paths[i] .. \"/\" .. data ) then\
- \9\9\9return includefile( self, self.include_paths[i] .. \"/\" .. data )\
- \9\9end\
- \9end\
- \9return error( \"Cannot find file '\" .. data .. \"'\", 0 )\
- end\
- \
- function module:require( data )\
- \9local data, lib = data\
- \9if data:find \"^.-%sas%s[%w_]+$\" then\
- \9\9data, lib = data:match \"^(.-)%sas%s([%w_]+)$\"\
- \9end\
- \9local file = data:gsub( \"%.\", \"/\" )\
- \9for i = 1, #self.include_paths do\
- \9\9if isFile( self.include_paths[i] .. \"/\" .. file .. \".lua\" ) then\
- \9\9\9return requirefile( self, self.include_paths[i] .. \"/\" .. file .. \".lua\", lib or data, lib ~= nil )\
- \9\9elseif isFile( self.include_paths[i] .. \"/\" .. file .. \"/\" .. fs.getName( file ) .. \".lua\" ) then\
- \9\9\9return requirefile( self, self.include_paths[i] .. \"/\" .. file .. \"/\" .. fs.getName( file ) .. \".lua\", lib or data, lib ~= nil )\
- \9\9elseif isFile( self.include_paths[i] .. \"/\" .. data ) then\
- \9\9\9return requirefile( self, self.include_paths[i] .. \"/\" .. data, lib or data, lib ~= nil )\
- \9\9end\
- \9end\
- \9return error( \"Cannot find file '\" .. data .. \"'\", 0 )\
- end\
- \
- function module:once()\
- \9self:write \"\"\
- \9self.included[self.active_include] = true\
- end\
- \
- return module",
- [ "Annex/lib/colour.lua" ] = "\
- -- @print Including colour\
- \
- -- @define TRANSPARENT 0\
- -- @define WHITE 1\
- -- @define ORANGE 2\
- -- @define MAGENTA 4\
- -- @define LIGHTBLUE 8\
- -- @define YELLOW 16\
- -- @define LIME 32\
- -- @define PINK 64\
- -- @define GREY 128\
- -- @define LIGHTGREY 256\
- -- @define CYAN 512\
- -- @define PURPLE 1024\
- -- @define BLUE 2048\
- -- @define BROWN 4096\
- -- @define GREEN 8192\
- -- @define RED 16384\
- -- @define BLACK 32768\
- \
- colour = {\
- \9transparent = TRANSPARENT;\
- \9white = WHITE;\
- \9orange = ORANGE;\
- \9magenta = MAGENTA;\
- \9lightBlue = LIGHTBLUE;\
- \9yellow = YELLOW;\
- \9lime = LIME;\
- \9pink = PINK;\
- \9grey = GREY;\
- \9lightGrey = LIGHTGREY;\
- \9cyan = CYAN;\
- \9purple = PURPLE;\
- \9blue = BLUE;\
- \9brown = BROWN;\
- \9green = GREEN;\
- \9red = RED;\
- \9black = BLACK;\
- }",
- [ "Annex/module/console.lua" ] = "\
- local module = {}\
- \
- function module:error( data )\
- \9error( data:gsub( \"$([%w_]+)\", function( v )\
- \9\9return tostring( self.macros[v] or self.env[v] or \"UNDEFINED\" )\
- \9end ), 0 )\
- end\
- \
- function module:print( data )\
- \9self:write \"\"\
- \9print( data:gsub( \"$([%w_]+)\", function( v )\
- \9\9return tostring( self.macros[v] or self.env[v] or \"UNDEFINED\" )\
- \9end ), nil )\
- end\
- \
- return module",
- [ "Annex/debug" ] = "\
- local args = { ... }\
- \
- local path = _G._ANNEX_PATH or shell.getRunningProgram():gsub( \"/debug\", \"\" )\
- local env = setmetatable( { _ANNEX_PATH = path }, { __index = _ENV } )\
- local preprocessor\
- \
- local help = [[HELP TEXT]]\
- \
- local paths = {}\
- local main = \"main\"\
- local penv = {}\
- local outputs = {}\
- \
- local mode = \"path\"\
- \
- local h = fs.open( path .. \"/preprocessor.lua\", \"r\" )\
- if h then\
- \9local content = h.readAll()\
- \9h.close()\
- \
- \9local f, err = load( content, \"preprocessor\", nil, env )\
- \9if f then\
- \9\9preprocessor = f()\
- \9else\
- \9\9error( err, 0 )\
- \9end\
- else\
- \9return error( \"Cannot find file 'preprocessor'\", 0 )\
- end\
- \
- env.preprocessor = preprocessor\
- \
- local prev\
- for i = 1, #args do\
- \9prev = mode ~= \"main\" and mode or prev\
- \9if args[i] == \"help\" or args[i] == \"-h\" or args[i] == \"-help\" then\
- \9\9print( help )\
- \9elseif args[i] == \"-m\" then\
- \9\9mode = \"main\"\
- \9elseif args[i] == \"-d\" then\
- \9\9mode = \"path\"\
- \9elseif args[i] == \"-o\" then\
- \9\9mode = \"output\"\
- \9elseif args[i] == \"-f\" then\
- \9\9mode = \"flag\"\
- \9elseif mode == \"main\" then\
- \9\9main = args[i]:gsub( \"%.\", \"/\" )\
- \9\9mode = prev\
- \9elseif mode == \"path\" then\
- \9\9paths[#paths + 1] = args[i]:gsub( \"%.\", \"/\" )\
- \9elseif mode == \"output\" then\
- \9\9outputs[#outputs + 1] = args[i]\
- \9elseif mode == \"flag\" then\
- \9\9penv[args[i]] = true\
- \9end\
- end\
- \
- outputs[1] = outputs[1] or \"?/output\"\
- \
- if #paths == 0 then\
- \9return error( \"Expected one or more build paths\", 0 )\
- end\
- \
- local p = preprocessor()\
- p.include_paths = paths\
- p.env = penv\
- p.active_include = main .. \".lua\"\
- \
- paths[#paths + 1] = \"\"\
- paths[#paths + 1] = \"Annex/lib\"\
- \
- for i = 1, #paths do\
- \9local h = fs.open( paths[i] .. \"/\" .. main .. \".lua\", \"r\" )\
- \9if h then\
- \9\9local content = h.readAll()\
- \9\9h.close()\
- \
- \9\9p:push( content )\
- \
- \9\9local r = p:build()\
- \
- \9\9local f, err = load( r, \"output\", nil, _ENV )\
- \9\9if not f then return error( err, 0 ) end\
- \9\9return f()\
- \
- \9end\
- end\
- \
- return error( \"Failed to find main file (\" .. main .. \".lua) in paths given\", 0 )",
- [ "Annex/lib/class.lua" ] = "\
- -- @print Including Annex.lib.class\
- \
- class = {}\
- local classobj = setmetatable( {}, { __index = class } )\
- local names = {}\
- local last_created\
- \
- local supportedMetaMethods = {\
- \9__add = true;\
- \9__sub = true;\
- \9__mul = true;\
- \9__div = true;\
- \9__mod = true;\
- \9__pow = true;\
- \9__unm = true;\
- \9__len = true;\
- \9__eq = true;\
- \9__lt = true;\
- \9__lte = true;\
- \9__tostring = true;\
- \9__concat = true;\
- }\
- \
- local function _tostring( self )\
- \9return \"[Class] \" .. self:type()\
- end\
- local function _concat( a, b )\
- \9return tostring( a ) .. tostring( b )\
- end\
- \
- local function newSuper( object, super )\
- \
- \9local superProxy = {}\
- \
- \9if super.super then\
- \9\9superProxy.super = newSuper( object, super.super )\
- \9end\
- \
- \9setmetatable( superProxy, { __index = function( t, k )\
- \
- \9\9if type( super[k] ) == \"function\" then\
- \9\9\9return function( self, ... )\
- \
- \9\9\9\9if self == superProxy then\
- \9\9\9\9\9self = object\
- \9\9\9\9end\
- \9\9\9\9object.super = superProxy.super\
- \9\9\9\9local v = { super[k]( self, ... ) }\
- \9\9\9\9object.super = superProxy\
- \9\9\9\9return unpack( v )\
- \
- \9\9\9end\
- \9\9else\
- \9\9\9return super[k]\
- \9\9end\
- \
- \9end, __newindex = super, __tostring = function( self )\
- \9\9return \"[Super] \" .. tostring( super ) .. \" of \" .. tostring( object )\
- \9end } )\
- \
- \9return superProxy\
- \
- end\
- \
- function classobj:new( ... )\
- \
- \9local mt = { __index = self, __INSTANCE = true }\
- \9local instance = setmetatable( { class = self, meta = mt }, mt )\
- \
- \9if self.super then\
- \9\9instance.super = newSuper( instance, self.super )\
- \9end\
- \
- \9for k, v in pairs( self.meta ) do\
- \9\9if supportedMetaMethods[k] then\
- \9\9\9mt[k] = v\
- \9\9end\
- \9end\
- \
- \9if mt.__tostring == _tostring then\
- \9\9function mt:__tostring()\
- \9\9\9return self:tostring()\
- \9\9end\
- \9end\
- \
- \9function instance:type()\
- \9\9return self.class:type()\
- \9end\
- \
- \9function instance:typeOf( class )\
- \9\9return self.class:typeOf( class )\
- \9end\
- \
- \9if not self.tostring then\
- \9\9function instance:tostring()\
- \9\9\9return \"[Instance] \" .. self:type()\
- \9\9end\
- \9end\
- \
- \9local ob = self\
- \9while ob do\
- \9\9if ob[ob.meta.__type] then\
- \9\9\9ob[ob.meta.__type]( instance, ... )\
- \9\9\9break\
- \9\9end\
- \9\9ob = ob.super\
- \9end\
- \
- \9return instance\
- \
- end\
- \
- function classobj:extends( super )\
- \
- \9self.super = super\
- \9self.meta.__index = super\
- \
- end\
- \
- function classobj:type()\
- \
- \9return tostring( self.meta.__type )\
- \
- end\
- \
- function classobj:typeOf( super )\
- \
- \9return super == self or ( self.super and self.super:typeOf( super ) ) or false\
- \
- end\
- \
- function classobj:implement( t )\
- \
- \9for k, v in pairs( t ) do\
- \9\9self[k] = v\
- \9end\
- \9return self\
- \
- end\
- \
- function class:new( name )\
- \
- \9if type( name or self ) ~= \"string\" then\
- \9\9return error( \"expected string class name, got \" .. type( name or self ) )\
- \9end\
- \
- \9local mt = { __index = classobj, __CLASS = true, __tostring = _tostring, __concat = _concat, __call = classobj.new, __type = name or self }\
- \9local obj = setmetatable( { meta = mt }, mt )\
- \
- \9names[name] = obj\
- \9last_created = obj\
- \
- \9_ENV[name] = obj\
- \
- \9return function( t )\
- \9\9if not last_created then\
- \9\9\9return error \"no class to define\"\
- \9\9end\
- \
- \9\9for k, v in pairs( t ) do\
- \9\9\9last_created[k] = v\
- \9\9end\
- \9\9last_created = nil\
- \9end\
- \
- end\
- \
- function class.type( object )\
- \
- \9local _type = type( object )\
- \
- \9if _type == \"table\" then\
- \9\9pcall( function()\
- \9\9\9local mt = getmetatable( object )\
- \9\9\9_type = ( ( mt.__CLASS or mt.__INSTANCE ) and object:type() ) or _type\
- \9\9end )\
- \9end\
- \
- \9return _type\
- \
- end\
- \
- function class.typeOf( object, class )\
- \
- \9if type( object ) == \"table\" then\
- \9\9local ok, v = pcall( function() return getmetatable( object ).__CLASS or getmetatable( object ).__INSTANCE or error() end )\
- \9\9return ok and v\
- \9end\
- \
- \9return false\
- \
- end\
- \
- function class.isClass( object )\
- \
- \9return pcall( function() if not getmetatable( object ).__CLASS then error() end end ), nil\
- \
- end\
- \
- function class.isInstance( object )\
- \
- \9return pcall( function() if not getmetatable( object ).__INSTANCE then error() end end ), nil\
- \
- end\
- \
- setmetatable( class, {\
- \9__call = class.new;\
- } )\
- \
- function extends( name )\
- \
- \9if not last_created then\
- \9\9return error \"no class to extend\"\
- \9end\
- \
- \9if not names[name] then\
- \9\9return error( \"no such class '\" .. tostring( name ) .. \"'\" )\
- \9end\
- \
- \9last_created:extends( names[name] )\
- \9\
- \9return function( t )\
- \9\9if not last_created then\
- \9\9\9return error \"no class to define\"\
- \9\9end\
- \
- \9\9for k, v in pairs( t ) do\
- \9\9\9last_created[k] = v\
- \9\9end\
- \9\9last_created = nil\
- \9end\
- \
- end\
- \
- function implements( name )\
- \9\
- \9if not last_created then\
- \9\9return error \"no class to modify\"\
- \9end\
- \
- \9if type( name ) == \"string\" then\
- \9\9if not names[name] then\
- \9\9\9return error( \"no such class '\" .. tostring( name ) .. \"'\" )\
- \9\9end\
- \9\9last_created:implements( names[name] )\
- \9elseif type( name ) == \"table\" then\
- \9\9last_created:implements( name )\
- \9end\
- \9\
- \9return function( t )\
- \9\9if not last_created then\
- \9\9\9return error \"no class to define\"\
- \9\9end\
- \
- \9\9for k, v in pairs( t ) do\
- \9\9\9last_created[k] = v\
- \9\9end\
- \9\9last_created = nil\
- \9end\
- \
- end",
- [ "Annex/preprocessor.lua" ] = "\
- local preprocessor = {}\
- \
- local function loadm( self, path, name )\
- \9if self.module_loaded[path] then return end\
- \9self.module_loaded[path] = true\
- \
- \9local h = fs.open( path, \"r\")\
- \9if h then\
- \9\9local content = h.readAll()\
- \9\9h.close()\
- \
- \9\9local f, err = load( content, name, nil, _ENV )\
- \9\9if f then\
- \9\9\9f, err = pcall( f, self )\
- \9\9end\
- \9\9if not f then\
- \9\9\9return error( err, 0 )\
- \9\9end\
- \9\9if type( err ) == \"table\" then\
- \9\9\9for k, v in pairs( err ) do\
- \9\9\9\9if type( v ) ~= \"function\" then\
- \9\9\9\9\9return error( \"Module tried to add non-function instruction\", 0 )\
- \9\9\9\9elseif self.instruction_list[k] then\
- \9\9\9\9\9return error( \"Conflicting instruction name '\" .. k .. \"'\", 0 )\
- \9\9\9\9end\
- \9\9\9\9self.instruction_list[k] = v\
- \9\9\9end\
- \9\9end\
- \9end\
- end\
- \
- local function load( self, data )\
- \9self:write \"\"\
- \9if fs.exists( data:gsub( \"%.\", \"/\" ) .. \".lua\" ) then\
- \9\9loadm( self, data:gsub( \"%.\", \"/\" ) .. \".lua\", data )\
- \9elseif fs.exists( _ANNEX_PATH .. \"/module/\" .. data:gsub( \"%.\", \"/\" ) .. \".lua\" ) then\
- \9\9loadm( self, _ANNEX_PATH .. \"/module/\" .. data:gsub( \"%.\", \"/\" ) .. \".lua\", data )\
- \9else\
- \9\9return error( \"No such module '\" .. data .. \"'\", 0 )\
- \9end\
- end\
- \
- function preprocessor:fetch()\
- \9local s = self.stack[#self.stack]\
- \9if not s then return end\
- \9s.line = s.line + 1\
- \9if s.instructions[s.iptr] and s.instructions[s.iptr].line == s.line then\
- \9\9s.iptr = s.iptr + 1\
- \9\9return s.instructions[s.iptr - 1].data, s.instructions[s.iptr - 1].instruction\
- \9end\
- \9return s.lines[s.line], nil\
- end\
- \
- function preprocessor:write( data )\
- \9local s = self.stack[#self.stack]\
- \9if not s then return end\
- \9local c, _c = 1\
- \9while c ~= 0 do\
- \9\9c = 0\
- \9\9for k, v in pairs( self.macros ) do\
- \9\9\9data, _c = ( \" \" .. data .. \" \" ):gsub( \"([^%w_])\" .. k .. \"([^%w_])\", \"%1\" .. v .. \"%2\" )\
- \9\9\9data = data:sub( 2, -2 )\
- \9\9\9c = c + _c\
- \9\9end\
- \9end\
- \9s.output[#s.output + 1] = data\
- end\
- \
- function preprocessor:execute( i, d )\
- \9if self.instruction_list[i] then\
- \9\9self.instruction_list[i]( self, d )\
- \9else\
- \9\9return error( \"Unknown instruction '\" .. i .. \"'\", 0 )\
- \9end\
- end\
- \
- function preprocessor:push( str )\
- \9local lines, instructions, line = {}, {}\
- \
- \9while str do\
- \9\9line = str:match \"^(.-)\\n\" or str\
- \9\9str = str:match \"^.-\\n(.+)\"\
- \9\9if line and line:find \"^%s*%-?%-?%s*@%s*%w\" then\
- \9\9\9instructions[#instructions + 1] = { line = #lines + 1, instruction = line:match \"^%s*%-?%-?%s*@%s*(%w+)\":lower(), data = line:gsub( \"^%s*%-?%-?%s*@%s*%w+%s*\", \"\" ) }\
- \9\9\9line = \"\"\
- \9\9end\
- \
- \9\9lines[#lines + 1] = line\
- \9end\
- \
- \9self.stack[#self.stack + 1] = {\
- \9\9lines = lines;\
- \9\9instructions = instructions;\
- \9\9line = 0;\
- \9\9iptr = 1;\
- \9\9output = {};\
- \9}\
- end\
- \
- function preprocessor:build()\
- \9local d, i = self:fetch()\
- \9while d do\
- \9\9if i then\
- \9\9\9self:execute( i, d )\
- \9\9else\
- \9\9\9self:write( d )\
- \9\9end\
- \9\9d, i = self:fetch()\
- \9end\
- \9return table.concat( table.remove( self.stack, #self.stack ).output, \"\\n\" )\
- end\
- \
- return function()\
- \
- \9local s = {}\
- \
- \9s.env = {}\
- \9s.include_paths = { \"\" }\
- \9s.instruction_list = { load = load }\
- \
- \9s.module_loaded = {}\
- \9s.macros = {}\
- \
- \9s.stack = {}\
- \
- \9loadm( s, _ANNEX_PATH .. \"/module/conditional.lua\", \"conditional\" )\
- \9loadm( s, _ANNEX_PATH .. \"/module/console.lua\", \"error\" )\
- \9loadm( s, _ANNEX_PATH .. \"/module/define.lua\", \"define\" )\
- \9loadm( s, _ANNEX_PATH .. \"/module/include.lua\", \"include\" )\
- \
- \9return setmetatable( s, { __index = preprocessor } )\
- \
- end",
- [ "Annex/module/define.lua" ] = "\
- local module = {}\
- \
- function module:define( data )\
- \9self:write \"\"\
- \9local w = data:match \"^[%w_]+\"\
- \9if not w then\
- \9\9return error( \"Expected word to define in '@define', got \" .. (\"%q\"):format( data ) )\
- \9end\
- \9local r = data:match \"^[%w_]+%s*(.-)$\"\
- \9if #r > 0 then\
- \9\9self.macros[w] = r\
- \9\9self.env[w] = r ~= \"false\"\
- \9else\
- \9\9self.macros[w] = \"true\"\
- \9\9self.env[w] = true\
- \9end\
- end\
- \
- function module:undef( data )\
- \9self:write \"\"\
- \9self.env[data] = nil\
- \9self.macros[data] = nil\
- end\
- \
- function module:defineifndef( data )\
- \9self:write \"\"\
- \9local w = data:match \"^[%w_]+\"\
- \9if not w then\
- \9\9return error( \"Expected word to define in '@define', got \" .. (\"%q\"):format( data ) )\
- \9end\
- \9if self.env[w] == nil then\
- \9\9local r = data:match \"^[%w_]+%s*(.-)$\"\
- \9\9if #r > 0 then\
- \9\9\9self.macros[w] = r\
- \9\9\9self.env[w] = r ~= \"false\"\
- \9\9else\
- \9\9\9self.macros[w] = \"true\"\
- \9\9\9self.env[w] = true\
- \9\9end\
- \9end\
- end\
- \
- return module",
- }fs.makeDir "Annex"
- for k, v in pairs( files ) do
- print( "Writing to file " .. k )
- local h = fs.open( k, "w" )
- if h then
- h.write( v )
- h.close()
- else
- return error( "Failed to open file", 0 )
- end
- end
- print "Would you like to patch the startup file to aid development? (enter to confirm)"
- if select( 2, os.pullEvent "key" ) == keys.enter then
- local h = fs.open( "startup", "r" )
- local content = h and h.readAll() or ""
- if h then h.close() end
- content = "shell.setPath( \"Annex:\" .. shell.path() )\n" .. content
- local h = fs.open( "startup", "w" )
- if h then
- h.write( content )
- h.close()
- else
- print "Failed to open startup, is it a folder?"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement