Debian Codegraph @

texmf-dist/tex/lualatex/luacas/luacas_init.lua

raw
1 --- @class LuaCAS
2 --- @class Module
3 --- A table that initalizes and stores luacas modules.
4 --- @field moduleinfo table<string, table>
5 --- @field core Module
6 --- @field algebra Module
7 --- @field calculus Module
8 local luacas = {}
9
10
11 -- a table containing a list of module names, descriptions, class names, variables, required files, and dependent modules
12 luacas.moduleinfo = {
13 ["core"]=
14 {
15 "Classes needed for creating basic expressions such as symbols, binary operations, and functions.",
16 {
17 "Expression",
18 "AtomicExpression",
19 "CompoundExpression",
20 "ConstantExpression",
21 "SymbolExpression",
22 "BinaryOperation",
23 "FunctionExpression",
24
25 "__ExpressionOperations"
26 },
27 {"E", "I", "PI"},
28 {
29 "_lib.luacas-table",
30 "core.luacas-expression",
31 "core.luacas-atomicexpression",
32 "core.luacas-compoundexpression",
33 "core.luacas-constantexpression",
34 "core.luacas-symbolexpression",
35 "core.luacas-binaryoperation",
36 "core.luacas-functionexpression",
37 "core.binaryoperation.luacas-power",
38 "core.binaryoperation.luacas-product",
39 "core.binaryoperation.luacas-sum",
40 "core.binaryoperation.luacas-quotient",
41 "core.binaryoperation.luacas-difference",
42 },
43 {}
44 },
45
46 ["algebra"]=
47 {
48 "Classes for elements of rings (integers, rationals, polynomials, etc., and operations on those elements (polynomial factoring). Also includes some elementary functions like logs and trig functions.",
49 {
50 "Ring",
51 "EuclideanDomain",
52 "Field",
53 "PolynomialRing",
54 "Integer",
55 "Rational",
56 "IntegerModN",
57 "SqrtExpression",
58 "AbsExpression",
59 "Equation",
60 "FactorialExpression",
61 "Logarithm",
62 "RootExpression",
63 "TrigExpression",
64
65 "__RingOperations", -- Not classes, but we need to set the environment for this
66 "__EuclideanOperations",
67 "__FieldOperations",
68 "__PolynomialOperations",
69 "__IntegerOperations",
70 "__IntegerModNOperations",
71 "__RationalOperations"
72 },
73 {"ABS", "ARCCOS", "ARCCOT", "ARCCSC", "ARCSEC", "ARCSIN", "ARCTAN", "COS", "COT", "CSC", "FACT", "LN", "LOG", "SEC", "SIN", "TAN"},
74 {
75 "algebra.luacas-ring",
76 "algebra.luacas-euclideandomain",
77 "algebra.luacas-field",
78 "algebra.luacas-polynomialring",
79 "algebra.luacas-integer",
80 "algebra.luacas-rational",
81 "algebra.luacas-integerquotientring",
82 "algebra.luacas-sqrtexpression",
83 "algebra.luacas-absexpression",
84 "algebra.luacas-equation",
85 "algebra.luacas-factorialexpression",
86 "algebra.luacas-logarithm",
87 "algebra.luacas-rootexpression",
88 "algebra.luacas-trigexpression",
89 "algebra.polynomialring.luacas-berlekampfactoring",
90 "algebra.polynomialring.luacas-zassenhausfactoring",
91 "algebra.polynomialring.luacas-decomposition"
92 },
93 {"core"}
94 },
95
96 ["calculus"]=
97 {
98 "Classes for symbolic differentation and integration.",
99 {
100 "DerivativeExpression",
101 "IntegralExpression",
102 "DiffExpression"
103 },
104 {"DD", "INT"},
105 {
106 "calculus.luacas-derivativeexpression",
107 "calculus.luacas-integralexpression",
108 "calculus.luacas-diffexpression"
109 },
110 {"algebra"}
111 }
112 }
113
114 --- Retrieves a short description for a module.
115 --- @param mod string
116 --- @return string
117 function luacas:moduledesc(mod)
118 if self.moduleinfo[mod] then
119 return self.moduleinfo[mod][1]
120 end
121 return "Module not found."
122 end
123
124 --- Retrieves a list of class names included in a module.
125 --- @param mod string
126 --- @return table<number,string>
127 function luacas:moduleclasses(mod)
128 if self.moduleinfo[mod] then
129 return self.moduleinfo[mod][2]
130 end
131 return {}
132 end
133
134 --- Retrieves a list of fields included in a module.
135 --- @param mod string
136 --- @return table<number,string>
137 function luacas:modulefields(mod)
138 if self.moduleinfo[mod] then
139 return self.moduleinfo[mod][3]
140 end
141 return {}
142 end
143
144 --- Retrieves a list of files included in a module.
145 --- @param mod string
146 --- @return table<number,string>
147 function luacas:modulefiles(mod)
148 if self.moduleinfo[mod] then
149 return self.moduleinfo[mod][4]
150 end
151 return {}
152 end
153
154 --- Retrieves a list of a module's immediate dependencies.
155 --- @param mod string
156 --- @return table<number,string>
157 function luacas:moduledependencies(mod)
158 if self.moduleinfo[mod] then
159 return self.moduleinfo[mod][5]
160 end
161 return {}
162 end
163
164 --- Initalizes a LuaCAS module and returns it.
165 --- Since this creates temporary global variables, it should be treated as atomic to avoid collisions.
166 --- @param mod string
167 --- @return Module|nil
168 function luacas:initmodule(mod)
169 if not self.moduleinfo[mod] then
170 return nil
171 end
172
173 -- TODO: This is a special case, since the algebra module is dependent on the core module and vice versa,
174 -- but our initialization scheme does not allow for circular dependencies.
175 if mod == "core" then
176 self:initmodule("algebra")
177 end
178
179 -- Since other modules require globals from their dependencies, we only remove global variables after all modules have been initalized
180 local G = self:saveglobalstate()
181 self:_initmodulerec(mod)
182 self:restoreglobalstate(G)
183
184 return self[mod]
185 end
186
187 --- Recursive part of initalizing modules.
188 --- @param mod string
189 function luacas:_initmodulerec(mod)
190
191 for _, dep in ipairs(self:moduledependencies(mod)) do
192 self:_initmodulerec(dep)
193 end
194
195 if self[mod] then
196 -- Since globals from a dependency might be needed to initialize a module, we create globals even if a module has already been initalized.
197 for class, _ in self[mod] do
198 _G[class] = self[mod][class]
199 end
200 else
201 -- Creates globals needed for modules dependent on this one.
202 self:initglobalmodule(mod)
203
204 -- Adds classes to module table.
205 self[mod] = {}
206 for _, class in ipairs(self:moduleclasses(mod)) do
207 self[mod][class] = _G[class]
208 end
209
210 -- Sets the environment for each method of each class to include all dependent classes of this class.
211 self:setmoduleenvironment(mod)
212 -- TODO: This is a special case, since the algebra module is dependent on the core module and vice versa,
213 -- but our initialization scheme does not allow for circular dependencies.
214 if mod == "algebra" then
215 self:setmoduleenvironment("core")
216 end
217
218 -- Adds fields to module tables
219 for _, class in ipairs(self:modulefields(mod)) do
220 self[mod][class] = _G[class]
221 end
222 end
223 end
224
225 --- Sets the environment for each method of each class in a module to the current global environment, so it can keep using global variables when reset.
226 --- @param mod string
227 function luacas:setmoduleenvironment(mod)
228 local realenv = self:saveglobalstate()
229 for _, class in pairs(self[mod]) do
230 -- print(_)
231 for _, func in pairs(class) do
232 -- print(" ", _)
233 if type(func) == "function" then
234 --- Functions only have an _ENV upvalue if they have a global variable, so this should work
235 local _envloc = debug.findupvalue(func, "_ENV")
236 if _envloc then
237 debug.upvaluejoin(func, debug.findupvalue(func, "_ENV"), function () return realenv end, 1)
238 end
239 end
240 end
241
242 -- local meta = getmetatable(class)
243 -- if meta then
244 -- for _, func in pairs(meta) do
245 -- print(" ", _)
246 -- if type(func) == "function" then
247 -- --- Functions only have an _ENV upvalue if they have a global variable, so this should work
248 -- local _envloc = debug.findupvalue(func, "_ENV")
249 -- if _envloc then
250 -- debug.upvaluejoin(func, debug.findupvalue(func, "_ENV"), function () return realenv end, 1)
251 -- end
252 -- end
253 -- end
254 -- end
255 end
256 end
257
258 --- Saves the current global state as a table and returns it.
259 --- @return table
260 function luacas:saveglobalstate()
261 local G = {}
262 for key, value in pairs(_G) do
263 G[key] = value
264 end
265 return G
266 end
267
268 --- Saves the current global state as a table and returns it.
269 --- @param G table
270 function luacas:restoreglobalstate(G)
271 for key, _ in pairs(_G) do
272 if key ~= "_G" and key ~= "pairs" then
273 _G[key] = nil
274 end
275 end
276 for key, value in pairs(G) do
277 _G[key] = value
278 end
279 end
280
281 --- Initalizes a LuaCAS module and creates global variables for all classes in that module.
282 --- DOES NOT HANDLE DEPENDENCIES, so dependent modules must be globally initalized first.
283 --- @param mod string
284 --- @return Module|nil
285 function luacas:initglobalmodule(mod)
286 for _, filename in ipairs(self:modulefiles(mod)) do
287 require(filename)
288 end
289 end
290
291 --- Why is Lua like this?
292 function debug.findupvalue(fn, search_name)
293 local i = 1
294 while true do
295 local name, val = debug.getupvalue(fn, i)
296 if not name then break end
297 if name == search_name then
298 return i, val
299 end
300 i = i + 1
301 end
302 end
303
304 return luacas