aboutsummaryrefslogtreecommitdiff
path: root/core/operators.nom
blob: a6d99771dc35a254c3e6760c753956ac22331f56 (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
#..
    This file contains definitions of operators like "+" and "and".

use "core/metaprogramming.nom"

# Indexing:
immediately:
    #.. NOTE!!! It's critical that there are spaces around %key if it's a string,
        otherwise, Lua will get confused and interpret %obj[[[foo]]] as %obj("[foo]")
        instead of %obj[ "foo" ].
        It's also critical to have parens around %obj, otherwise Lua is too dumb to
        realize that {x=1}["x"] is the same as ({x=1})["x"] or that
        {x=1}.x is the same as ({x=1}).x
    parse [..]
        %obj' %key, %obj's %key, %key in %obj, %key'th in %obj, %key of %obj,
        %key st in %obj, %key nd in %obj, %key rd in %obj, %key th in %obj,
    ..as: %obj.%key

# Comparison Operators
immediately:
    compile [%x < %y] to: Lua value "(\(%x as lua expr) < \(%y as lua expr))"
    compile [%x > %y] to: Lua value "(\(%x as lua expr) > \(%y as lua expr))"
    compile [%x <= %y] to: Lua value "(\(%x as lua expr) <= \(%y as lua expr))"
    compile [%x >= %y] to: Lua value "(\(%x as lua expr) >= \(%y as lua expr))"
    # TODO: optimize case of [%x,%y] = [1,2]
    compile [%a is %b, %a = %b, %a == %b] to:
        lua> ".."
            local safe = {Text=true, Number=true};
            local a_lua, b_lua = nomsu:tree_to_lua(\%a), nomsu:tree_to_lua(\%b);
            if safe[\%a.type] or safe[\%b.type] then
                return Lua.Value(tree.source, "(", a_lua, " == ", b_lua, ")");
            else
                return Lua.Value(tree.source, "utils.equivalent(", a_lua, ", ", b_lua, ")");
            end
    compile [%a isn't %b, %a is not %b, %a not= %b, %a != %b] to:
        lua> ".."
            local safe = {Text=true, Number=true};
            local a_lua, b_lua = nomsu:tree_to_lua(\%a), nomsu:tree_to_lua(\%b);
            if safe[\%a.type] or safe[\%b.type] then
                return Lua.Value(tree.source, "(", a_lua, " ~= ", b_lua, ")");
            else
                return Lua.Value(tree.source, "(not utils.equivalent(", a_lua, ", ", b_lua, "))");
            end
    # For strict identity checking, use (%x's id) is (%y's id)
    compile [%'s id, id of %] to: Lua value "nomsu.ids[\(% as lua expr)]"

# Variable assignment operator
immediately:
    compile [%var <- %value] to:
        lua> "local \%var_lua = nomsu:tree_to_lua(\%var);"
        assume %var_lua.is_value or barf "Invalid target for assignment: \(%var's source code)"
        lua> "local \%value_lua = nomsu:tree_to_lua(\%value);"
        assume %value_lua.is_value or barf "Invalid value for assignment: \(%value's source code)"
        lua> ".."
            local \%lua = Lua(tree.source, \%var_lua, ' = ', \%value_lua, ';');
            if \%var.type == 'Var' then
                \%lua:add_free_vars(nomsu:var_to_lua_identifier(\%var.value));
            end
            return \%lua;

immediately:
    # Simultaneous mutli-assignments like: x,y,z = 1,x,3;
    compile [<- %assignments] to:
        assume ((%assignments' "type") is "Dict") or barf ".."
            Expected a Dict for the assignments part of '<- %' statement, not \(%assignments' source code)
        lua> ".."
            local lhs, rhs = Lua(tree.source), Lua(\%assignments.source);
            for i, item in ipairs(\%assignments.value) do
                local target, value = item.key, item.value;
                local target_lua = nomsu:tree_to_lua(target);
                if not target_lua.is_value then error("Invalid target for assignment: "..target:get_src()); end
                local value_lua = nomsu:tree_to_lua(value);
                if not value_lua.is_value then error("Invalid value for assignment: "..value:get_src()); end
                if target.type == "Var" then
                    lhs:add_free_vars(nomsu:var_to_lua_identifier(target.value));
                end
                if i > 1 then
                    lhs:append(", ");
                    rhs:append(", ");
                end
                lhs:append(target_lua);
                rhs:append(value_lua);
            end
            return Lua(tree.source, lhs, " = ", rhs, ";");

immediately:
    compile [export %var <- %value] to:
        %var_lua <- (%var as lua)
        assume %var_lua.is_value or barf "Invalid target for assignment: \(%var's source code)"
        %value_lua <- (%value as lua)
        assume %value_lua.is_value or barf "Invalid value for assignment: \(%value's source code)"
        return: Lua "\(%var_lua) = \(%value_lua);"

    compile [exporting %exported %body] to:
        %body_lua <- (%body as lua)
        lua> "\%body_lua:declare_locals(\%exported);"
        return %body_lua

    parse [with %assignments %body] as:
        # TODO: clean up and handle: with {%x:1}: %y <- 2
        lua> "do"
        <- %assignments
        lua> "end"

immediately:
    # Math Operators
    compile [%x + %y] to: Lua value "(\(%x as lua expr) + \(%y as lua expr))"
    compile [%x - %y] to: Lua value "(\(%x as lua expr) - \(%y as lua expr))"
    compile [%x * %y] to: Lua value "(\(%x as lua expr) * \(%y as lua expr))"
    compile [%x / %y] to: Lua value "(\(%x as lua expr) / \(%y as lua expr))"
    compile [%x ^ %y] to: Lua value "(\(%x as lua expr) ^ \(%y as lua expr))"
    compile [%x wrapped around %y, %x mod %y] to: Lua value "(\(%x as lua expr) % \(%y as lua expr))"

    # 3-part chained comparisons
    # (uses a lambda to avoid re-evaluating middle value, while still being an expression)
    parse [%x <  %y <  %z] as: =lua "(function(x,y,z) return x <  y and y <  z; end)(\%x,\%y,\%z)"
    parse [%x <= %y <  %z] as: =lua "(function(x,y,z) return x <= y and y <  z; end)(\%x,\%y,\%z)"
    parse [%x <  %y <= %z] as: =lua "(function(x,y,z) return x <  y and y <= z; end)(\%x,\%y,\%z)"
    parse [%x <= %y <= %z] as: =lua "(function(x,y,z) return x <= y and y <= z; end)(\%x,\%y,\%z)"
    parse [%x >  %y >  %z] as: =lua "(function(x,y,z) return x >  y and y >  z; end)(\%x,\%y,\%z)"
    parse [%x >= %y >  %z] as: =lua "(function(x,y,z) return x >= y and y >  z; end)(\%x,\%y,\%z)"
    parse [%x >  %y >= %z] as: =lua "(function(x,y,z) return x >  y and y >= z; end)(\%x,\%y,\%z)"
    parse [%x >= %y >= %z] as: =lua "(function(x,y,z) return x >= y and y >= z; end)(\%x,\%y,\%z)"
    # TODO: optimize for common case where x,y,z are all either variables or number literals

    # Boolean Operators
    compile [%x and %y] to: Lua value "(\(%x as lua expr) and \(%y as lua expr))"
    compile [%x or %y] to: Lua value "(\(%x as lua expr) or \(%y as lua expr))"

    # Bitwise Operators
    compile [%a OR %b, %a | %b] to: Lua value "bit32.bor(\(%a as lua expr), \(%b as lua expr))"
    compile [%a XOR %b] to: Lua value "bit32.bxor(\(%a as lua expr), \(%b as lua expr))"
    compile [%a AND %b, %a & %b] to: Lua value "bit32.band(\(%a as lua expr), \(%b as lua expr))"
    compile [NOT %, ~ %] to: Lua value "bit32.bnot(\(% as lua expr))"
    compile [%x LSHIFT %shift, %x << %shift] to: Lua value "bit32.lshift(\(%x as lua expr), \(%shift as lua expr))"
    compile [%x RSHIFT %shift, %x >>> %shift] to: Lua value "bit32.rshift(\(%x as lua expr), \(%shift as lua expr))"
    compile [%x ARSHIFT %shift, %x >> %shift] to: Lua value "bit32.arshift(\(%x as lua expr), \(%shift as lua expr))"
    # TODO: implement OR, XOR, AND for multiple operands?

    # Unary operators
    compile [- %] to: Lua value "(- \(% as lua expr))"
    compile [not %] to: Lua value "(not \(% as lua expr))"

# Update operators
immediately:
    parse [%var + <- %, %var +<- %] as: %var <- (%var + %)
    parse [%var - <- %, %var -<- %] as: %var <- (%var - %)
    parse [%var * <- %, %var *<- %] as: %var <- (%var * %)
    parse [%var / <- %, %var /<- %] as: %var <- (%var / %)
    parse [%var ^ <- %, %var ^<- %] as: %var <- (%var ^ %)
    parse [%var and <- %] as: %var <- (%var and %)
    parse [%var or <- %] as: %var <- (%var or %)
    parse [wrap %var around %] as: %var <- (%var wrapped around %)