aboutsummaryrefslogtreecommitdiff
path: root/lib/metaprogramming.nom
blob: 5ff1ab301956b58f5dfd2f8ad3cc5741bc008f95 (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
#..
    This File contains actions for making actions and compile-time actions and some helper
    functions to make that easier.

# Helper function
immediately
    lua> ".."
        nomsu.parse_spec = function(nomsu, spec)
            if spec.type == 'List' then
                local names = {};
                for i, alias in ipairs(spec.value) do
                    if alias.type == "FunctionCall" then
                        names[i] = alias.src;
                    elseif alias.type == "Text" then
                        names[i] = nomsu:tree_to_value(alias);
                    end
                end
                local junk, arg_names, junk = nomsu:get_stub(names[1]);
                local args = {};
                for i, a in ipairs(arg_names) do args[i] = nomsu:var_to_lua_identifier(a); end
                return names, args;
            else
                local alias = nomsu:tree_to_value(spec);
                print("ALIAS!!! "..repr(alias).." from "..repr(spec));
                local junk, arg_names, junk = nomsu:get_stub(alias);
                local args = {};
                for i, a in ipairs(arg_names) do args[i] = nomsu:var_to_lua_identifier(a); end
                return {alias}, args;
            end
        end

# Compile-time action to make compile-time actions:
# TODO: reduce code duplication here
immediately
    lua> ".."
        nomsu:define_compile_action("compile %names to %body", \(__line_no__), function(\%names, \%body)
            local names, args = nomsu:parse_spec(\%names);
            names, args = repr(names), table.concat(args, ", ");
            local body_lua = nomsu:tree_to_lua(\%body);
            body_lua = body_lua.statements or ("return "..body_lua.expr..";");
            local lua = ([[
        do
            local function compile_action(%s)
                %s
            end
            local function compile_action_wrapper(%s) return {expr=compile_action(%s)}; end
            nomsu:define_compile_action(%s, %s, compile_action_wrapper, %s);
        end]]):format(args, body_lua, args, args, names, repr(\%names:get_line_no()),
            repr(("compile %s\\n..to %s"):format(\%names.src, \%body.src)));
            return {statements=lua};
        end, \(__src__ 1));

    lua> ".."
        nomsu:define_compile_action("compile %names to code %body", \(__line_no__), function(\%names, \%body)
            local names, args = nomsu:parse_spec(\%names);
            names, args = repr(names), table.concat(args, ", ");
            local body_lua = nomsu:tree_to_lua(\%body);
            body_lua = body_lua.statements or ("return "..body_lua.expr..";");
            local lua = ([[
        do
            local function compile_action(%s)
                %s
            end
            local function compile_action_wrapper(%s) return {statements=compile_action(%s)}; end
            nomsu:define_compile_action(%s, %s, compile_action_wrapper, %s);
        end]]):format(args, body_lua, args, args, names, repr(\%names:get_line_no()),
            repr(("compile %s\\n..to code %s"):format(\%names.src, \%body.src)));
            return {statements=lua};
        end, \(__src__ 1));

# Compile-time action to make actions
immediately
    compile [action %names %body] to code
        lua> ".."
            local names, args = nomsu:parse_spec(\%names);
            names, args = repr(names), table.concat(args, ", ");
            local body_lua = nomsu:tree_to_lua(\%body);
            body_lua = body_lua.statements or ("return "..body_lua.expr..";");
            local src = nomsu:dedent(nomsu:source_code(0));
            local def_lua = ([[
            nomsu:define_action(%s, \(__line_no__), function(%s)
                %s
            end, %s);]]):format(names, args, body_lua, repr(src));
            return def_lua;

# Macro to make nomsu macros:
immediately
    lua> ".."
        nomsu:define_compile_action("parse %shorthand as %longhand", \(__line_no__), (function(\%shorthand, \%longhand)
            local names, args = nomsu:parse_spec(\%shorthand);
            names, args = repr(names), table.concat(args, ", ");
            local template;
            if \%longhand.type == "Block" then
                template = {};
                for i, line in ipairs(\%longhand.value) do
                    template[i] = nomsu:dedent(line.src);
                end
                template = repr(table.concat(template, "\\n"));
            else
                template = repr(nomsu:dedent(\%longhand.src));
            end
            local junk, arg_names, junk = nomsu:get_stub(\%shorthand.value[1]);
            local replacements = {};
            for i, a in ipairs(arg_names) do replacements[i] = "["..repr(a).."]="..nomsu:var_to_lua_identifier(a); end
            replacements = "{"..table.concat(replacements, ", ").."}";
            local lua_code = ([[
        nomsu:define_compile_action(%s, %s, (function(%s)
            local template = nomsu:parse(%s, %s);
            local replacement = nomsu:tree_with_replaced_vars(template, %s);
            return nomsu:tree_to_lua(replacement);
        end), %s)]]):format(names, repr(\%shorthand:get_line_no()), args, template,
            repr(\%shorthand:get_line_no()), replacements, repr(nomsu:source_code(0)));
            return {statements=lua_code};
        end), \(__src__ 1));

action [remove action %stub]
    lua> ".."
        local fn = ACTIONS[\%stub];
        local metadata = ACTION_METADATA[fn];
        for i=#metadata.aliases,1,-1 do
            metadata.arg_orders[metadata.aliases[i]] = nil;
            table.remove(metadata.aliases, i);
        end
        ACTIONS[\%stub] = nil;

immediately
    action [%tree as lua]
        =lua "nomsu:tree_to_lua(\%tree).expr"
    action [%tree as lua statements]
        lua> ".."
            local lua = nomsu:tree_to_lua(\%tree);
            return lua.statements or (lua.expr..";");
    action [%tree as value]
        =lua "nomsu:tree_to_value(\%tree)"
    compile [repr %obj] to
        "repr(\(%obj as lua))"
    compile [indented %obj] to
        "nomsu:indent(\(%obj as lua))"
    compile [dedented %obj] to
        "nomsu:dedent(\(%obj as lua))"
    compile [type %obj, type of %obj] to
        "type(\(%obj as lua))"

immediately
    parse [lua do> %block] as
        lua> "do"
        lua> %block
        lua> "end"

compile [nomsu] to "nomsu"

compile [nomsu's %key] to "nomsu[\(%key as lua)]"
compile [nomsu %method %args] to "nomsu[\(%method as lua)](nomsu, unpack(\(%args as lua)))"
compile [tree %tree with %replacements] to ".."
    nomsu:tree_with_replaced_vars(\(%tree as lua), \(%replacements as lua))

action [action %names metadata]
        =lua "ACTION_METADATA[ACTIONS[\%names]]"

# Get the source code for a function
action [help %action]
    lua> ".."
        local metadata = \(action %action metadata);
        if not metadata then
            nomsu:writeln("Action not found: "..repr(\%action));
        else
            nomsu:writeln(metadata.src or "<unknown source code>");
        end

# Compiler tools
parse [eval %code, run %code] as: nomsu "run" [%code]
action [source code from tree %tree]
    lua> ".."
        local junk,junk,leading_space = \%tree.src:find("\\n(%s*)%S");
        if leading_space then
            local chunk1, chunk2 = \%tree.src:match(":%s*([^\\n]*)(\\n.*)");
            chunk2 = chunk2:gsub("\\n"..leading_space, "\\n");
            return chunk1..chunk2.."\\n";
        else
            return \%tree.src:match(":%s*(%S.*)").."\\n";
        end
parse [source code %body] as: source code from tree \%body

parse [parse tree %code] as: nomsu "tree_to_str" [\%code]

parse [enable debugging] as: lua> "nomsu.debug = true"
parse [disable debugging] as: lua> "nomsu.debug = false"

compile [say %str] to
    lua> ".."
        if \%str.type == "Text" then
            return "nomsu:writeln("..\(%str as lua)..")";
        else
            return "nomsu:writeln(stringify("..\(%str as lua).."))";
        end

# Error functions
compile [barf!] to "error(nil, 0)"
compile [barf %msg] to "error(\(%msg as lua), 0)"
compile [assume %condition] to "assert(\(%condition as lua))"
compile [assume %condition or barf %msg] to "assert(\(%condition as lua), \(%msg as lua))"

# Literals
compile [yes] to "true"
compile [no] to "false"
compile [nothing, nil, null] to "nil"