From b98cb7256e814db5de6229a4dff1cee6ec4ed30b Mon Sep 17 00:00:00 2001 From: Jakub Jankiewicz Date: Tue, 23 Apr 2019 09:58:52 +0200 Subject: [PATCH] fix print and stdout in user code & better babel fix --- CHANGELOG.md | 4 ++++ README.md | 6 +++--- dist/lips.js | 42 +++++++++++++++++++++++++----------------- dist/lips.min.js | 6 +++--- src/lips.js | 35 +++++++++++++++++++++++------------ 5 files changed, 58 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a18ca4a..9e4373fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.10.3 +### Bug fixes +* fix print that get stdout from global env not from user one + better babel fix added in 0.10.1 + ## 0.10.2 ### Bug fixes * fix version number in source code diff --git a/README.md b/README.md index 6e834bb2..f61bf6f0 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ ## LIPS is Pretty Simple -[![npm](https://img.shields.io/badge/npm-0.10.2-blue.svg)](https://www.npmjs.com/package/@jcubic/lips) -[![travis](https://travis-ci.org/jcubic/jquery.terminal.svg?branch=master&dbc5022acc1943aee088a1c22f809abea1406e54)](https://travis-ci.org/jcubic/jquery.terminal) -[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=master&4b1fff165354dc9dcd90095d14379037)](https://coveralls.io/github/jcubic/lips?branch=master) +[![npm](https://img.shields.io/badge/npm-DEV-blue.svg)](https://www.npmjs.com/package/@jcubic/lips) +[![travis](https://travis-ci.org/jcubic/jquery.terminal.svg?branch=devel&ebe5b27f4c4a5bd291ad1ca0c9a7c1201a18404a)](https://travis-ci.org/jcubic/jquery.terminal) +[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&4b1fff165354dc9dcd90095d14379037)](https://coveralls.io/github/jcubic/lips?branch=devel) LIPS is very simple Lisp, similar to Scheme written in JavaScript. diff --git a/dist/lips.js b/dist/lips.js index 8033197f..ef3fb33a 100644 --- a/dist/lips.js +++ b/dist/lips.js @@ -1,5 +1,5 @@ /**@license - * LIPS is Pretty Simple - simple scheme like lisp in JavaScript - v. 0.10.2 + * LIPS is Pretty Simple - simple scheme like lisp in JavaScript - v. DEV * * Copyright (c) 2018-2019 Jakub T. Jankiewicz * Released under the MIT license @@ -21,7 +21,7 @@ * http://javascript.nwbox.com/ContentLoaded/ * http://javascript.nwbox.com/ContentLoaded/MIT-LICENSE * - * build: Mon, 22 Apr 2019 20:50:49 +0000 + * build: Tue, 23 Apr 2019 07:56:30 +0000 */ (function () { 'use strict'; @@ -2272,6 +2272,15 @@ function _typeof(obj) { } // ---------------------------------------------------------------------- + function unbind(obj) { + if (typeof obj === 'function' && obj.__bind) { + return obj.__bind.fn; + } + + return obj; + } // ---------------------------------------------------------------------- + + function setFnLength(fn, length) { try { Object.defineProperty(fn, 'length', { @@ -2836,7 +2845,10 @@ function _typeof(obj) { }; // ---------------------------------------------------------------------- - Environment.prototype.get = function (symbol) { + Environment.prototype.get = function (symbol, weak, context) { + // we keep original environment as context for bind + // so print will get user stdout + context = context || this; var value; var defined = false; @@ -2858,14 +2870,18 @@ function _typeof(obj) { } if (typeof value === 'function') { - return weakBind(value, this); + if (weak) { + return weakBind(value, context); + } + + return value.bind(context); } return value; } if (this.parent instanceof Environment) { - return this.parent.get(symbol); + return this.parent.get(symbol, weak, context); } else { var name; @@ -3820,13 +3836,7 @@ function _typeof(obj) { 'get': get, '.': get, // ------------------------------------------------------------------ - 'unbind': doc(function (obj) { - if (typeof obj === 'function' && obj.__bind) { - return obj.__bind.fn; - } - - return obj; - }, "(unbind fn)\n Function remove bidning from function so you can get props from it."), + 'unbind': doc(unbind, "(unbind fn)\n\n Function remove bidning from function so you can get props from it."), // ------------------------------------------------------------------ type: doc(type, "(type object)\n\n Function return type of an object as string."), // ------------------------------------------------------------------ @@ -3980,8 +3990,6 @@ function _typeof(obj) { }, "(Find fn list)\n\n Higher order Function find first value for which function\n return true."), // ------------------------------------------------------------------ 'for-each': doc(function (fn) { - var _this$get2; - typecheck('for-each', fn, 'function'); // we need to use call(this because babel transpile this code into: // var ret = map.apply(void 0, [fn].concat(args)); // it don't work with weakBind @@ -3990,7 +3998,7 @@ function _typeof(obj) { args[_key18 - 1] = arguments[_key18]; } - var ret = (_this$get2 = this.get('map')).call.apply(_this$get2, [this, fn].concat(args)); + var ret = this.get('map').apply(void 0, [fn].concat(args)); if (isPromise(ret)) { return ret.then(function () {}); @@ -4633,7 +4641,7 @@ function _typeof(obj) { } if (first instanceof _Symbol) { - value = env.get(first); + value = env.get(first, true); if (value instanceof Macro) { return evaluate_macro(value, rest, eval_args); @@ -4855,7 +4863,7 @@ function _typeof(obj) { return { - version: '0.10.2', + version: 'DEV', exec: exec, parse: parse, tokenize: tokenize, diff --git a/dist/lips.min.js b/dist/lips.min.js index b3240042..da1832ac 100644 --- a/dist/lips.min.js +++ b/dist/lips.min.js @@ -1,5 +1,5 @@ /**@license - * LIPS is Pretty Simple - simple scheme like lisp in JavaScript - v. 0.10.2 + * LIPS is Pretty Simple - simple scheme like lisp in JavaScript - v. DEV * * Copyright (c) 2018-2019 Jakub T. Jankiewicz * Released under the MIT license @@ -21,6 +21,6 @@ * http://javascript.nwbox.com/ContentLoaded/ * http://javascript.nwbox.com/ContentLoaded/MIT-LICENSE * - * build: Mon, 22 Apr 2019 20:50:49 +0000 + * build: Tue, 23 Apr 2019 07:56:30 +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 kn(n,e,t){if(r()){kn=Reflect.construct}else{kn=function n(e,t,r){var i=[null];i.push.apply(i,t);var o=Function.bind.apply(e,i);var a=new o;if(r)setPrototypeOf(a,r.prototype);return a}}return kn.apply(null,arguments)}function i(n){if(Array.isArray(n))return n}function o(n,e){var t=[];var r=true;var i=false;var o=undefined;try{for(var a=n[Symbol.iterator](),u;!(r=(u=a.next()).done);r=true){t.push(u.value);if(e&&t.length===e)break}}catch(n){i=true;o=n}finally{try{if(!r&&a["return"]!=null)a["return"]()}finally{if(i)throw o}}return t}function a(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function jn(n,e){return i(n)||o(n,e)||a()}function n(n,e){return e={exports:{}},n(e,e.exports),e.exports}var e=n(function(n){var e=function(o){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 a(n,e,t,r){var i=e&&e.prototype instanceof u?e:u;var o=Object.create(i.prototype);var a=new A(r||[]);o._invoke=j(n,t,a);return o}o.wrap=a;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)}})}o.isGeneratorFunction=function(n){var e=typeof n==="function"&&n.constructor;return e?e===y||(e.displayName||e.name)==="GeneratorFunction":false};o.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};o.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 o=i.arg;var a=o.value;if(a&&typeof a==="object"&&s.call(a,"__await")){return Promise.resolve(a.__await).then(function(n){c("next",n,t,r)},function(n){c("throw",n,t,r)})}return Promise.resolve(a).then(function(n){o.value=n;t(o)},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};o.AsyncIterator=k;o.async=function(n,e,t,r){var i=new k(a(n,e,t,r));return o.isGeneratorFunction(e)?i:i.next().then(function(n){return n.done?n.value:i.next()})};function j(a,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=F(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 o=f(a,u,c);if(o.type==="normal"){s=c.done?d:h;if(o.arg===v){continue}return{value:o.arg,done:c.done}}else if(o.type==="throw"){s=d;c.method="throw";c.arg=o.arg}}}}function F(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;F(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 S(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(S,this);this.reset(true)}o.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 o=i.completion;if(i.tryLoc==="root"){return n("end")}if(i.tryLoc<=this.prev){var a=s.call(i,"catchLoc");var u=s.call(i,"finallyLoc");if(a&&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 o}(n.exports);try{regeneratorRuntime=e}catch(n){Function("r","regeneratorRuntime = r")(e)}});var Fn=e;function c(n,e,t,r,i,o,a){try{var u=n[o](a);var c=u.value}catch(n){t(n);return}if(u.done){e(c)}else{Promise.resolve(c).then(r,i)}}function Sn(u){return function(){var n=this,a=arguments;return new Promise(function(e,t){var r=u.apply(n,a);function i(n){c(r,e,t,i,o,"next",n)}function o(n){c(r,e,t,i,o,"throw",n)}i(undefined)})}}function u(n){if(Array.isArray(n)){for(var e=0,t=new Array(n.length);e1&&arguments[1]!==d?arguments[1]:1;return n[n.length-e]}function a(n){if(typeof n==="string"){var e=/([-\\^$[\]()+{}?*.|])/g;return n.replace(e,"\\$1")}}function c(n){var i=e();n=n.replace(/\n\r|\r/g,"\n");var o=0;var a=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:a,token:n,offset:o};u.push(r);c.push(r);o+=n.length;s+=n.length;a+=(n.match("\n")||[]).length;return}n.split(i).filter(Boolean).forEach(function(n){var e={col:s,line:a,token:n,offset:o};s+=n.length;o+=n.length;u.push(e);c.push(e);if(n==="\n"){++a;c=[];s=0}})});return u}function p(n,e){if(e){return c(n)}else{return c(n).map(function(n){return n.token.trim()}).filter(function(n){return n&&!n.match(/^;/)})}}var y={"'":new S("quote"),"`":new S("quasiquote"),",@":new S("unquote-splicing"),",":new S("unquote")};function m(n){var o=[];var a=[];var u=null;var c=Object.keys(y);var e=c.map(function(n){return y[n].name});var s=0;var f=false;var l=[];var h=[];var p=0;function d(){var n=o[o.length-1];if(n instanceof Array&&n[0]instanceof S&&e.includes(n[0].name)&&o.length>1){o.pop();if(o[o.length-1].length===1&&o[o.length-1][0]instanceof S){o[o.length-1].push(n)}else if(o[o.length-1].length===0){o[o.length-1]=n}else if(o[o.length-1]instanceof q){if(o[o.length-1].cdr instanceof q){o[o.length-1]=new q(o[o.length-1],q.fromArray(n))}else{o[o.length-1].cdr=q.fromArray(n)}}else{o[o.length-1].push(n)}}}n.forEach(function(n){var e=o[o.length-1];if(c.indexOf(n)!==-1){p++;u=n;o.push([y[u]]);if(!u){h=[]}h.push(u)}else{if(u){l.push(h);h=[]}if(n==="("){f=true;s++;o.push([]);u=null;p=0}else if(n==="."&&!f){o[o.length-1]=q.fromArray(e)}else if(n===")"){s--;if(!o.length){throw new Error("Unbalanced parenthesis")}if(o.length===1){a.push(o.pop())}else if(o.length>1){var t=o.pop();e=o[o.length-1];if(e instanceof Array){e.push(t)}else if(e instanceof q){e.append(q.fromArray(t))}if(l.length){h=l.pop();while(h.length){d();h.pop()}}else{d()}}if(s===0&&o.length){a.push(o.pop())}}else{f=false;var r=v(n);if(u){while(p--){o[o.length-1][1]=r;r=o.pop()}p=0;u=false}e=o[o.length-1];if(e instanceof q){var i=e;while(true){if(i.cdr===A){if(r instanceof Array){i.cdr=q.fromArray(r)}else{i.cdr=r}break}else{i=i.cdr}}}else if(!o.length){a.push(r)}else{e.push(r)}}}});if(o.length){throw new Error("Unbalanced parenthesis 2")}return a.map(function(n){if(n instanceof Array){return q.fromArray(n)}return n})}function g(n){var e=arguments.length>1&&arguments[1]!==d?arguments[1]:function(n){return n};if(H(n)){return n.then(e)}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 b(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=1;var t=n.length;while(e>0){var r=n[--t];if(!r){return}if(r.token==="("){e--}else if(r.token===")"){e++}}return n.slice(t)}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){this._code=n.replace(/\r/g,"")}k.defaults={offset:0,indent:2,specials:["define","lambda","let","let*","define-macro"]};k.prototype._options=function n(e){var t=k.defaults;if(typeof e==="undefined"){return Object.assign({},t)}var r=e&&e.specials||[];return Object.assign({},t,e,{specials:t.specials.concat(r)})};k.prototype.indent=function n(e){var t=p(this._code,true);return this._indent(t,e)};k.prototype._indent=function n(e,t){var r=this._options(t);var i=x(e);var o=r.specials;var a=_(e);if(a){if(a[0].line>0){r.offset=0}if(a.length===1){return r.offset+a[0].col+1}else if(o.indexOf(a[1].token)!==-1){return r.offset+a[0].col+r.indent}else if(a[0].line3&&a[1].line===a[3].line){if(a[1].token==="("){return r.offset+a[1].col}return r.offset+a[3].col}else if(a[0].line===a[1].line){return r.offset+r.indent+a[0].col}else{var u=a.slice(2);for(var c=0;c"}else if(typeof n==="string"){return JSON.stringify(n)}else if(H(n)){return"<#Promise>"}else if(n instanceof S||n instanceof nn||n instanceof q||n===A){return n.toString()}else if(n instanceof Array){return n.map(L)}else if(An(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}}q.prototype.toString=function(){var n=["("];if(this.car!==d){var e=L(this.car);if(e){n.push(e)}if(this.cdr instanceof q){n.push(" ");n.push(this.cdr.toString().replace(/^\(|\)$/g,""))}else if(typeof this.cdr!=="undefined"&&this.cdr!==A){if(typeof this.cdr==="string"){n=n.concat([" . ",JSON.stringify(this.cdr)])}else{n=n.concat([" . ",L(this.cdr)])}}}n.push(")");return n.join("")};q.prototype.append=function(n){if(n instanceof Array){return this.append(q.fromArray(n))}var e=this;if(e.car===d){if(n instanceof q){this.car=n.car;this.cdr=n.cdr}else{this.car=n}}else{while(true){if(e instanceof q&&e.cdr!==A){e=e.cdr}else{break}}if(n instanceof q){e.cdr=n}else if(n!==A){e.cdr=new q(n,A)}}return this};function O(n,e){if(n instanceof nn&&e instanceof nn){return n.cmp(e)===0}else if(typeof n==="number"||typeof e==="number"){return nn(n).cmp(nn(e))}else if(n instanceof S&&e instanceof S){return n.name===e.name}else{return n===e}}function I(n){return n instanceof q&&n.isEmptyList()||n===A}function P(n,e,t){if(typeof this!=="undefined"&&this.constructor!==P||typeof this==="undefined"){return new P(n,e)}ln("Macro",n,"string",1);ln("Macro",e,"function",2);this.__doc__=t;this.name=n;this.fn=e}P.defmacro=function(n,e,t){var r=new P(n,e,t);r.defmacro=true;return r};P.prototype.invoke=function(n,e,t){var r=e.env,i=e.dynamic_scope,o=e.error;var a={dynamic_scope:i,error:o,macro_expand:t};var u=this.fn.call(r,n,a,this.name);return t?rn(u):u};P.prototype.toString=function(){return"#"};var M="define-macro";function B(o){return function(){var t=Sn(Fn.mark(function n(t,c){var s,f,r,i;return Fn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:r=function n(){r=Sn(Fn.mark(function n(t){var r,i,o,a,u;return Fn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:if(!(t instanceof q&&t.car instanceof S)){e.next=13;break}e.prev=1;r=s.get(t.car);if(!(r instanceof P&&r.defmacro)){e.next=9;break}e.next=6;return r.invoke(t.cdr,c,true);case 6:i=e.sent;if(!(i instanceof q)){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:o=t.car;if(!(o instanceof q)){e.next=18;break}e.next=17;return f(o);case 17:o=e.sent;case 18:a=t.cdr;if(!(a instanceof q)){e.next=23;break}e.next=22;return f(a);case 22:a=e.sent;case 23:u=new q(o,a);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(!o){e.next=12;break}e.t0=rn;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",rn(i.car));case 22:case"end":return e.stop()}}},n,this)}));return function(n,e){return t.apply(this,arguments)}}()}function R(n){return typeof n==="undefined"||n===A||n===null}function T(n){return typeof n==="function"&&n.toString().match(/\{\s*\[native code\]\s*\}/)}function H(n){return n instanceof Promise||n&&typeof n!=="undefined"&&typeof n.then==="function"}function J(n,o){var a=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")(o.car);var f=s.inherit("let");var l=0;return function n(){var e=c[l++];function t(n){if(H(n)){return n.then(t)}else if(typeof n==="undefined"){f.set(e.car,A)}else{f.set(e.car,n)}}if(a){a=h?f:s}if(!e){var r=new q(new S("begin"),o.cdr);return yn(r,{env:f,dynamic_scope:a,error:u})}else{var i=yn(e.cdr.car,{env:h?f:s,dynamic_scope:a,error:u});return g(t(i),n)}}()})}function G(n){for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;r2?r-2:0),o=2;o1?n-1:0),t=1;t=a){return o.apply(this,r)}else{return i}}return i.apply(this,arguments)}}function W(r,i){ln("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 nn(this.value.clone()[n](e,r))}};var en={"+":"add","-":"sub","*":"mul","/":"div","%":"mod","|":"or","&":"and","~":"neg","<<":"shl",">>":"shr"};Object.keys(en).forEach(function(e){nn.prototype[en[e]]=function(n){return this.op(e,n)}});nn.prototype.sqrt=function(){var n;if(nn.isNative(this.value)){n=Math.sqrt(this.value)}else if(nn.isBN(this.value)){n=this.value.sqrt()}return new nn(n)};nn.prototype.pow=function(n){n=this.coerce(n);if(nn.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(nn.isBN(this.value)){n.value=this.value.pow(n.value)}else{n.value=Math.pow(this.value,n.value)}return n};nn.prototype.neg=function(){var n=this.value;if(nn.isNative(n)){n=-n}else if(nn.isBN(n)){n=n.neg()}return new nn(n)};nn.prototype.abs=function(){var n=this.value;if(nn.isNative(this.value)){if(n<0){n=-n}}else if(nn.isBN(n)){n.iabs()}return new nn(n)};nn.prototype.isOdd=function(){if(nn.isNative(this.value)){if(this.isBigNumber()){return this.value%BigInt(2)===BigInt(1)}return this.value%2===1}else if(nn.isBN(this.value)){return this.value.isOdd()}};nn.prototype.isEven=function(){return!this.isOdd()};nn.prototype.cmp=function(n){n=this.coerce(n);if(nn.isNative(this.value)){if(this.value1&&arguments[1]!==d?arguments[1]:{};if(An(n)==="object"){e=n}if(!n||An(n)==="object"){n="child of "+(this.name||"unknown")}return new tn(e||{},this,n)};tn.prototype.get=function(n){var e;var t=false;if(n instanceof S){if(n.name in this.env){e=this.env[n.name];t=true}}else if(typeof n==="string"){if(typeof this.env[n]!=="undefined"){e=this.env[n];t=true}}if(t){if(nn.isNumber(e)){return nn(e)}if(typeof e==="function"){return J(e,this)}return e}if(this.parent instanceof tn){return this.parent.get(n)}else{var r;if(n instanceof S){r=n.name}else if(typeof n==="string"){r=n}if(r){var i=An(u[r]);if(i==="function"){if(T(u[r])){return u[r].bind(u)}else{return u[r]}}else if(i!=="undefined"){return u[r]}}}r=(r.name||r).toString();throw new Error("Unbound variable `"+r+"'")};tn.prototype.set=function(n,e){if(nn.isNumber(e)){e=nn(e)}if(n instanceof S){n=n.name}this.env[n]=e};tn.prototype.has=function(n){return typeof this.env[n]!=="undefined"};tn.prototype.ref=function(n){var e=this;while(true){if(!e){break}if(e.has(n)){return e}e=e.parent}};function rn(n){if(H(n)){return n.then(rn)}if(n instanceof q||n instanceof S){n.data=true}return n}function on(n,e){this.value=n;this.count=e}on.prototype.toString=function(){return"<#unquote["+this.count+"] "+this.value+">"};var an=function(){var e=0;return function(){var n=arguments.length>0&&arguments[0]!==d?arguments[0]:null;if(n!==null){return new S(u.Symbol("#"+n))}e++;return new S(u.Symbol("#gensym_"+e))}}();var un=new tn({nil:A,undefined:d,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:b(function(n){return n.__doc__},"(help object)\n\n Function returns documentation for function or macro."),cons:b(function(n,e){if(I(e)){e=A}return new q(n,e)},"(cons left right)\n\n Function return new Pair out of two arguments."),car:b(function(n){ln("car",n,"pair");return n.car},"(car pair)\n\n Function returns car (head) of the list/pair."),cdr:b(function(n){ln("cdr",n,"pair");return n.cdr},"(cdr pair)\n\n Function returns cdr (tail) of the list/pair."),"set!":b(new P("set!",function(e){var n=arguments.length>1&&arguments[1]!==d?arguments[1]:{},t=n.dynamic_scope,r=n.error;if(t){t=this}var i=yn(e.cdr.car,{env:this,dynamic_scope:t,error:r});i=pn(i);var o;function a(e,t){if(H(e)){return e.then(function(n){return a(n,t)})}if(H(t)){return t.then(function(n){return a(e,n)})}s[e]=t;return t}if(e.car instanceof q&&S.is(e.car.car,".")){var u=e.car.cdr.car;var c=e.car.cdr.cdr.car;var s=yn(u,{env:this,dynamic_scope:t,error:r});var f=yn(c,{env:this,dynamic_scope:t,error:r});return a(f,i)}if(!(e.car instanceof S)){throw new Error("set! first argument need to be a symbol or "+"dot accessor that evaluate to object.")}o=this.ref(e.car.name);if(!o){o=this}return g(i,function(n){o.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!":b(function(n,e){ln("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!":b(function(n,e){ln("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?":b(function(n){return typeof n==="undefined"||I(n)},"(empty? object)\n\n Function return true if value is undfined empty list."),assoc:b(function(n,e){if(n instanceof q&&!(e instanceof q)){throw new Error("First argument to assoc ned to be a key")}var t=e;while(true){if(!(t instanceof q)||this.get("empty?")(t)){break}var r=t.car.car;if(O(r,n)){return t.car}else{t=t.cdr}}return A},"(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:b(an,"(gensym)\n\n Function generate unique symbol, to use with macros as meta name."),load:b(function(n){ln("load",n,"string");return u.fetch(n).then(function(n){return n.text()}).then(mn)},"(load filename)\n\n Function fetch the file and evaluate its content as LIPS code."),while:b(new P("while",function(r,n){var i=n.dynamic_scope,o=n.error;var a=this;var u=new q(new S("begin"),r.cdr);var c;if(i){i=a}return function e(){var n=yn(r.car,{env:a,dynamic_scope:i,error:o});function t(n){if(n&&!R(n)&&!I(n)){c=yn(u,{env:a,dynamic_scope:i,error:o});if(H(c)){return c.then(function(n){c=n;return e()})}else{return e()}}else{return c}}return g(n,t)}()}),"(while cond . body)\n\n Macro that create a loop, it exectue body untill cond expression is false"),if:b(new P("if",function(t,n){var r=n.dynamic_scope,i=n.error;if(r){r=this}var o=this;var e=function n(e){ln("if",e,"boolean");if(e){return yn(t.cdr.car,{env:o,dynamic_scope:r,error:i})}else{return yn(t.cdr.cdr.car,{env:o,dynamic_scope:r,error:i})}};var a=yn(t.car,{env:o,dynamic_scope:r,error:i});return g(a,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*":b($(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:b($(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:b(new P("begin",function(n,e){var r=e.dynamic_scope,i=e.error;var o=this.get("list->array")(n);if(r){r=this}var a=this;var u;return function e(){if(o.length){var n=o.shift();var t=yn(n,{env:a,dynamic_scope:r,error:i});return g(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."),nop:b(function(){},"(nop)\n\n Empty function you can pass list of exressions to the function.\n like every function each expression will be evaluated and it will\n not return any value. you can also put this function as last to\n let or begin. This function is usefull if you want to return\n undefined, like when you call function from terminal and don't\n want any output."),timer:b(new P("timer",function(e){var n=arguments.length>1&&arguments[1]!==d?arguments[1]:{},t=n.dynamic_scope,r=n.error;ln("timer",e.car,"number");var i=this;if(t){t=this}return new Promise(function(n){setTimeout(function(){n(yn(e.cdr.car,{env:i,dynamic_scope:t,error:r}))},e.car)}).then(rn)}),"(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:b(P.defmacro("define",function(e,n){var t=this;if(e.car instanceof q&&e.car.car instanceof S){var r=new q(new S("define"),new q(e.car.car,new q(new q(new S("lambda"),new q(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 q){i=yn(i,n)}else if(i instanceof S){i=t.get(i)}if(e.car instanceof S){g(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!":b(function(n,e,t){if(An(n)!=="object"||R(n)){throw new Error(fn("set-obj!",hn(n),"object"))}n[e]=t},"(set-obj! obj key value)\n\n Function set property of JavaScript object"),eval:b(function(n){var t=this;if(n instanceof q){return yn(n,{env:this,dynamic_scope:this,error:function n(e){return t.get("print")(e.message)}})}if(n instanceof Array){var e;n.forEach(function(n){e=yn(n,{env:t,dynamic_scope:t,error:function n(e){return t.get("print")(e.message)}})});return e}},"(eval list)\n\n Function evalute LIPS code as list structure."),lambda:new P("lambda",function(s){var n=arguments.length>1&&arguments[1]!==d?arguments[1]:{},f=n.dynamic_scope,l=n.error;var h=this;var p;if(s.cdr instanceof q&&typeof s.cdr.car==="string"&&s.cdr.cdr!==A){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 S||!I(e)){for(var i=arguments.length,o=new Array(i),a=0;a2&&arguments[2]!==d?arguments[2]:o;if(n instanceof q&&!I(n)){var r=n.car;var i=n.cdr;if(t(r)){r=e(r)}if(t(i)){i=e(i)}if(H(r)||H(i)){return Promise.all([r,i]).then(function(n){var e=jn(n,2),t=e[0],r=e[1];return new q(t,r)})}else{return new q(r,i)}}return n}function l(n,e){if(n instanceof q){n.append(e)}else{n=new q(n,e)}return n}function h(t){if(t instanceof q&&!I(t)){var n;if(S.is(t.car.car,"unquote-splicing")){n=yn(t.car.cdr.car,{env:c,dynamic_scope:a,error:u});return g(n,function(e){if(!e instanceof q){throw new Error("Value of unquote-splicing need"+" to be pair")}var n=h(t.cdr);return g(n,function(n){return l(e,n)})})}if(S.is(t.car,"unquote")){var e=t.cdr;var r=e;var i=r;var o=1;while(S.is(r.car.car,"unquote")){i=r;o++;r=r.car.cdr.car}if(o>s){s=o}if(i===r){if(t.cdr.cdr!==A){return g(h(t.cdr.cdr),function(n){return new q(new on(t.cdr.car,o),n)})}else{return new on(t.cdr.car,o)}}else if(i.cdr.cdr!==A){return g(h(i.cdr.cdr),function(n){i.car.cdr=new q(new on(r,o),i.cdr===A?A:n);return e.car})}else{i.car.cdr=new on(r,o)}return e.car}return f(t,h)}return t}var t=function n(e){return o(e)||e instanceof on};function r(n){if(n instanceof on){if(s===n.count){return yn(n.value,{env:c,dynamic_scope:a,error:u})}else{return g(r(n.value),function(n){return new q(new S("unquote"),new q(n,A))})}}return f(n,r,t)}return g(h(n.car),function(n){return g(r(n),rn)})}),"(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:b(function(n){return n.clone()},"(clone list)\n\n Function return clone of the list."),append:b(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!":b(function(n,e){if(R(e)||I(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:b(function(n){if(n instanceof q){var e=this.get("list->array")(n).reverse();return this.get("array->list")(e)}else if(!(n instanceof Array)){throw new Error(fn("reverse",hn(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:b(function(n,e){if(e instanceof q){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:b(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:b(function(n,e,t){return t.replace(n,e)},"(replace pattern replacement string)\n\n Function change patter to replacement inside string."),match:b(function(n,e){return this.get("array->list")(e.match(n))},"(match pattern string)\n\n function return match object from JavaScript as list."),search:b(function(n,e){return e.search(n)},"(search pattern string)\n\n Function return first found index of the pattern inside a string"),string:b(function e(n,t){if(typeof jQuery!=="undefined"&&n instanceof jQuery.fn.init){return"<#jQuery("+n.length+")>"}if(n instanceof nn){return n.value.toString()}if(typeof n==="undefined"){return"<#undefined>"}if(typeof n==="function"){if(T(n)){return"<#function(native)>"}return"<#function>"}if(n===A){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 q||n instanceof S){return n.toString()}if(u.HTMLElement&&n instanceof u.HTMLElement){return"<#HTMLElement(".concat(n.tagName.toLowerCase(),")>")}if(An(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:b(function(n){n=n||this;var e=Object.keys(n.env);var t;if(e.length){t=q.fromArray(e)}else{t=A}if(n.parent!==d){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:b(function(n){for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;rlist":b(function(n){return q.fromArray(n)},"(array->list array)\n\n Function convert JavaScript array to LIPS list."),"list->array":b(function(n){if(n instanceof q&&n.isEmptyList()){return[]}var e=[];var t=n;while(true){if(t instanceof q){e.push(t.car);t=t.cdr}else{break}}return e},"(list->array list)\n\n Function convert LIPS list into JavaScript array."),apply:b(function(n,e){ln("call",n,"function",1);ln("call",e,"pair",2);return n.apply(void 0,En(this.get("list->array")(e)))},"(apply fn args)\n\n Function that call function with list of arguments."),length:b(function(n){if(!n){return nn(0)}if(n instanceof q){return nn(n.length())}if("length"in n){return nn(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."),find:b(function e(t,r){if(R(r)){return A}var n=s("find",t);return g(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":b(function(n){var e;ln("for-each",n,"function");for(var t=arguments.length,r=new Array(t>1?t-1:0),i=1;i1?n-1:0),r=1;rarray")(n)});var a=[];return function e(t){function n(n){a.push(n);return e(++t)}if(t===o[0].length){return q.fromArray(a)}var r=o.map(function(n,e){return o[e][t]});return g(i.apply(void 0,En(r)),n)}(0)},"(map fn . args)\n\n Higher order function that call function `fn` by for each\n value of the argument. If you provide more then one list as argument\n it will take each value from each list and call `fn` function\n with that many argument as number of list arguments. The return\n values of the function call is acumulated in result list and\n returned by the call to map."),some:b(function e(t,r){if(R(r)){return false}else{return g(t(r.car),function(n){return n||e(t,r.cdr)})}},"(some fn list)\n\n Higher order function that call argument on each element of the list.\n It stops when function fn return true for a value if so it will\n return true. If it don't find the value it will return false"),fold:b(U("fold",function(n,e,t){for(var r=arguments.length,i=new Array(r>3?r-3:0),o=3;o3?i-3:0),a=3;aarray")(e);var o=[];var a=s("filter",n);return function e(t){function n(n){if(n){o.push(r)}return e(++t)}if(t===i.length){return q.fromArray(o)}var r=i[t];return g(a(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:b(function(n){if(n instanceof nn){n=n.valueOf()}return q.fromArray(new Array(n).fill(0).map(function(n,e){return nn(e)}))},"(range n)\n\n Function return list of n numbers from 0 to n - 1"),compose:b(C,"(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:b(Y,"(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:b(K,"(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:b(V(function(n){return nn(n).isOdd()}),"(odd number)\n Function check if number os odd."),even:b(V(function(n){return nn(n).isEvent()}),"(even number)\n\n Function check if number is even."),"*":b(X(function(n,e){return nn(n).mul(e)}),"(* . numbers)\n\n Multiplicate all numbers passed as arguments. If single value is passed\n it will return that value."),"+":b(X(function(n,e){return nn(n).add(e)}),"(+ . numbers)\n\n Sum all numbers passed as arguments. If single value is passed it will\n return that value."),"-":b(function(){for(var n=arguments.length,e=new Array(n),t=0;t":b(function(n,e){return nn(n).cmp(e)===1},"(> a b)\n\n Function compare two numbers and check if first argument is greater\n than the second one"),"<":b(function(n,e){return nn(n).cmp(e)===-1},"(< a b)\n\n Function compare two numbers and check if first argument is less\n than the second one"),"<=":b(function(n,e){return[0,-1].includes(nn(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"),">=":b(function(n,e){return[0,1].includes(nn(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?":b(O,"(eq? a b)\n\n Function compare two values if they are identical."),or:b(new P("or",function(n,e){var i=e.dynamic_scope,o=e.error;var a=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=a.shift();if(typeof t==="undefined"){if(c){return c}else{return false}}else{var r=yn(t,{env:u,dynamic_scope:i,error:o});return g(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:b(new P("and",function(n){var e=arguments.length>1&&arguments[1]!==d?arguments[1]:{},i=e.dynamic_scope,o=e.error;var a=this.get("list->array")(n);var u=this;if(i){i=u}if(!a.length){return true}var c;return function e(){function n(n){c=n;if(!c){return false}else{return e()}}var t=a.shift();if(typeof t==="undefined"){if(c){return c}else{return false}}else{var r=yn(t,{env:u,dynamic_scope:i,error:o});return g(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."),"|":b(function(n,e){return nn(n).or(e)},"(& a b)\n\n Function calculate or bit operation."),"&":b(function(n,e){return nn(n).and(e)},"(& a b)\n\n Function calculate and bit operation."),"~":b(function(n){return nn(n).neg()},"(~ number)\n\n Function negate the value."),">>":b(function(n,e){return nn(n).shr(e)},"(>> a b)\n\n Function right shit the value a by value b."),"<<":b(function(n,e){return nn(n).shl(e)},"(<< a b)\n\n Function left shit the value a by value b."),not:b(function(n){if(I(n)){return true}return!n},"(not object)\n\n Function return negation of the argument."),"->":b(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.")},d,"global");["floor","round","ceil"].forEach(function(e){un.set(e,b(function(n){if(n instanceof nn){return n[e]()}throw new Error("".concat(An(n)," ").concat(n.toString()," is not a number"))},"(".concat(e," number)\n\n Function calculate ").concat(e," of a number.")))});function cn(n){if(n.length===1){return n[0]}else{var e=[];var t=cn(n.slice(1));for(var r=0;r3&&arguments[3]!==d?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 ln(n,e,t){var r=arguments.length>3&&arguments[3]!==d?arguments[3]:null;var i=hn(e);if(i!==t){throw new Error(fn(n,i,t,r))}}function hn(n){var e={pair:q,symbol:S,macro:P,array:Array,native_symbol:u.Symbol};if(n===A){return"nil"}if(n===null){return"null"}var t=Object.entries(e);for(var r=0;r1&&arguments[1]!==d?arguments[1]:{},t=n.env,r=n.dynamic_scope,i=n.error,o=i===void 0?function(){}:i;try{if(r===true){t=r=t||un}else if(t===true){t=r=un}else{t=t||un}var a={env:t,dynamic_scope:r,error:o};var u;if(R(e)){return e}if(I(e)){return N()}var c=e.car;var s=e.cdr;if(c instanceof q){u=pn(yn(c,a));if(H(u)){return u.then(function(n){return yn(new q(n,e.cdr),a)})}else if(typeof u!=="function"){throw new Error(hn(u)+" "+t.get("string")(u)+" is not a function while evaluating "+e.toString())}}if(c instanceof S){u=t.get(c);if(u instanceof P){return vn(u,s,a)}else if(typeof u!=="function"){if(u){var f="".concat(hn(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=dn(s,a);return g(l,function(n){var e=r||t;return rn(pn(u.apply(e,n)))})}else if(e instanceof S){u=t.get(e);if(u==="undefined"){throw new Error("Unbound variable `"+e.name+"'")}return u}else if(e instanceof q){u=c&&c.toString();throw new Error("".concat(hn(c)," ").concat(u," is not a function"))}else{return e}}catch(n){o&&o(n,e)}}function mn(n,e,t){return gn.apply(this,arguments)}function gn(){gn=Sn(Fn.mark(function n(t,r,i){var o,a,u,c,s;return Fn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:if(i===true){r=i=r||un}else if(r===true){r=i=un}else{r=r||un}o=p(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(/^;/)});a=m(o);u=[];case 4:c=a.shift();if(c){e.next=10;break}return e.abrupt("return",u);case 10:e.next=12;return yn(c,{env:r,dynamic_scope:i,error:function n(e,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 gn.apply(this,arguments)}function bn(e){return function(n){if(!n){return false}return(n.token||n).match(e)}}var wn=bn(/[()]/);function _n(n){var e=typeof n==="string"?p(n):n;var t=e.filter(wn);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}q.unDry=function(n){return new q(n.car,n.cdr)};q.prototype.toDry=function(){return{value:{car:this.car,cdr:this.cdr}}};E.prototype.toDry=function(){return{value:null}};E.unDry=function(){return A};S.prototype.toDry=function(){return{value:{name:this.name}}};S.unDry=function(n){return new S(n.name)};function xn(){var i="text/x-lips";if(window.document){var o=Array.from(document.querySelectorAll("script"));return function n(){var e=o.shift();if(e){var t=e.getAttribute("type");if(t===i){var r=e.getAttribute("src");if(r){return u.fetch(r).then(function(n){return n.text()}).then(mn).then(n)}else{return mn(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,xn)}return{version:"0.10.2",exec:mn,parse:m,tokenize:p,evaluate:yn,Environment:tn,global_environment:un,env:un,balanced_parenthesis:_n,Macro:P,quote:rn,Pair:q,Formatter:k,specials:y,nil:A,maybe_promise:pn,Symbol:S,LNumber:nn}})})(); \ 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 jn(n,e,t){if(r()){jn=Reflect.construct}else{jn=function n(e,t,r){var i=[null];i.push.apply(i,t);var o=Function.bind.apply(e,i);var a=new o;if(r)setPrototypeOf(a,r.prototype);return a}}return jn.apply(null,arguments)}function i(n){if(Array.isArray(n))return n}function o(n,e){var t=[];var r=true;var i=false;var o=undefined;try{for(var a=n[Symbol.iterator](),u;!(r=(u=a.next()).done);r=true){t.push(u.value);if(e&&t.length===e)break}}catch(n){i=true;o=n}finally{try{if(!r&&a["return"]!=null)a["return"]()}finally{if(i)throw o}}return t}function a(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function Fn(n,e){return i(n)||o(n,e)||a()}function n(n,e){return e={exports:{}},n(e,e.exports),e.exports}var e=n(function(n){var e=function(o){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 a(n,e,t,r){var i=e&&e.prototype instanceof u?e:u;var o=Object.create(i.prototype);var a=new A(r||[]);o._invoke=j(n,t,a);return o}o.wrap=a;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)}})}o.isGeneratorFunction=function(n){var e=typeof n==="function"&&n.constructor;return e?e===y||(e.displayName||e.name)==="GeneratorFunction":false};o.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};o.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 o=i.arg;var a=o.value;if(a&&typeof a==="object"&&s.call(a,"__await")){return Promise.resolve(a.__await).then(function(n){c("next",n,t,r)},function(n){c("throw",n,t,r)})}return Promise.resolve(a).then(function(n){o.value=n;t(o)},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};o.AsyncIterator=k;o.async=function(n,e,t,r){var i=new k(a(n,e,t,r));return o.isGeneratorFunction(e)?i:i.next().then(function(n){return n.done?n.value:i.next()})};function j(a,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=F(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 o=f(a,u,c);if(o.type==="normal"){s=c.done?d:h;if(o.arg===v){continue}return{value:o.arg,done:c.done}}else if(o.type==="throw"){s=d;c.method="throw";c.arg=o.arg}}}}function F(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;F(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 S(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(S,this);this.reset(true)}o.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 o=i.completion;if(i.tryLoc==="root"){return n("end")}if(i.tryLoc<=this.prev){var a=s.call(i,"catchLoc");var u=s.call(i,"finallyLoc");if(a&&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 o}(n.exports);try{regeneratorRuntime=e}catch(n){Function("r","regeneratorRuntime = r")(e)}});var Sn=e;function c(n,e,t,r,i,o,a){try{var u=n[o](a);var c=u.value}catch(n){t(n);return}if(u.done){e(c)}else{Promise.resolve(c).then(r,i)}}function En(u){return function(){var n=this,a=arguments;return new Promise(function(e,t){var r=u.apply(n,a);function i(n){c(r,e,t,i,o,"next",n)}function o(n){c(r,e,t,i,o,"throw",n)}i(undefined)})}}function u(n){if(Array.isArray(n)){for(var e=0,t=new Array(n.length);e1&&arguments[1]!==d?arguments[1]:1;return n[n.length-e]}function a(n){if(typeof n==="string"){var e=/([-\\^$[\]()+{}?*.|])/g;return n.replace(e,"\\$1")}}function c(n){var i=e();n=n.replace(/\n\r|\r/g,"\n");var o=0;var a=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:a,token:n,offset:o};u.push(r);c.push(r);o+=n.length;s+=n.length;a+=(n.match("\n")||[]).length;return}n.split(i).filter(Boolean).forEach(function(n){var e={col:s,line:a,token:n,offset:o};s+=n.length;o+=n.length;u.push(e);c.push(e);if(n==="\n"){++a;c=[];s=0}})});return u}function p(n,e){if(e){return c(n)}else{return c(n).map(function(n){return n.token.trim()}).filter(function(n){return n&&!n.match(/^;/)})}}var y={"'":new S("quote"),"`":new S("quasiquote"),",@":new S("unquote-splicing"),",":new S("unquote")};function m(n){var o=[];var a=[];var u=null;var c=Object.keys(y);var e=c.map(function(n){return y[n].name});var s=0;var f=false;var l=[];var h=[];var p=0;function d(){var n=o[o.length-1];if(n instanceof Array&&n[0]instanceof S&&e.includes(n[0].name)&&o.length>1){o.pop();if(o[o.length-1].length===1&&o[o.length-1][0]instanceof S){o[o.length-1].push(n)}else if(o[o.length-1].length===0){o[o.length-1]=n}else if(o[o.length-1]instanceof q){if(o[o.length-1].cdr instanceof q){o[o.length-1]=new q(o[o.length-1],q.fromArray(n))}else{o[o.length-1].cdr=q.fromArray(n)}}else{o[o.length-1].push(n)}}}n.forEach(function(n){var e=o[o.length-1];if(c.indexOf(n)!==-1){p++;u=n;o.push([y[u]]);if(!u){h=[]}h.push(u)}else{if(u){l.push(h);h=[]}if(n==="("){f=true;s++;o.push([]);u=null;p=0}else if(n==="."&&!f){o[o.length-1]=q.fromArray(e)}else if(n===")"){s--;if(!o.length){throw new Error("Unbalanced parenthesis")}if(o.length===1){a.push(o.pop())}else if(o.length>1){var t=o.pop();e=o[o.length-1];if(e instanceof Array){e.push(t)}else if(e instanceof q){e.append(q.fromArray(t))}if(l.length){h=l.pop();while(h.length){d();h.pop()}}else{d()}}if(s===0&&o.length){a.push(o.pop())}}else{f=false;var r=v(n);if(u){while(p--){o[o.length-1][1]=r;r=o.pop()}p=0;u=false}e=o[o.length-1];if(e instanceof q){var i=e;while(true){if(i.cdr===A){if(r instanceof Array){i.cdr=q.fromArray(r)}else{i.cdr=r}break}else{i=i.cdr}}}else if(!o.length){a.push(r)}else{e.push(r)}}}});if(o.length){throw new Error("Unbalanced parenthesis 2")}return a.map(function(n){if(n instanceof Array){return q.fromArray(n)}return n})}function g(n){var e=arguments.length>1&&arguments[1]!==d?arguments[1]:function(n){return n};if(H(n)){return n.then(e)}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 b(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=1;var t=n.length;while(e>0){var r=n[--t];if(!r){return}if(r.token==="("){e--}else if(r.token===")"){e++}}return n.slice(t)}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){this._code=n.replace(/\r/g,"")}k.defaults={offset:0,indent:2,specials:["define","lambda","let","let*","define-macro"]};k.prototype._options=function n(e){var t=k.defaults;if(typeof e==="undefined"){return Object.assign({},t)}var r=e&&e.specials||[];return Object.assign({},t,e,{specials:t.specials.concat(r)})};k.prototype.indent=function n(e){var t=p(this._code,true);return this._indent(t,e)};k.prototype._indent=function n(e,t){var r=this._options(t);var i=x(e);var o=r.specials;var a=_(e);if(a){if(a[0].line>0){r.offset=0}if(a.length===1){return r.offset+a[0].col+1}else if(o.indexOf(a[1].token)!==-1){return r.offset+a[0].col+r.indent}else if(a[0].line3&&a[1].line===a[3].line){if(a[1].token==="("){return r.offset+a[1].col}return r.offset+a[3].col}else if(a[0].line===a[1].line){return r.offset+r.indent+a[0].col}else{var u=a.slice(2);for(var c=0;c"}else if(typeof n==="string"){return JSON.stringify(n)}else if(H(n)){return"<#Promise>"}else if(n instanceof S||n instanceof en||n instanceof q||n===A){return n.toString()}else if(n instanceof Array){return n.map(L)}else if(qn(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}}q.prototype.toString=function(){var n=["("];if(this.car!==d){var e=L(this.car);if(e){n.push(e)}if(this.cdr instanceof q){n.push(" ");n.push(this.cdr.toString().replace(/^\(|\)$/g,""))}else if(typeof this.cdr!=="undefined"&&this.cdr!==A){if(typeof this.cdr==="string"){n=n.concat([" . ",JSON.stringify(this.cdr)])}else{n=n.concat([" . ",L(this.cdr)])}}}n.push(")");return n.join("")};q.prototype.append=function(n){if(n instanceof Array){return this.append(q.fromArray(n))}var e=this;if(e.car===d){if(n instanceof q){this.car=n.car;this.cdr=n.cdr}else{this.car=n}}else{while(true){if(e instanceof q&&e.cdr!==A){e=e.cdr}else{break}}if(n instanceof q){e.cdr=n}else if(n!==A){e.cdr=new q(n,A)}}return this};function O(n,e){if(n instanceof en&&e instanceof en){return n.cmp(e)===0}else if(typeof n==="number"||typeof e==="number"){return en(n).cmp(en(e))}else if(n instanceof S&&e instanceof S){return n.name===e.name}else{return n===e}}function I(n){return n instanceof q&&n.isEmptyList()||n===A}function P(n,e,t){if(typeof this!=="undefined"&&this.constructor!==P||typeof this==="undefined"){return new P(n,e)}hn("Macro",n,"string",1);hn("Macro",e,"function",2);this.__doc__=t;this.name=n;this.fn=e}P.defmacro=function(n,e,t){var r=new P(n,e,t);r.defmacro=true;return r};P.prototype.invoke=function(n,e,t){var r=e.env,i=e.dynamic_scope,o=e.error;var a={dynamic_scope:i,error:o,macro_expand:t};var u=this.fn.call(r,n,a,this.name);return t?on(u):u};P.prototype.toString=function(){return"#"};var M="define-macro";function B(o){return function(){var t=En(Sn.mark(function n(t,c){var s,f,r,i;return Sn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:r=function n(){r=En(Sn.mark(function n(t){var r,i,o,a,u;return Sn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:if(!(t instanceof q&&t.car instanceof S)){e.next=13;break}e.prev=1;r=s.get(t.car);if(!(r instanceof P&&r.defmacro)){e.next=9;break}e.next=6;return r.invoke(t.cdr,c,true);case 6:i=e.sent;if(!(i instanceof q)){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:o=t.car;if(!(o instanceof q)){e.next=18;break}e.next=17;return f(o);case 17:o=e.sent;case 18:a=t.cdr;if(!(a instanceof q)){e.next=23;break}e.next=22;return f(a);case 22:a=e.sent;case 23:u=new q(o,a);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(!o){e.next=12;break}e.t0=on;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",on(i.car));case 22:case"end":return e.stop()}}},n,this)}));return function(n,e){return t.apply(this,arguments)}}()}function R(n){return typeof n==="undefined"||n===A||n===null}function T(n){return typeof n==="function"&&n.toString().match(/\{\s*\[native code\]\s*\}/)}function H(n){return n instanceof Promise||n&&typeof n!=="undefined"&&typeof n.then==="function"}function J(n,o){var a=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")(o.car);var f=s.inherit("let");var l=0;return function n(){var e=c[l++];function t(n){if(H(n)){return n.then(t)}else if(typeof n==="undefined"){f.set(e.car,A)}else{f.set(e.car,n)}}if(a){a=h?f:s}if(!e){var r=new q(new S("begin"),o.cdr);return mn(r,{env:f,dynamic_scope:a,error:u})}else{var i=mn(e.cdr.car,{env:h?f:s,dynamic_scope:a,error:u});return g(t(i),n)}}()})}function Y(n){for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;r2?r-2:0),o=2;o1?n-1:0),t=1;t=a){return o.apply(this,r)}else{return i}}return i.apply(this,arguments)}}function Z(r,i){hn("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 en(this.value.clone()[n](e,r))}};var tn={"+":"add","-":"sub","*":"mul","/":"div","%":"mod","|":"or","&":"and","~":"neg","<<":"shl",">>":"shr"};Object.keys(tn).forEach(function(e){en.prototype[tn[e]]=function(n){return this.op(e,n)}});en.prototype.sqrt=function(){var n;if(en.isNative(this.value)){n=Math.sqrt(this.value)}else if(en.isBN(this.value)){n=this.value.sqrt()}return new en(n)};en.prototype.pow=function(n){n=this.coerce(n);if(en.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(en.isBN(this.value)){n.value=this.value.pow(n.value)}else{n.value=Math.pow(this.value,n.value)}return n};en.prototype.neg=function(){var n=this.value;if(en.isNative(n)){n=-n}else if(en.isBN(n)){n=n.neg()}return new en(n)};en.prototype.abs=function(){var n=this.value;if(en.isNative(this.value)){if(n<0){n=-n}}else if(en.isBN(n)){n.iabs()}return new en(n)};en.prototype.isOdd=function(){if(en.isNative(this.value)){if(this.isBigNumber()){return this.value%BigInt(2)===BigInt(1)}return this.value%2===1}else if(en.isBN(this.value)){return this.value.isOdd()}};en.prototype.isEven=function(){return!this.isOdd()};en.prototype.cmp=function(n){n=this.coerce(n);if(en.isNative(this.value)){if(this.value1&&arguments[1]!==d?arguments[1]:{};if(qn(n)==="object"){e=n}if(!n||qn(n)==="object"){n="child of "+(this.name||"unknown")}return new rn(e||{},this,n)};rn.prototype.get=function(n,e,t){t=t||this;var r;var i=false;if(n instanceof S){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(en.isNumber(r)){return en(r)}if(typeof r==="function"){if(e){return J(r,t)}return r.bind(t)}return r}if(this.parent instanceof rn){return this.parent.get(n,e,t)}else{var o;if(n instanceof S){o=n.name}else if(typeof n==="string"){o=n}if(o){var a=qn(u[o]);if(a==="function"){if(T(u[o])){return u[o].bind(u)}else{return u[o]}}else if(a!=="undefined"){return u[o]}}}o=(o.name||o).toString();throw new Error("Unbound variable `"+o+"'")};rn.prototype.set=function(n,e){if(en.isNumber(e)){e=en(e)}if(n instanceof S){n=n.name}this.env[n]=e};rn.prototype.has=function(n){return typeof this.env[n]!=="undefined"};rn.prototype.ref=function(n){var e=this;while(true){if(!e){break}if(e.has(n)){return e}e=e.parent}};function on(n){if(H(n)){return n.then(on)}if(n instanceof q||n instanceof S){n.data=true}return n}function an(n,e){this.value=n;this.count=e}an.prototype.toString=function(){return"<#unquote["+this.count+"] "+this.value+">"};var un=function(){var e=0;return function(){var n=arguments.length>0&&arguments[0]!==d?arguments[0]:null;if(n!==null){return new S(u.Symbol("#"+n))}e++;return new S(u.Symbol("#gensym_"+e))}}();var cn=new rn({nil:A,undefined:d,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:b(function(n){return n.__doc__},"(help object)\n\n Function returns documentation for function or macro."),cons:b(function(n,e){if(I(e)){e=A}return new q(n,e)},"(cons left right)\n\n Function return new Pair out of two arguments."),car:b(function(n){hn("car",n,"pair");return n.car},"(car pair)\n\n Function returns car (head) of the list/pair."),cdr:b(function(n){hn("cdr",n,"pair");return n.cdr},"(cdr pair)\n\n Function returns cdr (tail) of the list/pair."),"set!":b(new P("set!",function(e){var n=arguments.length>1&&arguments[1]!==d?arguments[1]:{},t=n.dynamic_scope,r=n.error;if(t){t=this}var i=mn(e.cdr.car,{env:this,dynamic_scope:t,error:r});i=dn(i);var o;function a(e,t){if(H(e)){return e.then(function(n){return a(n,t)})}if(H(t)){return t.then(function(n){return a(e,n)})}s[e]=t;return t}if(e.car instanceof q&&S.is(e.car.car,".")){var u=e.car.cdr.car;var c=e.car.cdr.cdr.car;var s=mn(u,{env:this,dynamic_scope:t,error:r});var f=mn(c,{env:this,dynamic_scope:t,error:r});return a(f,i)}if(!(e.car instanceof S)){throw new Error("set! first argument need to be a symbol or "+"dot accessor that evaluate to object.")}o=this.ref(e.car.name);if(!o){o=this}return g(i,function(n){o.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!":b(function(n,e){hn("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!":b(function(n,e){hn("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?":b(function(n){return typeof n==="undefined"||I(n)},"(empty? object)\n\n Function return true if value is undfined empty list."),assoc:b(function(n,e){if(n instanceof q&&!(e instanceof q)){throw new Error("First argument to assoc ned to be a key")}var t=e;while(true){if(!(t instanceof q)||this.get("empty?")(t)){break}var r=t.car.car;if(O(r,n)){return t.car}else{t=t.cdr}}return A},"(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:b(un,"(gensym)\n\n Function generate unique symbol, to use with macros as meta name."),load:b(function(n){hn("load",n,"string");return u.fetch(n).then(function(n){return n.text()}).then(gn)},"(load filename)\n\n Function fetch the file and evaluate its content as LIPS code."),while:b(new P("while",function(r,n){var i=n.dynamic_scope,o=n.error;var a=this;var u=new q(new S("begin"),r.cdr);var c;if(i){i=a}return function e(){var n=mn(r.car,{env:a,dynamic_scope:i,error:o});function t(n){if(n&&!R(n)&&!I(n)){c=mn(u,{env:a,dynamic_scope:i,error:o});if(H(c)){return c.then(function(n){c=n;return e()})}else{return e()}}else{return c}}return g(n,t)}()}),"(while cond . body)\n\n Macro that create a loop, it exectue body untill cond expression is false"),if:b(new P("if",function(t,n){var r=n.dynamic_scope,i=n.error;if(r){r=this}var o=this;var e=function n(e){hn("if",e,"boolean");if(e){return mn(t.cdr.car,{env:o,dynamic_scope:r,error:i})}else{return mn(t.cdr.cdr.car,{env:o,dynamic_scope:r,error:i})}};var a=mn(t.car,{env:o,dynamic_scope:r,error:i});return g(a,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*":b(G(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:b(G(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:b(new P("begin",function(n,e){var r=e.dynamic_scope,i=e.error;var o=this.get("list->array")(n);if(r){r=this}var a=this;var u;return function e(){if(o.length){var n=o.shift();var t=mn(n,{env:a,dynamic_scope:r,error:i});return g(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."),nop:b(function(){},"(nop)\n\n Empty function you can pass list of exressions to the function.\n like every function each expression will be evaluated and it will\n not return any value. you can also put this function as last to\n let or begin. This function is usefull if you want to return\n undefined, like when you call function from terminal and don't\n want any output."),timer:b(new P("timer",function(e){var n=arguments.length>1&&arguments[1]!==d?arguments[1]:{},t=n.dynamic_scope,r=n.error;hn("timer",e.car,"number");var i=this;if(t){t=this}return new Promise(function(n){setTimeout(function(){n(mn(e.cdr.car,{env:i,dynamic_scope:t,error:r}))},e.car)}).then(on)}),"(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:b(P.defmacro("define",function(e,n){var t=this;if(e.car instanceof q&&e.car.car instanceof S){var r=new q(new S("define"),new q(e.car.car,new q(new q(new S("lambda"),new q(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 q){i=mn(i,n)}else if(i instanceof S){i=t.get(i)}if(e.car instanceof S){g(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!":b(function(n,e,t){if(qn(n)!=="object"||R(n)){throw new Error(ln("set-obj!",pn(n),"object"))}n[e]=t},"(set-obj! obj key value)\n\n Function set property of JavaScript object"),eval:b(function(n){var t=this;if(n instanceof q){return mn(n,{env:this,dynamic_scope:this,error:function n(e){return t.get("print")(e.message)}})}if(n instanceof Array){var e;n.forEach(function(n){e=mn(n,{env:t,dynamic_scope:t,error:function n(e){return t.get("print")(e.message)}})});return e}},"(eval list)\n\n Function evalute LIPS code as list structure."),lambda:new P("lambda",function(s){var n=arguments.length>1&&arguments[1]!==d?arguments[1]:{},f=n.dynamic_scope,l=n.error;var h=this;var p;if(s.cdr instanceof q&&typeof s.cdr.car==="string"&&s.cdr.cdr!==A){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 S||!I(e)){for(var i=arguments.length,o=new Array(i),a=0;a2&&arguments[2]!==d?arguments[2]:o;if(n instanceof q&&!I(n)){var r=n.car;var i=n.cdr;if(t(r)){r=e(r)}if(t(i)){i=e(i)}if(H(r)||H(i)){return Promise.all([r,i]).then(function(n){var e=Fn(n,2),t=e[0],r=e[1];return new q(t,r)})}else{return new q(r,i)}}return n}function l(n,e){if(n instanceof q){n.append(e)}else{n=new q(n,e)}return n}function h(t){if(t instanceof q&&!I(t)){var n;if(S.is(t.car.car,"unquote-splicing")){n=mn(t.car.cdr.car,{env:c,dynamic_scope:a,error:u});return g(n,function(e){if(!e instanceof q){throw new Error("Value of unquote-splicing need"+" to be pair")}var n=h(t.cdr);return g(n,function(n){return l(e,n)})})}if(S.is(t.car,"unquote")){var e=t.cdr;var r=e;var i=r;var o=1;while(S.is(r.car.car,"unquote")){i=r;o++;r=r.car.cdr.car}if(o>s){s=o}if(i===r){if(t.cdr.cdr!==A){return g(h(t.cdr.cdr),function(n){return new q(new an(t.cdr.car,o),n)})}else{return new an(t.cdr.car,o)}}else if(i.cdr.cdr!==A){return g(h(i.cdr.cdr),function(n){i.car.cdr=new q(new an(r,o),i.cdr===A?A:n);return e.car})}else{i.car.cdr=new an(r,o)}return e.car}return f(t,h)}return t}var t=function n(e){return o(e)||e instanceof an};function r(n){if(n instanceof an){if(s===n.count){return mn(n.value,{env:c,dynamic_scope:a,error:u})}else{return g(r(n.value),function(n){return new q(new S("unquote"),new q(n,A))})}}return f(n,r,t)}return g(h(n.car),function(n){return g(r(n),on)})}),"(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:b(function(n){return n.clone()},"(clone list)\n\n Function return clone of the list."),append:b(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!":b(function(n,e){if(R(e)||I(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:b(function(n){if(n instanceof q){var e=this.get("list->array")(n).reverse();return this.get("array->list")(e)}else if(!(n instanceof Array)){throw new Error(ln("reverse",pn(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:b(function(n,e){if(e instanceof q){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:b(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:b(function(n,e,t){return t.replace(n,e)},"(replace pattern replacement string)\n\n Function change patter to replacement inside string."),match:b(function(n,e){return this.get("array->list")(e.match(n))},"(match pattern string)\n\n function return match object from JavaScript as list."),search:b(function(n,e){return e.search(n)},"(search pattern string)\n\n Function return first found index of the pattern inside a string"),string:b(function e(n,t){if(typeof jQuery!=="undefined"&&n instanceof jQuery.fn.init){return"<#jQuery("+n.length+")>"}if(n instanceof en){return n.value.toString()}if(typeof n==="undefined"){return"<#undefined>"}if(typeof n==="function"){if(T(n)){return"<#function(native)>"}return"<#function>"}if(n===A){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 q||n instanceof S){return n.toString()}if(u.HTMLElement&&n instanceof u.HTMLElement){return"<#HTMLElement(".concat(n.tagName.toLowerCase(),")>")}if(qn(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:b(function(n){n=n||this;var e=Object.keys(n.env);var t;if(e.length){t=q.fromArray(e)}else{t=A}if(n.parent!==d){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:b(function(n){for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;rlist":b(function(n){return q.fromArray(n)},"(array->list array)\n\n Function convert JavaScript array to LIPS list."),"list->array":b(function(n){if(n instanceof q&&n.isEmptyList()){return[]}var e=[];var t=n;while(true){if(t instanceof q){e.push(t.car);t=t.cdr}else{break}}return e},"(list->array list)\n\n Function convert LIPS list into JavaScript array."),apply:b(function(n,e){hn("call",n,"function",1);hn("call",e,"pair",2);return n.apply(void 0,An(this.get("list->array")(e)))},"(apply fn args)\n\n Function that call function with list of arguments."),length:b(function(n){if(!n){return en(0)}if(n instanceof q){return en(n.length())}if("length"in n){return en(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."),find:b(function e(t,r){if(R(r)){return A}var n=s("find",t);return g(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":b(function(n){hn("for-each",n,"function");for(var e=arguments.length,t=new Array(e>1?e-1:0),r=1;r1?n-1:0),r=1;rarray")(n)});var a=[];return function e(t){function n(n){a.push(n);return e(++t)}if(t===o[0].length){return q.fromArray(a)}var r=o.map(function(n,e){return o[e][t]});return g(i.apply(void 0,An(r)),n)}(0)},"(map fn . args)\n\n Higher order function that call function `fn` by for each\n value of the argument. If you provide more then one list as argument\n it will take each value from each list and call `fn` function\n with that many argument as number of list arguments. The return\n values of the function call is acumulated in result list and\n returned by the call to map."),some:b(function e(t,r){if(R(r)){return false}else{return g(t(r.car),function(n){return n||e(t,r.cdr)})}},"(some fn list)\n\n Higher order function that call argument on each element of the list.\n It stops when function fn return true for a value if so it will\n return true. If it don't find the value it will return false"),fold:b(V("fold",function(n,e,t){for(var r=arguments.length,i=new Array(r>3?r-3:0),o=3;o3?i-3:0),a=3;aarray")(e);var o=[];var a=s("filter",n);return function e(t){function n(n){if(n){o.push(r)}return e(++t)}if(t===i.length){return q.fromArray(o)}var r=i[t];return g(a(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:b(function(n){if(n instanceof en){n=n.valueOf()}return q.fromArray(new Array(n).fill(0).map(function(n,e){return en(e)}))},"(range n)\n\n Function return list of n numbers from 0 to n - 1"),compose:b(U,"(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:b(C,"(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:b(W,"(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:b(z(function(n){return en(n).isOdd()}),"(odd number)\n Function check if number os odd."),even:b(z(function(n){return en(n).isEvent()}),"(even number)\n\n Function check if number is even."),"*":b(K(function(n,e){return en(n).mul(e)}),"(* . numbers)\n\n Multiplicate all numbers passed as arguments. If single value is passed\n it will return that value."),"+":b(K(function(n,e){return en(n).add(e)}),"(+ . numbers)\n\n Sum all numbers passed as arguments. If single value is passed it will\n return that value."),"-":b(function(){for(var n=arguments.length,e=new Array(n),t=0;t":b(function(n,e){return en(n).cmp(e)===1},"(> a b)\n\n Function compare two numbers and check if first argument is greater\n than the second one"),"<":b(function(n,e){return en(n).cmp(e)===-1},"(< a b)\n\n Function compare two numbers and check if first argument is less\n than the second one"),"<=":b(function(n,e){return[0,-1].includes(en(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"),">=":b(function(n,e){return[0,1].includes(en(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?":b(O,"(eq? a b)\n\n Function compare two values if they are identical."),or:b(new P("or",function(n,e){var i=e.dynamic_scope,o=e.error;var a=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=a.shift();if(typeof t==="undefined"){if(c){return c}else{return false}}else{var r=mn(t,{env:u,dynamic_scope:i,error:o});return g(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:b(new P("and",function(n){var e=arguments.length>1&&arguments[1]!==d?arguments[1]:{},i=e.dynamic_scope,o=e.error;var a=this.get("list->array")(n);var u=this;if(i){i=u}if(!a.length){return true}var c;return function e(){function n(n){c=n;if(!c){return false}else{return e()}}var t=a.shift();if(typeof t==="undefined"){if(c){return c}else{return false}}else{var r=mn(t,{env:u,dynamic_scope:i,error:o});return g(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."),"|":b(function(n,e){return en(n).or(e)},"(& a b)\n\n Function calculate or bit operation."),"&":b(function(n,e){return en(n).and(e)},"(& a b)\n\n Function calculate and bit operation."),"~":b(function(n){return en(n).neg()},"(~ number)\n\n Function negate the value."),">>":b(function(n,e){return en(n).shr(e)},"(>> a b)\n\n Function right shit the value a by value b."),"<<":b(function(n,e){return en(n).shl(e)},"(<< a b)\n\n Function left shit the value a by value b."),not:b(function(n){if(I(n)){return true}return!n},"(not object)\n\n Function return negation of the argument."),"->":b(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.")},d,"global");["floor","round","ceil"].forEach(function(e){cn.set(e,b(function(n){if(n instanceof en){return n[e]()}throw new Error("".concat(qn(n)," ").concat(n.toString()," is not a number"))},"(".concat(e," number)\n\n Function calculate ").concat(e," of a number.")))});function sn(n){if(n.length===1){return n[0]}else{var e=[];var t=sn(n.slice(1));for(var r=0;r3&&arguments[3]!==d?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 hn(n,e,t){var r=arguments.length>3&&arguments[3]!==d?arguments[3]:null;var i=pn(e);if(i!==t){throw new Error(ln(n,i,t,r))}}function pn(n){var e={pair:q,symbol:S,macro:P,array:Array,native_symbol:u.Symbol};if(n===A){return"nil"}if(n===null){return"null"}var t=Object.entries(e);for(var r=0;r1&&arguments[1]!==d?arguments[1]:{},t=n.env,r=n.dynamic_scope,i=n.error,o=i===void 0?function(){}:i;try{if(r===true){t=r=t||cn}else if(t===true){t=r=cn}else{t=t||cn}var a={env:t,dynamic_scope:r,error:o};var u;if(R(e)){return e}if(I(e)){return N()}var c=e.car;var s=e.cdr;if(c instanceof q){u=dn(mn(c,a));if(H(u)){return u.then(function(n){return mn(new q(n,e.cdr),a)})}else if(typeof u!=="function"){throw new Error(pn(u)+" "+t.get("string")(u)+" is not a function while evaluating "+e.toString())}}if(c instanceof S){u=t.get(c,true);if(u instanceof P){return yn(u,s,a)}else if(typeof u!=="function"){if(u){var f="".concat(pn(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=vn(s,a);return g(l,function(n){var e=r||t;return on(dn(u.apply(e,n)))})}else if(e instanceof S){u=t.get(e);if(u==="undefined"){throw new Error("Unbound variable `"+e.name+"'")}return u}else if(e instanceof q){u=c&&c.toString();throw new Error("".concat(pn(c)," ").concat(u," is not a function"))}else{return e}}catch(n){o&&o(n,e)}}function gn(n,e,t){return bn.apply(this,arguments)}function bn(){bn=En(Sn.mark(function n(t,r,i){var o,a,u,c,s;return Sn.wrap(function n(e){while(1){switch(e.prev=e.next){case 0:if(i===true){r=i=r||cn}else if(r===true){r=i=cn}else{r=r||cn}o=p(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(/^;/)});a=m(o);u=[];case 4:c=a.shift();if(c){e.next=10;break}return e.abrupt("return",u);case 10:e.next=12;return mn(c,{env:r,dynamic_scope:i,error:function n(e,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 bn.apply(this,arguments)}function wn(e){return function(n){if(!n){return false}return(n.token||n).match(e)}}var _n=wn(/[()]/);function xn(n){var e=typeof n==="string"?p(n):n;var t=e.filter(_n);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}q.unDry=function(n){return new q(n.car,n.cdr)};q.prototype.toDry=function(){return{value:{car:this.car,cdr:this.cdr}}};E.prototype.toDry=function(){return{value:null}};E.unDry=function(){return A};S.prototype.toDry=function(){return{value:{name:this.name}}};S.unDry=function(n){return new S(n.name)};function kn(){var i="text/x-lips";if(window.document){var o=Array.from(document.querySelectorAll("script"));return function n(){var e=o.shift();if(e){var t=e.getAttribute("type");if(t===i){var r=e.getAttribute("src");if(r){return u.fetch(r).then(function(n){return n.text()}).then(gn).then(n)}else{return gn(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,kn)}return{version:"DEV",exec:gn,parse:m,tokenize:p,evaluate:mn,Environment:rn,global_environment:cn,env:cn,balanced_parenthesis:xn,Macro:P,quote:on,Pair:q,Formatter:k,specials:y,nil:A,maybe_promise:dn,Symbol:S,LNumber:en}})})(); \ No newline at end of file diff --git a/src/lips.js b/src/lips.js index a805acae..e1a56b08 100644 --- a/src/lips.js +++ b/src/lips.js @@ -1119,6 +1119,13 @@ return setFnLength(binded, binded.__bind.fn.length); } // ---------------------------------------------------------------------- + function unbind(obj) { + if (typeof obj === 'function' && obj.__bind) { + return obj.__bind.fn; + } + return obj; + } + // ---------------------------------------------------------------------- function setFnLength(fn, length) { try { Object.defineProperty(fn, 'length', { @@ -1584,7 +1591,10 @@ return new Environment(obj || {}, this, name); }; // ---------------------------------------------------------------------- - Environment.prototype.get = function(symbol) { + Environment.prototype.get = function(symbol, weak, context) { + // we keep original environment as context for bind + // so print will get user stdout + context = context || this; var value; var defined = false; if (symbol instanceof Symbol) { @@ -1603,12 +1613,15 @@ return LNumber(value); } if (typeof value === 'function') { - return weakBind(value, this); + if (weak) { + return weakBind(value, context); + } + return value.bind(context); } return value; } if (this.parent instanceof Environment) { - return this.parent.get(symbol); + return this.parent.get(symbol, weak, context); } else { var name; if (symbol instanceof Symbol) { @@ -2528,13 +2541,11 @@ 'get': get, '.': get, // ------------------------------------------------------------------ - 'unbind': doc(function(obj) { - if (typeof obj === 'function' && obj.__bind) { - return obj.__bind.fn; - } - return obj; - }, `(unbind fn) - Function remove bidning from function so you can get props from it.`), + 'unbind': doc( + unbind, + `(unbind fn) + + Function remove bidning from function so you can get props from it.`), // ------------------------------------------------------------------ type: doc( type, @@ -2724,7 +2735,7 @@ // we need to use call(this because babel transpile this code into: // var ret = map.apply(void 0, [fn].concat(args)); // it don't work with weakBind - var ret = this.get('map').call(this, fn, ...args); + var ret = this.get('map')(fn, ...args); if (isPromise(ret)) { return ret.then(() => {}); } @@ -3346,7 +3357,7 @@ } } if (first instanceof Symbol) { - value = env.get(first); + value = env.get(first, true); if (value instanceof Macro) { return evaluate_macro(value, rest, eval_args); } else if (typeof value !== 'function') {