[1650] | 1 | ------------------------------------------------------------------- |
---|
| 2 | -- Real globals |
---|
| 3 | -- _ALERT |
---|
| 4 | -- _ERRORMESSAGE |
---|
| 5 | -- _VERSION |
---|
| 6 | -- _G |
---|
| 7 | -- assert |
---|
| 8 | -- error |
---|
| 9 | -- metatable |
---|
| 10 | -- next |
---|
| 11 | -- print |
---|
| 12 | -- require |
---|
| 13 | -- tonumber |
---|
| 14 | -- tostring |
---|
| 15 | -- type |
---|
| 16 | -- unpack |
---|
| 17 | |
---|
| 18 | ------------------------------------------------------------------- |
---|
| 19 | -- collectgarbage |
---|
| 20 | -- gcinfo |
---|
| 21 | |
---|
| 22 | -- globals |
---|
| 23 | |
---|
| 24 | -- call -> protect(f, err) |
---|
| 25 | -- loadfile |
---|
| 26 | -- loadstring |
---|
| 27 | |
---|
| 28 | -- rawget |
---|
| 29 | -- rawset |
---|
| 30 | |
---|
| 31 | -- getargs = Main.getargs ?? |
---|
| 32 | |
---|
| 33 | rawtype = type |
---|
| 34 | |
---|
| 35 | function do_ (f, err) |
---|
[2509] | 36 | if not f then print(err); return end |
---|
| 37 | local a,b = pcall(f) |
---|
| 38 | if not a then print(b); return nil |
---|
| 39 | else return b or true |
---|
| 40 | end |
---|
[1650] | 41 | end |
---|
| 42 | |
---|
| 43 | function dostring(s) return do_(loadstring(s)) end |
---|
| 44 | -- function dofile(s) return do_(loadfile(s)) end |
---|
| 45 | |
---|
| 46 | ------------------------------------------------------------------- |
---|
| 47 | -- Table library |
---|
| 48 | local tab = table |
---|
| 49 | foreach = tab.foreach |
---|
| 50 | foreachi = tab.foreachi |
---|
| 51 | getn = tab.getn |
---|
| 52 | tinsert = tab.insert |
---|
| 53 | tremove = tab.remove |
---|
| 54 | sort = tab.sort |
---|
| 55 | |
---|
| 56 | ------------------------------------------------------------------- |
---|
| 57 | -- Debug library |
---|
| 58 | local dbg = debug |
---|
| 59 | getinfo = dbg.getinfo |
---|
| 60 | getlocal = dbg.getlocal |
---|
| 61 | setcallhook = function () error"`setcallhook' is deprecated" end |
---|
| 62 | setlinehook = function () error"`setlinehook' is deprecated" end |
---|
| 63 | setlocal = dbg.setlocal |
---|
| 64 | |
---|
| 65 | ------------------------------------------------------------------- |
---|
| 66 | -- math library |
---|
| 67 | local math = math |
---|
| 68 | abs = math.abs |
---|
| 69 | acos = function (x) return math.deg(math.acos(x)) end |
---|
| 70 | asin = function (x) return math.deg(math.asin(x)) end |
---|
| 71 | atan = function (x) return math.deg(math.atan(x)) end |
---|
| 72 | atan2 = function (x,y) return math.deg(math.atan2(x,y)) end |
---|
| 73 | ceil = math.ceil |
---|
| 74 | cos = function (x) return math.cos(math.rad(x)) end |
---|
| 75 | deg = math.deg |
---|
| 76 | exp = math.exp |
---|
| 77 | floor = math.floor |
---|
| 78 | frexp = math.frexp |
---|
| 79 | ldexp = math.ldexp |
---|
| 80 | log = math.log |
---|
| 81 | log10 = math.log10 |
---|
| 82 | max = math.max |
---|
| 83 | min = math.min |
---|
| 84 | mod = math.mod |
---|
| 85 | PI = math.pi |
---|
| 86 | --??? pow = math.pow |
---|
| 87 | rad = math.rad |
---|
| 88 | random = math.random |
---|
| 89 | randomseed = math.randomseed |
---|
| 90 | sin = function (x) return math.sin(math.rad(x)) end |
---|
| 91 | sqrt = math.sqrt |
---|
| 92 | tan = function (x) return math.tan(math.rad(x)) end |
---|
| 93 | |
---|
| 94 | ------------------------------------------------------------------- |
---|
| 95 | -- string library |
---|
| 96 | local str = string |
---|
| 97 | strbyte = str.byte |
---|
| 98 | strchar = str.char |
---|
| 99 | strfind = str.find |
---|
| 100 | format = str.format |
---|
| 101 | gsub = str.gsub |
---|
| 102 | strlen = str.len |
---|
| 103 | strlower = str.lower |
---|
| 104 | strrep = str.rep |
---|
| 105 | strsub = str.sub |
---|
| 106 | strupper = str.upper |
---|
| 107 | |
---|
| 108 | ------------------------------------------------------------------- |
---|
| 109 | -- os library |
---|
| 110 | clock = os.clock |
---|
| 111 | date = os.date |
---|
| 112 | difftime = os.difftime |
---|
| 113 | execute = os.execute --? |
---|
| 114 | exit = os.exit |
---|
| 115 | getenv = os.getenv |
---|
| 116 | remove = os.remove |
---|
| 117 | rename = os.rename |
---|
| 118 | setlocale = os.setlocale |
---|
| 119 | time = os.time |
---|
| 120 | tmpname = os.tmpname |
---|
| 121 | |
---|
| 122 | ------------------------------------------------------------------- |
---|
| 123 | -- compatibility only |
---|
| 124 | getglobal = function (n) return _G[n] end |
---|
| 125 | setglobal = function (n,v) _G[n] = v end |
---|
| 126 | |
---|
| 127 | ------------------------------------------------------------------- |
---|
| 128 | |
---|
| 129 | local io, tab = io, table |
---|
| 130 | |
---|
| 131 | -- IO library (files) |
---|
| 132 | _STDIN = io.stdin |
---|
| 133 | _STDERR = io.stderr |
---|
| 134 | _STDOUT = io.stdout |
---|
| 135 | _INPUT = io.stdin |
---|
| 136 | _OUTPUT = io.stdout |
---|
| 137 | seek = io.stdin.seek -- sick ;-) |
---|
| 138 | tmpfile = io.tmpfile |
---|
| 139 | closefile = io.close |
---|
| 140 | openfile = io.open |
---|
| 141 | |
---|
| 142 | function flush (f) |
---|
[2509] | 143 | if f then f:flush() |
---|
| 144 | else _OUTPUT:flush() |
---|
| 145 | end |
---|
[1650] | 146 | end |
---|
| 147 | |
---|
| 148 | function readfrom (name) |
---|
[2509] | 149 | if name == nil then |
---|
| 150 | local f, err, cod = io.close(_INPUT) |
---|
| 151 | _INPUT = io.stdin |
---|
| 152 | return f, err, cod |
---|
| 153 | else |
---|
| 154 | local f, err, cod = io.open(name, "r") |
---|
| 155 | _INPUT = f or _INPUT |
---|
| 156 | return f, err, cod |
---|
| 157 | end |
---|
[1650] | 158 | end |
---|
| 159 | |
---|
| 160 | function writeto (name) |
---|
[2509] | 161 | if name == nil then |
---|
| 162 | local f, err, cod = io.close(_OUTPUT) |
---|
| 163 | _OUTPUT = io.stdout |
---|
| 164 | return f, err, cod |
---|
| 165 | else |
---|
| 166 | local f, err, cod = io.open(name, "w") |
---|
| 167 | _OUTPUT = f or _OUTPUT |
---|
| 168 | return f, err, cod |
---|
| 169 | end |
---|
[1650] | 170 | end |
---|
| 171 | |
---|
| 172 | function appendto (name) |
---|
[2509] | 173 | local f, err, cod = io.open(name, "a") |
---|
| 174 | _OUTPUT = f or _OUTPUT |
---|
| 175 | return f, err, cod |
---|
[1650] | 176 | end |
---|
| 177 | |
---|
| 178 | function read (...) |
---|
[2509] | 179 | local f = _INPUT |
---|
| 180 | if rawtype(arg[1]) == 'userdata' then |
---|
| 181 | f = tab.remove(arg, 1) |
---|
| 182 | end |
---|
| 183 | return f:read(unpack(arg)) |
---|
[1650] | 184 | end |
---|
| 185 | |
---|
| 186 | function write (...) |
---|
[2509] | 187 | local f = _OUTPUT |
---|
| 188 | if rawtype(arg[1]) == 'userdata' then |
---|
| 189 | f = tab.remove(arg, 1) |
---|
| 190 | end |
---|
| 191 | return f:write(unpack(arg)) |
---|
[1650] | 192 | end |
---|
| 193 | |
---|