aboutsummaryrefslogtreecommitdiff
path: root/nomnom/code_obj.nom
blob: f5735999d4cf62576c52ad537aab410bc80266cb (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
# This file contains objects that are used to track code positions and incrementally
    build up generated code, while keeping track of where it came from, and managing
    indentation levels.

use "lib/object.nom"

object (Code):
    my action [set up]:
        assume %me.source
        %old_bits = %me.bits
        %me.bits = []
        if (%me.source is text):
            %me.source = (Source from text %me.source)
        for % in %old_bits:
            %me::add %

    my action [as text]:
        barf "Not implemented"

    my action [as smext]:
        if (%me.__str == (nil)):
            set {%buff:[], %indent:0}
            for %bit in %me.bits:
                if (%bit is text):
                    %spaces = (%bit::matching "\n([ ]*)[^\n]*$")
                    if %spaces: %indent = (size of %spaces.1)
                ..else:
                    %bit = (%bit::as smext)
                    if (%indent > 0):
                        %bit = (%bit::with "\n" -> "\n\(" "::* %indent)")
                %buff::add %bit
            %me.__str = (%buff::joined)
        return %me.__str

    my action [as lua]:
        barf
        return "\(%me.class.name::as lua id)_from_1_2(\(%me.source::as lua), \(%me.bits::as lua))"

    my action [as nomsu] (..)
        "(\(%me.class.name) \(%me.source::as nomsu) \(%me.bits::as nomsu))"

    my action [size] (size of (%me::as smext))
    
    my action [mark as dirty]:
        %me.__str = (nil)
        %me._trailing_line_len = (nil)
        %me._num_lines = (nil)
            
    my action [add %new_bits]:
        unless (%new_bits is a "List"):
            %new_bits = [%new_bits]
        for % in %new_bits:
            if (% == ""): do next %
            #if ((% isn't text) and (% isn't a (Code))):
                % = (%::as lua)
            %me.bits::add %
        %me::mark as dirty
    
    my action [trailing line length]:
        if (%me._trailing_line_len == (nil)):
            %me._trailing_line_len = (size of ((%me::as smext)::matching "[^\n]*$"))
        return %me._trailing_line_len
    
    my action [number of lines]:
        unless %me._num_lines:
            %num_lines = 1
            for % in %me:
                if (% is text):
                    %num_lines += (size of (%::all matches of "\n"))
                ..else:
                    %num_lines += ((%::number of lines) - 1)
            %me._num_lines = %num_lines
        return %me._num_lines

    my action [is multiline, is multi-line] ((%me::number of lines) > 1)
    my action [is one line, is single line] ((%me::number of lines) == 1)

    my action [add %values joined with %joiner]:
        %me::add %values joined with %joiner or %joiner

    my action [add %values joined with %joiner or %wrapping_joiner]:
        %line_len = 0
        %bits = %me.bits
        for %value in %values at %i:
            if (%i > 1):
                if (%line_len > 80):
                    %bits::add %wrapping_joiner
                    %line_len = 0
                ..else:
                    %bits::add %joiner
            %bits::add %value
            unless (%value is text):
                %value = (%value::as smext)
            %line = (%value::matching "\n([^\n]*)$")
            if %line:
                %line_len = (size of %line)
            ..else:
                %line_len += (size of %value)
        %me::mark as dirty
    
    my action [prepend %]:
        #if ((% isn't text) and (% isn't a %me.__type)):
            % = (%::as lua)
        %me.bits::add % at index 1
        %me::mark as dirty

    my action [parenthesize]:
        %me.bits::add "(" at index 1
        %me.bits::add ")"
        %me::mark as dirty


object (Lua Code) extends (Code):
    my action [add free vars %vars]:
        if ((size of %vars) == 0): return
        %seen = (%v = (yes) for %v in %me.free_vars)
        for %var in %vars:
            assume (%var is text)
            unless %seen.%var:
                %me.free_vars::add %var
                %seen.%var = (yes)
        %me::mark as dirty
    
    my action [remove free vars %vars]:
        if ((size of %vars) == 0): return
        %removals = {}
        for %var in %vars:
            assume (%var is text)
            %removals.%var = (yes)
        
        %stack = [%me]
        repeat while ((size of %stack) > 0):
            %lua = (%stack::pop)
            for %i in (size of %lua.free_vars) to 1 by -1:
                if %removals.(%lua.free_vars.%i):
                    lua> "table.remove(\%lua.free_vars, \%i)"
                    #TODO: reinstate this
                    #%lua.free_vars::remove at index %i
            for % in %lua.bits:
                unless (% is text):
                    %stack::add %
        %me::mark as dirty

    my action [declare locals] (%me::declare locals (nil))
    my action [declare locals %to_declare]:
        unless %to_declare:
            set {%to_declare:[], %seen:{}}
            for %lua in recursive %me:
                for %var in %lua.free_vars:
                    unless %seen.%var:
                        %seen.%var = (yes)
                        %to_declare::add %var
                for % in %lua.bits:
                    unless (% is text):
                        recurse %lua on %
        if ((size of %to_declare) > 0):
            %me::remove free vars %to_declare
            %me::prepend "local \(%to_declare::joined with ", ");\n"
        return %to_declare

    my action [as statements] (%me::as statements "" ";")
    my action [as statements %prefix] (%me::as statements %prefix ";")
    my action [as statements %prefix %suffix]:
        unless %me.is_value:
            return %me
        %statements = (Lua Code from %me.source [])
        if ((%prefix or "") != ""):
            %statements::add %prefix
        %statements::add %me
        if (%suffix != ""):
            %statements::add (%suffix or ";")
        return %statements

    action [Lua Code from %source %bits]:
        if (%bits is text): %bits = [%bits]
        if (%source is a "Syntax Tree"): %source = %source.source
        return (..)
            Lua Code {source:%source, bits:%bits, is_value:(no), free_vars:[]}
    action [Lua Code from %source] (Lua Code from %source [])
    action [Lua Value from %source %bits]:
        if (%bits is text): %bits = [%bits]
        if (%source is a "Syntax Tree"): %source = %source.source
        return (..)
            Lua Code {source:%source, bits:%bits, is_value:(yes), free_vars:[]}
    action [Lua Value from %source] (Lua Value from %source [])

(Lua Code).add_free_vars = (Lua Code).add_free_vars_1
(Lua Code).remove_free_vars = (Lua Code).remove_free_vars_1
(Lua Code).declare_locals = (Lua Code).declare_locals_1
(Lua Code).as_statements = (Lua Code).as_statements_1_2

object (Nomsu Code) extends (Code):
    action [Nomsu Code from %source %bits]:
        if (%bits is text): %bits = [%bits]
        if (%source is a "Syntax Tree"): %source = %source.source
        return (..)
            Nomsu Code {source:%source, bits:%bits}
    action [Nomsu Code from %source] (Nomsu Code from %source [])