Cleaned up some and added rule modifying rule.
This commit is contained in:
parent
4c584bd2ed
commit
3dc68105e1
83
game1.moon
83
game1.moon
@ -2,7 +2,7 @@
|
||||
nomic = require 'nomic'
|
||||
game = nomic()
|
||||
|
||||
|
||||
------------------ CORE STUFF ---------------------
|
||||
game\def [[say $str]], (locals)=>
|
||||
with locals
|
||||
print(.str)
|
||||
@ -15,7 +15,43 @@ game\def [[do $thunk]], (locals)=>
|
||||
|
||||
game\def {[[true]], [[yes]]}, (locals)=> true
|
||||
game\def {[[false]], [[no]]}, (locals)=> false
|
||||
game\def {[[nil]], [[None]], [[nop]]}, (locals)=> nil
|
||||
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 {}]]
|
||||
|
||||
|
||||
------------------ BASIC TESTS ---------------------
|
||||
|
||||
game\run [[say "Hello world!"]]
|
||||
game\run [[
|
||||
@ -27,6 +63,9 @@ game\def {[[greet]], [[say hello]]}, [[say "Hello!"]]
|
||||
game\run[[greet]]
|
||||
game\run[[say hello]]
|
||||
|
||||
game\run [["ping" := {say "pong"}]]
|
||||
game\run [[ping]]
|
||||
|
||||
|
||||
game\run [[say (return "returned value")]]
|
||||
|
||||
@ -72,6 +111,7 @@ game\def [[remember that $key $relation $value]], (locals)=>
|
||||
assert .relation, "no relation!!"
|
||||
if not @relations[.relation] then @relations[.relation] = {}
|
||||
@relations[.relation][.key] = .value
|
||||
return nil
|
||||
|
||||
game\def [[remember that $key $relation]], [[remember that $key $relation (true)]]
|
||||
|
||||
@ -79,6 +119,7 @@ game\def [[forget about $key $relation]], (locals)=>
|
||||
with locals
|
||||
if not @relations[.relation] then @relations[.relation] = {}
|
||||
@relations[.relation][.key] = nil
|
||||
return nil
|
||||
|
||||
game\def [[the value of $key $relation]], (locals)=>
|
||||
with locals
|
||||
@ -92,44 +133,6 @@ game\def [[it is true that $key $relation]], [[return ((the value of $key $relat
|
||||
game\run [[remember that "socrates" "is mortal"]]
|
||||
game\run [[say (the value of "socrates" "is mortal")]]
|
||||
|
||||
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
|
||||
|
||||
-- TODO: the rest of the comparisons
|
||||
|
||||
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 [[all keys where $relation is $value]], (locals)=>
|
||||
with locals
|
||||
|
19
nomic.moon
19
nomic.moon
@ -5,12 +5,6 @@ type = moon.type
|
||||
|
||||
export __DEBUG__
|
||||
|
||||
invocation_def = [[
|
||||
name <- {| {chunk} (" " {chunk})* |} -> semicolonify
|
||||
chunk <- ({"$"} %S+) / ({%S+})
|
||||
]]
|
||||
invocation_def = re.compile(invocation_def, {semicolonify: (bits) -> table.concat(bits, ";")})
|
||||
|
||||
|
||||
as_value = (x, globals, locals)->
|
||||
assert (globals and locals), "Shit's fucked"
|
||||
@ -115,9 +109,14 @@ class Rule
|
||||
unless thunk
|
||||
error("failed to parse!")
|
||||
@fn = (globals,locals)-> thunk\run(globals, locals)
|
||||
else
|
||||
elseif type(action) == Thunk
|
||||
@body_str = tostring(action)
|
||||
@fn = (globals,locals)-> action\run(globals, locals)
|
||||
elseif type(action) == 'function'
|
||||
@body_str = "<lua function>"
|
||||
@fn = action
|
||||
else
|
||||
error("Invalid action type: #{type(action)}")
|
||||
|
||||
eq = (x,y)->
|
||||
if #x != #y then return false
|
||||
@ -166,4 +165,8 @@ run = (game, str)->
|
||||
return ret
|
||||
|
||||
return ()->
|
||||
{rules:{}, relations:{}, :run, :def}
|
||||
game = {rules:{}, relations:{}, :run, :def}
|
||||
game\def [[$invocations := $body]], (locals)=>
|
||||
game\def locals.invocations, locals.body
|
||||
return nil
|
||||
return game
|
||||
|
Loading…
Reference in New Issue
Block a user