- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv.lua
More file actions
Latest commit
171 lines (153 loc) · 5.03 KB
/
Copy pathenv.lua
File metadata and controls
171 lines (153 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
--[[
I wanted to make my grammar-parser-generaor first, but meh
--]]
returnfunction(env)
localshowcode=require'template.showcode'
localLuaFixedParser=require'langfix.parser'
-- set globals here
env=envor_G
-- put ffi in env namespace for idiv // operator
localffi-- store it here for the langfix env table
xpcall(function()
ffi=require'ffi'
env.ffi=ffi
end, function()
end)
-- put 'bit' in the env if it's not there ... for vanilla-lua 5.2 and 5.3(compat?)
localnative_bitops=load'x=y|z'
ifnotnative_bitopsthen
env.bit=bit
ifnotenv.bitthen
-- TODO in this case, bit32 doesn't have all operations that the luajit bit table does ...
env.bit=bit32
ifnotenv.bit32then
env.bit=require'bit' -- will this be there? or should I try ext.op ?
end
end
end
locallangfix= {} -- for builtin helpers. maybe I'll put bitwise metatable invocation here. maybe I'll put other helper functions here.
env.langfix=langfix
langfix.ternary=function(t, cbt, cbf)
iftthen
returncbt()
else
returncbf()
end
end
-- fwd varargs in case cb() uses them
langfix.nilcoalescing=function(t, cb, ...)
ift~=nilthen
returnt
else
returncb(...)
end
end
langfix.optindex=function(t, k)
ift==nilthen
returnnil
end
returnt[k]
end
langfix.optcall=function(v, ...)
ifv==nilthen
returnnil
end
returnv(...)
end
langfix.optcallself=function(t, k, ...)
returnlangfix.optcall(langfix.optindex(t, k), t, ...)
end
-- t!.k ...
-- if t is nil then do we error? yes?
-- if k is nil then we error.
-- how about if we're assigning ...
-- what does `t!.k = v` even mean?
-- that we want to error if k is nil, but we still want to overwrite it?
-- who would ever want to do that?
-- but honestly, same with `t?.k = v` ... optassign is just as bad ... just use `??=` and be done with it.
langfix.assertindex=function(t, k, assertassign)
ift==nilthen
error("table is nil")
end
localv=t[k]
ifv==nilthen
error("table index "..tostring(k).." is nil")
end
ifassertassignthen
v=assertassign()
t[k] =v
end
returnv
end
langfix.assertcall=function(v, ...)
ifv==nilthen
error("function is nil")
end
returnv(...)
end
langfix.assertcallself=function(t, k, assertassign, ...)
returnlangfix.assertcall(langfix.assertindex(t, k, assertassign), t, ...)
end
--local ztable = require '0-based'
-- notice ffi doesn't load in vanilla lua, so for vanilla lua < 5.3 this will all break
localnative_idiv=load'x=y//z'
localintptr_t=ffiandffi.typeof'intptr_t' ornil
ifffithen-- luajit
langfix.idiv=function(a, b)
-- [[ as integers, but requires ffi access ...
-- parenthesis required?
-- I think I got away with not using () to wrap generated-code because of the fact that always the code was generated from sources with correct precedence as it was parsed
-- so by the fact that the language was specified correctly, so was the AST represented correctly, and so the regenerated code was also correct.
returnintptr_t(a) /intptr_t(b)
--]]
--[[ as floats but with floor ... ? needs a math.sign or math.trunc function, how easy is that to write without generating anonymous lambdas or temp variables?
return '((function()'
..' local x = '..args:concat'/'
..' return x < 0 and math.ceil(x) or math.floor(x)'
..' end)())'
--]]
end
else-- vanilla-lua without //
langfix.idiv=function(a,b)
returnmath.floor(a/b)
end
end
langfix.luaToFixed=function(data, source)
localtree, result
localparser=LuaFixedParser()
localsuccess, msg=parser:setData(data, source)
ifnotsuccessthenreturnnil, msgend
tree=parser.tree
result=tree:toLua{maintainSpan=true}
--DEBUG:print('\n'..tostring(source)..'\n'..showcode(result)..'\n')
returnresult
end
langfix.loadstate=require'ext.load'(env)
langfix.loadstate.xforms:insert(function(data, source, mode, ...)
mode=modeor'bt'
localcanbin=mode:find'b'
localcantxt=mode:find't'
-- if it's binary data then oldload will handle it
ifcanbinanddata:sub(1,3) =='\x1bLJ' then
returndata
end
ifcantxtthen
returnlangfix.luaToFixed(data, source)
end
returnfalse, 'attempt to load chunk with wrong mode'
end)
--[[
this gets returned by `require 'langfix'`, and I am suspicious someone is writing it to _G.langfix, which can overwrite env.langfix ... all very ugly ... idk
somehow somewhere someone was setting _G.langfix=true, and I can only guess it's because someone was setting _G.langfix = require'langfix' (only when using the -l option in vanilla-lua, even 5.3 and 5.4)
and couple that with the fact that this function returns nothing, so `require 'langfix'` will return true ...
in other words the need to return env.langfix here all stems from a design behavior that -l<library> should always assign `_G.library = require'library'`
see the behavior yourself:
a.lua:
a=42
lua -la -e "print('a',a)"
a true
lua -e "require 'a' print('a', a)"
a 42
--]]
returnlangfix
end