summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/tex/luacas_init.lua
blob: 8d58e259e720bb5f49bc96063eff3ad47af4862f (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
--- @class LuaCAS
--- @class Module
--- A table that initalizes and stores luacas modules.
--- @field moduleinfo table<string, table>
--- @field core Module
--- @field algebra Module
--- @field calculus Module
local luacas = {}


-- a table containing a list of module names, descriptions, class names, variables, required files, and dependent modules
luacas.moduleinfo = {
    ["core"]=
    {
        "Classes needed for creating basic expressions such as symbols, binary operations, and functions.",
        {
            "Expression",
            "AtomicExpression",
            "CompoundExpression",
            "ConstantExpression",
            "SymbolExpression",
            "BinaryOperation",
            "FunctionExpression",

            "__ExpressionOperations"
        },
        {"E", "I", "PI"},
        {
            "_lib.luacas-table",
            "core.luacas-expression",
            "core.luacas-atomicexpression",
            "core.luacas-compoundexpression",
            "core.luacas-constantexpression",
            "core.luacas-symbolexpression",
            "core.luacas-binaryoperation",
            "core.luacas-functionexpression",
            "core.binaryoperation.luacas-power",
            "core.binaryoperation.luacas-product",
            "core.binaryoperation.luacas-sum",
            "core.binaryoperation.luacas-quotient",
            "core.binaryoperation.luacas-difference",
        },
        {}
    },

    ["algebra"]=
    {
        "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.",
        {
            "Ring",
            "EuclideanDomain",
            "Field",
            "PolynomialRing",
            "Integer",
            "Rational",
            "IntegerModN",
            "SqrtExpression",
            "AbsExpression",
            "Equation",
            "FactorialExpression",
            "Logarithm",
            "RootExpression",
            "TrigExpression",

            "__RingOperations", -- Not classes, but we need to set the environment for this
            "__EuclideanOperations",
            "__FieldOperations",
            "__PolynomialOperations",
            "__IntegerOperations",
            "__IntegerModNOperations",
            "__RationalOperations"
        },
        {"ABS", "ARCCOS", "ARCCOT", "ARCCSC", "ARCSEC", "ARCSIN", "ARCTAN", "COS", "COT", "CSC", "FACT", "LN", "LOG", "SEC", "SIN", "TAN"},
        {
            "algebra.luacas-ring",
            "algebra.luacas-euclideandomain",
            "algebra.luacas-field",
            "algebra.luacas-polynomialring",
            "algebra.luacas-integer",
            "algebra.luacas-rational",
            "algebra.luacas-integerquotientring",
            "algebra.luacas-sqrtexpression",
            "algebra.luacas-absexpression",
            "algebra.luacas-equation",
            "algebra.luacas-factorialexpression",
            "algebra.luacas-logarithm",
            "algebra.luacas-rootexpression",
            "algebra.luacas-trigexpression",
            "algebra.polynomialring.luacas-berlekampfactoring",
            "algebra.polynomialring.luacas-zassenhausfactoring",
            "algebra.polynomialring.luacas-decomposition"
        },
        {"core"}
    },

    ["calculus"]=
    {
        "Classes for symbolic differentation and integration.",
        {
            "DerivativeExpression",
            "IntegralExpression",
            "DiffExpression"
        },
        {"DD", "INT"},
        {
            "calculus.luacas-derivativeexpression",
            "calculus.luacas-integralexpression",
            "calculus.luacas-diffexpression"
        },
        {"algebra"}
    }
}

--- Retrieves a short description for a module.
--- @param mod string
--- @return string
function luacas:moduledesc(mod)
    if self.moduleinfo[mod] then
        return self.moduleinfo[mod][1]
    end
    return "Module not found."
end

--- Retrieves a list of class names included in a module.
--- @param mod string
--- @return table<number,string>
function luacas:moduleclasses(mod)
    if self.moduleinfo[mod] then
        return self.moduleinfo[mod][2]
    end
    return {}
end

--- Retrieves a list of fields included in a module.
--- @param mod string
--- @return table<number,string>
function luacas:modulefields(mod)
    if self.moduleinfo[mod] then
        return self.moduleinfo[mod][3]
    end
    return {}
end

--- Retrieves a list of files included in a module.
--- @param mod string
--- @return table<number,string>
function luacas:modulefiles(mod)
    if self.moduleinfo[mod] then
        return self.moduleinfo[mod][4]
    end
    return {}
end

--- Retrieves a list of a module's immediate dependencies.
--- @param mod string
--- @return table<number,string>
function luacas:moduledependencies(mod)
    if self.moduleinfo[mod] then
        return self.moduleinfo[mod][5]
    end
    return {}
end

--- Initalizes a LuaCAS module and returns it.
--- Since this creates temporary global variables, it should be treated as atomic to avoid collisions.
--- @param mod string
--- @return Module|nil
function luacas:initmodule(mod)
    if not self.moduleinfo[mod] then
        return nil
    end

    -- TODO: This is a special case, since the algebra module is dependent on the core module and vice versa,
    -- but our initialization scheme does not allow for circular dependencies.
    if mod == "core" then
        self:initmodule("algebra")
    end

    -- Since other modules require globals from their dependencies, we only remove global variables after all modules have been initalized
    local G = self:saveglobalstate()
    self:_initmodulerec(mod)
    self:restoreglobalstate(G)

    return self[mod]
end

--- Recursive part of initalizing modules.
--- @param mod string
function luacas:_initmodulerec(mod)

    for _, dep in ipairs(self:moduledependencies(mod)) do
        self:_initmodulerec(dep)
    end

    if self[mod] then
        -- Since globals from a dependency might be needed to initialize a module, we create globals even if a module has already been initalized.
        for class, _ in self[mod] do
            _G[class] = self[mod][class]
        end
    else
        -- Creates globals needed for modules dependent on this one.
        self:initglobalmodule(mod)

        -- Adds classes to module table.
        self[mod] = {}
        for _, class in ipairs(self:moduleclasses(mod)) do
            self[mod][class] = _G[class]
        end

        -- Sets the environment for each method of each class to include all dependent classes of this class.
        self:setmoduleenvironment(mod)
        -- TODO: This is a special case, since the algebra module is dependent on the core module and vice versa,
        -- but our initialization scheme does not allow for circular dependencies.
        if mod == "algebra" then
            self:setmoduleenvironment("core")
        end

        -- Adds fields to module tables
        for _, class in ipairs(self:modulefields(mod)) do
            self[mod][class] = _G[class]
        end
    end
end

--- 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.
--- @param mod string
function luacas:setmoduleenvironment(mod)
    local realenv = self:saveglobalstate()
    for _, class in pairs(self[mod]) do
        -- print(_)
        for _, func in pairs(class) do
            -- print("    ", _)
            if type(func) == "function" then
                --- Functions only have an _ENV upvalue if they have a global variable, so this should work
                local _envloc = debug.findupvalue(func, "_ENV")
                if _envloc then
                    debug.upvaluejoin(func, debug.findupvalue(func, "_ENV"), function () return realenv end, 1)
                end
            end
        end

        -- local meta = getmetatable(class)
        -- if meta then
        --     for _, func in pairs(meta) do
        --         print("    ", _)
        --         if type(func) == "function" then
        --             --- Functions only have an _ENV upvalue if they have a global variable, so this should work
        --             local _envloc = debug.findupvalue(func, "_ENV")
        --             if _envloc then
        --                 debug.upvaluejoin(func, debug.findupvalue(func, "_ENV"), function () return realenv end, 1)
        --             end
        --         end
        --     end
        -- end
    end
end

--- Saves the current global state as a table and returns it.
--- @return table
function luacas:saveglobalstate()
    local G = {}
    for key, value in pairs(_G) do
        G[key] = value
    end
    return G
end

--- Saves the current global state as a table and returns it.
--- @param G table
function luacas:restoreglobalstate(G)
    for key, _ in pairs(_G) do
        if key ~= "_G" and key ~= "pairs" then
            _G[key] = nil
        end
    end
    for key, value in pairs(G) do
        _G[key] = value
    end
end

--- Initalizes a LuaCAS module and creates global variables for all classes in that module.
--- DOES NOT HANDLE DEPENDENCIES, so dependent modules must be globally initalized first.
--- @param mod string
--- @return Module|nil
function luacas:initglobalmodule(mod)
    for _, filename in ipairs(self:modulefiles(mod)) do
        require(filename)
    end
end

--- Why is Lua like this?
function debug.findupvalue(fn, search_name)
    local i = 1
    while true do
      local name, val = debug.getupvalue(fn, i)
      if not name then break end
      if name == search_name then
        return i, val
      end
      i = i + 1
    end
end

return luacas