diff options
| -rw-r--r-- | core.moon | 79 | ||||
| -rwxr-xr-x | game1.moon | 99 | ||||
| -rw-r--r-- | nomic.moon | 8 |
3 files changed, 104 insertions, 82 deletions
diff --git a/core.moon b/core.moon new file mode 100644 index 0000000..0523996 --- /dev/null +++ b/core.moon @@ -0,0 +1,79 @@ +#!/usr/bin/env moon +nomic = require 'nomic' +game = nomic() + +game\def {[[print $str]], [[say $str]]}, (locals)=> + with locals + print(.str) + return nil + +game\def [[return $retval]], (locals)=> locals.retval + +game\def [[do $thunk]], (locals)=> + locals.thunk\run(@, locals) + +game\def {[[true]], [[yes]]}, (locals)=> true +game\def {[[false]], [[no]]}, (locals)=> false +game\def {[[nil]], [[None]], [[nop]], [[done]]}, (locals)=> nil + +game\def [[$x == $y]], (locals)=> + with locals + print("testing equality of #{.x} and #{.y}") + if type(.x) != type(.y) + return false + if type(.x) == 'table' + for k,v in pairs(.x) + if .y[k] != v + return false + for k,v in pairs(.y) + if .x[k] != v + return false + return true + else + return .x == .y + +game\def [[not $x]], (locals)=> not locals.x +game\def [[$x != $y]], [[return (not (x == y))]] +game\def [[$x < $y]], (locals)=> locals.x < locals.y +game\def [[$x <= $y]], (locals)=> locals.x <= locals.y +game\def [[$x > $y]], (locals)=> locals.x > locals.y +game\def [[$x >= $y]], (locals)=> locals.x >= locals.y + + +game\def [[if $condition then $body else $else_body]], (locals)=> + with locals + if .condition + return .body\run(@, locals) + else return .else_body\run(@, locals) + +game\def [[if $condition then $body]], [[if $condition then $body else {}]] +game\def [[when $condition do $body]], [[if $condition then $body else {}]] + + +game\def [[sum $items]], (locals)=> + tot = 0 + for x in *locals.items do tot += x + return tot + +game\def {[[average $items]], [[avg $items]]}, (locals)=> + tot = 0 + for x in *locals.items do tot += x + return tot / #locals.items + +game\def {[[min $items]], [[smallest $items]], [[lowest $items]], [[fewest $items]]}, (locals)=> + with locals + min = .items[1] + for i=2,#.items + if .items[i] < min + min = .items[i] + return min + +game\def {[[max $items]], [[largest $items]], [[highest $items]], [[most $items]]}, (locals)=> + with locals + max = .items[1] + for i=2,#.items + if .items[i] > max + max = .items[i] + return max + +return game @@ -1,100 +1,41 @@ #!/usr/bin/env moon -nomic = require 'nomic' -game = nomic() - ------------------- CORE STUFF --------------------- -game\def [[say $str]], (locals)=> - with locals - print(.str) - return nil - -game\def [[return $retval]], (locals)=> locals.retval - -game\def [[do $thunk]], (locals)=> - locals.thunk\run(@, locals) - -game\def {[[true]], [[yes]]}, (locals)=> true -game\def {[[false]], [[no]]}, (locals)=> false -game\def {[[nil]], [[None]], [[nop]], [[done]]}, (locals)=> nil - -game\def [[$x == $y]], (locals)=> - with locals - print("testing equality of #{.x} and #{.y}") - if type(.x) != type(.y) - return false - if type(.x) == 'table' - for k,v in pairs(.x) - if .y[k] != v - return false - for k,v in pairs(.y) - if .x[k] != v - return false - return true - else - return .x == .y - -game\def [[not $x]], (locals)=> not locals.x -game\def [[$x != $y]], [[return (not (x == y))]] -game\def [[$x < $y]], (locals)=> locals.x < locals.y -game\def [[$x <= $y]], (locals)=> locals.x <= locals.y -game\def [[$x > $y]], (locals)=> locals.x > locals.y -game\def [[$x >= $y]], (locals)=> locals.x >= locals.y - - -game\def [[if $condition then $body else $else_body]], (locals)=> - with locals - if .condition - return .body\run(@, locals) - else return .else_body\run(@, locals) - -game\def [[if $condition then $body]], [[if $condition then $body else {}]] -game\def [[when $condition do $body]], [[if $condition then $body else {}]] - +game = require 'core' +export __DEBUG__ +__DEBUG__ = true ------------------ BASIC TESTS --------------------- - -game\run [[say "Hello world!"]] game\run [[ - say "Hello!" - say "World!" -]] -game\def {[[greet]], [[say hello]]}, [[say "Hello!"]] -game\run[[greet]] -game\run[[say hello]] +say "=========== INITIALIZING GAME ============" -game\run [["ping" := {say "pong"}]] -game\run [[ping]] +"fart" := {say "poot"} +fart -game\run [[say (return "returned value")]] +"fart twice" := { + fart + fart +} -game\run [[do {say "did"}]] +fart twice -game\run [[say 5]] -game\run [[say -5]] +["greet", "say hello"] := { say "Hello!" } +greet -game\def [[fart]], [[say "poot"]] -game\run [[fart]] -game\def [[fart twice]], [[ - say "poot" - say "poot again" -]] -game\run [[fart twice]] +say (return "returned value") +do {say "did"} +say 6 +say -6 +say [1,2,3] -game\def [[sum $items]], (locals)=> - tot = 0 - for x in *locals.items do tot += x - return tot +]] +error("done") game\run "say [1,2,3]" game\run "sum [1,2,3]" game\run "say (sum [1,2,3])" -game\def [[print $x]], [[say $x]] -game\run [[print "printing variables works"]] - game\def [[you]], (_)=> @you game\run [[you]] @@ -82,7 +82,7 @@ class Thunk lingo = [[ actions <- {| (%nl " "*)* ((" "*) action ((%nl " "*)+ action)*)? (%nl " "*)* |} -> Thunk action <- {| token (" "+ token)* " "* |} -> Action - token <- expression / ({(!"[" [^ {}()$])+} -> Word) + token <- expression / ({(!"[" !%nl [^ {}()$])+} -> Word) expression <- number / string / list / variable / thunk / subexpression number <- ('-'? [0-9]+ ("." [0-9]+)?) -> tonumber string <- ('"' {(("\\" .) / [^"])*} '"') -> tostring @@ -151,10 +151,12 @@ def = (game, invocation, action)-> rule = Rule(invocations, action) for invocation in *rule.invocations game.rules[invocation] = rule - print rule + if __DEBUG__ + print rule run = (game, str)-> - print(">> #{str\gsub("\n", "\n.. ")}") + if __DEBUG__ + print(">> #{str\gsub("\n", "\n.. ")}") thunk = lingo\match str unless thunk error("failed to parse nomic:\n#{str}") |
