/* Copyright (c) 2008 Alessandro Warth */ {eval(readFile("Worlds_Library"));WJSParser=BSJSParser.delegated({"isKeyword":function(){var $elf=this,x;return (function(){x=$elf._apply("anything");return $elf._or((function(){return $elf._pred((x == "abort"))}),(function(){return $elf._pred((x == "world"))}),(function(){return $elf._pred((x == "scope"))}),(function(){return BSJSParser._superApplyWithArgs($elf,"isKeyword",x)}))})()},"primExprHd":function(){var $elf=this;return $elf._or((function(){return (function(){$elf._applyWithArgs("token","world");return ["world"]})()}),(function(){return (function(){$elf._applyWithArgs("token","scope");return ["scope"]})()}),(function(){return (function(){$elf._applyWithArgs("token","new");$elf._applyWithArgs("token","world");return ["newWorld"]})()}),(function(){return BSJSParser._superApplyWithArgs($elf,"primExprHd")}))},"stmt":function(){var $elf=this,tb,eb,w,b;return $elf._or((function(){return (function(){$elf._applyWithArgs("token","abort");return ["abort"]})()}),(function(){return (function(){$elf._applyWithArgs("token","try");tb=$elf._apply("block");$elf._applyWithArgs("token","else");eb=$elf._apply("block");return ["wtry",tb,eb]})()}),(function(){return (function(){$elf._applyWithArgs("token","in");w=$elf._apply("expr");b=$elf._apply("block");return ["in",w,b]})()}),(function(){return BSJSParser._superApplyWithArgs($elf,"stmt")}))}});makeFunction=(function (fs,body){return ((((("(function() { var $staticScope = $scope;" + " return function($world, $this) {") + " var $scope = $staticScope.makeChild();") + fs) + body) + "}})()")});WJSTranslator=BSJSTranslator.delegated({"initialize":function(){var $elf=this;return $elf["level"]=(0)},"fargs":function(){var $elf=this,fs;return (function(){$elf._form(function(){return fs=$elf._many(function(){return $elf._apply("anything")})});return (function (){var ss=[];fs.each((function (v,i){ss.push((((("$scope.decl($world, \"" + v) + "\", arguments[") + (i + (2))) + "]);"))}));return ss.join("")})()})()},"trans":function(){var $elf=this,n,v,n,n,v,p,x,p,x,v,f_and_as,m,r,as,x,as,fs,body,tb,te,w,b,xs,xs;return $elf._or((function(){return (function(){$elf._form(function(){return $elf._applyWithArgs("exactly","world")});return "$world"})()}),(function(){return (function(){$elf._form(function(){return $elf._applyWithArgs("exactly","scope")});return "$scope"})()}),(function(){return (function(){$elf._form(function(){return $elf._applyWithArgs("exactly","this")});return "$this"})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","var");n=$elf._apply("anything");return v=$elf._apply("trans")})()});return (((("$scope.decl($world, \"" + n) + "\", ") + v) + ")")})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","get");return n=$elf._apply("anything")})()});return (("$scope.get($world, \"" + n) + "\")")})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","set");$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","get");return n=$elf._apply("anything")})()});return v=$elf._apply("trans")})()});return (((("$scope.set($world, \"" + n) + "\", ") + v) + ")")})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","getp");p=$elf._apply("trans");return x=$elf._apply("trans")})()});return (((("$world.get(" + x) + ", ") + p) + ")")})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","set");$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","getp");p=$elf._apply("trans");return x=$elf._apply("trans")})()});return v=$elf._apply("trans")})()});return (((((("$world.set(" + x) + ", ") + p) + ", ") + v) + ")")})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","call");return f_and_as=$elf._many1(function(){return $elf._apply("trans")})})()});return (("$world.call(null, " + f_and_as.join(",")) + ")")})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","send");m=$elf._apply("anything");r=$elf._apply("trans");return as=$elf._many(function(){return $elf._apply("trans")})})()});return (((((("$world.send(" + r) + ", \"") + m) + "\"") + as.map((function (a){return ("," + a)})).join("")) + ")")})()}),(function(){return (function(){$elf._form(function(){return $elf._applyWithArgs("exactly","newWorld")});return "$world.makeChild()"})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","new");x=$elf._apply("anything");return as=$elf._many(function(){return $elf._apply("trans")})})()});return (((("newAndInitialize($world, $scope.get($world, \"" + x) + "\")") + as.map((function (a){return ("," + a)})).join("")) + ")")})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","func");fs=$elf._apply("fargs");$elf["level"]++;body=$elf._apply("trans");return $elf["level"]--})()});return makeFunction(fs,body)})()}),(function(){return (function(){$elf._form(function(){return $elf._applyWithArgs("exactly","abort")});return "throw abort"})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","wtry");tb=$elf._apply("trans");return te=$elf._apply("trans")})()});return (function (){var r=((((("$world.tryElse(function($world){" + tb) + "; return nothing},") + "function($world){") + te) + "; return nothing})");return ($elf["level"] > (0))?(("if (($_ = " + r) + ")!==nothing) return $_"):r})()})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","in");w=$elf._apply("trans");return b=$elf._apply("trans")})()});return (((("(function($world) {" + b) + "})(") + w) + ")")})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","arr");return xs=$elf._many(function(){return $elf._apply("trans")})})()});return (("[" + xs.join(",")) + "].toWJSArray()")})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","json");return xs=$elf._many(function(){return $elf._apply("trans")})})()});return (("({" + xs.join(",")) + "}).toWJSObject()")})()}),(function(){return (function(){$elf._not(function(){return $elf._form(function(){return (function(){$elf._applyWithArgs("exactly","mset");return $elf._many(function(){return $elf._apply("anything")})})()})});$elf._not(function(){return $elf._form(function(){return (function(){$elf._applyWithArgs("exactly","forIn");return $elf._many(function(){return $elf._apply("anything")})})()})});return BSJSTranslator._superApplyWithArgs($elf,"trans")})()}))}});compileWJS=(function (code){var tree=WJSParser.matchAll(code,"topLevel",undefined,(function (m,i){throw fail.delegated(({"errorPos": i}))}));return WJSTranslator.match(tree,"trans")});$scope.decl($world,"eval",(function ($world,$this,s){return eval(compileWJS(s))}));$scope.decl($world,"prompt",(function ($world,$this,s){return prompt(s)}));$scope.decl($world,"confirm",(function ($world,$this,s){return confirm(s)}));translateCode=compileWJS}