summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/tex/core/luacas-constantexpression.lua
blob: 6d9f8e96d22e77d3cafe8085281974bd50fff378 (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
--- @class ConstantExpression
--- @alias Constant ConstantExpression
--- Interface for a mathematical expression without any symbols.
--- ConstantExpressions are AtomicExpressions by default, but individual classes may overwrite that inheritance.
ConstantExpression = {}
local __ConstantExpression = {}

----------------------
-- Instance methods --
----------------------


--- @param symbol SymbolExpression
--- @return boolean
function ConstantExpression:freeof(symbol)
    return true
end

--- @return boolean
function ConstantExpression:isconstant()
    return true
end

--- @param other Expression
--- @return boolean
function ConstantExpression:order(other)

    -- Constants come before non-constants.
    if not other:isconstant() then
        return true
    end

    if self ~= E and self ~= PI and self ~= I then
        if other ~= E and other ~= PI and other ~= I then
            -- If both self and other are ring elements, we use the total order on the ring to sort.
            return self < other
        end
        -- Special constants come after ring elements.
        return true
    end

    -- Special constants come after ring elements.
    if other ~= E and other ~= PI and other ~= I then
        return false
    end

    -- Ensures E < PI < I.

    if self == E then return true end

    if self == I then return false end

    return other == I
end

-----------------
-- Inheritance --
-----------------

__ConstantExpression.__index = AtomicExpression
ConstantExpression = setmetatable(ConstantExpression, __ConstantExpression)