diff --git a/package.json b/package.json index db02e7b2b2..c1bd554fbb 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "@reduxjs/toolkit": "^1.9.7", "@sentry/react": "^10.5.0", "@sourceacademy/c-slang": "^1.0.21", + "@sourceacademy/language-directory": "https://github.com/loyaltypollution/language-directory.git#866cc934372a19e514af0025947ce7f1f5a6e1fc", "@sourceacademy/sharedb-ace": "2.1.1", "@sourceacademy/sling-client": "^0.1.0", "@szhsin/react-menu": "^4.0.0", @@ -67,7 +68,6 @@ "js-slang": "^1.0.85", "js-yaml": "^4.1.0", "konva": "^9.2.0", - "language-directory": "https://github.com/source-academy/language-directory.git", "lodash": "^4.17.21", "lz-string": "^1.4.4", "mdast-util-from-markdown": "^2.0.0", diff --git a/public/evaluators/pie-slang/pie.js b/public/evaluators/pie-slang/pie.js new file mode 100644 index 0000000000..b5c084a0fe --- /dev/null +++ b/public/evaluators/pie-slang/pie.js @@ -0,0 +1,2 @@ +!function(){"use strict";class t extends Error{name="ConductorError";errorType="__unknown";constructor(t){super(t)}}class e extends t{name="ConductorInternalError";errorType="__internal";constructor(t){super(t)}}class n{conductor;async startEvaluator(t){const n=await this.conductor.requestFile(t);if(!n)throw new e("Cannot load entrypoint file");for(await this.evaluateFile(t,n);;){const t=await this.conductor.requestChunk();await this.evaluateChunk(t)}}async evaluateFile(t,e){return this.evaluateChunk(e)}constructor(t){this.conductor=t}}async function r(t){return(await import(t)).plugin}"function"==typeof SuppressedError&&SuppressedError;class s{name;__port;__subscribers=new Set;__isAlive=!0;__waitingMessages=[];send(t,e){this.__verifyAlive(),this.__port.postMessage(t,e??[])}subscribe(t){if(this.__verifyAlive(),this.__subscribers.add(t),this.__waitingMessages){for(const e of this.__waitingMessages)t(e);delete this.__waitingMessages}}unsubscribe(t){this.__verifyAlive(),this.__subscribers.delete(t)}close(){this.__verifyAlive(),this.__isAlive=!1,this.__port?.close()}__verifyAlive(){if(!this.__isAlive)throw new e(`Channel ${this.name} has been closed`)}__dispatch(t){if(this.__verifyAlive(),this.__waitingMessages)this.__waitingMessages.push(t);else for(const e of this.__subscribers)e(t)}listenToPort(t){t.addEventListener("message",t=>this.__dispatch(t.data)),t.start()}replacePort(t){this.__verifyAlive(),this.__port?.close(),this.__port=t,this.listenToPort(t)}constructor(t,e){this.name=t,this.replacePort(e)}}class i{__s1=[];__s2=[];push(t){this.__s2.push(t)}pop(){if(0===this.__s1.length){if(0===this.__s2.length)throw new Error("queue is empty");let t=this.__s1;this.__s1=this.__s2.reverse(),this.__s2=t}return this.__s1.pop()}get length(){return this.__s1.length+this.__s2.length}clone(){const t=new i;return t.__s1=[...this.__s1],t.__s2=[...this.__s2],t}}class a{__inputQueue=new i;__promiseQueue=new i;push(t){0!==this.__promiseQueue.length?this.__promiseQueue.pop()(t):this.__inputQueue.push(t)}async pop(){return 0!==this.__inputQueue.length?this.__inputQueue.pop():new Promise((t,e)=>{this.__promiseQueue.push(t)})}tryPop(){if(0!==this.__inputQueue.length)return this.__inputQueue.pop()}constructor(){this.push=this.push.bind(this)}}class o{name;__channel;__messageQueue=new a;async receive(){return this.__messageQueue.pop()}tryReceive(){return this.__messageQueue.tryPop()}send(t,e){this.__channel.send(t,e)}close(){this.__channel.unsubscribe(this.__messageQueue.push)}constructor(t){this.name=t.name,this.__channel=t,this.__channel.subscribe(this.__messageQueue.push)}}class c{__alive=!0;__link;__parent;__channels=new Map;__pluginMap=new Map;__plugins=[];__negotiateChannel(t){const{port1:e,port2:n}=new MessageChannel,r=new s(t,e);this.__link.postMessage([t,n],[n]),this.__channels.set(t,r)}__verifyAlive(){if(!this.__alive)throw new e("Conduit already terminated")}registerPlugin(t,...n){this.__verifyAlive();const r=[];for(const e of t.channelAttach)this.__channels.has(e)||this.__negotiateChannel(e),r.push(this.__channels.get(e));const s=new t(this,r,...n);if(void 0!==s.name){if(this.__pluginMap.has(s.name))throw new e(`Plugin ${s.name} already registered`);this.__pluginMap.set(s.name,s)}return this.__plugins.push(s),s}unregisterPlugin(t){this.__verifyAlive();let e=0;for(let n=0;n=n;--t)delete this.__plugins[t];t.name&&this.__pluginMap.delete(t.name),t.destroy?.()}lookupPlugin(t){if(this.__verifyAlive(),!this.__pluginMap.has(t))throw new e(`Plugin ${t} not registered`);return this.__pluginMap.get(t)}terminate(){this.__verifyAlive();for(const t of this.__plugins)t.destroy?.();this.__link.terminate?.(),this.__alive=!1}__handlePort(t){const[e,n]=t;if(this.__channels.has(e)){const t=this.__channels.get(e);this.__parent?t.listenToPort(n):t.replacePort(n)}else{const t=new s(e,n);this.__channels.set(e,t)}}constructor(t,e=!1){this.__link=t,t.addEventListener("message",t=>this.__handlePort(t.data)),this.__parent=e}}class u{type=0;data;constructor(t,e,n){this.data={fn:t,args:e,invokeId:n}}}class h{type=2;data;constructor(t,e){this.data={invokeId:t,err:e}}}class l{type=1;data;constructor(t,e){this.data={invokeId:t,res:e}}}function p(t){}var w;!function(t){t[t.VOID=0]="VOID",t[t.BOOLEAN=1]="BOOLEAN",t[t.NUMBER=2]="NUMBER",t[t.CONST_STRING=3]="CONST_STRING",t[t.EMPTY_LIST=4]="EMPTY_LIST",t[t.PAIR=5]="PAIR",t[t.ARRAY=6]="ARRAY",t[t.CLOSURE=7]="CLOSURE",t[t.OPAQUE=8]="OPAQUE",t[t.LIST=9]="LIST"}(w||(w={}));class f{type=1;data;constructor(t){this.data={minVersion:t}}}class y{type=0;data={version:0}}class d{type=3;data;constructor(t){this.data=t}}let m=(()=>{let t,n,s=[p],i=[];return class{static{n=this}static{const e="function"==typeof Symbol&&Symbol.metadata?Object.create(null):void 0;!function(t,e,n,r,s,i){function a(t){if(void 0!==t&&"function"!=typeof t)throw new TypeError("Function expected");return t}for(var o,c=r.kind,u="getter"===c?"get":"setter"===c?"set":"value",h=!e&&t?r.static?t:t.prototype:null,l=e||(h?Object.getOwnPropertyDescriptor(h,r.name):{}),p=!1,w=n.length-1;w>=0;w--){var f={};for(var y in r)f[y]="access"===y?{}:r[y];for(var y in r.access)f.access[y]=r.access[y];f.addInitializer=function(t){if(p)throw new TypeError("Cannot add initializers after decoration has completed");i.push(a(t||null))};var d=(0,n[w])("accessor"===c?{get:l.get,set:l.set}:l[u],f);if("accessor"===c){if(void 0===d)continue;if(null===d||"object"!=typeof d)throw new TypeError("Object expected");(o=a(d.get))&&(l.get=o),(o=a(d.set))&&(l.set=o),(o=a(d.init))&&s.unshift(o)}else(o=a(d))&&("field"===c?s.unshift(o):l[u]=o)}h&&Object.defineProperty(h,r.name,l),p=!0}(null,t={value:n},s,{kind:"class",name:n.name,metadata:e},null,i),n=t.value,e&&Object.defineProperty(n,Symbol.metadata,{enumerable:!0,configurable:!0,writable:!0,value:e})}name="__runner_main";__evaluator;__isCompatibleWithModules;__conduit;__fileRpc;__chunkQueue;__serviceChannel;__ioQueue;__errorChannel;__statusChannel;__serviceHandlers=new Map([[0,function(t){t.data.version<0?(this.__serviceChannel.send(new f(0)),console.error(`Host's protocol version (${t.data.version}) must be at least 0`)):console.log(`Host is using protocol version ${t.data.version}`)}],[1,function(t){console.error(`Host expects at least protocol version ${t.data.minVersion}, but we are on version 0`),this.__conduit.terminate()}],[2,function(t){this.__evaluator.startEvaluator(t.data)}]]);requestFile(t){return this.__fileRpc.requestFile(t)}async requestChunk(){return(await this.__chunkQueue.receive()).chunk}async requestInput(){const{message:t}=await this.__ioQueue.receive();return t}tryRequestInput(){const t=this.__ioQueue.tryReceive();return t?.message}sendOutput(t){this.__ioQueue.send({message:t})}sendError(t){this.__errorChannel.send({error:t})}updateStatus(t,e){this.__statusChannel.send({status:t,isActive:e})}hostLoadPlugin(t){this.__serviceChannel.send(new d(t))}registerPlugin(t,...e){return this.__conduit.registerPlugin(t,...e)}unregisterPlugin(t){this.__conduit.unregisterPlugin(t)}registerModule(t){if(!this.__isCompatibleWithModules)throw new e("Evaluator has no data interface");return this.registerPlugin(t,this.__evaluator)}unregisterModule(t){this.unregisterPlugin(t)}async importAndRegisterExternalPlugin(t){const e=await r(t);return this.registerPlugin(e)}async importAndRegisterExternalModule(t){const e=await async function(t){return await r(t)}(t);return this.registerModule(e)}static channelAttach=["__file_rpc","__chunk","__service","__stdio","__error","__status"];constructor(t,[e,n,r,s,i,a],c){this.__conduit=t,this.__fileRpc=function(t,e){const n=[];let r=0;return t.subscribe(async r=>{switch(r.type){case 0:{const{fn:n,args:s,invokeId:i}=r.data;try{const r=await e[n](...s);i>0&&t.send(new l(i,r))}catch(e){i>0&&t.send(new h(i,e))}break}case 1:{const{invokeId:t,res:e}=r.data;n[t]?.[0]?.(e),delete n[t];break}case 2:{const{invokeId:t,err:e}=r.data;n[t]?.[1]?.(e),delete n[t];break}}}),new Proxy({},{get(e,s,i){const a=Reflect.get(e,s,i);if(a)return a;const o="string"==typeof s&&"$"===s.charAt(0)?(...e)=>{t.send(new u(s,e,0))}:(...e)=>{const i=++r;return t.send(new u(s,e,i)),new Promise((t,e)=>{n[i]=[t,e]})};return Reflect.set(e,s,o,i),o}})}(e,{}),this.__chunkQueue=new o(n),this.__serviceChannel=r,this.__ioQueue=new o(s),this.__errorChannel=i,this.__statusChannel=a,this.__serviceChannel.send(new y),this.__serviceChannel.subscribe(t=>{this.__serviceHandlers.get(t.type)?.call(this,t)}),this.__evaluator=new c(this),this.__isCompatibleWithModules=this.__evaluator.hasDataInterface??!1}static{!function(t,e,n){for(var r=arguments.length>2,s=0;s=g.INTEGER)return n;const r=function(t){if(1!==(t.match(/\//g)||[]).length)return new P(!1);const e=t.split("/");if(2!==e.length)return new P(!1);const[n,r]=e,s=N(n),i=N(r);return s.result&&i.result?new P(!0,n,r):new P(!1)}(t);if(r.result&&e>=g.RATIONAL)return r;const s=function(t){function e(t){if(function(t){return"+inf.0"===t||"-inf.0"===t||"+nan.0"===t||"-nan.0"===t}(t))return new T(!0,t);const e=(t.match(/\./g)||[]).length;if(e>1)return new T(!1);if(0===e){const e=N(t);return new T(e.result,e.value)}const[n,r]=t.split("."),s=N(n),i=N(r),a=s.result||""===n,o=i.result||""===r;return"+"===n||"-"===n?""===r?new T(!1):new T(!0,`${n}0`,t):s.result&&o||a&&i.result?i.result&&i.isSigned()?new T(!1):new T(!0,s.value,i.value):new T(!1)}return 0===(t.match(/[eE]/g)||[]).length?e(t):function(t){const n=t.indexOf("e"),r=t.indexOf("E");if(-1===n&&-1===r)return new T(!1);const s=-1===n?r:n,i=t.substring(0,s),a=t.substring(s+1);if(""===i||""==a)return new T(!1);const o=e(i);if(!o.result)return new T(!1);const c=x(a,g.REAL);return c.result?new T(!0,o.integer,o.decimal,c):new T(!1)}(t)}(t);if(s.result&&e>=g.REAL)return s;const i=function(t){if((t.match(/i/g)||[]).length<1)return new b(!1);if("i"!==t[t.length-1])return new b(!1);const e=t.search(/(?=g.COMPLEX?i:new E(!1)}class _{numberType=g.INTEGER;value;static EXACT_ZERO=new _(0n);constructor(t){this.value=t}static build(t,e=!1){const n=BigInt(t);return 0n===n?_.EXACT_ZERO:new _(n)}promote(t){switch(t){case g.INTEGER:return this;case g.RATIONAL:return k.build(this.value,1n,!0);case g.REAL:return $.build(this.coerce(),!0);case g.COMPLEX:return S.build(this,_.EXACT_ZERO,!0)}}equals(t){return t instanceof _&&this.value===t.value}greaterThan(t){return this.value>t.value}negate(){return this===_.EXACT_ZERO?this:_.build(-this.value)}multiplicativeInverse(){if(this===_.EXACT_ZERO)throw new Error("Division by zero");return k.build(1n,this.value,!1)}add(t){return _.build(this.value+t.value)}multiply(t){return _.build(this.value*t.value)}getBigInt(){return this.value}coerce(){return this.value>Number.MAX_SAFE_INTEGER?1/0:this.value0n===e?t:r(e,t.valueOf()%e.valueOf()),s=r(t,e),i=t<0n?-1n:1n,a=e<0n?-1n:1n,o=i*a;return t*=i,1n!==(e*=a)||n?new k(o*t/s,e/s):_.build(o*t)}getNumerator(){return this.numerator}getDenominator(){return this.denominator}promote(t){switch(t){case g.RATIONAL:return this;case g.REAL:return $.build(this.coerce(),!0);case g.COMPLEX:return S.build(this,_.EXACT_ZERO,!0);default:throw new Error("Unable to demote rational")}}equals(t){return t instanceof k&&this.numerator===t.numerator&&this.denominator===t.denominator}greaterThan(t){return this.numerator*t.denominator>t.numerator*this.denominator}negate(){return k.build(-this.numerator,this.denominator)}multiplicativeInverse(){if(0n===this.numerator)throw new Error("Division by zero");return k.build(this.denominator,this.numerator)}add(t){const e=this.numerator*t.denominator+t.numerator*this.denominator,n=this.denominator*t.denominator;return k.build(e,n)}multiply(t){const e=this.numerator*t.numerator,n=this.denominator*t.denominator;return k.build(e,n)}coerce(){const t=this.numerator<0n?-this.numerator:this.numerator;let e=this.denominator;const n=Number(t/e);if(n>Number.MAX_VALUE)return this.numerator<0n?-1/0:1/0;let r=t%e;for(;r>Number.MAX_SAFE_INTEGER||e>Number.MAX_SAFE_INTEGER;)r/=2n,e/=2n;const s=Number(r)/Number(e);return this.numerator<0n?-(n+s):n+s}toString(){return`${this.numerator}/${this.denominator}`}}class ${numberType=g.REAL;value;static INEXACT_ZERO=new $(0);static INEXACT_NEG_ZERO=new $(-0);static INFINITY=new $(1/0);static NEG_INFINITY=new $(-1/0);static NAN=new $(NaN);static build(t,e=!1){return t===1/0?$.INFINITY:t===-1/0?$.NEG_INFINITY:isNaN(t)?$.NAN:0===t?$.INEXACT_ZERO:-0===t?$.INEXACT_NEG_ZERO:new $(t)}constructor(t){this.value=t}promote(t){switch(t){case g.REAL:return this;case g.COMPLEX:return S.build(this,_.EXACT_ZERO,!0);default:throw new Error("Unable to demote real")}}equals(t){return t instanceof $&&this.value===t.value}greaterThan(t){return this.value>t.value}negate(){return $.build(-this.value)}multiplicativeInverse(){if(this===$.INEXACT_ZERO||this===$.INEXACT_NEG_ZERO)throw new Error("Division by zero");return $.build(1/this.value)}add(t){return $.build(this.value+t.value)}multiply(t){return $.build(this.value*t.value)}coerce(){return this.value}toString(){return this===$.INFINITY?"+inf.0":this===$.NEG_INFINITY?"-inf.0":this===$.NAN?"+nan.0":this.value.toString()}}class S{numberType=g.COMPLEX;real;imaginary;static build(t,e,n=!1){return S.simplify(new S(t,e),n)}constructor(t,e){this.real=t,this.imaginary=e}static simplify(t,e){return!e&&I(t.imaginary,_.EXACT_ZERO)?t.real:t}promote(t){if(t===g.COMPLEX)return this;throw new Error("Unable to demote complex")}negate(){return S.build(this.real.negate(),this.imaginary.negate())}equals(t){return I(this.real,t.real)&&I(this.imaginary,t.imaginary)}greaterThan(t){return C(this.real,t.real)&&C(this.imaginary,t.imaginary)}multiplicativeInverse(){const t=B(R(this.real,this.real),R(this.imaginary,this.imaginary));return S.build(R(t.multiplicativeInverse(),this.real),R(t.multiplicativeInverse(),this.imaginary.negate()))}add(t){return S.build(B(this.real,t.real),B(this.imaginary,t.imaginary))}multiply(t){const e=(n=R(this.real,t.real),r=R(this.imaginary,t.imaginary),B(n,function(t){return t.negate()}(r)));var n,r;const s=B(R(this.real,t.imaginary),R(this.imaginary,t.real));return S.build(e,s)}getReal(){return this.real}getImaginary(){return this.imaginary}coerce(){throw new Error("Cannot coerce a complex number to a javascript number")}toPolar(){const t=this.real.promote(g.REAL),e=this.imaginary.promote(g.REAL),n=$.build(Math.sqrt(t.coerce()*t.coerce()+e.coerce()*e.coerce())),r=$.build(Math.atan2(e.coerce(),t.coerce()));return O.build(n,r)}toString(){return`${this.real}+${this.imaginary}i`}}class O{magnitude;angle;constructor(t,e){this.magnitude=t,this.angle=e}static build(t,e){return new O(t,e)}toCartesian(){const t=$.build(this.magnitude.coerce()*Math.cos(this.angle.coerce())),e=$.build(this.magnitude.coerce()*Math.sin(this.angle.coerce()));return S.build(t,e)}}function L(t){switch(t.numberType){case g.INTEGER:return t;case g.RATIONAL:return 1n===t.getDenominator()?_.build(t.getNumerator()):t;case g.REAL:return t;case g.COMPLEX:return S.build(L(t.getReal()),L(t.getImaginary()))}}function A(t,e){return t.numberType>e.numberType?[t,e.promote(t.numberType)]:t.numberType");return new K(t)}var e;if(2===t.length){const e=t[0];if(Y(e)&&function(t){return t.type===G.APOSTROPHE||t.type===G.BACKTICK||t.type===G.HASH_VECTOR||t.type===G.COMMA||t.type===G.COMMA_AT}(e))return new K(t)}const n=t[0],r=t[t.length-1];if(Y(n)&&Y(r)&&(i=r,(s=n).type===G.LEFT_PAREN&&i.type===G.RIGHT_PAREN||s.type===G.LEFT_BRACKET&&i.type===G.RIGHT_BRACKET))return new K(t);var s,i;const a=new K(t);throw new Q("",a.location.start,a,"matching parentheses")}first(){return this.elements[0]}firstToken(){const t=this.first();return Y(t)?t:t.firstToken()}firstPos(){return this.firstToken().pos}last(){return this.elements[this.elements.length-1]}lastToken(){const t=this.last();return Y(t)?t:t.lastToken()}lastPos(){return this.lastToken().pos}isParenthesized(){const t=this.first();return Y(t)&&(t.type===G.LEFT_PAREN||t.type===G.LEFT_BRACKET)}isSingleIdentifier(){return!this.isParenthesized()&&1===this.length()}unwrap(){return this.isParenthesized()?this.elements.slice(1,this.elements.length-1):this.elements}length(){return this.unwrap().length}toString(){return this.elements.map(t=>t.toString()).join(" ")}}function Y(t){return t instanceof Z}function j(t){return t instanceof K}class Z{type;lexeme;literal;start;end;pos;endPos;constructor(t,e,n,r,s,i,a){this.type=t,this.lexeme=e,this.literal=n,this.start=r,this.end=s,this.pos=new M(i,a),this.endPos=new M(i,a+e.length-1)}convertToken(){switch(this.type){case G.APOSTROPHE:return new Z(G.QUOTE,this.lexeme,this.literal,this.start,this.end,this.pos.line,this.pos.column);case G.BACKTICK:return new Z(G.QUASIQUOTE,this.lexeme,this.literal,this.start,this.end,this.pos.line,this.pos.column);case G.HASH_VECTOR:return new Z(G.VECTOR,this.lexeme,this.literal,this.start,this.end,this.pos.line,this.pos.column);case G.COMMA:return new Z(G.UNQUOTE,this.lexeme,this.literal,this.start,this.end,this.pos.line,this.pos.column);case G.COMMA_AT:return new Z(G.UNQUOTE_SPLICING,this.lexeme,this.literal,this.start,this.end,this.pos.line,this.pos.column);default:return this}}toString(){return`${this.lexeme}`}}class W extends SyntaxError{loc;constructor(t,e,n){super(t),this.loc={line:e,column:n}}toString(){return this.message}}class J extends W{char;constructor(t,e,n){super(`Unexpected character '${n}' (${t}:${e})`,t,e),this.char=n,this.name="UnexpectedCharacterError"}}class tt extends W{constructor(t,e){super(`Unexpected EOF (${t}:${e})`,t,e),this.name="UnexpectedEOFError"}}const et=new Map([[".",G.DOT],["if",G.IF],["let",G.LET],["cond",G.COND],["else",G.ELSE],["set!",G.SET],["begin",G.BEGIN],["delay",G.DELAY],["quote",G.QUOTE],["export",G.EXPORT],["import",G.IMPORT],["define",G.DEFINE],["lambda",G.LAMBDA],["define-syntax",G.DEFINE_SYNTAX],["syntax-rules",G.SYNTAX_RULES]]);class nt{source;tokens;start=0;current=0;line=1;col=0;constructor(t){this.source=t,this.tokens=[]}isAtEnd(){return this.current>=this.source.length}advance(){return this.col++,this.source.charAt(this.current++)}jump(){this.start=this.current,this.col++,this.current++}addToken(t,e=null){const n=this.source.substring(this.start,this.current);this.tokens.push(new Z(t,n,e,this.start,this.current,this.line,this.col))}scanTokens(){for(;!this.isAtEnd();)this.start=this.current,this.scanToken();return this.tokens.push(new Z(G.EOF,"",null,this.start,this.current,this.line,this.col)),this.tokens}scanToken(){const t=this.advance();switch(t){case"(":this.addToken(G.LEFT_PAREN);break;case")":this.addToken(G.RIGHT_PAREN);break;case"[":this.addToken(G.LEFT_BRACKET);break;case"]":this.addToken(G.RIGHT_BRACKET);break;case"'":this.addToken(G.APOSTROPHE);break;case"`":this.addToken(G.BACKTICK);break;case",":if(this.match("@")){this.addToken(G.COMMA_AT);break}this.addToken(G.COMMA);break;case"#":if(this.match("t")||this.match("f"))this.booleanToken();else if(this.match("|"))this.comment();else if(this.match(";"))this.addToken(G.HASH_SEMICOLON);else{if("("!==this.peek()&&"["!==this.peek())throw new J(this.line,this.col,t);this.addToken(G.HASH_VECTOR)}break;case";":for(;"\n"!=this.peek()&&!this.isAtEnd();)this.advance();break;case" ":case"\r":case"\t":break;case"\n":this.line++,this.col=0;break;case'"':this.stringToken();break;case"|":this.identifierTokenLoose();break;default:if(this.isDigit(t)||"-"===t||"+"===t||"."===t||"i"===t||"n"===t)this.identifierNumberToken();else{if(!this.isValidIdentifier(t))throw new J(this.line,this.col,t);this.identifierToken()}}}comment(){for(;("|"!=this.peek()||"#"!=this.peekNext())&&!this.isAtEnd();)"\n"===this.peek()&&(this.line++,this.col=0),this.advance();if(this.isAtEnd())throw new tt(this.line,this.col);this.jump(),this.jump()}identifierToken(){for(;this.isValidIdentifier(this.peek());)this.advance();this.addToken(this.checkKeyword())}identifierTokenLoose(){for(this.advance();"|"!=this.peek()&&!this.isAtEnd();)"\n"===this.peek()&&(this.line++,this.col=0),this.advance();if(this.isAtEnd())throw new tt(this.line,this.col);this.advance(),this.addToken(this.checkKeyword())}identifierNumberToken(){for(;this.isValidIdentifier(this.peek());)this.advance();const t=this.source.substring(this.start,this.current);x(t,g.COMPLEX).result?this.addToken(G.NUMBER,t):this.addToken(this.checkKeyword())}checkKeyword(){const t=this.source.substring(this.start,this.current);return et.has(t)?et.get(t):G.IDENTIFIER}stringToken(){for(;'"'!=this.peek()&&!this.isAtEnd();)"\n"===this.peek()&&(this.line++,this.col=0),this.advance();if(this.isAtEnd())throw new tt(this.line,this.col);this.advance();const t=this.source.substring(this.start+1,this.current-1);this.addToken(G.STRING,t)}booleanToken(){this.addToken(G.BOOLEAN,"t"===this.peekPrev())}match(t){return!this.isAtEnd()&&(this.source.charAt(this.current)==t&&(this.current++,!0))}peek(){return this.isAtEnd()?"\0":this.source.charAt(this.current)}peekNext(){return this.current+1>=this.source.length?"\0":this.source.charAt(this.current+1)}peekPrev(){return this.current-1<0?"\0":this.source.charAt(this.current-1)}isDigit(t){return t>="0"&&t<="9"}isSpecialSyntax(t){return"("===t||")"===t||"["===t||"]"===t||";"===t||"|"===t}isValidIdentifier(t){return!this.isWhitespace(t)&&!this.isSpecialSyntax(t)}isWhitespace(t){return" "===t||"\0"===t||"\n"===t||"\r"===t||"\t"===t}}var rt,st;!function(t){class e{location;expressions;constructor(t,e){this.location=t,this.expressions=e}accept(t){return t.visitSequence(this)}equals(t){if(t instanceof e){if(this.expressions.length!==t.expressions.length)return!1;for(let e=0;e=this.tokens.length}previous(){return this.tokens[this.current-1]}peek(){return this.tokens[this.current]}validateChapter(t,e){if(this.chapter{}){if(0===t.length)return[[],void 0];if(1===t.length)return e(t[0]),[[this.parseExpression(t[0])],void 0];const n=t.at(-2);if(Y(n)&&n.type===G.DOT){const n=t.at(-1),r=t.slice(0,-2);return e(n),r.forEach(e),[r.map(this.parseExpression.bind(this)),this.parseExpression(n)]}const r=t;return r.forEach(e),[r.map(this.parseExpression.bind(this)),void 0]}grouping(t){const e=[];let n=!1;t&&(n=!0,e.push(t));do{const t=this.advance();switch(t.type){case G.LEFT_PAREN:case G.LEFT_BRACKET:const r=this.grouping(t);e.push(r);break;case G.RIGHT_PAREN:case G.RIGHT_BRACKET:if(!n)throw new F(this.source,t.pos,t);e.push(t),n=!1;break;case G.APOSTROPHE:case G.BACKTICK:case G.COMMA:case G.COMMA_AT:case G.HASH_VECTOR:let s;do{s=this.grouping()}while(!s);e.push(this.affect(t,s));break;case G.QUOTE:case G.QUASIQUOTE:case G.UNQUOTE:case G.UNQUOTE_SPLICING:case G.IDENTIFIER:case G.NUMBER:case G.BOOLEAN:case G.STRING:case G.DOT:case G.DEFINE:case G.IF:case G.ELSE:case G.COND:case G.LAMBDA:case G.LET:case G.SET:case G.BEGIN:case G.DELAY:case G.IMPORT:case G.EXPORT:case G.DEFINE_SYNTAX:case G.SYNTAX_RULES:e.push(t);break;case G.HASH_SEMICOLON:for(;!this.grouping(););break;case G.EOF:throw new D(this.source,t.pos);default:throw new F(this.source,t.pos,t)}}while(n);if(0!==e.length)try{return K.build(e)}catch(t){if(t instanceof Q)throw new Q(this.source,t.loc,t.form,t.expected);throw t}}affect(t,e){return K.build([t,e])}parseExpression(t){return Y(t)?this.parseToken(t):t.isSingleIdentifier()?this.parseToken(t.unwrap()[0]):this.parseGroup(t)}parseToken(t){switch(t.type){case G.IDENTIFIER:return this.quoteMode===it.NONE?new rt.Identifier(this.toLocation(t),t.lexeme):new rt.Symbol(this.toLocation(t),t.lexeme);case G.NUMBER:return new rt.NumericLiteral(this.toLocation(t),t.literal);case G.BOOLEAN:return new rt.BooleanLiteral(this.toLocation(t),t.literal);case G.STRING:return new rt.StringLiteral(this.toLocation(t),t.literal);default:if(this.quoteMode!==it.NONE||this.chapter>=5)return new rt.Symbol(this.toLocation(t),t.lexeme);throw new F(this.source,t.pos,t)}}parseGroup(t){if(!t.isParenthesized())return this.parseAffectorGroup(t);switch(this.quoteMode){case it.NONE:return this.parseNormalGroup(t);case it.QUOTE:case it.QUASIQUOTE:return this.parseQuotedGroup(t)}}parseAffectorGroup(t){const[e,n]=t.unwrap();switch(e.type){case G.APOSTROPHE:case G.QUOTE:if(this.validateChapter(e,2),this.quoteMode!==it.NONE){const t=this.parseExpression(n),r=new rt.Symbol(this.toLocation(e),"quote"),s=r.location.merge(t.location);return new st.List(s,[r,t])}this.quoteMode=it.QUOTE;const r=this.parseExpression(n);return this.quoteMode=it.NONE,r;case G.BACKTICK:case G.QUASIQUOTE:if(this.validateChapter(e,2),this.quoteMode!==it.NONE){const t=this.parseExpression(n),r=new rt.Symbol(this.toLocation(e),"quasiquote"),s=r.location.merge(t.location);return new st.List(s,[r,t])}this.quoteMode=it.QUASIQUOTE;const s=this.parseExpression(n);return this.quoteMode=it.NONE,s;case G.COMMA:case G.UNQUOTE:this.validateChapter(e,2);const i=this.quoteMode;if(i===it.NONE)throw new X(this.source,e.pos,e);if(i===it.QUOTE){const t=this.parseExpression(n),r=new rt.Symbol(this.toLocation(e),"unquote"),s=r.location.merge(t.location);return new st.List(s,[r,t])}this.quoteMode=it.NONE;const a=this.parseExpression(n);return this.quoteMode=i,a;case G.COMMA_AT:case G.UNQUOTE_SPLICING:this.validateChapter(e,2);const o=this.quoteMode;if(o===it.NONE)throw new F(this.source,e.pos,e);if(o===it.QUOTE){const t=this.parseExpression(n),r=new rt.Symbol(this.toLocation(e),"unquote-splicing"),s=r.location.merge(t.location);return new st.List(s,[r,t])}this.quoteMode=it.NONE;const c=this.parseExpression(n);this.quoteMode=o;const u=this.toLocation(e).merge(c.location);return new rt.SpliceMarker(u,c);case G.HASH_VECTOR:this.validateChapter(e,3);const h=this.quoteMode;this.quoteMode=it.QUOTE;const l=this.parseVector(t);return this.quoteMode=h,l;default:throw new F(this.source,e.pos,e)}}parseNormalGroup(t){if(0===t.length()){if(this.chapter>=5)return new rt.Nil(t.location);throw new Q(this.source,t.location.start,t,"non-empty group")}const e=t.unwrap()[0];if(Y(e))switch(e.type){case G.LAMBDA:return this.validateChapter(e,1),this.parseLambda(t);case G.DEFINE:return this.validateChapter(e,1),this.parseDefinition(t);case G.IF:return this.validateChapter(e,1),this.parseConditional(t);case G.LET:return this.validateChapter(e,1),this.parseLet(t);case G.COND:return this.validateChapter(e,1),this.parseExtendedCond(t);case G.QUOTE:case G.APOSTROPHE:case G.QUASIQUOTE:case G.BACKTICK:case G.UNQUOTE:case G.COMMA:case G.UNQUOTE_SPLICING:case G.COMMA_AT:return this.validateChapter(e,2),this.parseAffectorGroup(t);case G.BEGIN:return this.validateChapter(e,3),this.parseBegin(t);case G.DELAY:return this.validateChapter(e,3),this.parseDelay(t);case G.SET:return this.validateChapter(e,3),this.parseSet(t);case G.DEFINE_SYNTAX:return this.validateChapter(e,5),this.parseDefineSyntax(t);case G.SYNTAX_RULES:throw new F(this.source,e.pos,e);case G.IMPORT:return this.validateChapter(e,1),this.parseImport(t);case G.EXPORT:return this.validateChapter(e,1),this.parseExport(t);case G.VECTOR:return this.validateChapter(e,3),this.parseAffectorGroup(t);default:return this.parseApplication(t)}return this.parseApplication(t)}parseQuotedGroup(t){if(0===t.length())return new rt.Nil(t.location);if(1===t.length()){const e=[this.parseExpression(t.unwrap()[0])];return new st.List(t.location,e)}const e=t.unwrap(),[n,r]=this.destructureList(e);return new st.List(t.location,n,r)}parseLambda(t){if(t.length()<3)throw new Q(this.source,t.location.start,t,"(lambda (* . ?) +) | (lambda +)");const e=t.unwrap(),n=e[1],r=e.slice(2);let s,i=[];if(Y(n)){if(n.type!==G.IDENTIFIER)throw new Q(this.source,n.pos,n,"");s=new rt.Identifier(this.toLocation(n),n.lexeme)}else{const t=n.unwrap();[i,s]=this.destructureList(t,t=>{if(!Y(t))throw new Q(this.source,t.pos,t,"");if(t.type!==G.IDENTIFIER)throw new Q(this.source,t.pos,t,"")})}const a=r.map(this.parseExpression.bind(this));if(a.length<1)throw new Q(this.source,t.location.start,t,"(lambda ... +)");if(1===a.length)return new rt.Lambda(t.location,a[0],i,s);const o=a.at(0).location.merge(a.at(-1).location),c=new rt.Sequence(o,a);return new rt.Lambda(t.location,c,i,s)}parseDefinition(t){if(t.length()<3)throw new Q(this.source,t.location.start,t,"(define ) | (define ( ) +)");const e=t.unwrap(),n=e[1],r=e.slice(2);let s,i,a=[],o=!1;if(j(n)){o=!0;const t=n.unwrap(),e=t[0],r=t.splice(1);if(!Y(e))throw new Q(this.source,e.location.start,e,"");if(e.type!==G.IDENTIFIER)throw new Q(this.source,e.pos,e,"");s=new rt.Identifier(this.toLocation(e),e.lexeme),[a,i]=this.destructureList(r,t=>{if(!Y(t))throw new Q(this.source,t.pos,t,"");if(t.type!==G.IDENTIFIER)throw new Q(this.source,t.pos,t,"")})}else{if(n.type!==G.IDENTIFIER)throw new Q(this.source,n.pos,n,"");s=new rt.Identifier(this.toLocation(n),n.lexeme),o=!1}if(r.length<1)throw new Q(this.source,t.location.start,t,"(define ... +)");if(o){const e=r.map(this.parseExpression.bind(this));if(1===e.length)return new st.FunctionDefinition(t.location,s,e[0],a,i);const n=e.at(0).location.merge(e.at(-1).location),o=new rt.Sequence(n,e);return new st.FunctionDefinition(t.location,s,o,a,i)}if(r.length>1)throw new Q(this.source,t.location.start,t,"(define )");const c=this.parseExpression(r[0]);return new rt.Definition(t.location,s,c)}parseConditional(t){if(t.length()<3||t.length()>4)throw new Q(this.source,t.location.start,t,"(if ?)");const e=t.unwrap(),n=e[1],r=e[2],s=t.length()>3?e[3]:void 0,i=this.parseExpression(n),a=this.parseExpression(r),o=s?this.parseExpression(s):new rt.Identifier(t.location,"undefined");return new rt.Conditional(t.location,i,a,o)}parseApplication(t){if(t.length()<1)throw new Q(this.source,t.location.start,t,"( *)");const e=t.unwrap(),n=e[0],r=e.splice(1),s=this.parseExpression(n),i=[];for(const t of r)i.push(this.parseExpression(t));return new rt.Application(t.location,s,i)}parseLet(t){if(this.chapter>=5){return t.unwrap().slice(1).forEach(t=>{this.parseExpression(t)}),new st.Let(t.location,[],[],new rt.Identifier(t.location,"undefined"))}if(t.length()<3)throw new Q(this.source,t.location.start,t,"(let (( )*) +)");const e=t.unwrap(),n=e[1],r=e.slice(2);if(!j(n))throw new Q(this.source,n.pos,n,"(( )*)");const s=[],i=[],a=n.unwrap();for(const t of a){if(!j(t))throw new Q(this.source,t.pos,t,"( )");if(2!==t.length())throw new Q(this.source,t.location.start,t,"( )");const[e,n]=t.unwrap();if(!Y(e))throw new Q(this.source,e.location.start,e,"");if(e.type!==G.IDENTIFIER)throw new Q(this.source,e.pos,e,"");s.push(new rt.Identifier(this.toLocation(e),e.lexeme)),i.push(this.parseExpression(n))}const o=r.map(this.parseExpression.bind(this));if(o.length<1)throw new Q(this.source,t.location.start,t,"(let ... +)");if(1===o.length)return new st.Let(t.location,s,i,o[0]);const c=o.at(0).location.merge(o.at(-1).location),u=new rt.Sequence(c,o);return new st.Let(t.location,s,i,u)}parseExtendedCond(t){if(this.chapter>=5){return t.unwrap().slice(1).forEach(t=>{this.parseExpression(t)}),new st.Cond(t.location,[],[],new rt.Identifier(t.location,"undefined"))}if(t.length()<2)throw new Q(this.source,t.location.start,t,"(cond ( *)* (else )?)");const e=t.unwrap().splice(1),n=e.pop(),r=[],s=[];for(const t of e){if(!j(t))throw new Q(this.source,t.pos,t,"( *)");if(t.length()<1)throw new Q(this.source,t.firstToken().pos,t.firstToken(),"( *)");const[e,...n]=t.unwrap();if(Y(e)&&e.type===G.ELSE)throw new Q(this.source,e.pos,e,"");const i=this.parseExpression(e),a=n.map(this.parseExpression.bind(this)),o=n.length<1?i.location:a.at(0).location.merge(a.at(-1).location),c=n.length<1?i:n.length<2?a[0]:new rt.Sequence(o,a);r.push(i),s.push(c)}if(!j(n))throw new Q(this.source,n.pos,n,"( +) | (else )");if(n.length()<2)throw new Q(this.source,n.firstToken().pos,n.firstToken(),"( +) | (else )");const[i,...a]=n.unwrap();let o=!1;if(Y(i)&&i.type===G.ELSE&&(o=!0,1!==a.length))throw new Q(this.source,n.location.start,n,"(else )");if(a.length<1)throw new Q(this.source,n.location.start,n,"( +)");const c=a.map(this.parseExpression.bind(this)),u=c.at(0).location.merge(c.at(-1).location),h=1===a.length?c[0]:new rt.Sequence(u,c);if(o)return new st.Cond(t.location,r,s,h);const l=this.parseExpression(i);return r.push(l),s.push(h),new st.Cond(t.location,r,s)}parseSet(t){if(3!==t.length())throw new Q(this.source,t.location.start,t,"(set! )");const e=t.unwrap(),n=e[1],r=e[2];if(j(n))throw new Q(this.source,n.location.start,n,"");if(n.type!==G.IDENTIFIER)throw new Q(this.source,n.pos,n,"");const s=new rt.Identifier(this.toLocation(n),n.lexeme),i=this.parseExpression(r);return new rt.Reassignment(t.location,s,i)}parseBegin(t){if(t.length()<2)throw new Q(this.source,t.location.start,t,"(begin +)");const e=t.unwrap().slice(1),n=[];for(const t of e)n.push(this.parseExpression(t));return new st.Begin(t.location,n)}parseDelay(t){if(this.chapter>=5){return t.unwrap().slice(1).forEach(t=>{this.parseExpression(t)}),new st.Delay(t.location,new rt.Identifier(t.location,"undefined"))}if(2!==t.length())throw new Q(this.source,t.location.start,t,"(delay )");const e=t.unwrap()[1],n=this.parseExpression(e);return new st.Delay(t.location,n)}parseDefineSyntax(t){if(3!==t.length())throw new Q(this.source,t.location.start,t,"(define-syntax )");const e=t.unwrap(),n=e[1],r=e[2];this.quoteMode=it.QUOTE;const s=this.parseExpression(n);if(this.quoteMode=it.NONE,!(s instanceof rt.Symbol))throw new Q(this.source,s.location.start,n,"");if(!j(r))throw new Q(this.source,r.pos,r,"");if(r.length()<2)throw new Q(this.source,r.firstToken().pos,r,"(syntax-rules ...)");const i=r.unwrap()[0];if(!Y(r.unwrap()[0]))throw new Q(this.source,r.firstToken().pos,i,"syntax-rules");if(i.type!==G.SYNTAX_RULES)throw new Q(this.source,i.pos,i,"syntax-rules");const a=this.parseSyntaxRules(r);return new rt.DefineSyntax(t.location,s,a)}isValidPattern(t){if(t instanceof st.List){if(void 0===t.terminator){if(t.elements.filter(t=>t instanceof rt.Symbol&&"..."===t.value).length>1)return!1;const e=t.elements.findIndex(t=>t instanceof rt.Symbol&&"..."===t.value);if(-1!=e&&0===e)return!1;for(const e of t.elements)if(!this.isValidPattern(e))return!1;return!0}{if(t.elements.filter(t=>t instanceof rt.Symbol&&"..."===t.value).length>1)return!1;const e=t.elements.findIndex(t=>t instanceof rt.Symbol&&"..."===t.value);if(-1!=e){if(0===e)return!1;if(e===t.elements.length-1)return!1}for(const e of t.elements)if(!this.isValidPattern(e))return!1;return this.isValidPattern(t.terminator)}}return t instanceof rt.Symbol||t instanceof rt.BooleanLiteral||t instanceof rt.NumericLiteral||t instanceof rt.StringLiteral||t instanceof rt.Nil}isValidTemplate(t){if(t instanceof st.List){if(void 0===t.terminator){if(0===t.elements.length)return!1;if(2===t.elements.length&&t.elements[0]instanceof rt.Symbol&&"..."===t.elements[0].value)return this.isValidTemplate(t.elements[1]);let e=!1;for(let n=0;n*) +)");const e=t.unwrap(),n=e[1],r=e.slice(2),s=[];if(!j(n))throw new Q(this.source,n.pos,n,"(*)");this.quoteMode=it.QUOTE;for(const t of n.unwrap()){if(!Y(t))throw new Q(this.source,t.location.start,t,"");const e=this.parseExpression(t);if(!(e instanceof rt.Symbol))throw new Q(this.source,t.pos,t,"");s.push(e)}const i=[];for(const t of r){if(!j(t))throw new Q(this.source,t.pos,t,"(