aboutsummaryrefslogtreecommitdiff
path: root/lib/control_flow.nom
blob: 8d5f8ab76f881c6e01a6cabcd8380ed285819915 (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
require "lib/metaprogramming.nom"
require "lib/operators.nom"
require "lib/utils.nom"

# Conditionals
macro statement [if %condition %if_body] =:
    ".."|if \%condition as lua\ then
        |    \(lua expr "vars.if_body.value") as lua\
        |end

macro statement [if %condition %if_body else %else_body] =:
    ".."|if \%condition as lua\ then
        |    \(lua expr "vars.if_body.value") as lua\
        |else
        |    \(lua expr "vars.else_body.value") as lua\
        |end

# Return
macro statement [return] =: "do return end"
macro block [return %return-value] =: ".."
    |return \%return-value as lua\

macro [do %action] =: ".."
    |(\%action as lua\)(nomsu, setmetatable({}, {__index=vars}))


# GOTOs
macro statement [-> %label] =: ".."
    |::label_\nomsu "var_to_lua_identifier" [%label]\::
macro statement [go to %label] =: ".."
    |goto label_\nomsu "var_to_lua_identifier" [%label]\

# Loop control flow
macro statement [break] =: "break"
macro statement [break for] =: "goto break_for"
macro statement [break for-all] =: "goto break_for_all"
macro statement [break repeat] =: "goto break_repeat"
macro statement [break repeat-until] =: "goto break_repeat_until"
macro statement [break repeat-while] =: "goto break_repeat_while"
macro statement [break %var, stop getting %var, no more %var] =: ".."
    |goto break_\nomsu "var_to_lua_identifier" [%var]\

macro statement [continue] =: "continue"
macro statement [continue for] =: "goto continue_for"
macro statement [continue for-all] =: "goto continue_for_all"
macro statement [continue repeat] =: "goto continue_repeat"
macro statement [continue repeat-until] =: "goto continue_repeat_until"
macro statement [continue repeat-while] =: "goto continue_repeat_while"
macro statement [continue %var, go to next %var, on to the next %var] =: ".."
    |goto continue_\nomsu "var_to_lua_identifier" [%var]\

# While loops
macro block [repeat %body] =:
    ".."|while true do
        |    \(lua expr "vars.body.value") as lua\
        |    ::continue_repeat::
        |end
        |::break_repeat::
macro block [repeat while %condition %body] =:
    ".."|while \%condition as lua\ do
        |    \(lua expr "vars.body.value") as lua\
        |    ::continue_repeat_while::
        |end
        |::break_repeat_while::
macro block [repeat until %condition %body] =:
    ".."|while not (\%condition as lua\) do
        |    \(lua expr "vars.body.value") as lua\
        |    ::continue_repeat_until::
        |end
        |::break_repeat_until::

# For loops
macro block [for %var in %iterable %body] =:
    %var-type =: lua expr "vars.var.type"
    assert (%var-type == "Var") ".."
        |For loop has the wrong type for the loop variable. Expected Var, but got: \%var-type\
    # This trashes the loop variables, just like in Python.
    ".."
        |for i,value in ipairs(\%iterable as lua\) do
        |    \%var as lua\ = value
        |    \(lua expr "vars.body.value") as lua\
        |    ::continue_for::
        |    ::continue_\nomsu "var_to_lua_identifier" [%var]\::
        |end
        |::break_for::
        |::break_\nomsu "var_to_lua_identifier" [%var]\::

macro block [for all %iterable %body] =:
    pass # TODO: fix compiler bug
    # This trashes the loop variables, just like in Python.
    ".."|for i,value in ipairs(\%iterable as lua\) do
        |    vars.it = value
        |    \(lua expr "vars.body.value") as lua\
        |    ::continue_for_all::
        |end
        |::break_for_all::

# Switch statement/multi-branch if
macro block [when %body] =:
    %result =: ""
    for %statement in (lua expr "vars.body.value.value"):
        %func-call =: lua expr "vars.statement.value"
        assert ((lua expr "vars['func-call'].type") == "FunctionCall") ".."
            |Invalid format for 'when' statement. Only '?' blocks are allowed.
        %tokens =: lua expr "vars['func-call'].value"
        %q =: lua expr "vars.tokens[1]"
        assert (((lua expr "vars.q.type") == "Word") and ((lua expr "vars.q.value") == "?")) ".."
            |Invalid format for 'when' statement. Lines must begin with '?'
        %thunk =: lua expr "vars.tokens[#vars.tokens]"
        assert ((lua expr "vars.thunk.type") == "Thunk") ".."
            |Invalid format for 'when' statement. Lines must have a body.
        %condition_bits =: []
        for %i in (2 up to (lua expr "#vars.tokens")):
            lua block "table.insert(vars['condition_bits'], vars.tokens[vars.i])"

        if (lua expr "#vars.condition_bits == 0"):
            %result join=: ".."
                |
                |do
                |    \(lua expr "vars.thunk.value") as lua\
                |    goto finished_when
                |end
        ..else:
            if (lua expr "#vars.condition_bits == 1 and vars.condition_bits[1].type ~= 'Word'"):
                %condition =: lua expr "vars.condition_bits[1]"
            ..else:
                %condition =: dict [..]
                    ["type",lua expr "vars['func-call'].type"]
                    ["src",lua expr "vars['func-call'].src"]
                    ["value", %condition_bits]
            %result join=: ".."
                |
                |if \%condition as lua\ then
                |    \(lua expr "vars.thunk.value") as lua\
                |    goto finished_when
                |end
    %result join=: "\n::finished_when::"
    %result

# Switch statement
macro block [when %branch-value %body] =:
    %result =: ".."|local branch_value = \%branch-value as lua\
    for %statement in (lua expr "vars.body.value.value"):
        %func-call =: lua expr "vars.statement.value"
        assert ((lua expr "vars['func-call'].type") == "FunctionCall") ".."
            |Invalid format for 'when' statement. Only == blocks are allowed.
        %tokens =: lua expr "vars['func-call'].value"
        %eq =: lua expr "vars.tokens[1]"
        assert (((lua expr "vars.eq.type") == "Word") and ((lua expr "vars.eq.value") == "==")) ".."
            |Invalid format for 'when' statement. Lines must begin with '=='
        %thunk =: lua expr "vars.tokens[#vars.tokens]"
        assert ((lua expr "vars.thunk.type") == "Thunk") ".."
            |Invalid format for 'when' statement. Lines must have a body.
        %condition_bits =: []
        for %i in (2 up to (lua expr "#vars.tokens")):
            lua block "table.insert(vars.condition_bits, vars.tokens[vars.i])"
        if (lua expr "#vars.condition_bits == 0"):
            %result join=: ".."
                |
                |do
                |    \(lua expr "vars.thunk.value") as lua\
                |    goto finished_when
                |end
        ..else:
            if (lua expr "#vars.condition_bits == 1 and vars.condition_bits[1].type ~= 'Word'"):
                %condition =: (lua expr "vars.condition_bits[1]")
            ..else:
                %condition =: dict [..]
                    ["type",lua expr "vars['func-call'].type"]
                    ["src",lua expr "vars['func-call'].src"]
                    ["value", %condition_bits]
            %result join=: ".."
                |
                |if nomsu.utils.equivalent(branch_condition, \%condition as lua\) then
                |    \(lua expr "vars.thunk.value") as lua\
                |    goto finished_when
                |end
    %result join=: "\n::finished_when::"
    %result