Advertisement
SergOmarov

The A Environment

Jan 5th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. --[[
  2.     The A Environment v1.0
  3.     Author: SergOmarov
  4.     Licenze: Licenze: Creative Commons «Attribution-NonCommercial-NoDerivatives» («Атрибуция — Некоммерческое использование — Без производных произведений») 4.0 Всемирная.
  5. ]]--
  6. local component=require("component")
  7. local un =require("unicode")
  8. local fs =require("filesystem")
  9. local event=require("event")
  10. local keyboard=require("keyboard")
  11. local fs=require("filesystem")
  12. local shell=require("shell")
  13. local info_types={err={"Error",0xFF0000},ok={"OK",0x00AA00},warn={"Warning",0xFF6600},info={"Info",0x62B1F6}}
  14. local running=true
  15. local listeners={}
  16. local args, options=shell.parse(...)
  17. local appname=""
  18. local s={}
  19. local _debug=true
  20. local gpu
  21. local aa="log.log"
  22. function info(type_x,mesg,...)
  23.     local args={...}
  24.     for i=1,#args do
  25.         mesg=mesg.." , "..args[i]
  26.     end
  27.     if gpu then
  28.             if info_types[type_x] then
  29.                 if( _debug)then
  30.                 gpu.setForeground(info_types[type_x][2])
  31.                 io.write(" - "..info_types[type_x][1]..": "..mesg.."\n")
  32.                 end
  33.                 local f=fs.open(aa,"a");
  34.             f.fs.write(f.handle,info_types[type_x][1]..": "..mesg.."\n");
  35.             f.fs.close(f.handle);
  36.                 return true
  37.             end
  38.         return false
  39.     end
  40. end
  41. function s.off(event_name,handler)
  42.     listeners[event_name]=listeners[event_name] or {}
  43.     table.remove(listeners[event_name],handler)
  44. end
  45.  
  46. s.info=info
  47. s.getTime=getTime
  48. function s.on(event_name,handler)
  49.     listeners[event_name]=listeners[event_name] or {}
  50.     table.insert(listeners[event_name],handler)
  51. end
  52. if component.isAvailable("gpu") then
  53.         gpu=component.gpu
  54.     end
  55.    
  56.     if #args > 0 then
  57.         appname=args[1]
  58.     else
  59.         error("app name must be non null")
  60.     end
  61.    
  62.     if options.h  then
  63.         print("The A Enveroument v0.1")
  64.         print("Usage:")
  65.         print("  a appname [-d] [-h]")
  66.         print("Options:")
  67.         print("  -d,--nodebug=do not show information for debug")
  68.         print("  -h,--help=show help")
  69.         os.exit()
  70.     end
  71.     if options.d then
  72.         _debug=false
  73.     end
  74.    
  75.     if not fs.exists(appname) then
  76.         info("err","file not found")
  77.         os.exit()
  78.     end
  79.     if fs.isDirectory(appname) then
  80.         print(appname)
  81.         info("err","appname must be a filename.")
  82.         os.exit()
  83.     end
  84.     s.appname=appname
  85.     aa=appname.."Log.log"
  86.     local libs={"component","computer"}
  87.     for file in fs.list("/lib/") do
  88.         if fs.isDirectory(file) == false and file~="io.lua" then
  89.             libs[#libs+1]=file:sub(0,-5);
  90.         end
  91.     end
  92.             info("info","loading application "..appname)
  93.             local a=fs.open(appname)
  94.             local data=a.fs.read(a.handle,math.huge)
  95.             a.fs.close(a.handle)
  96.             local lang={{"with(.+)do(.+)end","with( %1 ,function() %2 end)"}}
  97.             --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'}}
  98.             local len = 1
  99.             for i=1,#lang do
  100.                 while len > 0 do
  101.                     data, len = data:gsub(lang[i][1], lang[i][2])
  102.                 end
  103.                 len = 1
  104.             end
  105.             for i=1,#libs do
  106.                 if(data:match(libs[i])) and not data:match("=require('"..libs[i].."')") and not data:match('=require("'..libs[i]..'")')then
  107.                     data="local "..libs[i].."=require('"..libs[i].."') "..data
  108.                    
  109.                 end
  110.                
  111.             end
  112.             data="local function with(env,functionCall) env.print=print _ENV=env functionCall() _ENV=_G end "..data
  113.             data="local a=({...})[1] "..data
  114.             print(data);
  115.             local ok,err=pcall(load(data),s)
  116.             if not ok then
  117.                 info("err","error loading application "..appname.." : "..tostring(err))
  118.                 os.exit()
  119.             else
  120.                 info("info","loadedd application "..appname)
  121.             end
  122.     while running do
  123.         local e={event.pull()}
  124.             if listeners[e[1]] ~= nil  and #listeners[e[1]]>0 then
  125.                 for i=1,#listeners[e[1]] do
  126.                 local ok,err=pcall(listeners[e[1]][i],e)
  127.                 if not ok then
  128.                     info("err","error occurred while processing the event "..e[1].." : "..err.."in function "..i.." for event "..e[1])
  129.                 end
  130.                 end
  131.             end
  132.     end
  133.     gpu.setForeground(0xffffff)
  134.     info("ok",appname.." is stopped.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement