aboutsummaryrefslogtreecommitdiff
path: root/lib/metaprogramming.nom
blob: 9dcc9dff31f4490c79debfa21725f9bc294278cf (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
#..
    This File contains rules for making rules and macros and some helper functions to make
    that easier.

# Nil
lua code ".."
    |nomsu:defmacro("nil", function(nomsu, vars) return "nil", nil end)
..with value 0

# Macros:
lua code ".."
    |nomsu:def("parse %shorthand as %longhand", function(nomsu, vars)
    |    local alias = nomsu:get_alias(vars.shorthand)
    |    local template = vars.longhand
    |    nomsu:defmacro(alias, function(nomsu, vars)
    |        return nomsu:tree_to_lua(nomsu:replaced_vars(template, vars))
    |    end)
    |end)
..with value (nil)

parse \(lua code %code) as\: lua code %code with value (nil)
parse \(lua block %block) as\:
    lua code ".."
        |do
        |    \(%block)
        |end
    ..with value (nil)
parse \(lua value %value) as\: lua code (nil) with value %value

parse \(nomsu) as\: lua value "nomsu"
parse \(nomsu's %key) as\:
    lua value "nomsu[\(%key as lua)]"
parse \(nomsu %method %args) as\:
    lua block ".."
        |local args = {"nomsu"}
        |for _,arg in ipairs(vars.args.value) do
        |    table.insert(args, nomsu:tree_to_lua(arg))
        |end
        |local method_name = nomsu:repr(nomsu:tree_to_value(vars.method, vars))
    ..with value ".."
        |("nomsu[%s](%s)"):format(method_name, table.concat(args, ", "))
parse \(repr %) as\: nomsu "repr" [%]

# Rule that lets you make new rules
lua block ".."
    |nomsu:def("rule helper %rule_def = %body", function(nomsu, vars)
    |    nomsu:def(nomsu:get_alias(vars.rule_def), vars.body)
    |end)
parse \(rule %rule_def = %body) as\: rule helper \(%rule_def) = \(%body)
parse \(rule %rule_name) as\: lua value "nomsu.defs[nomsu:get_alias(\(%rule_name))]"

# Get the source code for a function
rule (help %rule) =:
    lua block ".."
        |local fn_def = nomsu:get_fn_def(vars.rule)
        |if not fn_def then
        |    nomsu:writeln("Rule not found: "..nomsu:repr(vars.rule))
        |else
        |    nomsu:writeln("rule "..nomsu:repr(nomsu.utils.keys(fn_def.aliases))
        |        .." ="..(fn_def.src or ":\\n    <unknown source code>"))
        |end

# Macro helper functions
rule (%tree as value) =:
    lua expr "nomsu:tree_to_value(vars.tree, vars)"

rule (%tree as lua) =:
    lua expr "nomsu:tree_to_lua(vars.tree)"

# Compiler tools
rule (eval %code; run %code) =: nomsu "run" [%code]
rule (source code from tree %tree) =:
    lua block ".."
        |local _,_,leading_space = vars.tree.src:find("\\n(%s*)%S")
        |if leading_space then
        |    local chunk1, chunk2 = vars.tree.src:match(":%s*([^\\n]*)(\\n.*)")
        |    chunk2 = chunk2:gsub("\\n"..leading_space, "\\n")
        |    return chunk1..chunk2.."\\n"
        |else
        |    return vars.tree.src:match(":%s*(%S.*)").."\\n"
        |end

macro (source code %body) =:
    nomsu "repr" [nomsu "call" ["source code from tree %", %body]]

macro (parse tree %code) =:
    nomsu "repr" [nomsu "stringify_tree" [lua expr "vars.code.value"]]