aboutsummaryrefslogtreecommitdiff
path: root/core.moon
blob: 1ea22dca9439a607adaa9c0ecbb66ffc8c42f9a4 (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
#!/usr/bin/env moon
nomic = require 'nomic'
utils = require 'utils'
game = nomic()

game\def {[[print $str]], [[say $str]]}, (args)=> print(utils.repr(args.str))
game\def {[[printf $str]]}, (args)=>
    for s in *args.str do io.write(utils.repr(s))
    io.write("\n")

game\def [[return $value]], (args)=> args.value

game\macro [[let $var = $value]], (var, value)->
    "locals[#{var}] = #{value}"

game\def [[$signature := $body]], (args)=>
    invocations = if type(args.signature) == 'table' then args.signature else {args.signature}
    for i in *invocations
        unless @check_authorization(i)
            print "You are not permitted to redefine this function"
            return

    @\def args.signature, args.body
    print "Defined new rule: #{utils.repr(args.signature)}"
    --print debug.getinfo(args.body, "S").source
    return nil

game\def [[compile $body]], (args)=>
    print debug.getinfo(args.body, "S").source
    return nil

game\def [[help $invocation]], (args)=>
    print_rule = (invocation, rule)->
        print "\"#{invocation}\" :="
        info = debug.getinfo(rule, "S")
        i = 0
        for line in info.source\gmatch("[^\n]+")
            i+=1
            if i >= info.linedefined+1
                print(line)
            if i >= info.lastlinedefined-1
                return
        print "    #{info.short_src}:#{info.linedefined}-#{info.lastlinedefined}"

    with args
        if rule = @rules[.invocation]
            print_rule .invocation, rule
            return nil
        words = [w for w in .invocation\gmatch("%S+")]
        match_count = (i)->
            iws = [w for w in i\gmatch("[^ $]+")]
            count = 0
            for w in *words
                for iw in *iws
                    if w == iw then count += 1
            count += 1/#iws
            return count
        rules = {}
        invocations = {}
        for i,r in pairs(@rules)
            c = match_count(i)
            if c > (rules[r] or 0)
                rules[r] = c
                invocations[r] = i
        best = [r for r in pairs rules]
        utils.sort best, rules
        if rules[best[1]] > 0
            for r in *best
                if rules[r] < rules[best[1]]
                    break
                print_rule invocations[r], r
        return nil

game\macro "true", -> "true"
game\macro "yes", -> "true"
game\macro "false", -> "false"
game\macro "no", -> "false"
game\macro "nil", -> "nil"
game\macro "None", -> "nil"
game\macro "null", -> "nil"
game\def [[nop]], ((args)=> nil), "... does nothing, returns nil ..."

game\def [[$x == $y]], (args)=> utils.equivalent(args.x, args.y)
game\run [=[
    ["$x != $y", "$x <> $y", "$x ~= $y"] := {return (not (x == y))}
]=]
game\def [[$x < $y]], (args)=> args.x < args.y
game\def [[$x <= $y]], (args)=> args.x <= args.y
game\def [[$x > $y]], (args)=> args.x > args.y
game\def [[$x >= $y]], (args)=> args.x >= args.y

game\macro [[$x + $y]], (x,y)-> "(#{x} + #{y})"
game\macro [[$x - $y]], (x,y)-> "(#{x} - #{y})"
game\macro [[$x * $y]], (x,y)-> "(#{x} * #{y})"
game\macro [[$x / $y]], (x,y)-> "(#{x} / #{y})"
game\macro [[$x ^ $y]], (x,y)-> "(#{x} ^ #{y})"

game\macro [[$x < $y]], (x,y)-> "(#{x} < #{y})"
game\macro [[$x <= $y]], (x,y)-> "(#{x} <= #{y})"
game\macro [[$x > $y]], (x,y)-> "(#{x} > #{y})"
game\macro [[$x >= $y]], (x,y)-> "(#{x} >= #{y})"

game\macro [[not $x]], (x)-> "(not #{x})"
game\macro [[$x and $y]], (x,y)-> "(#{x} and #{y})"
game\macro [[$x or $y]], (x,y)-> "(#{x} or #{y})"


[==[
game\def [[if $condition $body else $else_body]], (args)=>
    with args
        if .condition
            return .body(@, args)
        else return .else_body(@, args)

game\run [=[
    ["if $condition $body", "when $condition $body"] := {if $condition $body else {}}
    ["unless $condition $body"] := {if (not $condition) $body else {}}
    ["unless $condition $body else $else_body"] := {if (not $condition) $body else $else_body}
]=]
]==]

game\def [[random]], -> math.random()

game\def [[sum $items]], (args)=> utils.sum(args.items)
game\def [[all $items]], (args)=> utils.all(args.items)
game\def [[any $items]], (args)=> utils.any(args.items)
game\def {[[average $items]], [[avg $items]]}, (args)=> utils.sum(items)/#items
game\def {[[min $items]], [[smallest $items]], [[lowest $items]], [[fewest $items]]}, (args)=>
    utils.min(args.items)

game\def {[[max $items]], [[largest $items]], [[highest $items]], [[most $items]]}, (args)=>
    utils.max(args.items)

game\def {[[argmin $items]]}, (args)=>
    utils.min(args.items, ((i)->i[2]))
game\def {[[argmax $items]]}, (args)=>
    utils.max(args.items, ((i)->i[2]))

game\def {[[min $items with respect to $keys]]}, (args)=>
    utils.min(args.items, args.keys)
game\def {[[max $items with respect to $keys]]}, (args)=>
    utils.max(args.items, args.keys)

game\def {[[$index st in $list]], [[$index nd in $list]], [[$index rd in $list]], [[$index th in $list]]}, (args)=>
    with args
        if type(.list) != 'table'
            print "Not a list: #{.list}"
            return
        .list[.index]

game\def {[[index of $item in $list]]}, (args)=>
    with args
        if type(.list) != 'table'
            print "Not a list: #{.list}"
            return
        utils.key_for(args.list, args.item)

game\run [=[
    ["$item is in $list", "$list contains $item"] := {(index of $item in $list) != (nil)}
]=]

game\def {[[# $list]], [[length of $list]], [[size of $list]]}, (args)=>
    with args
        if type(.list) != 'table'
            print "Not a list: #{.list}"
            return
        return #(.list)


return game