From 53e08fd34b6a625664c8c5bac7a20aae0bdccf02 Mon Sep 17 00:00:00 2001 From: Jakub Jankiewicz Date: Wed, 1 May 2019 12:31:43 +0200 Subject: [PATCH] fix c[ad]+r when access nil + typececk --- CHANGELOG.md | 2 ++ dist/lips.js | 26 +++++++++++++++++++++++++- dist/lips.min.js | 4 ++-- src/lips.js | 16 ++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b2c6f48..d5e11344 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ * remove `nop` (you can use `(begin undefined)`) ### Bug fixes * fix unquote-splicing on nil (e.g. processing of `` `(list ,@nil)``) +* cdr and car should return nil on empty list +* typecheck and process nil in c[ad]+r functions ## 0.11.1 ### Bug fixes diff --git a/dist/lips.js b/dist/lips.js index 933ba85f..750c50b5 100644 --- a/dist/lips.js +++ b/dist/lips.js @@ -21,7 +21,7 @@ * http://javascript.nwbox.com/ContentLoaded/ * http://javascript.nwbox.com/ContentLoaded/MIT-LICENSE * - * build: Wed, 01 May 2019 10:23:31 +0000 + * build: Wed, 01 May 2019 10:31:05 +0000 */ (function () { 'use strict'; @@ -3381,12 +3381,30 @@ function _typeof(obj) { }, "(cons left right)\n\n Function return new Pair out of two arguments."), // ------------------------------------------------------------------ car: doc(function (list) { + if (list === nil) { + return nil; + } + typecheck('car', list, 'pair'); + + if (isEmptyList(list)) { + return nil; + } + return list.car; }, "(car pair)\n\n Function returns car (head) of the list/pair."), // ------------------------------------------------------------------ cdr: doc(function (list) { + if (list === nil) { + return nil; + } + typecheck('cdr', list, 'pair'); + + if (isEmptyList(list)) { + return nil; + } + return list.cdr; }, "(cdr pair)\n\n Function returns cdr (tail) of the list/pair."), // ------------------------------------------------------------------ @@ -4816,6 +4834,12 @@ function _typeof(obj) { var name = 'c' + spec + 'r'; global_env.set(name, doc(function (arg) { return chars.reduce(function (list, type) { + if (list === nil) { + return nil; + } + + typecheck(name, list, 'pair'); + if (type === 'a') { return list.car; } else { diff --git a/dist/lips.min.js b/dist/lips.min.js index fb9fe137..bba062bc 100644 --- a/dist/lips.min.js +++ b/dist/lips.min.js @@ -21,6 +21,6 @@ * http://javascript.nwbox.com/ContentLoaded/ * http://javascript.nwbox.com/ContentLoaded/MIT-LICENSE * - * build: Wed, 01 May 2019 10:23:31 +0000 + * build: Wed, 01 May 2019 10:31:05 +0000 */ -(function(){"use strict";function t(n,e){t=Object.setPrototypeOf||function n(e,t){e.__proto__=t;return e};return t(n,e)}function r(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Date.prototype.toString.call(Reflect.construct(Date,[],function(){}));return true}catch(n){return false}}function Pn(n,e,t){if(r()){Pn=Reflect.construct}else{Pn=function n(e,t,r){var i=[null];i.push.apply(i,t);var a=Function.bind.apply(e,i);var o=new a;if(r)setPrototypeOf(o,r.prototype);return o}}return Pn.apply(null,arguments)}function n(n,e){return e={exports:{}},n(e,e.exports),e.exports}var e=n(function(n){var e=function(a){var n=Object.prototype;var s=n.hasOwnProperty;var c;var e=typeof Symbol==="function"?Symbol:{};var i=e.iterator||"@@iterator";var t=e.asyncIterator||"@@asyncIterator";var r=e.toStringTag||"@@toStringTag";function o(n,e,t,r){var i=e&&e.prototype instanceof u?e:u;var a=Object.create(i.prototype);var o=new A(r||[]);a._invoke=j(n,t,o);return a}a.wrap=o;function f(n,e,t){try{return{type:"normal",arg:n.call(e,t)}}catch(n){return{type:"throw",arg:n}}}var l="suspendedStart";var h="suspendedYield";var p="executing";var d="completed";var v={};function u(){}function y(){}function m(){}var g={};g[i]=function(){return this};var b=Object.getPrototypeOf;var w=b&&b(b(q([])));if(w&&w!==n&&s.call(w,i)){g=w}var _=m.prototype=u.prototype=Object.create(g);y.prototype=_.constructor=m;m.constructor=y;m[r]=y.displayName="GeneratorFunction";function x(n){["next","throw","return"].forEach(function(e){n[e]=function(n){return this._invoke(e,n)}})}a.isGeneratorFunction=function(n){var e=typeof n==="function"&&n.constructor;return e?e===y||(e.displayName||e.name)==="GeneratorFunction":false};a.mark=function(n){if(Object.setPrototypeOf){Object.setPrototypeOf(n,m)}else{n.__proto__=m;if(!(r in n)){n[r]="GeneratorFunction"}}n.prototype=Object.create(_);return n};a.awrap=function(n){return{__await:n}};function k(u){function c(n,e,t,r){var i=f(u[n],u,e);if(i.type==="throw"){r(i.arg)}else{var a=i.arg;var o=a.value;if(o&&typeof o==="object"&&s.call(o,"__await")){return Promise.resolve(o.__await).then(function(n){c("next",n,t,r)},function(n){c("throw",n,t,r)})}return Promise.resolve(o).then(function(n){a.value=n;t(a)},function(n){return c("throw",n,t,r)})}}var e;function n(t,r){function n(){return new Promise(function(n,e){c(t,r,n,e)})}return e=e?e.then(n,n):n()}this._invoke=n}x(k.prototype);k.prototype[t]=function(){return this};a.AsyncIterator=k;a.async=function(n,e,t,r){var i=new k(o(n,e,t,r));return a.isGeneratorFunction(e)?i:i.next().then(function(n){return n.done?n.value:i.next()})};function j(o,u,c){var s=l;return function n(e,t){if(s===p){throw new Error("Generator is already running")}if(s===d){if(e==="throw"){throw t}return N()}c.method=e;c.arg=t;while(true){var r=c.delegate;if(r){var i=S(r,c);if(i){if(i===v)continue;return i}}if(c.method==="next"){c.sent=c._sent=c.arg}else if(c.method==="throw"){if(s===l){s=d;throw c.arg}c.dispatchException(c.arg)}else if(c.method==="return"){c.abrupt("return",c.arg)}s=p;var a=f(o,u,c);if(a.type==="normal"){s=c.done?d:h;if(a.arg===v){continue}return{value:a.arg,done:c.done}}else if(a.type==="throw"){s=d;c.method="throw";c.arg=a.arg}}}}function S(n,e){var t=n.iterator[e.method];if(t===c){e.delegate=null;if(e.method==="throw"){if(n.iterator["return"]){e.method="return";e.arg=c;S(n,e);if(e.method==="throw"){return v}}e.method="throw";e.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var r=f(t,n.iterator,e.arg);if(r.type==="throw"){e.method="throw";e.arg=r.arg;e.delegate=null;return v}var i=r.arg;if(!i){e.method="throw";e.arg=new TypeError("iterator result is not an object");e.delegate=null;return v}if(i.done){e[n.resultName]=i.value;e.next=n.nextLoc;if(e.method!=="return"){e.method="next";e.arg=c}}else{return i}e.delegate=null;return v}x(_);_[r]="Generator";_[i]=function(){return this};_.toString=function(){return"[object Generator]"};function F(n){var e={tryLoc:n[0]};if(1 in n){e.catchLoc=n[1]}if(2 in n){e.finallyLoc=n[2];e.afterLoc=n[3]}this.tryEntries.push(e)}function E(n){var e=n.completion||{};e.type="normal";delete e.arg;n.completion=e}function A(n){this.tryEntries=[{tryLoc:"root"}];n.forEach(F,this);this.reset(true)}a.keys=function(t){var r=[];for(var n in t){r.push(n)}r.reverse();return function n(){while(r.length){var e=r.pop();if(e in t){n.value=e;n.done=false;return n}}n.done=true;return n}};function q(e){if(e){var n=e[i];if(n){return n.call(e)}if(typeof e.next==="function"){return e}if(!isNaN(e.length)){var t=-1,r=function n(){while(++t=0;--e){var i=this.tryEntries[e];var a=i.completion;if(i.tryLoc==="root"){return n("end")}if(i.tryLoc<=this.prev){var o=s.call(i,"catchLoc");var u=s.call(i,"finallyLoc");if(o&&u){if(this.prev=0;--t){var r=this.tryEntries[t];if(r.tryLoc<=this.prev&&s.call(r,"finallyLoc")&&this.prev=0;--e){var t=this.tryEntries[e];if(t.finallyLoc===n){this.complete(t.completion,t.afterLoc);E(t);return v}}},catch:function(n){for(var e=this.tryEntries.length-1;e>=0;--e){var t=this.tryEntries[e];if(t.tryLoc===n){var r=t.completion;if(r.type==="throw"){var i=r.arg;E(t)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(n,e,t){this.delegate={iterator:q(n),resultName:e,nextLoc:t};if(this.method==="next"){this.arg=c}return v}};return a}(n.exports);try{regeneratorRuntime=e}catch(n){Function("r","regeneratorRuntime = r")(e)}});var Mn=e;function c(n,e,t,r,i,a,o){try{var u=n[a](o);var c=u.value}catch(n){t(n);return}if(u.done){e(c)}else{Promise.resolve(c).then(r,i)}}function Bn(u){return function(){var n=this,o=arguments;return new Promise(function(e,t){var r=u.apply(n,o);function i(n){c(r,e,t,i,a,"next",n)}function a(n){c(r,e,t,i,a,"throw",n)}i(undefined)})}}function i(n){if(Array.isArray(n)){for(var e=0,t=new Array(n.length);e1&&arguments[1]!==g?arguments[1]:1;return n[n.length-e]}function o(n){if(typeof n==="string"){var e=/([-\\^$[\]()+{}?*.|])/g;return n.replace(e,"\\$1")}}function u(n){var i=e();n=n.replace(/\n\r|\r/g,"\n");var a=0;var o=0;var u=[];var c=[];var s=0;n.split(f).filter(Boolean).forEach(function(n){if(n.match(f)){s=0;if(c.length){var e=h(c);if(e.token.match(/\n/)){var t=e.token.split("\n").pop();s+=t.length}else{s+=e.token.length}s+=e.col}var r={col:s,line:o,token:n,offset:a};u.push(r);c.push(r);a+=n.length;s+=n.length;o+=(n.match("\n")||[]).length;return}n.split(i).filter(Boolean).forEach(function(n){var e={col:s,line:o,token:n,offset:a};s+=n.length;a+=n.length;u.push(e);c.push(e);if(n==="\n"){++o;c=[];s=0}})});return u}function b(n,e){if(e){return u(n)}else{return u(n).map(function(n){return n.token.trim()}).filter(function(n){return n&&!n.match(/^;/)})}}var m={"'":new L("quote"),"`":new L("quasiquote"),",@":new L("unquote-splicing"),",":new L("unquote")};function p(n){if(typeof n==="string"){throw new Error("parse require tokenized array of tokens not string")}var a=[];var o=[];var u=null;var c=Object.keys(m);var s=c.map(function(n){return m[n].name});var f=0;var l=false;var h=[];var p=[];var d=0;function v(){var n=a[a.length-1];if(n instanceof Array&&n[0]instanceof L&&s.includes(n[0].name)&&a.length>1&&!n[0].literal){a.pop();if(a[a.length-1].length===1&&a[a.length-1][0]instanceof L){a[a.length-1].push(n)}else if(a[a.length-1].length===0){a[a.length-1]=n}else if(a[a.length-1]instanceof M){if(a[a.length-1].cdr instanceof M){a[a.length-1]=new M(a[a.length-1],M.fromArray(n))}else{a[a.length-1].cdr=M.fromArray(n)}}else{a[a.length-1].push(n)}}}n.forEach(function(n){var e=a[a.length-1];if(c.indexOf(n)!==-1){d++;u=n;a.push([m[u]]);if(!u){p=[]}p.push(u)}else{if(u){h.push(p);p=[]}if(n==="("){l=true;f++;a.push([]);u=null;d=0}else if(n==="."&&!l){a[a.length-1]=M.fromArray(e)}else if(n===")"){f--;if(!a.length){throw new Error("Unbalanced parenthesis")}if(a.length===1){o.push(a.pop())}else if(a.length>1){var t=a.pop();e=a[a.length-1];if(e instanceof Array){e.push(t)}else if(e instanceof M){e.append(M.fromArray(t))}if(h.length){p=h.pop();while(p.length){v();p.pop()}}else{v()}}if(f===0&&a.length){o.push(a.pop())}}else{l=false;var r=y(n);if(u){while(d--){a[a.length-1][1]=r;r=a.pop()}d=0;u=false}else if(r instanceof L&&s.includes(r.name)){r.literal=true}e=a[a.length-1];if(e instanceof M){var i=e;while(true){if(i.cdr===P){if(r instanceof Array){i.cdr=M.fromArray(r)}else{i.cdr=r}break}else{i=i.cdr}}}else if(!a.length){o.push(r)}else{e.push(r)}}}});if(a.length){throw new Error("Unbalanced parenthesis 2")}return o.map(function(n){if(n instanceof Array){return M.fromArray(n)}return n})}function d(n){var e=arguments.length>1&&arguments[1]!==g?arguments[1]:function(n){return n};var t=arguments.length>2&&arguments[2]!==g?arguments[2]:null;if(Y(n)){var r=n.then(e);if(t===null){return r}else{return r.catch(t)}}return e(n)}function s(n,e){if(e instanceof RegExp){return function(n){return String(n).match(e)}}else if(typeof e!=="function"){throw new Error("".concat(n," argument need to be a function or RegExp"))}else{return e}}function v(n,e,t){if(e){if(t){n.__doc__=e}else{n.__doc__=w(e)}}return n}function w(n){return n.split("\n").map(function(n){return n.trim()}).join("\n")}function _(n){var e=arguments.length>1&&arguments[1]!==g?arguments[1]:1;var t=n.length;if(e<=0){throw Error("previousSexp: Invlaid argument sexp = ".concat(e))}n:while(e--&&t>=0){var r=1;while(r>0){var i=n[--t];if(!i){break n}if(i==="("||i.token==="("){r--}else if(i===")"||i.token===")"){r++}}t--}return n.slice(t+1)}function x(n){if(!n||!n.length){return 0}var e=n.length;if(n[e-1].token==="\n"){return 0}while(--e){if(n[e].token==="\n"){var t=(n[e+1]||{}).token;if(t){return t.length}}}return 0}function k(n,e){return u(n,e)===e.length;function u(n,e){var t=0;var r={};for(var i=0;i0){continue}}}else if(n[t]instanceof Array){var o=u(n[t],e.slice(i));if(o===-1||o+i>e.length){return-1}i+=o-1;t++;continue}else{return-1}t++}if(n.length!==t){return-1}return e.length}}function j(n){this._code=n.replace(/\r/g,"")}j.defaults={offset:0,indent:2,specials:["define","lambda","let","let*","define-macro"]};j.match=k;j.prototype._options=function n(e){var t=j.defaults;if(typeof e==="undefined"){return Object.assign({},t)}var r=e&&e.specials||[];return Object.assign({},t,e,{specials:t.specials.concat(r)})};j.prototype.indent=function n(e){var t=b(this._code,true);return this._indent(t,e)};j.prototype._indent=function n(e,t){var r=this._options(t);var i=x(e);var a=r.specials;var o=_(e);if(o){if(o[0].line>0){r.offset=0}if(o.length===1){return r.offset+o[0].col+1}else if(a.indexOf(o[1].token)!==-1){return r.offset+o[0].col+r.indent}else if(o[0].line3&&o[1].line===o[3].line){if(o[1].token==="("){return r.offset+o[1].col}return r.offset+o[3].col}else if(o[0].line===o[1].line){return r.offset+r.indent+o[0].col}else{var u=o.slice(2);for(var c=0;c"}else if(typeof n==="string"){return JSON.stringify(n)}else if(Y(n)){return"<#Promise>"}else if(n instanceof L||n instanceof ln||n instanceof M||n===P){return n.toString()}else if(n instanceof Array){return n.map(C)}else if(Tn(n)==="object"){if(n===null){return"null"}var e=n.constructor.name;if(e==="Object"){return JSON.stringify(n)}return"<#object("+n.constructor.name+")>"}else if(typeof n!=="undefined"){return n}}M.prototype.markCycles=function(){R(this);return this};M.prototype.haveCycles=function(){var n=arguments.length>0&&arguments[0]!==g?arguments[0]:null;if(!n){return this.haveCycles("car")||this.haveCycles("cdr")}return!!(this.cycles&&this.cycles[n])};function R(n){var i=[];var e=[];function a(n){if(n instanceof M){if(i.includes(n)){if(!e.includes(n)){e.push(n)}return"#".concat(e.length-1,"#")}}}function o(n){if(n instanceof M){i.push(n);var e={};var t=a(n.car);var r=a(n.cdr);if(t){e["car"]=t}else{o(n.car)}if(r){e["cdr"]=r}else{o(n.cdr)}if(t||r){n.cycles=e}else if(n.cycles){delete n.cycles}}}o(n)}M.prototype.toString=function(){var n=["("];if(this.car!==g){var e;if(this.cycles&&this.cycles.car){e=this.cycles.car}else{e=C(this.car)}if(e){n.push(e)}if(this.cdr instanceof M){if(this.cycles&&this.cycles.cdr){n.push(" . ");n.push(this.cycles.cdr)}else{var t;if(this.cycles&&this.cycles.cdr){t=this.cycles.cdr}var r=this.cdr.toString(t).replace(/^\(|\)$/g,"");n.push(" ");n.push(r)}}else if(typeof this.cdr!=="undefined"&&this.cdr!==P){if(typeof this.cdr==="string"){n=n.concat([" . ",JSON.stringify(this.cdr)])}else{n=n.concat([" . ",C(this.cdr)])}}}n.push(")");return n.join("")};M.prototype.set=function(n,e){this[n]=e;if(e instanceof M){this.markCycles()}};M.prototype.append=function(n){if(n instanceof Array){return this.append(M.fromArray(n))}var e=this;if(e.car===g){if(n instanceof M){this.car=n.car;this.cdr=n.cdr}else{this.car=n}}else{while(true){if(e instanceof M&&e.cdr!==P){e=e.cdr}else{break}}if(n instanceof M){e.cdr=n}else if(n!==P){e.cdr=new M(n,P)}}return this};function T(n,e){if(n instanceof ln&&e instanceof ln){return n.cmp(e)===0}else if(typeof n==="number"||typeof e==="number"){return ln(n).cmp(ln(e))}else if(n instanceof L&&e instanceof L){return n.name===e.name}else{return n===e}}function H(n){return n instanceof M&&n.isEmptyList()||n===P}function J(n,e,t){if(typeof this!=="undefined"&&this.constructor!==J||typeof this==="undefined"){return new J(n,e)}_n("Macro",n,"string",1);_n("Macro",e,"function",2);this.__doc__=t;this.name=n;this.fn=e}J.defmacro=function(n,e,t){var r=new J(n,e,t);r.defmacro=true;return r};J.prototype.invoke=function(n,e,t){var r=e.env,i=e.dynamic_scope,a=e.error;var o={dynamic_scope:i,error:a,macro_expand:t};var u=this.fn.call(r,n,o,this.name);return t?dn(u):u};J.prototype.toString=function(){return"#"};var $="define-macro";function D(a){return function(){var t=Bn(Mn.mark(function n(t,c){var s,f,r,i;return Mn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:r=function n(){r=Bn(Mn.mark(function n(t){var r,i,a,o,u;return Mn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:if(!(t instanceof M&&t.car instanceof L)){e.next=13;break}e.prev=1;r=s.get(t.car);if(!(r instanceof J&&r.defmacro)){e.next=9;break}e.next=6;return r.invoke(t.cdr,c,true);case 6:i=e.sent;if(!(i instanceof M)){e.next=9;break}return e.abrupt("return",i);case 9:e.next=13;break;case 11:e.prev=11;e.t0=e["catch"](1);case 13:a=t.car;if(!(a instanceof M)){e.next=18;break}e.next=17;return f(a);case 17:a=e.sent;case 18:o=t.cdr;if(!(o instanceof M)){e.next=23;break}e.next=22;return f(o);case 22:o=e.sent;case 23:u=new M(a,o);return e.abrupt("return",u);case 25:case"end":return e.stop()}}},n,null,[[1,11]])}));return r.apply(this,arguments)};f=function n(e){return r.apply(this,arguments)};s=c["env"]=this;i=t;if(!a){e.next=12;break}e.t0=dn;e.next=8;return f(t);case 8:e.t1=e.sent.car;return e.abrupt("return",(0,e.t0)(e.t1));case 12:e.next=15;return f(t);case 15:i=e.sent;if(!(t.toString()===i.toString())){e.next=18;break}return e.abrupt("break",21);case 18:t=i;e.next=12;break;case 21:return e.abrupt("return",dn(i.car));case 22:case"end":return e.stop()}}},n,this)}));return function(n,e){return t.apply(this,arguments)}}()}function G(n){return typeof n==="undefined"||n===P||n===null}function Y(n){return n instanceof Promise||n&&typeof n!=="undefined"&&typeof n.then==="function"}function U(n,a){var o=function n(){for(var e=arguments.length,t=new Array(e),r=0;r2?e-2:0),r=2;r1?t-1:0),i=1;i1?e-1:0),r=1;rarray")(a.car);var f=s.inherit("let");var l=0;return function n(){var e=c[l++];function t(n){if(Y(n)){return n.then(t)}else if(typeof n==="undefined"){f.set(e.car,P)}else{f.set(e.car,n)}}if(o){o=h?f:s}if(!e){var r=new M(new L("begin"),a.cdr);return Fn(r,{env:f,dynamic_scope:o,error:u})}else{var i=Fn(e.cdr.car,{env:h?f:s,dynamic_scope:o,error:u});return d(t(i),n)}}()})}function Z(n){for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;r2?r-2:0),a=2;a1?n-1:0),t=1;t=o){return a.apply(this,r)}else{return i}}return i.apply(this,arguments)}}function sn(r,i){_n("limit",i,"function",2);return function(){for(var n=arguments.length,e=new Array(n),t=0;t1?e-1:0),r=1;r>":function n(e,t){return e>>t},"<<":function n(e,t){return e<>":"ishln"};n=i[n];return ln(this.value.clone()[n](e,r))}};var hn={"+":"add","-":"sub","*":"mul","/":"div","%":"mod","|":"or","&":"and","~":"neg","<<":"shl",">>":"shr"};Object.keys(hn).forEach(function(e){ln.prototype[hn[e]]=function(n){return this.op(e,n)}});ln.prototype.sqrt=function(){var n;if(ln.isNative(this.value)){n=Math.sqrt(this.value)}else if(ln.isBN(this.value)){n=this.value.sqrt()}return new ln(n)};ln.prototype.pow=function(n){n=this.coerce(n);if(ln.isNative(this.value)){try{var e=new Function("a,b","return a**b;");n.value=e(this.value,n.value)}catch(n){throw new Error("Power operator not supported")}}else if(ln.isBN(this.value)){n.value=this.value.pow(n.value)}else{n.value=Math.pow(this.value,n.value)}return n};ln.prototype.neg=function(){var n=this.value;if(ln.isNative(n)){n=-n}else if(ln.isBN(n)){n=n.neg()}return new ln(n)};ln.prototype.abs=function(){var n=this.value;if(ln.isNative(this.value)){if(n<0){n=-n}}else if(ln.isBN(n)){n.iabs()}return new ln(n)};ln.prototype.isOdd=function(){if(ln.isNative(this.value)){if(this.isBigNumber()){return this.value%BigInt(2)===BigInt(1)}return this.value%2===1}else if(ln.isBN(this.value)){return this.value.isOdd()}};ln.prototype.isEven=function(){return!this.isOdd()};ln.prototype.cmp=function(n){n=this.coerce(n);if(ln.isNative(this.value)){if(this.value1&&arguments[1]!==g?arguments[1]:{};if(Tn(n)==="object"){e=n}if(!n||Tn(n)==="object"){n="child of "+(this.name||"unknown")}return new pn(e||{},this,n)};pn.prototype.get=function(n,e,t){t=t||this;var r;var i=false;if(n instanceof L){if(n.name in this.env){r=this.env[n.name];i=true}}else if(typeof n==="string"){if(typeof this.env[n]!=="undefined"){r=this.env[n];i=true}}if(i){if(ln.isNumber(r)){return ln(r)}if(r instanceof M){return r.markCycles()}if(typeof r==="function"){if(!r.name.match(/^bound /)){if(e){return U(r,t)}return V(r,t)}}return r}if(this.parent instanceof pn){return this.parent.get(n,e,t)}else{var a;if(n instanceof L){a=n.name}else if(typeof n==="string"){a=n}if(a){var o=Tn(c[a]);if(o==="function"){if(K(c[a])){return V(c[a],c)}return c[a]}else if(o!=="undefined"){return c[a]}}}a=(a.name||a).toString();throw new Error("Unbound variable `"+a+"'")};pn.prototype.set=function(n,e){if(ln.isNumber(e)){e=ln(e)}if(n instanceof L){n=n.name}this.env[n]=e};pn.prototype.has=function(n){return typeof this.env[n]!=="undefined"};pn.prototype.ref=function(n){var e=this;while(true){if(!e){break}if(e.has(n)){return e}e=e.parent}};function dn(n){if(Y(n)){return n.then(dn)}if(n instanceof M||n instanceof L){n.data=true}return n}function vn(n,e){this.value=n;this.count=e}vn.prototype.toString=function(){return"<#unquote["+this.count+"] "+this.value+">"};var yn=function(){var e=0;return function(){var n=arguments.length>0&&arguments[0]!==g?arguments[0]:null;if(n!==null){return new L(c.Symbol("#".concat(n)))}e++;return new L(c.Symbol("#gensym_".concat(e,"#")))}}();var mn=new pn({nil:P,undefined:g,true:true,NaN:NaN,false:false,stdout:{write:function n(){var e;(e=console).log.apply(e,arguments)}},stdin:{read:function n(){return new Promise(function(n){n(prompt(""))})}},help:v(function(n){return n.__doc__},"(help object)\n\n Function returns documentation for function or macro."),cons:v(function(n,e){if(H(e)){e=P}return new M(n,e)},"(cons left right)\n\n Function return new Pair out of two arguments."),car:v(function(n){_n("car",n,"pair");return n.car},"(car pair)\n\n Function returns car (head) of the list/pair."),cdr:v(function(n){_n("cdr",n,"pair");return n.cdr},"(cdr pair)\n\n Function returns cdr (tail) of the list/pair."),"set!":v(new J("set!",function(e){var n=arguments.length>1&&arguments[1]!==g?arguments[1]:{},t=n.dynamic_scope,r=n.error;if(t){t=this}var i=Fn(e.cdr.car,{env:this,dynamic_scope:t,error:r});i=kn(i);var a;function o(e,t){if(Y(e)){return e.then(function(n){return o(n,t)})}if(Y(t)){return t.then(function(n){return o(e,n)})}s[e]=t;return t}if(e.car instanceof M&&L.is(e.car.car,".")){var u=e.car.cdr.car;var c=e.car.cdr.cdr.car;var s=Fn(u,{env:this,dynamic_scope:t,error:r});var f=Fn(c,{env:this,dynamic_scope:t,error:r});return o(f,i)}if(!(e.car instanceof L)){throw new Error("set! first argument need to be a symbol or "+"dot accessor that evaluate to object.")}a=this.ref(e.car.name);if(!a){a=this}return d(i,function(n){a.set(e.car,n)})}),"(set! name value)\n\n Macro that can be used to set the value of the variable (mutate)\n it search the scope chain until it finds first non emtpy slot and set it."),"set-car!":v(function(n,e){_n("set-car!",n,"pair");n.car=e},"(set-car! obj value)\n\n Function that set car (head) of the list/pair to specified value.\n It can destroy the list. Old value is lost."),"set-cdr!":v(function(n,e){_n("set-cdr!",n,"pair");n.cdr=e},"(set-cdr! obj value)\n\n Function that set cdr (tail) of the list/pair to specified value.\n It can destroy the list. Old value is lost."),"empty?":v(function(n){return typeof n==="undefined"||H(n)},"(empty? object)\n\n Function return true if value is undfined empty list."),assoc:v(function(n,e){if(n instanceof M&&!(e instanceof M)){throw new Error("First argument to assoc ned to be a key")}var t=e;while(true){if(!(t instanceof M)||this.get("empty?")(t)){break}var r=t.car.car;if(T(r,n)){return t.car}else if(!t.haveCycles("cdr")){t=t.cdr}}return P},"(assoc key alist)\n\n Function search Alist (list of pairs) until it find the one that\n have head set equal to key, and return found pair."),gensym:v(yn,"(gensym)\n\n Function generate unique symbol, to use with macros as meta name."),load:v(function(n){_n("load",n,"string");return c.fetch(n).then(function(n){return n.text()}).then(En)},"(load filename)\n\n Function fetch the file and evaluate its content as LIPS code."),while:v(new J("while",function(r,n){var i=n.dynamic_scope,a=n.error;var o=this;var u=new M(new L("begin"),r.cdr);var c;if(i){i=o}return function e(){var n=Fn(r.car,{env:o,dynamic_scope:i,error:a});function t(n){if(n&&!G(n)&&!H(n)){c=Fn(u,{env:o,dynamic_scope:i,error:a});if(Y(c)){return c.then(function(n){c=n;return e()})}else{return e()}}else{return c}}return d(n,t)}()}),"(while cond . body)\n\n Macro that create a loop, it exectue body untill cond expression is false"),if:v(new J("if",function(t,n){var r=n.dynamic_scope,i=n.error;if(r){r=this}var a=this;var e=function n(e){_n("if",e,"boolean");if(e){return Fn(t.cdr.car,{env:a,dynamic_scope:r,error:i})}else{return Fn(t.cdr.cdr.car,{env:a,dynamic_scope:r,error:i})}};var o=Fn(t.car,{env:a,dynamic_scope:r,error:i});return d(o,e)}),"(if cond true-expr false-expr)\n\n Macro evaluate condition expression and if the value is true, it\n evaluate and return true expression if not it evaluate and return\n false expression"),"let*":v(W(true),"(let* ((a value-a) (b value-b)) body)\n\n Macro that creates new environment, then evaluate and assign values to\n names and then evaluate the body in context of that environment.\n Values are evaluated sequentialy and next value can access to\n previous values/names."),let:v(W(false),"(let ((a value-a) (b value-b)) body)\n\n Macro that creates new environment, then evaluate and assign values to\n names and then evaluate the body in context of that environment.\n Values are evaluated sequentialy but you can't access\n previous values/names when next are evaluated. You can only get them\n from body of let expression."),begin:v(new J("begin",function(n,e){var r=e.dynamic_scope,i=e.error;var a=this.get("list->array")(n);if(r){r=this}var o=this;var u;return function e(){if(a.length){var n=a.shift();var t=Fn(n,{env:o,dynamic_scope:r,error:i});return d(t,function(n){u=n;return e()})}else{return u}}()}),"(begin . args)\n\n Macro runs list of expression and return valuate of the list one.\n It can be used in place where you can only have single exression,\n like if expression."),ignore:new J("ignore",function(n,e){var t=e.dynamic_scope,r=e.error;var i={env:this,error:r};if(t){i.dynamic_scope=this}Fn(n,i)},"(ignore expression)\n\n Macro that will evaluate expression and swallow any promises that may\n be created. It wil run and ignore any value that may be returned by\n expression. The code should have side effects and/or when it's promise\n it should resolve to undefined."),timer:v(new J("timer",function(e){var n=arguments.length>1&&arguments[1]!==g?arguments[1]:{},t=n.dynamic_scope,r=n.error;_n("timer",e.car,"number");var i=this;if(t){t=this}return new Promise(function(n){setTimeout(function(){n(Fn(e.cdr.car,{env:i,dynamic_scope:t,error:r}))},e.car)}).then(dn)}),"(timer time expression)\n\n Function return a promise, and it will be automatically evaluated\n after specific time passes. The return value of the function\n will be value of the timer exprssion. If you want to do side effect\n only expression you can wrap your expression in nol call."),define:v(J.defmacro("define",function(e,n){var t=this;if(e.car instanceof M&&e.car.car instanceof L){var r=new M(new L("define"),new M(e.car.car,new M(new M(new L("lambda"),new M(e.car.cdr,e.cdr)))));return r}else if(n.macro_expand){return}if(n.dynamic_scope){n.dynamic_scope=this}n.env=t;var i=e.cdr.car;if(i instanceof M){i=Fn(i,n)}else if(i instanceof L){i=t.get(i)}if(e.car instanceof L){d(i,function(n){t.set(e.car,n)})}}),"(define name expression)\n (define (function-name . args) body)\n\n Macro for defining values. It can be used to define variables,\n or function. If first argument is list it will create function\n with name beeing first element of the list. The macro evalute\n code `(define function (lambda args body))`"),"set-obj!":v(function(n,e,t){if(Tn(n)!=="object"||G(n)){throw new Error(wn("set-obj!",xn(n),"object"))}n[e]=t},"(set-obj! obj key value)\n\n Function set property of JavaScript object"),"current-environment":v(function(){return this},"(current-environment)\n\n Function return current environement."),eval:v(function(n,t){var r=this;t=t||this;if(n instanceof L){return t.get(n)}if(n instanceof M){return Fn(n,{env:t,dynamic_scope:this,error:function n(e){return r.get("print")(e.message)}})}if(n instanceof Array){var i=this.get("eval");return n.reduce(function(n,e){return i(e,t)})}},"(eval list)\n\n Function evalute LIPS code as list structure."),lambda:new J("lambda",function(s){var n=arguments.length>1&&arguments[1]!==g?arguments[1]:{},f=n.dynamic_scope,l=n.error;var h=this;var p;if(s.cdr instanceof M&&typeof s.cdr.car==="string"&&s.cdr.cdr!==P){p=s.cdr.car}function e(){var n=(f?this:h).inherit("lambda");var e=s.car;var t=0;var r;if(e instanceof L||!H(e)){for(var i=arguments.length,a=new Array(i),o=0;o2&&arguments[2]!==g?arguments[2]:a;if(n instanceof M&&!H(n)){var r=n.car;var i=n.cdr;if(t(r)){r=e(r)}if(t(i)){i=e(i)}if(Y(r)||Y(i)){return Promise.all([r,i]).then(function(n){var e=Rn(n,2),t=e[0],r=e[1];return new M(t,r)})}else{return new M(r,i)}}return n}function l(n,e){if(n instanceof M){if(H(n)&&e===P){return P}n.append(e)}else{n=new M(n,e)}return n}function h(t){if(t instanceof M&&!H(t)){var n;if(L.is(t.car.car,"unquote-splicing")){n=Fn(t.car.cdr.car,{env:c,dynamic_scope:o,error:u});return d(n,function(e){if(!e instanceof M){throw new Error("Value of unquote-splicing need"+" to be pair")}var n=h(t.cdr);if(n===P&&e===P){return g}return d(n,function(n){return l(e,n)})})}if(L.is(t.car,"unquote")){var e=t.cdr;var r=e;var i=r;var a=1;while(L.is(r.car.car,"unquote")){i=r;a++;r=r.car.cdr.car}if(a>s){s=a}if(i===r){if(t.cdr.cdr!==P){return d(h(t.cdr.cdr),function(n){return new M(new vn(t.cdr.car,a),n)})}else{return new vn(t.cdr.car,a)}}else if(i.cdr.cdr!==P){return d(h(i.cdr.cdr),function(n){i.car.cdr=new M(new vn(r,a),i.cdr===P?P:n);return e.car})}else{i.car.cdr=new vn(r,a)}return e.car}return f(t,h)}return t}var t=function n(e){return a(e)||e instanceof vn};function r(n){if(n instanceof vn){if(s===n.count){return Fn(n.value,{env:c,dynamic_scope:o,error:u})}else{return d(r(n.value),function(n){return new M(new L("unquote"),new M(n,P))})}}return f(n,r,t)}return d(h(n.car),function(n){return d(r(n),dn)})}),"(quasiquote list ,value ,@value)\n\n Similar macro to `quote` but inside it you can use special\n expressions unquote abbreviated to , that will evaluate expresion inside\n and return its value or unquote-splicing abbreviated to ,@ that will\n evaluate expression but return value without parenthesis (it will join)\n the list with its value. Best used with macros but it can be used outside"),clone:v(function(n){return n.clone()},"(clone list)\n\n Function return clone of the list."),append:v(function(n,e){return this.get("append!")(n.clone(),e)},"(append list item)\n\n Function will create new list with value appended to the end. It return\n New list."),"append!":v(function(n,e){if(G(e)||H(e)){return n}return n.append(e)},"(append! name expression)\n\n Destructive version of append, it modify the list in place. It return\n original list."),reverse:v(function(n){if(n instanceof M){var e=this.get("list->array")(n).reverse();return this.get("array->list")(e)}else if(!(n instanceof Array)){throw new Error(wn("reverse",xn(n),"array or pair"))}else{return n.reverse()}},"(reverse list)\n\n Function will reverse the list or array. If value is not a list\n or array it will throw exception."),nth:v(function(n,e){if(e instanceof M){var t=e;var r=0;while(rarray")(e).join(n)},"(join separator list)\n\n Function return string by joining elements of the list"),split:v(function(n,e){return this.get("array->list")(e.split(n))},"(split separator string)\n\n Function create list by splitting string by separatar that can\n be a string or regular expression."),replace:v(function(n,e,t){return t.replace(n,e)},"(replace pattern replacement string)\n\n Function change patter to replacement inside string."),match:v(function(n,e){var t=e.match(n);return t?this.get("array->list")(t):P},"(match pattern string)\n\n function return match object from JavaScript as list."),search:v(function(n,e){return e.search(n)},"(search pattern string)\n\n Function return first found index of the pattern inside a string"),string:v(function e(n,t){if(typeof jQuery!=="undefined"&&n instanceof jQuery.fn.init){return"<#jQuery("+n.length+")>"}if(n instanceof ln){return n.value.toString()}if(typeof n==="undefined"){return"<#undefined>"}if(typeof n==="function"){if(K(n)){return"<#function(native)>"}return"<#function>"}if(n===P){return"nil"}if(n instanceof Array){return"["+n.map(function(n){return e(n,true)}).join(", ")+"]"}if(n===null||typeof n==="string"&&t){return JSON.stringify(n)}if(n instanceof M){return new In.Formatter(n.toString()).break().format()}if(n instanceof L){return n.toString()}if(c.HTMLElement&&n instanceof c.HTMLElement){return"<#HTMLElement(".concat(n.tagName.toLowerCase(),")>")}if(Tn(n)==="object"){var r=n.constructor.name;if(r!==""){return"<#"+r+">"}return"<#Object>"}if(typeof n!=="string"){return n.toString()}return n},"(string obj)\n\n Function return string LIPS representation of an object as string."),env:v(function(n){n=n||this;var e=Object.keys(n.env);var t;if(e.length){t=M.fromArray(e)}else{t=P}if(n.parent!==g){return this.get("env").call(this,n.parent).append(t)}return t},"(env obj)\n\n Function return list values (functions and variables) inside environment."),new:v(function(n){for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;rlist":v(function(n){return M.fromArray(n)},"(array->list array)\n\n Function convert JavaScript array to LIPS list."),"list->array":v(function(n){if(n instanceof M&&n.isEmptyList()){return[]}var e=[];var t=n;while(true){if(t instanceof M){if(t.haveCycles("cdr")){break}e.push(t.car);t=t.cdr}else{break}}return e},"(list->array list)\n\n Function convert LIPS list into JavaScript array."),apply:v(function(n,e){_n("call",n,"function",1);_n("call",e,"pair",2);return n.apply(void 0,Cn(this.get("list->array")(e)))},"(apply fn args)\n\n Function that call function with list of arguments."),length:v(function(n){if(!n){return ln(0)}if(n instanceof M){return ln(n.length())}if("length"in n){return ln(n.length)}},"(length expression)\n\n Function return length of the object, the object can be list\n or any object that have length property."),try:v(new J("try",function(a,n){var o=this;var u=n.dynamic_scope,c=n.error;return new Promise(function(i){var n={env:o,error:function n(e){var t=o.inherit("try");t.set(a.cdr.car.cdr.car.car,e);var r={env:t,error:c};if(u){r.dynamic_scope=o}d(Fn(new M(new L("begin"),a.cdr.car.cdr.cdr),r),function(n){i(n)})}};if(u){n.dynamic_scope=o}d(Fn(a.car,n),i).catch(n.error)})}),"(try expr (catch (e) code)"),find:v(function e(t,r){if(G(r)){return P}var n=s("find",t);return d(n(r.car),function(n){if(n){return r.car}return e(t,r.cdr)})},"(Find fn list)\n\n Higher order Function find first value for which function\n return true."),"for-each":v(function(n){_n("for-each",n,"function");for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;r1?e-1:0),a=1;a3?r-3:0),a=3;a3?i-3:0),o=3;oarray")(e);var a=[];var o=s("filter",n);return function e(t){function n(n){if(n){a.push(r)}return e(++t)}if(t===i.length){return M.fromArray(a)}var r=i[t];return d(o(r,t),n)}(0)},"(filter fn list)\n\n Higher order function that call `fn` for each element of the list\n and return list for only those elements for which funtion return\n true value."),range:v(function(n){if(n instanceof ln){n=n.valueOf()}return M.fromArray(new Array(n).fill(0).map(function(n,e){return ln(e)}))},"(range n)\n\n Function return list of n numbers from 0 to n - 1"),compose:v(en,"(compose . fns)\n\n Higher order function and create new function that apply all functions\n From right to left and return it's value. Reverse of compose.\n e.g.:\n ((compose (curry + 2) (curry * 3)) 3)\n 11\n "),pipe:v(nn,"(pipe . fns)\n\n Higher order function and create new function that apply all functions\n From left to right and return it's value. Reverse of compose.\n e.g.:\n ((pipe (curry + 2) (curry * 3)) 3)\n 15"),curry:v(cn,"(curry fn . args)\n\n Higher order function that create curried version of the function.\n The result function will have parially applied arguments and it\n will keep returning functions until all arguments are added\n\n e.g.:\n (define (add a b c d) (+ a b c d))\n (define add1 (curry add 1))\n (define add12 (add 2))\n (print (add12 3 4))"),odd:v(an(function(n){return ln(n).isOdd()}),"(odd number)\n Function check if number os odd."),even:v(an(function(n){return ln(n).isEvent()}),"(even number)\n\n Function check if number is even."),"*":v(un(function(n,e){return ln(n).mul(e)}),"(* . numbers)\n\n Multiplicate all numbers passed as arguments. If single value is passed\n it will return that value."),"+":v(un(function(n,e){return ln(n).add(e)}),"(+ . numbers)\n\n Sum all numbers passed as arguments. If single value is passed it will\n return that value."),"-":v(function(){for(var n=arguments.length,e=new Array(n),t=0;t":v(function(n,e){return ln(n).cmp(e)===1},"(> a b)\n\n Function compare two numbers and check if first argument is greater\n than the second one"),"<":v(function(n,e){return ln(n).cmp(e)===-1},"(< a b)\n\n Function compare two numbers and check if first argument is less\n than the second one"),"<=":v(function(n,e){return[0,-1].includes(ln(n).cmp(e))},"(<= a b)\n\n Function compare two numbers and check if first argument is less or\n equal to the second one"),">=":v(function(n,e){return[0,1].includes(ln(n).cmp(e))},"(>= a b)\n\n Function compare two numbers and check if first argument is less or\n equal to the second one"),"eq?":v(T,"(eq? a b)\n\n Function compare two values if they are identical."),or:v(new J("or",function(n,e){var i=e.dynamic_scope,a=e.error;var o=this.get("list->array")(n);var u=this;if(i){i=u}var c;return function e(){function n(n){c=n;if(c){return c}else{return e()}}var t=o.shift();if(typeof t==="undefined"){if(c){return c}else{return false}}else{var r=Fn(t,{env:u,dynamic_scope:i,error:a});return d(r,n)}}()}),"(or . expressions)\n\n Macro execute the values one by one and return the one that is truthy value.\n If there are no expression that evaluate to true it return false."),and:v(new J("and",function(n){var e=arguments.length>1&&arguments[1]!==g?arguments[1]:{},i=e.dynamic_scope,a=e.error;var o=this.get("list->array")(n);var u=this;if(i){i=u}if(!o.length){return true}var c;return function e(){function n(n){c=n;if(!c){return false}else{return e()}}var t=o.shift();if(typeof t==="undefined"){if(c){return c}else{return false}}else{var r=Fn(t,{env:u,dynamic_scope:i,error:a});return d(r,n)}}()}),"(and . expressions)\n\n Macro evalute each expression in sequence if any value return false it will\n return false. If each value return true it will return the last value.\n If it's called without arguments it will return true."),"|":v(function(n,e){return ln(n).or(e)},"(& a b)\n\n Function calculate or bit operation."),"&":v(function(n,e){return ln(n).and(e)},"(& a b)\n\n Function calculate and bit operation."),"~":v(function(n){return ln(n).neg()},"(~ number)\n\n Function negate the value."),">>":v(function(n,e){return ln(n).shr(e)},"(>> a b)\n\n Function right shit the value a by value b."),"<<":v(function(n,e){return ln(n).shl(e)},"(<< a b)\n\n Function left shit the value a by value b."),not:v(function(n){if(H(n)){return true}return!n},"(not object)\n\n Function return negation of the argument."),"->":v(function(n,e){for(var t=arguments.length,r=new Array(t>2?t-2:0),i=2;i obj name . args)\n\n Function get function from object and call it with arguments.")},g,"global");["floor","round","ceil"].forEach(function(e){mn.set(e,v(function(n){if(n instanceof ln){return n[e]()}throw new Error("".concat(Tn(n)," ").concat(n.toString()," is not a number"))},"(".concat(e," number)\n\n Function calculate ").concat(e," of a number.")))});function gn(n){if(n.length===1){return n[0]}else{var e=[];var t=gn(n.slice(1));for(var r=0;r3&&arguments[3]!==g?arguments[3]:null;var i=n?" in function `".concat(n,"`"):"";if(r!==null){i+=" argument ".concat(r)}return"Expecting ".concat(t," got ").concat(e).concat(i)}function _n(n,e,t){var r=arguments.length>3&&arguments[3]!==g?arguments[3]:null;var i=xn(e);if(i!==t){throw new Error(wn(n,i,t,r))}}function xn(n){var e={pair:M,symbol:L,macro:J,array:Array,native_symbol:c.Symbol};if(n===P){return"nil"}if(n===null){return"null"}var t=Object.entries(e);for(var r=0;r1&&arguments[1]!==g?arguments[1]:{},r=n.env,i=n.dynamic_scope,t=n.error,a=t===void 0?function(){}:t;try{if(i===true){r=i=r||mn}else if(r===true){r=i=mn}else{r=r||mn}var o={env:r,dynamic_scope:i,error:a};var u;if(G(e)){return e}if(H(e)){return B()}var c=e.car;var s=e.cdr;if(c instanceof M){u=kn(Fn(c,o));if(Y(u)){return u.then(function(n){return Fn(new M(n,e.cdr),o)})}else if(typeof u!=="function"){throw new Error(xn(u)+" "+r.get("string")(u)+" is not a function while evaluating "+e.toString())}}if(c instanceof L){u=r.get(c,true);if(u instanceof J){return d(Sn(u,s,o),function(n){if(n instanceof M){return n.markCycles()}return n})}else if(typeof u!=="function"){if(u){var f="".concat(xn(u)," `").concat(u,"' is not a function");throw new Error(f)}throw new Error("Unknown function `".concat(c.name,"'"))}}else if(typeof c==="function"){u=c}if(typeof u==="function"){var l=jn(s,o);return d(l,function(n){var e=i||r;var t=kn(u.apply(e,n));return d(t,function(n){if(n instanceof M){return dn(n.markCycles())}return n},a)})}else if(e instanceof L){u=r.get(e);if(u==="undefined"){throw new Error("Unbound variable `"+e.name+"'")}return u}else if(e instanceof M){u=c&&c.toString();throw new Error("".concat(xn(c)," ").concat(u," is not a function"))}else{return e}}catch(n){a&&a(n,e)}}function En(n,e,t){return An.apply(this,arguments)}function An(){An=Bn(Mn.mark(function n(t,r,i){var a,o,u,c,s;return Mn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:if(i===true){r=i=r||mn}else if(r===true){r=i=mn}else{r=r||mn}a=b(t,true).map(function(n){if(n.token.match(l)&&n.col){var e=new RegExp("^ {".concat(n.col+1,"}"));return n.token.split("\n").map(function(n){return n.replace(e,"")}).join("\n")}return n.token.trim()}).filter(function(n){return n&&!n.match(/^;/)});o=p(a);u=[];case 4:c=o.shift();if(c){e.next=10;break}return e.abrupt("return",u);case 10:e.next=12;return Fn(c,{env:r,dynamic_scope:i,error:function n(e,t){if(t){e.code=t.toString()}throw e}});case 12:s=e.sent;u.push(s);case 14:e.next=4;break;case 16:case"end":return e.stop()}}},n)}));return An.apply(this,arguments)}function qn(e){return function(n){if(!n){return false}return(n.token||n).match(e)}}var Nn=qn(/[()]/);function On(n){var e=typeof n==="string"?b(n):n;var t=e.filter(Nn);var r=t.filter(function(n){return(n.token||n)===")"});var i=t.filter(function(n){return(n.token||n)==="("});return r.length===i.length}M.unDry=function(n){return new M(n.car,n.cdr)};M.prototype.toDry=function(){return{value:{car:this.car,cdr:this.cdr}}};I.prototype.toDry=function(){return{value:null}};I.unDry=function(){return P};L.prototype.toDry=function(){return{value:{name:this.name}}};L.unDry=function(n){return new L(n.name)};function Ln(){var i="text/x-lips";if(window.document){var a=Array.from(document.querySelectorAll("script"));return function n(){var e=a.shift();if(e){var t=e.getAttribute("type");if(t===i){var r=e.getAttribute("src");if(r){return c.fetch(r).then(function(n){return n.text()}).then(En).then(n)}else{return En(e.innerHTML).then(n)}}else if(t&&t.match(/lips|lisp/)){console.warn("Expecting "+i+" found "+t)}return n()}}()}}if(typeof window!=="undefined"){n(window,Ln)}var In={version:"0.11.2",exec:En,parse:p,tokenize:b,evaluate:Fn,Environment:pn,global_environment:mn,globalEnvironment:mn,env:mn,balanced_parenthesis:On,balancedParenthesis:On,Macro:J,quote:dn,Pair:M,Formatter:j,specials:m,nil:P,resolvePromises:kn,Symbol:L,LNumber:ln};mn.set("lips",In);return In})})(); \ No newline at end of file +(function(){"use strict";function t(n,e){t=Object.setPrototypeOf||function n(e,t){e.__proto__=t;return e};return t(n,e)}function r(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Date.prototype.toString.call(Reflect.construct(Date,[],function(){}));return true}catch(n){return false}}function Pn(n,e,t){if(r()){Pn=Reflect.construct}else{Pn=function n(e,t,r){var i=[null];i.push.apply(i,t);var a=Function.bind.apply(e,i);var o=new a;if(r)setPrototypeOf(o,r.prototype);return o}}return Pn.apply(null,arguments)}function n(n,e){return e={exports:{}},n(e,e.exports),e.exports}var e=n(function(n){var e=function(a){var n=Object.prototype;var s=n.hasOwnProperty;var c;var e=typeof Symbol==="function"?Symbol:{};var i=e.iterator||"@@iterator";var t=e.asyncIterator||"@@asyncIterator";var r=e.toStringTag||"@@toStringTag";function o(n,e,t,r){var i=e&&e.prototype instanceof u?e:u;var a=Object.create(i.prototype);var o=new A(r||[]);a._invoke=j(n,t,o);return a}a.wrap=o;function f(n,e,t){try{return{type:"normal",arg:n.call(e,t)}}catch(n){return{type:"throw",arg:n}}}var l="suspendedStart";var h="suspendedYield";var p="executing";var d="completed";var v={};function u(){}function y(){}function m(){}var g={};g[i]=function(){return this};var b=Object.getPrototypeOf;var w=b&&b(b(q([])));if(w&&w!==n&&s.call(w,i)){g=w}var _=m.prototype=u.prototype=Object.create(g);y.prototype=_.constructor=m;m.constructor=y;m[r]=y.displayName="GeneratorFunction";function x(n){["next","throw","return"].forEach(function(e){n[e]=function(n){return this._invoke(e,n)}})}a.isGeneratorFunction=function(n){var e=typeof n==="function"&&n.constructor;return e?e===y||(e.displayName||e.name)==="GeneratorFunction":false};a.mark=function(n){if(Object.setPrototypeOf){Object.setPrototypeOf(n,m)}else{n.__proto__=m;if(!(r in n)){n[r]="GeneratorFunction"}}n.prototype=Object.create(_);return n};a.awrap=function(n){return{__await:n}};function k(u){function c(n,e,t,r){var i=f(u[n],u,e);if(i.type==="throw"){r(i.arg)}else{var a=i.arg;var o=a.value;if(o&&typeof o==="object"&&s.call(o,"__await")){return Promise.resolve(o.__await).then(function(n){c("next",n,t,r)},function(n){c("throw",n,t,r)})}return Promise.resolve(o).then(function(n){a.value=n;t(a)},function(n){return c("throw",n,t,r)})}}var e;function n(t,r){function n(){return new Promise(function(n,e){c(t,r,n,e)})}return e=e?e.then(n,n):n()}this._invoke=n}x(k.prototype);k.prototype[t]=function(){return this};a.AsyncIterator=k;a.async=function(n,e,t,r){var i=new k(o(n,e,t,r));return a.isGeneratorFunction(e)?i:i.next().then(function(n){return n.done?n.value:i.next()})};function j(o,u,c){var s=l;return function n(e,t){if(s===p){throw new Error("Generator is already running")}if(s===d){if(e==="throw"){throw t}return N()}c.method=e;c.arg=t;while(true){var r=c.delegate;if(r){var i=S(r,c);if(i){if(i===v)continue;return i}}if(c.method==="next"){c.sent=c._sent=c.arg}else if(c.method==="throw"){if(s===l){s=d;throw c.arg}c.dispatchException(c.arg)}else if(c.method==="return"){c.abrupt("return",c.arg)}s=p;var a=f(o,u,c);if(a.type==="normal"){s=c.done?d:h;if(a.arg===v){continue}return{value:a.arg,done:c.done}}else if(a.type==="throw"){s=d;c.method="throw";c.arg=a.arg}}}}function S(n,e){var t=n.iterator[e.method];if(t===c){e.delegate=null;if(e.method==="throw"){if(n.iterator["return"]){e.method="return";e.arg=c;S(n,e);if(e.method==="throw"){return v}}e.method="throw";e.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var r=f(t,n.iterator,e.arg);if(r.type==="throw"){e.method="throw";e.arg=r.arg;e.delegate=null;return v}var i=r.arg;if(!i){e.method="throw";e.arg=new TypeError("iterator result is not an object");e.delegate=null;return v}if(i.done){e[n.resultName]=i.value;e.next=n.nextLoc;if(e.method!=="return"){e.method="next";e.arg=c}}else{return i}e.delegate=null;return v}x(_);_[r]="Generator";_[i]=function(){return this};_.toString=function(){return"[object Generator]"};function F(n){var e={tryLoc:n[0]};if(1 in n){e.catchLoc=n[1]}if(2 in n){e.finallyLoc=n[2];e.afterLoc=n[3]}this.tryEntries.push(e)}function E(n){var e=n.completion||{};e.type="normal";delete e.arg;n.completion=e}function A(n){this.tryEntries=[{tryLoc:"root"}];n.forEach(F,this);this.reset(true)}a.keys=function(t){var r=[];for(var n in t){r.push(n)}r.reverse();return function n(){while(r.length){var e=r.pop();if(e in t){n.value=e;n.done=false;return n}}n.done=true;return n}};function q(e){if(e){var n=e[i];if(n){return n.call(e)}if(typeof e.next==="function"){return e}if(!isNaN(e.length)){var t=-1,r=function n(){while(++t=0;--e){var i=this.tryEntries[e];var a=i.completion;if(i.tryLoc==="root"){return n("end")}if(i.tryLoc<=this.prev){var o=s.call(i,"catchLoc");var u=s.call(i,"finallyLoc");if(o&&u){if(this.prev=0;--t){var r=this.tryEntries[t];if(r.tryLoc<=this.prev&&s.call(r,"finallyLoc")&&this.prev=0;--e){var t=this.tryEntries[e];if(t.finallyLoc===n){this.complete(t.completion,t.afterLoc);E(t);return v}}},catch:function(n){for(var e=this.tryEntries.length-1;e>=0;--e){var t=this.tryEntries[e];if(t.tryLoc===n){var r=t.completion;if(r.type==="throw"){var i=r.arg;E(t)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(n,e,t){this.delegate={iterator:q(n),resultName:e,nextLoc:t};if(this.method==="next"){this.arg=c}return v}};return a}(n.exports);try{regeneratorRuntime=e}catch(n){Function("r","regeneratorRuntime = r")(e)}});var Mn=e;function c(n,e,t,r,i,a,o){try{var u=n[a](o);var c=u.value}catch(n){t(n);return}if(u.done){e(c)}else{Promise.resolve(c).then(r,i)}}function Bn(u){return function(){var n=this,o=arguments;return new Promise(function(e,t){var r=u.apply(n,o);function i(n){c(r,e,t,i,a,"next",n)}function a(n){c(r,e,t,i,a,"throw",n)}i(undefined)})}}function i(n){if(Array.isArray(n)){for(var e=0,t=new Array(n.length);e1&&arguments[1]!==g?arguments[1]:1;return n[n.length-e]}function o(n){if(typeof n==="string"){var e=/([-\\^$[\]()+{}?*.|])/g;return n.replace(e,"\\$1")}}function u(n){var i=e();n=n.replace(/\n\r|\r/g,"\n");var a=0;var o=0;var u=[];var c=[];var s=0;n.split(f).filter(Boolean).forEach(function(n){if(n.match(f)){s=0;if(c.length){var e=h(c);if(e.token.match(/\n/)){var t=e.token.split("\n").pop();s+=t.length}else{s+=e.token.length}s+=e.col}var r={col:s,line:o,token:n,offset:a};u.push(r);c.push(r);a+=n.length;s+=n.length;o+=(n.match("\n")||[]).length;return}n.split(i).filter(Boolean).forEach(function(n){var e={col:s,line:o,token:n,offset:a};s+=n.length;a+=n.length;u.push(e);c.push(e);if(n==="\n"){++o;c=[];s=0}})});return u}function b(n,e){if(e){return u(n)}else{return u(n).map(function(n){return n.token.trim()}).filter(function(n){return n&&!n.match(/^;/)})}}var m={"'":new L("quote"),"`":new L("quasiquote"),",@":new L("unquote-splicing"),",":new L("unquote")};function p(n){if(typeof n==="string"){throw new Error("parse require tokenized array of tokens not string")}var a=[];var o=[];var u=null;var c=Object.keys(m);var s=c.map(function(n){return m[n].name});var f=0;var l=false;var h=[];var p=[];var d=0;function v(){var n=a[a.length-1];if(n instanceof Array&&n[0]instanceof L&&s.includes(n[0].name)&&a.length>1&&!n[0].literal){a.pop();if(a[a.length-1].length===1&&a[a.length-1][0]instanceof L){a[a.length-1].push(n)}else if(a[a.length-1].length===0){a[a.length-1]=n}else if(a[a.length-1]instanceof M){if(a[a.length-1].cdr instanceof M){a[a.length-1]=new M(a[a.length-1],M.fromArray(n))}else{a[a.length-1].cdr=M.fromArray(n)}}else{a[a.length-1].push(n)}}}n.forEach(function(n){var e=a[a.length-1];if(c.indexOf(n)!==-1){d++;u=n;a.push([m[u]]);if(!u){p=[]}p.push(u)}else{if(u){h.push(p);p=[]}if(n==="("){l=true;f++;a.push([]);u=null;d=0}else if(n==="."&&!l){a[a.length-1]=M.fromArray(e)}else if(n===")"){f--;if(!a.length){throw new Error("Unbalanced parenthesis")}if(a.length===1){o.push(a.pop())}else if(a.length>1){var t=a.pop();e=a[a.length-1];if(e instanceof Array){e.push(t)}else if(e instanceof M){e.append(M.fromArray(t))}if(h.length){p=h.pop();while(p.length){v();p.pop()}}else{v()}}if(f===0&&a.length){o.push(a.pop())}}else{l=false;var r=y(n);if(u){while(d--){a[a.length-1][1]=r;r=a.pop()}d=0;u=false}else if(r instanceof L&&s.includes(r.name)){r.literal=true}e=a[a.length-1];if(e instanceof M){var i=e;while(true){if(i.cdr===P){if(r instanceof Array){i.cdr=M.fromArray(r)}else{i.cdr=r}break}else{i=i.cdr}}}else if(!a.length){o.push(r)}else{e.push(r)}}}});if(a.length){throw new Error("Unbalanced parenthesis 2")}return o.map(function(n){if(n instanceof Array){return M.fromArray(n)}return n})}function d(n){var e=arguments.length>1&&arguments[1]!==g?arguments[1]:function(n){return n};var t=arguments.length>2&&arguments[2]!==g?arguments[2]:null;if(Y(n)){var r=n.then(e);if(t===null){return r}else{return r.catch(t)}}return e(n)}function s(n,e){if(e instanceof RegExp){return function(n){return String(n).match(e)}}else if(typeof e!=="function"){throw new Error("".concat(n," argument need to be a function or RegExp"))}else{return e}}function v(n,e,t){if(e){if(t){n.__doc__=e}else{n.__doc__=w(e)}}return n}function w(n){return n.split("\n").map(function(n){return n.trim()}).join("\n")}function _(n){var e=arguments.length>1&&arguments[1]!==g?arguments[1]:1;var t=n.length;if(e<=0){throw Error("previousSexp: Invlaid argument sexp = ".concat(e))}n:while(e--&&t>=0){var r=1;while(r>0){var i=n[--t];if(!i){break n}if(i==="("||i.token==="("){r--}else if(i===")"||i.token===")"){r++}}t--}return n.slice(t+1)}function x(n){if(!n||!n.length){return 0}var e=n.length;if(n[e-1].token==="\n"){return 0}while(--e){if(n[e].token==="\n"){var t=(n[e+1]||{}).token;if(t){return t.length}}}return 0}function k(n,e){return u(n,e)===e.length;function u(n,e){var t=0;var r={};for(var i=0;i0){continue}}}else if(n[t]instanceof Array){var o=u(n[t],e.slice(i));if(o===-1||o+i>e.length){return-1}i+=o-1;t++;continue}else{return-1}t++}if(n.length!==t){return-1}return e.length}}function j(n){this._code=n.replace(/\r/g,"")}j.defaults={offset:0,indent:2,specials:["define","lambda","let","let*","define-macro"]};j.match=k;j.prototype._options=function n(e){var t=j.defaults;if(typeof e==="undefined"){return Object.assign({},t)}var r=e&&e.specials||[];return Object.assign({},t,e,{specials:t.specials.concat(r)})};j.prototype.indent=function n(e){var t=b(this._code,true);return this._indent(t,e)};j.prototype._indent=function n(e,t){var r=this._options(t);var i=x(e);var a=r.specials;var o=_(e);if(o){if(o[0].line>0){r.offset=0}if(o.length===1){return r.offset+o[0].col+1}else if(a.indexOf(o[1].token)!==-1){return r.offset+o[0].col+r.indent}else if(o[0].line3&&o[1].line===o[3].line){if(o[1].token==="("){return r.offset+o[1].col}return r.offset+o[3].col}else if(o[0].line===o[1].line){return r.offset+r.indent+o[0].col}else{var u=o.slice(2);for(var c=0;c"}else if(typeof n==="string"){return JSON.stringify(n)}else if(Y(n)){return"<#Promise>"}else if(n instanceof L||n instanceof ln||n instanceof M||n===P){return n.toString()}else if(n instanceof Array){return n.map(C)}else if(Tn(n)==="object"){if(n===null){return"null"}var e=n.constructor.name;if(e==="Object"){return JSON.stringify(n)}return"<#object("+n.constructor.name+")>"}else if(typeof n!=="undefined"){return n}}M.prototype.markCycles=function(){R(this);return this};M.prototype.haveCycles=function(){var n=arguments.length>0&&arguments[0]!==g?arguments[0]:null;if(!n){return this.haveCycles("car")||this.haveCycles("cdr")}return!!(this.cycles&&this.cycles[n])};function R(n){var i=[];var e=[];function a(n){if(n instanceof M){if(i.includes(n)){if(!e.includes(n)){e.push(n)}return"#".concat(e.length-1,"#")}}}function o(n){if(n instanceof M){i.push(n);var e={};var t=a(n.car);var r=a(n.cdr);if(t){e["car"]=t}else{o(n.car)}if(r){e["cdr"]=r}else{o(n.cdr)}if(t||r){n.cycles=e}else if(n.cycles){delete n.cycles}}}o(n)}M.prototype.toString=function(){var n=["("];if(this.car!==g){var e;if(this.cycles&&this.cycles.car){e=this.cycles.car}else{e=C(this.car)}if(e){n.push(e)}if(this.cdr instanceof M){if(this.cycles&&this.cycles.cdr){n.push(" . ");n.push(this.cycles.cdr)}else{var t;if(this.cycles&&this.cycles.cdr){t=this.cycles.cdr}var r=this.cdr.toString(t).replace(/^\(|\)$/g,"");n.push(" ");n.push(r)}}else if(typeof this.cdr!=="undefined"&&this.cdr!==P){if(typeof this.cdr==="string"){n=n.concat([" . ",JSON.stringify(this.cdr)])}else{n=n.concat([" . ",C(this.cdr)])}}}n.push(")");return n.join("")};M.prototype.set=function(n,e){this[n]=e;if(e instanceof M){this.markCycles()}};M.prototype.append=function(n){if(n instanceof Array){return this.append(M.fromArray(n))}var e=this;if(e.car===g){if(n instanceof M){this.car=n.car;this.cdr=n.cdr}else{this.car=n}}else{while(true){if(e instanceof M&&e.cdr!==P){e=e.cdr}else{break}}if(n instanceof M){e.cdr=n}else if(n!==P){e.cdr=new M(n,P)}}return this};function T(n,e){if(n instanceof ln&&e instanceof ln){return n.cmp(e)===0}else if(typeof n==="number"||typeof e==="number"){return ln(n).cmp(ln(e))}else if(n instanceof L&&e instanceof L){return n.name===e.name}else{return n===e}}function H(n){return n instanceof M&&n.isEmptyList()||n===P}function J(n,e,t){if(typeof this!=="undefined"&&this.constructor!==J||typeof this==="undefined"){return new J(n,e)}_n("Macro",n,"string",1);_n("Macro",e,"function",2);this.__doc__=t;this.name=n;this.fn=e}J.defmacro=function(n,e,t){var r=new J(n,e,t);r.defmacro=true;return r};J.prototype.invoke=function(n,e,t){var r=e.env,i=e.dynamic_scope,a=e.error;var o={dynamic_scope:i,error:a,macro_expand:t};var u=this.fn.call(r,n,o,this.name);return t?dn(u):u};J.prototype.toString=function(){return"#"};var $="define-macro";function D(a){return function(){var t=Bn(Mn.mark(function n(t,c){var s,f,r,i;return Mn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:r=function n(){r=Bn(Mn.mark(function n(t){var r,i,a,o,u;return Mn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:if(!(t instanceof M&&t.car instanceof L)){e.next=13;break}e.prev=1;r=s.get(t.car);if(!(r instanceof J&&r.defmacro)){e.next=9;break}e.next=6;return r.invoke(t.cdr,c,true);case 6:i=e.sent;if(!(i instanceof M)){e.next=9;break}return e.abrupt("return",i);case 9:e.next=13;break;case 11:e.prev=11;e.t0=e["catch"](1);case 13:a=t.car;if(!(a instanceof M)){e.next=18;break}e.next=17;return f(a);case 17:a=e.sent;case 18:o=t.cdr;if(!(o instanceof M)){e.next=23;break}e.next=22;return f(o);case 22:o=e.sent;case 23:u=new M(a,o);return e.abrupt("return",u);case 25:case"end":return e.stop()}}},n,null,[[1,11]])}));return r.apply(this,arguments)};f=function n(e){return r.apply(this,arguments)};s=c["env"]=this;i=t;if(!a){e.next=12;break}e.t0=dn;e.next=8;return f(t);case 8:e.t1=e.sent.car;return e.abrupt("return",(0,e.t0)(e.t1));case 12:e.next=15;return f(t);case 15:i=e.sent;if(!(t.toString()===i.toString())){e.next=18;break}return e.abrupt("break",21);case 18:t=i;e.next=12;break;case 21:return e.abrupt("return",dn(i.car));case 22:case"end":return e.stop()}}},n,this)}));return function(n,e){return t.apply(this,arguments)}}()}function G(n){return typeof n==="undefined"||n===P||n===null}function Y(n){return n instanceof Promise||n&&typeof n!=="undefined"&&typeof n.then==="function"}function U(n,a){var o=function n(){for(var e=arguments.length,t=new Array(e),r=0;r2?e-2:0),r=2;r1?t-1:0),i=1;i1?e-1:0),r=1;rarray")(a.car);var f=s.inherit("let");var l=0;return function n(){var e=c[l++];function t(n){if(Y(n)){return n.then(t)}else if(typeof n==="undefined"){f.set(e.car,P)}else{f.set(e.car,n)}}if(o){o=h?f:s}if(!e){var r=new M(new L("begin"),a.cdr);return Fn(r,{env:f,dynamic_scope:o,error:u})}else{var i=Fn(e.cdr.car,{env:h?f:s,dynamic_scope:o,error:u});return d(t(i),n)}}()})}function Z(n){for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;r2?r-2:0),a=2;a1?n-1:0),t=1;t=o){return a.apply(this,r)}else{return i}}return i.apply(this,arguments)}}function sn(r,i){_n("limit",i,"function",2);return function(){for(var n=arguments.length,e=new Array(n),t=0;t1?e-1:0),r=1;r>":function n(e,t){return e>>t},"<<":function n(e,t){return e<>":"ishln"};n=i[n];return ln(this.value.clone()[n](e,r))}};var hn={"+":"add","-":"sub","*":"mul","/":"div","%":"mod","|":"or","&":"and","~":"neg","<<":"shl",">>":"shr"};Object.keys(hn).forEach(function(e){ln.prototype[hn[e]]=function(n){return this.op(e,n)}});ln.prototype.sqrt=function(){var n;if(ln.isNative(this.value)){n=Math.sqrt(this.value)}else if(ln.isBN(this.value)){n=this.value.sqrt()}return new ln(n)};ln.prototype.pow=function(n){n=this.coerce(n);if(ln.isNative(this.value)){try{var e=new Function("a,b","return a**b;");n.value=e(this.value,n.value)}catch(n){throw new Error("Power operator not supported")}}else if(ln.isBN(this.value)){n.value=this.value.pow(n.value)}else{n.value=Math.pow(this.value,n.value)}return n};ln.prototype.neg=function(){var n=this.value;if(ln.isNative(n)){n=-n}else if(ln.isBN(n)){n=n.neg()}return new ln(n)};ln.prototype.abs=function(){var n=this.value;if(ln.isNative(this.value)){if(n<0){n=-n}}else if(ln.isBN(n)){n.iabs()}return new ln(n)};ln.prototype.isOdd=function(){if(ln.isNative(this.value)){if(this.isBigNumber()){return this.value%BigInt(2)===BigInt(1)}return this.value%2===1}else if(ln.isBN(this.value)){return this.value.isOdd()}};ln.prototype.isEven=function(){return!this.isOdd()};ln.prototype.cmp=function(n){n=this.coerce(n);if(ln.isNative(this.value)){if(this.value1&&arguments[1]!==g?arguments[1]:{};if(Tn(n)==="object"){e=n}if(!n||Tn(n)==="object"){n="child of "+(this.name||"unknown")}return new pn(e||{},this,n)};pn.prototype.get=function(n,e,t){t=t||this;var r;var i=false;if(n instanceof L){if(n.name in this.env){r=this.env[n.name];i=true}}else if(typeof n==="string"){if(typeof this.env[n]!=="undefined"){r=this.env[n];i=true}}if(i){if(ln.isNumber(r)){return ln(r)}if(r instanceof M){return r.markCycles()}if(typeof r==="function"){if(!r.name.match(/^bound /)){if(e){return U(r,t)}return V(r,t)}}return r}if(this.parent instanceof pn){return this.parent.get(n,e,t)}else{var a;if(n instanceof L){a=n.name}else if(typeof n==="string"){a=n}if(a){var o=Tn(c[a]);if(o==="function"){if(K(c[a])){return V(c[a],c)}return c[a]}else if(o!=="undefined"){return c[a]}}}a=(a.name||a).toString();throw new Error("Unbound variable `"+a+"'")};pn.prototype.set=function(n,e){if(ln.isNumber(e)){e=ln(e)}if(n instanceof L){n=n.name}this.env[n]=e};pn.prototype.has=function(n){return typeof this.env[n]!=="undefined"};pn.prototype.ref=function(n){var e=this;while(true){if(!e){break}if(e.has(n)){return e}e=e.parent}};function dn(n){if(Y(n)){return n.then(dn)}if(n instanceof M||n instanceof L){n.data=true}return n}function vn(n,e){this.value=n;this.count=e}vn.prototype.toString=function(){return"<#unquote["+this.count+"] "+this.value+">"};var yn=function(){var e=0;return function(){var n=arguments.length>0&&arguments[0]!==g?arguments[0]:null;if(n!==null){return new L(c.Symbol("#".concat(n)))}e++;return new L(c.Symbol("#gensym_".concat(e,"#")))}}();var mn=new pn({nil:P,undefined:g,true:true,NaN:NaN,false:false,stdout:{write:function n(){var e;(e=console).log.apply(e,arguments)}},stdin:{read:function n(){return new Promise(function(n){n(prompt(""))})}},help:v(function(n){return n.__doc__},"(help object)\n\n Function returns documentation for function or macro."),cons:v(function(n,e){if(H(e)){e=P}return new M(n,e)},"(cons left right)\n\n Function return new Pair out of two arguments."),car:v(function(n){if(n===P){return P}_n("car",n,"pair");if(H(n)){return P}return n.car},"(car pair)\n\n Function returns car (head) of the list/pair."),cdr:v(function(n){if(n===P){return P}_n("cdr",n,"pair");if(H(n)){return P}return n.cdr},"(cdr pair)\n\n Function returns cdr (tail) of the list/pair."),"set!":v(new J("set!",function(e){var n=arguments.length>1&&arguments[1]!==g?arguments[1]:{},t=n.dynamic_scope,r=n.error;if(t){t=this}var i=Fn(e.cdr.car,{env:this,dynamic_scope:t,error:r});i=kn(i);var a;function o(e,t){if(Y(e)){return e.then(function(n){return o(n,t)})}if(Y(t)){return t.then(function(n){return o(e,n)})}s[e]=t;return t}if(e.car instanceof M&&L.is(e.car.car,".")){var u=e.car.cdr.car;var c=e.car.cdr.cdr.car;var s=Fn(u,{env:this,dynamic_scope:t,error:r});var f=Fn(c,{env:this,dynamic_scope:t,error:r});return o(f,i)}if(!(e.car instanceof L)){throw new Error("set! first argument need to be a symbol or "+"dot accessor that evaluate to object.")}a=this.ref(e.car.name);if(!a){a=this}return d(i,function(n){a.set(e.car,n)})}),"(set! name value)\n\n Macro that can be used to set the value of the variable (mutate)\n it search the scope chain until it finds first non emtpy slot and set it."),"set-car!":v(function(n,e){_n("set-car!",n,"pair");n.car=e},"(set-car! obj value)\n\n Function that set car (head) of the list/pair to specified value.\n It can destroy the list. Old value is lost."),"set-cdr!":v(function(n,e){_n("set-cdr!",n,"pair");n.cdr=e},"(set-cdr! obj value)\n\n Function that set cdr (tail) of the list/pair to specified value.\n It can destroy the list. Old value is lost."),"empty?":v(function(n){return typeof n==="undefined"||H(n)},"(empty? object)\n\n Function return true if value is undfined empty list."),assoc:v(function(n,e){if(n instanceof M&&!(e instanceof M)){throw new Error("First argument to assoc ned to be a key")}var t=e;while(true){if(!(t instanceof M)||this.get("empty?")(t)){break}var r=t.car.car;if(T(r,n)){return t.car}else if(!t.haveCycles("cdr")){t=t.cdr}}return P},"(assoc key alist)\n\n Function search Alist (list of pairs) until it find the one that\n have head set equal to key, and return found pair."),gensym:v(yn,"(gensym)\n\n Function generate unique symbol, to use with macros as meta name."),load:v(function(n){_n("load",n,"string");return c.fetch(n).then(function(n){return n.text()}).then(En)},"(load filename)\n\n Function fetch the file and evaluate its content as LIPS code."),while:v(new J("while",function(r,n){var i=n.dynamic_scope,a=n.error;var o=this;var u=new M(new L("begin"),r.cdr);var c;if(i){i=o}return function e(){var n=Fn(r.car,{env:o,dynamic_scope:i,error:a});function t(n){if(n&&!G(n)&&!H(n)){c=Fn(u,{env:o,dynamic_scope:i,error:a});if(Y(c)){return c.then(function(n){c=n;return e()})}else{return e()}}else{return c}}return d(n,t)}()}),"(while cond . body)\n\n Macro that create a loop, it exectue body untill cond expression is false"),if:v(new J("if",function(t,n){var r=n.dynamic_scope,i=n.error;if(r){r=this}var a=this;var e=function n(e){_n("if",e,"boolean");if(e){return Fn(t.cdr.car,{env:a,dynamic_scope:r,error:i})}else{return Fn(t.cdr.cdr.car,{env:a,dynamic_scope:r,error:i})}};var o=Fn(t.car,{env:a,dynamic_scope:r,error:i});return d(o,e)}),"(if cond true-expr false-expr)\n\n Macro evaluate condition expression and if the value is true, it\n evaluate and return true expression if not it evaluate and return\n false expression"),"let*":v(W(true),"(let* ((a value-a) (b value-b)) body)\n\n Macro that creates new environment, then evaluate and assign values to\n names and then evaluate the body in context of that environment.\n Values are evaluated sequentialy and next value can access to\n previous values/names."),let:v(W(false),"(let ((a value-a) (b value-b)) body)\n\n Macro that creates new environment, then evaluate and assign values to\n names and then evaluate the body in context of that environment.\n Values are evaluated sequentialy but you can't access\n previous values/names when next are evaluated. You can only get them\n from body of let expression."),begin:v(new J("begin",function(n,e){var r=e.dynamic_scope,i=e.error;var a=this.get("list->array")(n);if(r){r=this}var o=this;var u;return function e(){if(a.length){var n=a.shift();var t=Fn(n,{env:o,dynamic_scope:r,error:i});return d(t,function(n){u=n;return e()})}else{return u}}()}),"(begin . args)\n\n Macro runs list of expression and return valuate of the list one.\n It can be used in place where you can only have single exression,\n like if expression."),ignore:new J("ignore",function(n,e){var t=e.dynamic_scope,r=e.error;var i={env:this,error:r};if(t){i.dynamic_scope=this}Fn(n,i)},"(ignore expression)\n\n Macro that will evaluate expression and swallow any promises that may\n be created. It wil run and ignore any value that may be returned by\n expression. The code should have side effects and/or when it's promise\n it should resolve to undefined."),timer:v(new J("timer",function(e){var n=arguments.length>1&&arguments[1]!==g?arguments[1]:{},t=n.dynamic_scope,r=n.error;_n("timer",e.car,"number");var i=this;if(t){t=this}return new Promise(function(n){setTimeout(function(){n(Fn(e.cdr.car,{env:i,dynamic_scope:t,error:r}))},e.car)}).then(dn)}),"(timer time expression)\n\n Function return a promise, and it will be automatically evaluated\n after specific time passes. The return value of the function\n will be value of the timer exprssion. If you want to do side effect\n only expression you can wrap your expression in nol call."),define:v(J.defmacro("define",function(e,n){var t=this;if(e.car instanceof M&&e.car.car instanceof L){var r=new M(new L("define"),new M(e.car.car,new M(new M(new L("lambda"),new M(e.car.cdr,e.cdr)))));return r}else if(n.macro_expand){return}if(n.dynamic_scope){n.dynamic_scope=this}n.env=t;var i=e.cdr.car;if(i instanceof M){i=Fn(i,n)}else if(i instanceof L){i=t.get(i)}if(e.car instanceof L){d(i,function(n){t.set(e.car,n)})}}),"(define name expression)\n (define (function-name . args) body)\n\n Macro for defining values. It can be used to define variables,\n or function. If first argument is list it will create function\n with name beeing first element of the list. The macro evalute\n code `(define function (lambda args body))`"),"set-obj!":v(function(n,e,t){if(Tn(n)!=="object"||G(n)){throw new Error(wn("set-obj!",xn(n),"object"))}n[e]=t},"(set-obj! obj key value)\n\n Function set property of JavaScript object"),"current-environment":v(function(){return this},"(current-environment)\n\n Function return current environement."),eval:v(function(n,t){var r=this;t=t||this;if(n instanceof L){return t.get(n)}if(n instanceof M){return Fn(n,{env:t,dynamic_scope:this,error:function n(e){return r.get("print")(e.message)}})}if(n instanceof Array){var i=this.get("eval");return n.reduce(function(n,e){return i(e,t)})}},"(eval list)\n\n Function evalute LIPS code as list structure."),lambda:new J("lambda",function(s){var n=arguments.length>1&&arguments[1]!==g?arguments[1]:{},f=n.dynamic_scope,l=n.error;var h=this;var p;if(s.cdr instanceof M&&typeof s.cdr.car==="string"&&s.cdr.cdr!==P){p=s.cdr.car}function e(){var n=(f?this:h).inherit("lambda");var e=s.car;var t=0;var r;if(e instanceof L||!H(e)){for(var i=arguments.length,a=new Array(i),o=0;o2&&arguments[2]!==g?arguments[2]:a;if(n instanceof M&&!H(n)){var r=n.car;var i=n.cdr;if(t(r)){r=e(r)}if(t(i)){i=e(i)}if(Y(r)||Y(i)){return Promise.all([r,i]).then(function(n){var e=Rn(n,2),t=e[0],r=e[1];return new M(t,r)})}else{return new M(r,i)}}return n}function l(n,e){if(n instanceof M){if(H(n)&&e===P){return P}n.append(e)}else{n=new M(n,e)}return n}function h(t){if(t instanceof M&&!H(t)){var n;if(L.is(t.car.car,"unquote-splicing")){n=Fn(t.car.cdr.car,{env:c,dynamic_scope:o,error:u});return d(n,function(e){if(!e instanceof M){throw new Error("Value of unquote-splicing need"+" to be pair")}var n=h(t.cdr);if(n===P&&e===P){return g}return d(n,function(n){return l(e,n)})})}if(L.is(t.car,"unquote")){var e=t.cdr;var r=e;var i=r;var a=1;while(L.is(r.car.car,"unquote")){i=r;a++;r=r.car.cdr.car}if(a>s){s=a}if(i===r){if(t.cdr.cdr!==P){return d(h(t.cdr.cdr),function(n){return new M(new vn(t.cdr.car,a),n)})}else{return new vn(t.cdr.car,a)}}else if(i.cdr.cdr!==P){return d(h(i.cdr.cdr),function(n){i.car.cdr=new M(new vn(r,a),i.cdr===P?P:n);return e.car})}else{i.car.cdr=new vn(r,a)}return e.car}return f(t,h)}return t}var t=function n(e){return a(e)||e instanceof vn};function r(n){if(n instanceof vn){if(s===n.count){return Fn(n.value,{env:c,dynamic_scope:o,error:u})}else{return d(r(n.value),function(n){return new M(new L("unquote"),new M(n,P))})}}return f(n,r,t)}return d(h(n.car),function(n){return d(r(n),dn)})}),"(quasiquote list ,value ,@value)\n\n Similar macro to `quote` but inside it you can use special\n expressions unquote abbreviated to , that will evaluate expresion inside\n and return its value or unquote-splicing abbreviated to ,@ that will\n evaluate expression but return value without parenthesis (it will join)\n the list with its value. Best used with macros but it can be used outside"),clone:v(function(n){return n.clone()},"(clone list)\n\n Function return clone of the list."),append:v(function(n,e){return this.get("append!")(n.clone(),e)},"(append list item)\n\n Function will create new list with value appended to the end. It return\n New list."),"append!":v(function(n,e){if(G(e)||H(e)){return n}return n.append(e)},"(append! name expression)\n\n Destructive version of append, it modify the list in place. It return\n original list."),reverse:v(function(n){if(n instanceof M){var e=this.get("list->array")(n).reverse();return this.get("array->list")(e)}else if(!(n instanceof Array)){throw new Error(wn("reverse",xn(n),"array or pair"))}else{return n.reverse()}},"(reverse list)\n\n Function will reverse the list or array. If value is not a list\n or array it will throw exception."),nth:v(function(n,e){if(e instanceof M){var t=e;var r=0;while(rarray")(e).join(n)},"(join separator list)\n\n Function return string by joining elements of the list"),split:v(function(n,e){return this.get("array->list")(e.split(n))},"(split separator string)\n\n Function create list by splitting string by separatar that can\n be a string or regular expression."),replace:v(function(n,e,t){return t.replace(n,e)},"(replace pattern replacement string)\n\n Function change patter to replacement inside string."),match:v(function(n,e){var t=e.match(n);return t?this.get("array->list")(t):P},"(match pattern string)\n\n function return match object from JavaScript as list."),search:v(function(n,e){return e.search(n)},"(search pattern string)\n\n Function return first found index of the pattern inside a string"),string:v(function e(n,t){if(typeof jQuery!=="undefined"&&n instanceof jQuery.fn.init){return"<#jQuery("+n.length+")>"}if(n instanceof ln){return n.value.toString()}if(typeof n==="undefined"){return"<#undefined>"}if(typeof n==="function"){if(K(n)){return"<#function(native)>"}return"<#function>"}if(n===P){return"nil"}if(n instanceof Array){return"["+n.map(function(n){return e(n,true)}).join(", ")+"]"}if(n===null||typeof n==="string"&&t){return JSON.stringify(n)}if(n instanceof M){return new In.Formatter(n.toString()).break().format()}if(n instanceof L){return n.toString()}if(c.HTMLElement&&n instanceof c.HTMLElement){return"<#HTMLElement(".concat(n.tagName.toLowerCase(),")>")}if(Tn(n)==="object"){var r=n.constructor.name;if(r!==""){return"<#"+r+">"}return"<#Object>"}if(typeof n!=="string"){return n.toString()}return n},"(string obj)\n\n Function return string LIPS representation of an object as string."),env:v(function(n){n=n||this;var e=Object.keys(n.env);var t;if(e.length){t=M.fromArray(e)}else{t=P}if(n.parent!==g){return this.get("env").call(this,n.parent).append(t)}return t},"(env obj)\n\n Function return list values (functions and variables) inside environment."),new:v(function(n){for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;rlist":v(function(n){return M.fromArray(n)},"(array->list array)\n\n Function convert JavaScript array to LIPS list."),"list->array":v(function(n){if(n instanceof M&&n.isEmptyList()){return[]}var e=[];var t=n;while(true){if(t instanceof M){if(t.haveCycles("cdr")){break}e.push(t.car);t=t.cdr}else{break}}return e},"(list->array list)\n\n Function convert LIPS list into JavaScript array."),apply:v(function(n,e){_n("call",n,"function",1);_n("call",e,"pair",2);return n.apply(void 0,Cn(this.get("list->array")(e)))},"(apply fn args)\n\n Function that call function with list of arguments."),length:v(function(n){if(!n){return ln(0)}if(n instanceof M){return ln(n.length())}if("length"in n){return ln(n.length)}},"(length expression)\n\n Function return length of the object, the object can be list\n or any object that have length property."),try:v(new J("try",function(a,n){var o=this;var u=n.dynamic_scope,c=n.error;return new Promise(function(i){var n={env:o,error:function n(e){var t=o.inherit("try");t.set(a.cdr.car.cdr.car.car,e);var r={env:t,error:c};if(u){r.dynamic_scope=o}d(Fn(new M(new L("begin"),a.cdr.car.cdr.cdr),r),function(n){i(n)})}};if(u){n.dynamic_scope=o}d(Fn(a.car,n),i).catch(n.error)})}),"(try expr (catch (e) code)"),find:v(function e(t,r){if(G(r)){return P}var n=s("find",t);return d(n(r.car),function(n){if(n){return r.car}return e(t,r.cdr)})},"(Find fn list)\n\n Higher order Function find first value for which function\n return true."),"for-each":v(function(n){_n("for-each",n,"function");for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;r1?e-1:0),a=1;a3?r-3:0),a=3;a3?i-3:0),o=3;oarray")(e);var a=[];var o=s("filter",n);return function e(t){function n(n){if(n){a.push(r)}return e(++t)}if(t===i.length){return M.fromArray(a)}var r=i[t];return d(o(r,t),n)}(0)},"(filter fn list)\n\n Higher order function that call `fn` for each element of the list\n and return list for only those elements for which funtion return\n true value."),range:v(function(n){if(n instanceof ln){n=n.valueOf()}return M.fromArray(new Array(n).fill(0).map(function(n,e){return ln(e)}))},"(range n)\n\n Function return list of n numbers from 0 to n - 1"),compose:v(en,"(compose . fns)\n\n Higher order function and create new function that apply all functions\n From right to left and return it's value. Reverse of compose.\n e.g.:\n ((compose (curry + 2) (curry * 3)) 3)\n 11\n "),pipe:v(nn,"(pipe . fns)\n\n Higher order function and create new function that apply all functions\n From left to right and return it's value. Reverse of compose.\n e.g.:\n ((pipe (curry + 2) (curry * 3)) 3)\n 15"),curry:v(cn,"(curry fn . args)\n\n Higher order function that create curried version of the function.\n The result function will have parially applied arguments and it\n will keep returning functions until all arguments are added\n\n e.g.:\n (define (add a b c d) (+ a b c d))\n (define add1 (curry add 1))\n (define add12 (add 2))\n (print (add12 3 4))"),odd:v(an(function(n){return ln(n).isOdd()}),"(odd number)\n Function check if number os odd."),even:v(an(function(n){return ln(n).isEvent()}),"(even number)\n\n Function check if number is even."),"*":v(un(function(n,e){return ln(n).mul(e)}),"(* . numbers)\n\n Multiplicate all numbers passed as arguments. If single value is passed\n it will return that value."),"+":v(un(function(n,e){return ln(n).add(e)}),"(+ . numbers)\n\n Sum all numbers passed as arguments. If single value is passed it will\n return that value."),"-":v(function(){for(var n=arguments.length,e=new Array(n),t=0;t":v(function(n,e){return ln(n).cmp(e)===1},"(> a b)\n\n Function compare two numbers and check if first argument is greater\n than the second one"),"<":v(function(n,e){return ln(n).cmp(e)===-1},"(< a b)\n\n Function compare two numbers and check if first argument is less\n than the second one"),"<=":v(function(n,e){return[0,-1].includes(ln(n).cmp(e))},"(<= a b)\n\n Function compare two numbers and check if first argument is less or\n equal to the second one"),">=":v(function(n,e){return[0,1].includes(ln(n).cmp(e))},"(>= a b)\n\n Function compare two numbers and check if first argument is less or\n equal to the second one"),"eq?":v(T,"(eq? a b)\n\n Function compare two values if they are identical."),or:v(new J("or",function(n,e){var i=e.dynamic_scope,a=e.error;var o=this.get("list->array")(n);var u=this;if(i){i=u}var c;return function e(){function n(n){c=n;if(c){return c}else{return e()}}var t=o.shift();if(typeof t==="undefined"){if(c){return c}else{return false}}else{var r=Fn(t,{env:u,dynamic_scope:i,error:a});return d(r,n)}}()}),"(or . expressions)\n\n Macro execute the values one by one and return the one that is truthy value.\n If there are no expression that evaluate to true it return false."),and:v(new J("and",function(n){var e=arguments.length>1&&arguments[1]!==g?arguments[1]:{},i=e.dynamic_scope,a=e.error;var o=this.get("list->array")(n);var u=this;if(i){i=u}if(!o.length){return true}var c;return function e(){function n(n){c=n;if(!c){return false}else{return e()}}var t=o.shift();if(typeof t==="undefined"){if(c){return c}else{return false}}else{var r=Fn(t,{env:u,dynamic_scope:i,error:a});return d(r,n)}}()}),"(and . expressions)\n\n Macro evalute each expression in sequence if any value return false it will\n return false. If each value return true it will return the last value.\n If it's called without arguments it will return true."),"|":v(function(n,e){return ln(n).or(e)},"(& a b)\n\n Function calculate or bit operation."),"&":v(function(n,e){return ln(n).and(e)},"(& a b)\n\n Function calculate and bit operation."),"~":v(function(n){return ln(n).neg()},"(~ number)\n\n Function negate the value."),">>":v(function(n,e){return ln(n).shr(e)},"(>> a b)\n\n Function right shit the value a by value b."),"<<":v(function(n,e){return ln(n).shl(e)},"(<< a b)\n\n Function left shit the value a by value b."),not:v(function(n){if(H(n)){return true}return!n},"(not object)\n\n Function return negation of the argument."),"->":v(function(n,e){for(var t=arguments.length,r=new Array(t>2?t-2:0),i=2;i obj name . args)\n\n Function get function from object and call it with arguments.")},g,"global");["floor","round","ceil"].forEach(function(e){mn.set(e,v(function(n){if(n instanceof ln){return n[e]()}throw new Error("".concat(Tn(n)," ").concat(n.toString()," is not a number"))},"(".concat(e," number)\n\n Function calculate ").concat(e," of a number.")))});function gn(n){if(n.length===1){return n[0]}else{var e=[];var t=gn(n.slice(1));for(var r=0;r3&&arguments[3]!==g?arguments[3]:null;var i=n?" in function `".concat(n,"`"):"";if(r!==null){i+=" argument ".concat(r)}return"Expecting ".concat(t," got ").concat(e).concat(i)}function _n(n,e,t){var r=arguments.length>3&&arguments[3]!==g?arguments[3]:null;var i=xn(e);if(i!==t){throw new Error(wn(n,i,t,r))}}function xn(n){var e={pair:M,symbol:L,macro:J,array:Array,native_symbol:c.Symbol};if(n===P){return"nil"}if(n===null){return"null"}var t=Object.entries(e);for(var r=0;r1&&arguments[1]!==g?arguments[1]:{},r=n.env,i=n.dynamic_scope,t=n.error,a=t===void 0?function(){}:t;try{if(i===true){r=i=r||mn}else if(r===true){r=i=mn}else{r=r||mn}var o={env:r,dynamic_scope:i,error:a};var u;if(G(e)){return e}if(H(e)){return B()}var c=e.car;var s=e.cdr;if(c instanceof M){u=kn(Fn(c,o));if(Y(u)){return u.then(function(n){return Fn(new M(n,e.cdr),o)})}else if(typeof u!=="function"){throw new Error(xn(u)+" "+r.get("string")(u)+" is not a function while evaluating "+e.toString())}}if(c instanceof L){u=r.get(c,true);if(u instanceof J){return d(Sn(u,s,o),function(n){if(n instanceof M){return n.markCycles()}return n})}else if(typeof u!=="function"){if(u){var f="".concat(xn(u)," `").concat(u,"' is not a function");throw new Error(f)}throw new Error("Unknown function `".concat(c.name,"'"))}}else if(typeof c==="function"){u=c}if(typeof u==="function"){var l=jn(s,o);return d(l,function(n){var e=i||r;var t=kn(u.apply(e,n));return d(t,function(n){if(n instanceof M){return dn(n.markCycles())}return n},a)})}else if(e instanceof L){u=r.get(e);if(u==="undefined"){throw new Error("Unbound variable `"+e.name+"'")}return u}else if(e instanceof M){u=c&&c.toString();throw new Error("".concat(xn(c)," ").concat(u," is not a function"))}else{return e}}catch(n){a&&a(n,e)}}function En(n,e,t){return An.apply(this,arguments)}function An(){An=Bn(Mn.mark(function n(t,r,i){var a,o,u,c,s;return Mn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:if(i===true){r=i=r||mn}else if(r===true){r=i=mn}else{r=r||mn}a=b(t,true).map(function(n){if(n.token.match(l)&&n.col){var e=new RegExp("^ {".concat(n.col+1,"}"));return n.token.split("\n").map(function(n){return n.replace(e,"")}).join("\n")}return n.token.trim()}).filter(function(n){return n&&!n.match(/^;/)});o=p(a);u=[];case 4:c=o.shift();if(c){e.next=10;break}return e.abrupt("return",u);case 10:e.next=12;return Fn(c,{env:r,dynamic_scope:i,error:function n(e,t){if(t){e.code=t.toString()}throw e}});case 12:s=e.sent;u.push(s);case 14:e.next=4;break;case 16:case"end":return e.stop()}}},n)}));return An.apply(this,arguments)}function qn(e){return function(n){if(!n){return false}return(n.token||n).match(e)}}var Nn=qn(/[()]/);function On(n){var e=typeof n==="string"?b(n):n;var t=e.filter(Nn);var r=t.filter(function(n){return(n.token||n)===")"});var i=t.filter(function(n){return(n.token||n)==="("});return r.length===i.length}M.unDry=function(n){return new M(n.car,n.cdr)};M.prototype.toDry=function(){return{value:{car:this.car,cdr:this.cdr}}};I.prototype.toDry=function(){return{value:null}};I.unDry=function(){return P};L.prototype.toDry=function(){return{value:{name:this.name}}};L.unDry=function(n){return new L(n.name)};function Ln(){var i="text/x-lips";if(window.document){var a=Array.from(document.querySelectorAll("script"));return function n(){var e=a.shift();if(e){var t=e.getAttribute("type");if(t===i){var r=e.getAttribute("src");if(r){return c.fetch(r).then(function(n){return n.text()}).then(En).then(n)}else{return En(e.innerHTML).then(n)}}else if(t&&t.match(/lips|lisp/)){console.warn("Expecting "+i+" found "+t)}return n()}}()}}if(typeof window!=="undefined"){n(window,Ln)}var In={version:"0.11.2",exec:En,parse:p,tokenize:b,evaluate:Fn,Environment:pn,global_environment:mn,globalEnvironment:mn,env:mn,balanced_parenthesis:On,balancedParenthesis:On,Macro:J,quote:dn,Pair:M,Formatter:j,specials:m,nil:P,resolvePromises:kn,Symbol:L,LNumber:ln};mn.set("lips",In);return In})})(); \ No newline at end of file diff --git a/src/lips.js b/src/lips.js index d9bdc299..5d5592e0 100644 --- a/src/lips.js +++ b/src/lips.js @@ -2027,14 +2027,26 @@ Function return new Pair out of two arguments.`), // ------------------------------------------------------------------ car: doc(function(list) { + if (list === nil) { + return nil; + } typecheck('car', list, 'pair'); + if (isEmptyList(list)) { + return nil; + } return list.car; }, `(car pair) Function returns car (head) of the list/pair.`), // ------------------------------------------------------------------ cdr: doc(function(list) { + if (list === nil) { + return nil; + } typecheck('cdr', list, 'pair'); + if (isEmptyList(list)) { + return nil; + } return list.cdr; }, `(cdr pair) @@ -3513,6 +3525,10 @@ const name = 'c' + spec + 'r'; global_env.set(name, doc(function(arg) { return chars.reduce(function(list, type) { + if (list === nil) { + return nil; + } + typecheck(name, list, 'pair'); if (type === 'a') { return list.car; } else {