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