Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- The A Environment v1.0
- Author: SergOmarov
- Licenze: Licenze: Creative Commons «Attribution-NonCommercial-NoDerivatives» («Атрибуция — Некоммерческое использование — Без производных произведений») 4.0 Всемирная.
- ]]--
- local component=require("component")
- local un =require("unicode")
- local fs =require("filesystem")
- local event=require("event")
- local keyboard=require("keyboard")
- local fs=require("filesystem")
- local shell=require("shell")
- local info_types={err={"Error",0xFF0000},ok={"OK",0x00AA00},warn={"Warning",0xFF6600},info={"Info",0x62B1F6}}
- local running=true
- local listeners={}
- local args, options=shell.parse(...)
- local appname=""
- local s={}
- local _debug=true
- local gpu
- local aa="log.log"
- function info(type_x,mesg,...)
- local args={...}
- for i=1,#args do
- mesg=mesg.." , "..args[i]
- end
- if gpu then
- if info_types[type_x] then
- if( _debug)then
- gpu.setForeground(info_types[type_x][2])
- io.write(" - "..info_types[type_x][1]..": "..mesg.."\n")
- end
- local f=fs.open(aa,"a");
- f.fs.write(f.handle,info_types[type_x][1]..": "..mesg.."\n");
- f.fs.close(f.handle);
- return true
- end
- return false
- end
- end
- function s.off(event_name,handler)
- listeners[event_name]=listeners[event_name] or {}
- table.remove(listeners[event_name],handler)
- end
- s.info=info
- s.getTime=getTime
- function s.on(event_name,handler)
- listeners[event_name]=listeners[event_name] or {}
- table.insert(listeners[event_name],handler)
- end
- if component.isAvailable("gpu") then
- gpu=component.gpu
- end
- if #args > 0 then
- appname=args[1]
- else
- error("app name must be non null")
- end
- if options.h then
- print("The A Enveroument v0.1")
- print("Usage:")
- print(" a appname [-d] [-h]")
- print("Options:")
- print(" -d,--nodebug=do not show information for debug")
- print(" -h,--help=show help")
- os.exit()
- end
- if options.d then
- _debug=false
- end
- if not fs.exists(appname) then
- info("err","file not found")
- os.exit()
- end
- if fs.isDirectory(appname) then
- print(appname)
- info("err","appname must be a filename.")
- os.exit()
- end
- s.appname=appname
- aa=appname.."Log.log"
- local libs={"component","computer"}
- for file in fs.list("/lib/") do
- if fs.isDirectory(file) == false and file~="io.lua" then
- libs[#libs+1]=file:sub(0,-5);
- end
- end
- info("info","loading application "..appname)
- local a=fs.open(appname)
- local data=a.fs.read(a.handle,math.huge)
- a.fs.close(a.handle)
- local lang={{"with(.+)do(.+)end","with( %1 ,function() %2 end)"}}
- --local lang={{"with(.+){(.+)}","with(%1,function() %2 end)"},{"with(.+)do(.+)end","with( %1 ,function() %2 end)"},{'while(.+){(.+)}', 'while %1 do %2 end'},{'if(.+){(.+)}','if %1 then %2 end'}}
- local len = 1
- for i=1,#lang do
- while len > 0 do
- data, len = data:gsub(lang[i][1], lang[i][2])
- end
- len = 1
- end
- for i=1,#libs do
- if(data:match(libs[i])) and not data:match("=require('"..libs[i].."')") and not data:match('=require("'..libs[i]..'")')then
- data="local "..libs[i].."=require('"..libs[i].."') "..data
- end
- end
- data="local function with(env,functionCall) env.print=print _ENV=env functionCall() _ENV=_G end "..data
- data="local a=({...})[1] "..data
- print(data);
- local ok,err=pcall(load(data),s)
- if not ok then
- info("err","error loading application "..appname.." : "..tostring(err))
- os.exit()
- else
- info("info","loadedd application "..appname)
- end
- while running do
- local e={event.pull()}
- if listeners[e[1]] ~= nil and #listeners[e[1]]>0 then
- for i=1,#listeners[e[1]] do
- local ok,err=pcall(listeners[e[1]][i],e)
- if not ok then
- info("err","error occurred while processing the event "..e[1].." : "..err.."in function "..i.." for event "..e[1])
- end
- end
- end
- end
- gpu.setForeground(0xffffff)
- info("ok",appname.." is stopped.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement