bit2 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bit2.js +1 -1
- package/dist/src/surface/AST.d.ts +12 -1
- package/package.json +10 -3
package/dist/bit2.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.BiEval=t():e.BiEval=t()}(this,(()=>(()=>{"use strict";var e={612:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.evaluateToLambdaAST=h,t.forward=function(e){let t=u.parse(e),n=d.translate(t);return c.evaluateTermNode(h(n))},t.backward=function(e,t){let n=u.parse(e),r=h(d.translate(n)),o=(0,y.fuse)({},t,r).map((({newTermNode:e,remainingOperation:t})=>{let n=(0,s.flatten)((0,b.unLambdalize)(e));try{let e=p.unPartialEval(n),t=p.flatten(e),r=f.printToSurface(t);return console.log("biEval backward, success:\n",r),r}catch(e){const t=e;return console.log("biEval backward, fail:"+t.message),""}}));return console.log("solution list:",o),o=o.filter((e=>""!=e)),Array.from(new Set(o))};const a=i(n(661)),s=n(446),l=n(619),c=i(n(238)),p=i(n(887)),u=i(n(577)),d=i(n(775)),f=i(n(69)),y=n(963),b=n(784);function h(e){const t=new Map,n=a.scopelize(e),[r,o]=(0,s.partialEval)(t,n),i=(0,s.flatten)(o);return(0,l.lambdalize)(i)}},75:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.findVariablesAndFields=function(e){let t=[],n=[];return function e(r){switch(r.type){case"variable":t.push(r);break;case"field":"variable"===r.object.type?n.push({variable:r.object,field:r.field}):e(r.object);break;case"binary":e(r.left),e(r.right);break;case"unary":e(r.operand);break;case"array":r.elements.forEach(e);break;case"object":Object.values(r.fields).forEach(e);break;case"freeze":e(r.expression)}}(e),{variables:t,fields:n}}},143:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.prettyPrint=function e(t){switch(t.type){case"constant":let n=t.value;if("string"==typeof n)return'"'+String(n)+'"';if("object"!=typeof n||null===n)return String(n);if(!Array.isArray(n))return e(n);if(0===n.length)return"[]";if(0!==n.length)return"["+n.map(e).join(", ")+"]";case"variable":return t.name;case"binary":return e(t.left)+t.operator+e(t.right);case"unary":return t.operator+e(t.operand);case"field":return e(t.object)+"."+t.field;case"array":return"["+t.elements.map((t=>e(t))).join(",")+"]";case"freeze":return"!"+e(t.expression);case"object":let r=t.fields;return"{"+Object.keys(r).map((t=>t+":"+e(r[t]))).join(", ")+"}";default:return""}},t.prettyPrintBinaryOperator=function(e){return e},t.prettyPrintUnaryOperator=function(e){return e}},235:(e,t)=>{function n(e,t){if(null===e)console.log(`${t}null`);else if("number"==typeof e||"boolean"==typeof e||"string"==typeof e)console.log(`${t}${e}`);else if("object"==typeof e){console.log(`${t}{`);const r=e;for(const e in r)if(r.hasOwnProperty(e)){const o=t+" ";console.log(`${o}${e}:`),n(r[e],o+" ")}console.log(`${t}}`)}else{if("constant"!=typeof e)throw new Error("Unhandled value type: "+typeof e);n(e.value,t+" ")}}Object.defineProperty(t,"__esModule",{value:!0}),t.printExpression=function e(t,r=""){switch(t.type){case"constant":n(t.value,r+" ");break;case"variable":console.log(`${r}Variable: ${t.name}`);break;case"binary":console.log(`${r}BinaryOperation: ${t.operator}`),e(t.left,r+" "),e(t.right,r+" ");break;case"unary":console.log(`${r}UnaryOperation: ${t.operator}`),e(t.operand,r+" ");break;case"field":console.log(`${r}FieldAccess: ${t.field}`),e(t.object,r+" ");break;case"array":console.log(`${r}ArrayLiteral:`),t.elements.forEach((t=>e(t,r+" ")));break;case"object":console.log(`${r}ObjectLiteral:`),Object.keys(t.fields).forEach((e=>{console.log(`${r}${e} = ${t.fields[e].value}`)}));break;case"freeze":console.log(`${r}Freeze:`),e(t.expression,r+" ")}},t.printConstantValue=n},454:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.seq=function(...e){return{type:"seq",nodes:e}},t.constNode=function(e){return{type:"const",value:e}},t.space=function(e){return{type:"space",width:e}},t.declare=function(e,t){return{type:"declare",name:e,value:t}},t.exp=function(e){return{type:"exp",expression:e}},t.loop=function(e,t,n,r,o){return{type:"loop",lst:e,separator:t,front:n,rear:r,body:o}},t.ite=function(e,t,n){return{type:"ite",condition:e,trueBranch:t,falseBranch:n}},t.assign=function(e,t){return{type:"assign",name:e,value:t}},t.sep=function(e){return{type:"sep",value:e}},t.nop=function(){return{type:"nop"}},t.bot=function(){return{type:"bot"}},t.end=function(){return{type:"end"}}},69:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.printToSurface=function e(t){let n="";switch(t.type){case"const":return t.value;case"space":return" ".repeat(t.width);case"declare":return n="«",n+="var ",n+=a.prettyPrint(t.name),n+="=",n+=a.prettyPrint(t.value),n+="»",n;case"assign":return n="«",n+=a.prettyPrint(t.name),n+="=",n+=a.prettyPrint(t.value),n+="»",n;case"exp":return n="«",n+=a.prettyPrint(t.expression),n+="»",n;case"seq":return t.nodes.map((t=>e(t))).join("");case"ite":n="«if "+a.prettyPrint(t.condition)+"»",n+=e(t.trueBranch);let r=t.falseBranch;return"ite"===r.type||"seq"===r.type&&1==r.nodes.length?n+="«elseif"+e(r).slice(3):"bot"!==r.type&&"seq"===r.type&&0!=r.nodes.length&&(n+="«else»",n+=e(r)),n.endsWith("«endif»")||(n+="«endif»"),n;case"loop":return n="«for "+a.prettyPrint(t.body.variable)+" in "+a.prettyPrint(t.lst),t.separator.value&&(n+=' separator "'+t.separator.value+'"'),t.front.value&&(n+=' front "'+t.front.value+'"'),t.rear.value&&(n+=' rear "'+t.rear.value+'"'),n+="»",n+=e(t.body.body),n+="«endfor»",n;default:return""}return""};const a=i(n(143))},963:function(e,t,n){var r=this&&this.__rest||function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n};Object.defineProperty(t,"__esModule",{value:!0}),t.fuse=c,t.fuseExp=p,t.valueToConstantExpr=w,t.printEnvironment=function(e){console.log("{\n");for(const t in e)if(e.hasOwnProperty(t)){const[n,r]=e[t];console.log(`${t}: {`),console.log(" val: "),(0,s.printValue)(n," "),console.log(",marks: ["),r.forEach(((e,t)=>{(0,s.printValue)(e," "),t<r.length-1&&console.log(", ")})),console.log("]\n },\n")}console.log("}")},t.fuseBulk=m;const o=n(63),i=n(75),a=n(446),s=n(893),l=n(238);function c(e,t,n){if("const"!==n.type&&"sep"!==n.type&&"loopfront"!==n.type&&"looprear"!==n.type||"string"!=typeof n.value)if("space"===n.type)switch(t.type){case"insert":const{str:r,position:i}=t;if(0==n.width)return[{newEnv:E(e),newTermNode:n,remainingOperation:t},{newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:r},n]},remainingOperation:{type:"id"}}];if(0==i){let t=[{newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:r},n]},remainingOperation:{type:"id"}}];return(0,o.isWhitespace)(r)&&t.push({newEnv:E(e),newTermNode:{type:"space",width:n.width+r.length},remainingOperation:{type:"id"}}),t}if(i<n.width){if((0,o.isWhitespace)(r))return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:r.length+n.width}),remainingOperation:{type:"id"}}];throw new Error(`Cannot insert non-space between space term: ${t}`)}if(i===n.width){let t=[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"insert",str:r,position:i-n.width}}];return(0,o.isWhitespace)(r)&&t.push({newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:n.width+r.length}),remainingOperation:{type:"id"}}),t}{const t=i-n.width;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"insert",str:r,position:t}}]}case"delete":const{str:a,position:s}=t;if(0!==s){if(s<n.width&&s+a.length<=n.width){const t=n.width-a.length;return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:t}),remainingOperation:{type:"id"}}]}if(s<n.width&&s+a.length>n.width){const t=s,r=a.substring(n.width-s);return[{newEnv:E(e),newTermNode:{type:"space",width:t},remainingOperation:{type:"delete",str:r,position:0}}]}{const t=s-n.width;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"delete",str:a,position:t}}]}}if((0,o.isWhitespace)(a)&&a.length<=n.width){const t=n.width-a.length;return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:t}),remainingOperation:{type:"id"}}]}if(a.length>n.width){const t=a.slice(n.width);return[{newEnv:E(e),newTermNode:{type:"bot"},remainingOperation:{type:"delete",str:t,position:0}},{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:0}),remainingOperation:{type:"delete",str:t,position:0}}]}case"replace":const{str1:l,str2:c,position:p}=t;if(0!==p){if(p<n.width&&p+l.length<=n.width){if((0,o.isWhitespace)(c))return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:c.length+n.width-l.length}),remainingOperation:{type:"id"}}];{const t=" ".repeat(p)+c+" ".repeat(n.width-(p+l.length));return[{newEnv:E(e),newTermNode:{type:"const",value:t},remainingOperation:{type:"id"}}]}}if(p<n.width&&p+l.length>n.width){const t=" ".repeat(p),r=n.width-p,o=c.substring(0,r),i={type:"replace",str1:l.substring(r),str2:c.substring(r),position:0},a={type:"const",value:t+o};return[{newEnv:E(e),newTermNode:a,remainingOperation:i}]}{const t=p-n.width;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"replace",str1:l,str2:c,position:t}}]}}if(l.length<=n.width){if((0,o.isWhitespace)(c)){const t=n.width-l.length+c.length;return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:t}),remainingOperation:{type:"id"}}]}return[{newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:c},Object.assign(Object.assign({},n),{width:n.width-l.length})]},remainingOperation:{type:"id"}}]}if(l.length>n.width){const t=c.slice(0,n.width),r=l.slice(n.width),o=c.slice(n.width);return[{newEnv:E(e),newTermNode:{type:"const",value:t},remainingOperation:{type:"replace",str1:r,str2:o,position:0}}]}case"bulk":return m(e,t,n);case"id":return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"id"}}];default:throw new Error(`Unhandled operation type: ${t}`)}else if("exp"===n.type){const a=n.binding,l=a[0],u=a[1],d="number"==typeof(s=u)||"boolean"==typeof s||"string"==typeof s?String(s):"Unknown";switch(t.type){case"insert":const{str:a,position:s}=t;if(0===s){let t=[];if(t.push({newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:a},n]},remainingOperation:{type:"id"}}),!(0,o.containsNewlineOrSpace)(a)){let r=function(e,t){try{if(v(JSON.parse(t)))return t+e}catch(e){return}}(d,a);if(void 0!==r)try{let o=y(r,u),{newEnv:i,newExp:a}=p(e,o,l);t.push({newEnv:E(i),newTermNode:Object.assign(Object.assign({},n),{binding:[a,o]}),remainingOperation:{type:"id"}})}catch(e){}}return t}if(s<d.length){const t=y(d.slice(0,s)+a+d.slice(s),u);let{newEnv:r,newExp:o}=p(e,t,l);return[{newEnv:E(r),newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(s==d.length){let r=[];if(!(0,o.containsNewlineOrSpace)(a)){const t=function(e,t){try{if(v(JSON.parse(t)))return e+t}catch(e){return}}(d,a);if(void 0!==t)try{const o=y(t,u);let{newEnv:i,newExp:a}=p(e,o,l);r.push({newEnv:E(i),newTermNode:Object.assign(Object.assign({},n),{binding:[a,o]}),remainingOperation:{type:"id"}})}catch(e){console.error(e)}}let i=E(e);return l.name&&(i[l.name]=[u,[u]]),r.push({newEnv:i,newTermNode:n,remainingOperation:Object.assign(Object.assign({},t),{position:0})}),r}{const r=s-d.length;if(l.name){let t=l.name;e[t]=[u,[u]]}return[{newEnv:E(e),newTermNode:n,remainingOperation:Object.assign(Object.assign({},t),{position:r})}]}case"delete":const{str:f,position:g}=t;if(0===g){if(f.length<d.length){const t=y(d.slice(f.length),u);let{newEnv:r,newExp:o}=p(e,t,l);return[{newEnv:E(r),newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(f.length==d.length){if(l.name){let t=l.name;if("string"==typeof u){let{newEnv:r,newExp:o}=p(e,"",l);return[{newEnv:E(r),newTermNode:Object.assign(Object.assign({},n),{binding:[o,""]}),remainingOperation:{type:"id"}},{newEnv:b(E(e),t),newTermNode:{type:"bot"},remainingOperation:{type:"id"}},{newEnv:E(e),newTermNode:{type:"bot"},remainingOperation:{type:"id"}}]}return[{newEnv:b(E(e),t),newTermNode:{type:"bot"},remainingOperation:{type:"id"}}]}if(l){let t=l.object.name,n=l.field,r=e[t][0],o=e[t][1][0];if(0==o.fields[n].length){let i=h(r,n),a=h(o,n),s=b(E(e),t);return s[t]=[i,[a]],[{newEnv:E(s),newTermNode:{type:"bot"},remainingOperation:{type:"id"}}]}throw new Error(`field has been updaeted, cannot be remvoed: ${n}$`)}}else if(f.length>d.length){let t=f.slice(0,d.length),r=f.slice(d.length),o={type:"delete",str:t,position:0},i=c(E(e),o,n),a={type:"delete",str:r,position:0};return i.forEach((e=>{e.remainingOperation=a})),i}}else{if(g+f.length<=d.length){const t=y(d.slice(0,g)+d.slice(g+f.length),u);let{newEnv:r,newExp:o}=p(e,t,l);return[{newEnv:E(r),newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(g<=d.length&&g+f.length>d.length){const t=d.slice(0,g),r=f.slice(d.length-g),o=y(t,u);let{newEnv:i,newExp:a}=p(e,o,l);return[{newEnv:E(i),newTermNode:Object.assign(Object.assign({},n),{binding:[a,o]}),remainingOperation:{type:"delete",str:r,position:0}}]}if(g>d.length){const t=g-d.length;let[{newEnv:r,newTermNode:o,remainingOperation:i}]=c(E(e),{type:"id"},n);return[{newEnv:E(r),newTermNode:o,remainingOperation:{type:"delete",str:f,position:t}}]}}case"replace":const{str1:w,str2:O,position:j}=t;if(0===j){if(d.startsWith(w)){const t=y(O+d.slice(w.length),u);let{newEnv:r,newExp:o}=p(E(e),t,l);return[{newEnv:r,newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(w.startsWith(d)){const t=y(O.slice(0,d.length),u);let{newEnv:r,newExp:o}=p(E(e),t,l);const i=w.slice(d.length),a=O.slice(d.length);return[{newEnv:r,newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"replace",str1:i,str2:a,position:0}}]}throw new Error(`unsupported replacement: ${w}; ${d}`)}if(j<d.length){const t=y(d.slice(0,j)+O+d.slice(j+w.length),u);let{newEnv:r,newExp:o}=p(E(e),t,l);return[{newEnv:r,newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(j>=d.length){const r=j-d.length;let[{newEnv:o,newTermNode:i,remainingOperation:a}]=c(E(e),{type:"id"},n);return[{newEnv:E(o),newTermNode:i,remainingOperation:Object.assign(Object.assign({},t),{position:r})}]}throw new Error("Unsupported replacement");case"bulk":return m(e,t,n);case"id":let{variables:N,fields:x}=(0,i.findVariablesAndFields)(l);return N.forEach((t=>{e=function(e,t){if(!(e.name in t))throw new Error(`Variable ${e.name} not found in environment.`);let n=t[e.name][0],r=b(t,e.name);return r[e.name]=[n,[n]],r}(t,e)})),x.forEach((({variable:t,field:n})=>{e=function(e,t,n){let o=t.name;if(!(o in n))throw new Error(`Variable ${o} not found in environment.`);let i=n[o][0],a=function(e,t,n){const o=e.fields,i=t,a=(o[i],r(o,["symbol"==typeof i?i:i+""]));return a[t]=n.fields[t],{type:"object",fields:a}}(n[o][1][0],e,i),s=b(n,o);return s[o]=[i,[a]],s}(n,t,e)})),[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"id"}}];default:throw new Error(`Unhandled operation type: ${t}`)}}else{if("lambda"===n.type){let r=n.marker;if("loopitem"===r.type){let o=[],i=n.variable.name,a=n.binding[0],s=n.binding[1],l=E(e);l=j(l,i,s);let u="";if(!r.lst)throw new Error("exp is not a Variable with a name property in loopitem's marker");{let e=r.lst.name;u=e+"_new"}let d=c(E(l),t,n.body).map((({newEnv:t,newTermNode:r,remainingOperation:o})=>{let s=E(t);if(i in s){let t=s[i][0],l=s[u][0];"object"==typeof t&&null!==t&&"type"in t&&"object"===t.type&&0===Object.keys(t.fields).length||l.push(t),s[u]=[l,[l]],s=b(E(s),i);let{newEnv:c,newExp:d}=p(s,t,a);return e[i]&&(c[i]=e[i]),{newEnv:c,newTermNode:{type:"lambda",variable:n.variable,body:"seq"!=r.type?{type:"seq",nodes:[r]}:r,binding:[d,t],marker:n.marker},remainingOperation:o}}return e[i]&&(s[i]=e[i]),{newEnv:s,newTermNode:{type:"lambda",variable:n.variable,body:"seq"!=r.type?{type:"seq",nodes:[r]}:r,binding:[{type:"constant",value:null},null],marker:n.marker},remainingOperation:o}}));return o.concat(d)}{let r=[];if("bulk"===t.type&&t.operations.length>0){let o=t.operations;const[i,...a]=o;if("insert"===i.type&&0===i.position){let t=i.str.length,o={type:"bulk",operations:a.map((e=>{if("id"===e.type)return e;if("position"in e)return Object.assign(Object.assign({},e),{position:e.position-t});throw new Error("All operations must have positions")}))},s=c(E(e),o,n);s.map((e=>(e.newTermNode={type:"seq",nodes:[{type:"const",value:i.str},e.newTermNode]},e))),r=r.concat(s)}}else if("insert"===t.type&&0===t.position){let o=c(E(e),{type:"id"},n);o.map((e=>(e.newTermNode={type:"seq",nodes:[{type:"const",value:t.str},e.newTermNode]},e))),r=r.concat(o)}let o=n.variable.name,i=n.binding[0],a=n.binding[1],s=E(e);s=j(s,o,a);let l=c(E(s),t,n.body).map((({newEnv:t,newTermNode:r,remainingOperation:a})=>{let s=t[o][0];delete t[o];let l=function(e,t){let n=E(e);for(const e in t)t.hasOwnProperty(e)&&(n[e]=t[e]);return n}(e,t),{newEnv:c,newExp:u}=p(l,s,i);return{newEnv:c,newTermNode:{type:"lambda",variable:n.variable,body:r,binding:[u,s],marker:n.marker},remainingOperation:a}}));return r=r.concat(l),r}}if("branchstart"!==n.type&&"branchend"!==n.type&&"nop"!==n.type){if("seq"===n.type){if("bulk"===t.type)return m(e,t,n);const r=n.nodes;let o=[{newEnv:e,newTermNode:{type:"seq",nodes:[]},remainingOperation:t}];for(const e of r){const t=[];for(const n of o){const r=c(E(n.newEnv),n.remainingOperation,e);for(const e of r){const r=("seq"===n.newTermNode.type?n.newTermNode.nodes:[n.newTermNode]).concat("seq"===e.newTermNode.type?e.newTermNode.nodes:[e.newTermNode]);t.push({newEnv:e.newEnv,newTermNode:{type:"seq",nodes:r},remainingOperation:e.remainingOperation})}}o=t}return o.map((e=>e))}if("end"===n.type)return[{newEnv:E(e),newTermNode:n,remainingOperation:t}];throw new Error("Operation can only be applied to ConstNode with string value")}{if("branchend"===n.type){let t=n.condition[0],r=n.condition[1],[o,i]=(0,a.evaluateExpr)(g(e),t);if(i!==r)throw new Error("Violate BX properties: updates change if-then-else branch.")}let r=[];switch(r.push({newEnv:E(e),newTermNode:n,remainingOperation:t}),t.type){case"insert":const{str:o,position:i}=t;0===i&&r.push({newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:o},n]},remainingOperation:{type:"id"}});case"delete":case"replace":case"id":return r;case"bulk":return m(e,t,n);default:throw new Error(`Unhandled operation type: ${t}`)}}}else{if("loopfront"===n.type){let t=n.lst;if(!t.name)throw new Error("exp is not a Variable with a name property in loopfront");{let n=t.name;e[n+"_new"]=[[],[]]}}else if("looprear"===n.type){let t=n.lst;if(!t.name)throw new Error("exp is not a Variable with a name property in loopfront");{let n=t.name,r=e[n+"_new"][0];r=r.reverse(),e[n]=[r,[r]]}}const r=n.value;switch(t.type){case"insert":const{str:o,position:i}=t;if(0===i){let i=[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:o+r}),remainingOperation:{type:"id"}},{newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:o},n]},remainingOperation:{type:"id"}}];return 0===r.length&&i.push({newEnv:E(e),newTermNode:n,remainingOperation:t}),i}if(i<r.length){const t=r.slice(0,i)+o+r.slice(i);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}]}if(i===r.length)return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:r+o}),remainingOperation:{type:"id"}},{newEnv:E(e),newTermNode:n,remainingOperation:{type:"insert",str:o,position:i-r.length}}];{const t=i-r.length;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"insert",str:o,position:t}}]}case"delete":const{str:a,position:s}=t;if(0===s){if(r.startsWith(a)&&a.length<=r.length){const t=r.slice(a.length);let o=[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}];return a.length===r.length&&o.push({newEnv:E(e),newTermNode:{type:"bot"},remainingOperation:{type:"id"}}),o}if(a.startsWith(r)&&a.length>r.length){const t=a.slice(r.length);return[{newEnv:E(e),newTermNode:{type:"bot"},remainingOperation:{type:"delete",str:t,position:0}},{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:""}),remainingOperation:{type:"delete",str:t,position:0}}]}throw new Error("cannot delete, not match. delStr: "+a+", s_c: "+r)}if(s<r.length&&s+a.length<=r.length){const t=r.slice(0,s)+r.slice(s+a.length);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}]}if(s<r.length&&s+a.length>r.length){const t=r.substring(0,s),n=a.substring(r.length-s);return[{newEnv:E(e),newTermNode:{type:"const",value:t},remainingOperation:{type:"delete",str:n,position:0}}]}{const t=s-r.length;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"delete",str:a,position:t}}]}case"replace":const{str1:l,str2:c,position:p}=t;if(0!==p){if(p<r.length&&p+l.length<=r.length){const t=r.slice(0,p)+c+r.slice(p+l.length);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}]}if(p<r.length&&p+l.length>r.length){const t=r.substring(0,p),n=r.substring(p),o=c.substring(0,n.length),i={type:"replace",str1:l.substring(n.length),str2:c.substring(n.length),position:0},a={type:"const",value:t+o};return[{newEnv:E(e),newTermNode:a,remainingOperation:i}]}{const t=p-r.length;return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:r}),remainingOperation:{type:"replace",str1:l,str2:c,position:t}}]}}if(r.startsWith(l)&&l.length<=r.length){const t=c+r.slice(l.length);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}]}if(l.startsWith(r)&&l.length>r.length){const t=c.slice(0,r.length),o=l.slice(r.length),i=c.slice(r.length);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"replace",str1:o,str2:i,position:0}}]}case"bulk":return m(e,t,n);case"id":return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"id"}}];default:throw new Error(`Unhandled operation type: ${t}`)}}var s}function p(e,t,n){switch(n.type){case"constant":return{newEnv:e,newExp:w(t)};case"variable":if(!(n.name in e))throw new Error(`Variable ${n.name} not found in environment.`);const[o,i]=e[n.name];if(0===i.length){const r=E(e);return r[n.name]=[t,[t]],{newEnv:r,newExp:n}}if(1===i.length&&i[0]===t)return{newEnv:e,newExp:n};throw new Error(`Fail, variable cannot be updated to different value ${n.name}, previous: ${i[0]}, new: ${t}`);case"freeze":let[s,l]=(0,a.evaluateExpr)(g(e),n.expression);if(l!==t)throw new Error(`Fail, freezed expression cannot be changed, old: ${t}, new: ${l}`);return{newEnv:e,newExp:n};case"field":const c=n.object;if("variable"===c.type&&c.name in e){const[o,i]=e[c.name];if("object"===o.type){const a={type:"object",fields:Object.assign(Object.assign({},o.fields),{[n.field]:t})},s=function(e,t,n){const o=e.fields,i=t,a=(o[i],r(o,["symbol"==typeof i?i:i+""]));return a[t]=[n],{type:"object",fields:a}}(i[0],n.field,t),l=E(e);return l[c.name]=[a,[s]],{newEnv:l,newExp:n}}throw new Error("Field access's value is not an object")}throw new Error("Field access not start with variable.");case"binary":let d=g(e),[y,b]=(0,a.evaluateExpr)(d,n.left);if("number"==typeof t){if("number"!=typeof t||null===t)throw new Error("Value must be a non-null number");switch(n.operator){case"+":let r=p(e,t-b,n.right);return{newEnv:r.newEnv,newExp:Object.assign(Object.assign({},n),{right:r.newExp})};case"-":let o=p(e,b-t,n.right);return{newEnv:o.newEnv,newExp:Object.assign(Object.assign({},n),{right:o.newExp})};case"*":let i=p(e,t/b,n.right);return{newEnv:i.newEnv,newExp:Object.assign(Object.assign({},n),{right:i.newExp})};case"/":let a=p(e,b/t,n.right);return{newEnv:a.newEnv,newExp:Object.assign(Object.assign({},n),{right:a.newExp})}}}case"unary":return{newEnv:e,newExp:n};case"array":return{newEnv:{},newExp:{type:"array",elements:u(n.elements,t)}};case"object":let h={},m=t.fields;for(const e in m)if(m.hasOwnProperty(e)){const t=m[e];h[e]=f(t)}return{newEnv:{},newExp:{type:"object",fields:h}};default:throw new Error(`Unsupported expression type: ${n.type}`)}}function u(e,t){const n=[];for(let r=0;r<t.length;r++)r<e.length?n.push(d(e[r],t[r])):n.push(f(t[r]));return n}function d(e,t){switch(e.type){case"constant":return Object.assign(Object.assign({},e),{value:t});case"array":return Object.assign(Object.assign({},e),{elements:u(e.elements,t)});case"object":return Object.assign(Object.assign({},e),{fields:Object.keys(e.fields).reduce(((n,r)=>(n[r]=Object.assign(Object.assign({},e.fields[r]),{value:t.fields[r]}),n)),{})});default:throw new Error(`Unsupported expression type: ${e.type}`)}}function f(e){if("number"==typeof e||"boolean"==typeof e||"string"==typeof e||null===e)return{type:"constant",value:e};if(Array.isArray(e))return{type:"array",elements:e.map(f)};if("object"==typeof e&&null!==e)return{type:"object",fields:Object.keys(e.fields).reduce(((t,n)=>(t[n]={type:"constant",value:e.fields[n]},t)),{})};throw new Error("Unsupported value type: "+typeof e)}function y(e,t){if("number"==typeof t){const t=parseFloat(e);if(isNaN(t))throw new Error("convert to number fail: "+e);return t}if("boolean"==typeof t){if("true"===e)return!0;if("false"===e)return!1;throw new Error("convert to boolean fail: ${s}")}if("string"==typeof t)return e;throw new Error("Unsupported type: "+typeof t)}function b(e,t){const n=E(e);return delete n[t],n}function h(e,t){const n=e.fields,o=t;return n[o],{type:"object",fields:r(n,["symbol"==typeof o?o:o+""])}}function g(e){const t=new Map;for(const n in e)e.hasOwnProperty(n)&&t.set(n,e[n][0]);return t}function w(e){if(null===e||"number"==typeof e||"boolean"==typeof e||"string"==typeof e)return{type:"constant",value:e};if(Array.isArray(e))return{type:"constant",value:e.map((e=>w(e)))};if("object"===e.type){const t=e,n={};for(const e in t.fields)t.fields.hasOwnProperty(e)&&(n[e]=w(t.fields[e]));return{type:"constant",value:{type:"object",fields:n}}}throw new Error("Unhandled value type: "+typeof e)}function m(e,t,n){if("id"===t.type)return c(E(e),t,n);if("bulk"!==t.type||!t.operations)throw new Error("Invalid bulk operation");if(0==t.operations.length)return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"id"}}];if("seq"===n.type&&0==n.nodes.length)return[{newEnv:E(e),newTermNode:n,remainingOperation:t}];const[r,...o]=t.operations;if("seq"===n.type){const r=n.nodes[0],o=n.nodes.slice(1),i=m(e,t,r),a=[];for(const e of i){const t=m(e.newEnv,e.remainingOperation,{type:"seq",nodes:o});for(const n of t){let t=[];"seq"!=n.newTermNode.type?t.push(n.newTermNode):t=n.newTermNode.nodes,a.push({newEnv:n.newEnv,newTermNode:{type:"seq",nodes:[e.newTermNode,...t]},remainingOperation:n.remainingOperation})}}return a}if("lambda"===n.type){let r=N(t);return c(E(e),r,n)}return"insert"===r.type||"delete"===r.type||"replace"===r.type?c(E(e),r,n).map((e=>{let t=e.remainingOperation;if("id"===t.type||"insert"===t.type||"delete"===t.type||"replace"===t.type){let t=(0,l.evaluateTermNode)(e.newTermNode).length;const n=o.map((e=>{if("id"===e.type)return e;if("position"in e)return Object.assign(Object.assign({},e),{position:e.position-t});throw new Error("All operations must have positions")}));let r={type:"bulk",operations:[e.remainingOperation,...n]};return[{newEnv:e.newEnv,newTermNode:e.newTermNode,remainingOperation:r}]}throw new Error("nested bulk current not supported")})).reduce(((e,t)=>e.concat(t)),[]):"id"===r.type?0==o.length?c(E(e),r,n):m(e,{type:"bulk",operations:o},n):[]}function v(e){return!!("number"==typeof e||"boolean"==typeof e||"string"==typeof e||null===e||Array.isArray(e)&&e.every(v)||"object"==typeof e&&null!==e&&"object"===e.type&&"object"==typeof e.fields)}function O(e){if(null===e||"object"!=typeof e)return e;if(Array.isArray(e))return e.map(O);if(function(e){return e&&"object"==typeof e&&"object"===e.type&&"fields"in e}(e)){const t={};for(const n in e.fields)e.fields.hasOwnProperty(n)&&(t[n]=O(e.fields[n]));return{type:"object",fields:t}}const t={};for(const n in e)e.hasOwnProperty(n)&&(t[n]=O(e[n]));return t}function E(e){const t={};for(const n in e)if(e.hasOwnProperty(n)){const[r,o]=e[n];t[n]=[O(r),O(o)]}return t}function j(e,t,n){if("number"==typeof n||"boolean"==typeof n||"string"==typeof n||null===n)e[t]=[n,[]];else if(Array.isArray(n))e[t]=[n,[]];else if("object"==typeof n&&null!==n&&"fields"in n){const r={type:"object",fields:{}};for(const e in n.fields)r.fields[e]=[];e[t]=[n,[r]]}return e}function N(e){if("bulk"===e.type){const t=e.operations.map(N);return Object.assign(Object.assign({},e),{operations:t})}return Object.assign({},e)}},238:(e,t)=>{function n(e){if(null===e)return"null";if("number"==typeof e||"boolean"==typeof e||"string"==typeof e)return e.toString();if("object"===e.type){const t=e;let r="{";for(const e in t.fields)t.fields.hasOwnProperty(e)&&(r+=`${e}: ${n(t.fields[e])}, `);return r.length>1&&(r=r.slice(0,-2)),r+="}",r}if(Array.isArray(e)){const t=e;let r="[";for(const e of t)r+=`${n(e)}, `;return r.length>1&&(r=r.slice(0,-2)),r+="]",r}throw new Error("Unhandled value type: "+typeof e)}Object.defineProperty(t,"__esModule",{value:!0}),t.evaluateTermNode=function e(t){switch(t.type){case"const":case"sep":case"loopfront":case"looprear":return t.value;case"space":return" ".repeat(t.width);case"exp":return String(n(t.binding[1]));case"seq":return t.nodes.map((t=>e(t))).join("");case"lambda":return e(t.body);case"branchstart":case"branchend":case"end":case"bot":case"nop":return"";default:throw new Error(`Unhandled term node type: ${t.type}`)}},t.printValue=n},619:(e,t)=>{function n(e){let t=e.type;if("seq"===t)return r(e);if("declare"===t||"assign"===t)throw new Error("declare|assign node shall not be visible here.");if("lambda"===t){let t=e,r=n(t.body);return{type:"lambda",variable:t.variable,body:r,binding:t.binding,marker:t.marker}}return e}function r(e){let t=e.nodes,i=[];for(let e=0;e<t.length;e++){let a=t[e];if("declare"===a.type||"assign"===a.type){let n=a,s=n.name,[l,c]=o(t,e+1,s),{nodes:p}=r({type:"seq",nodes:l}),u=p.pop();if(void 0===u)throw new Error("lastNode should be declareend or others");if("declareend"!=u.type){p.push(u);let e={type:"lambda",variable:n.name,body:{type:"seq",nodes:p},binding:n.value,marker:{type:a.type}};i.push(e)}else{let e={type:"lambda",variable:n.name,body:{type:"seq",nodes:p},binding:n.value,marker:{type:a.type}};i.push(e),"declare"!=a.type&&i.push(u)}e=c}else i.push(n(a))}return{type:"seq",nodes:i}}function o(e,t,n){let r=[],o=t;for(;o<e.length;o++){if("declareend"===e[o].type&&e[o].name.name===n.name)return r.push(e[o]),[r,o];r.push(e[o])}return[r,o]}Object.defineProperty(t,"__esModule",{value:!0}),t.lambdalize=n},784:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.unLambdalize=function e(t){switch(t.type){case"lambda":{const{variable:n,body:r,binding:o,marker:i}=t;let a;switch(i.type){case"declare":return a=e(r),{type:"seq",nodes:[{type:"declare",name:n,value:o},a]};case"assign":return a=e(r),{type:"seq",nodes:[{type:"assign",name:n,value:o},a]};default:return{type:"lambda",variable:n,body:e(r),binding:o,marker:i}}}case"seq":return{type:"seq",nodes:t.nodes.map(e)};default:return t}}},893:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.printNode=function e(t,n=""){switch(console.log(n+t.type),t.type){case"const":case"sep":console.log(n+" Value: "+t.value);break;case"space":console.log(n+" Width: "+t.width);break;case"declare":case"assign":console.log(n+" Name: "+t.name.name),o(t.value,n+" ");break;case"exp":o(t.binding,n+" ");break;case"seq":t.nodes.forEach((t=>e(t,n+" ")));break;case"branchstart":o(t.condition,n+" ");break;case"loopfront":console.log(n+" front: "+t.value);break;case"looprear":console.log(n+" rear: "+t.value);break;case"lambda":console.log(n+" Variable: "+t.variable.name),e(t.body,n+" "),o(t.binding,n+" "),a(t.marker,n+" ");break;case"callstart":console.log(n+" Name: "+t.name);break;case"end":console.log(`${n} End`)}},t.printBinding=o,t.printValue=i,t.printMarker=a;const r=n(235);function o(e,t){(0,r.printExpression)(e[0],t),i(e[1],t)}function i(e,t){if(null===e)console.log(`${t}null`);else if("number"==typeof e||"boolean"==typeof e||"string"==typeof e)console.log(`${t}${e}`);else{if("object"!=typeof e)throw new Error("Unhandled value type: "+typeof e);{console.log(`${t}{`);const n=e;for(const e in n)if(n.hasOwnProperty(e)){const r=t+" ";console.log(`${r}${e}:`),i(n[e],r+" ")}console.log(`${t}}`)}}}function a(e,t=""){console.log(t+e.type)}},446:(e,t)=>{function n(e,t){switch(t.type){case"constant":return[e,t.value];case"variable":const r=e.get(t.name);if(void 0===r)throw new Error(`Variable '${t.name}' not found in environment`);return[e,r];case"binary":let[o,i]=n(e,t.left),[a,s]=n(o,t.right);switch(t.operator){case"+":return[a,i+s];case"-":return[a,i-s];case"*":return[a,i*s];case"/":return[a,i/s];case"&&":return[a,i&&s];case"||":return[a,i||s];case">":return[a,i>s];case"<":return[a,i<s];case">=":return[a,i>=s];case"<=":return[a,i<=s];case"==":return[a,i==s];case"!=":return[a,i!=s];default:throw new Error("Unsupported binary operator")}case"unary":let[l,c]=n(e,t.operand);if("not"===t.operator)return[l,!c];throw new Error("Unsupported unary operator");case"field":let p,[u,d]=n(e,t.object);if("string"!=typeof d&&("object"!=typeof d||null===d))throw new Error(`Cannot access field '${t.field}' from non-object`);if(p="string"==typeof d?d[t.field]:d.fields[t.field],void 0===p)throw new Error(`Field '${t.field}' not found in object`);return[u,p];case"array":let f=[],y=e;for(const e of t.elements){let[t,r]=n(y,e);f.push(r),y=t}return[y,f];case"object":let b={type:"object",fields:{}},h=e;for(const e in t.fields)if(t.fields.hasOwnProperty(e)){let[r,o]=n(h,t.fields[e]);b.fields[e]=o,h=r}return[h,b];case"freeze":return n(e,t.expression);default:throw new Error("Unhandled expression type")}}Object.defineProperty(t,"__esModule",{value:!0}),t.partialEval=function e(t,r){switch(r.type){case"const":return[t,{type:"const",value:r.value}];case"space":return[t,{type:"space",width:r.width}];case"declare":const[o,i]=n(t,r.value);return o.set(r.name.name,i),[o,{type:"declare",name:r.name,value:[r.value,i]}];case"declareend":return[t,r];case"assign":const[a,s]=n(t,r.value);return a.set(r.name.name,s),[a,{type:"assign",name:r.name,value:[r.value,s]}];case"exp":const[l,c]=n(t,r.expression);return[l,{type:"exp",binding:[r.expression,c]}];case"seq":let p=t;const u=[];for(const t of r.nodes){const[n,r]=e(p,t);u.push(r),p=n}return[p,{type:"seq",nodes:u}];case"ite":const[d,f]=n(t,r.condition);if(!0===f){const[t,n]=e(d,r.trueBranch);return[t,{type:"seq",nodes:[{type:"branchstart",condition:[r.condition,f],trueBranch:r.trueBranch,falseBranch:r.falseBranch},n,{type:"branchend",condition:[r.condition,f]}]}]}if(!1===f){const[t,n]=e(d,r.falseBranch);return[t,{type:"seq",nodes:[{type:"branchstart",condition:[r.condition,f],trueBranch:r.trueBranch,falseBranch:r.falseBranch},n,{type:"branchend",condition:[r.condition,f]}]}]}throw new Error(`Condition does not evaluate to boolean: ${f}`);case"loop":const{lst:y,separator:b,front:h,rear:g,body:w}=r,[m,v]=n(t,y);if(Array.isArray(v)&&0===v.length)return[m,{type:"seq",nodes:[{type:"loopfront",lst:y,value:h.value,body:w,separator:b},{type:"looprear",lst:y,value:g.value}]}];if(Array.isArray(v)&&1===v.length){const n=v[0],r=w.variable.name,o=new Map(t);o.set(r,n);const[i,a]=e(o,w.body);return[i,{type:"seq",nodes:[{type:"loopfront",lst:y,value:h.value,body:w,separator:b},{type:"lambda",variable:w.variable,binding:[{type:"constant",value:n},n],marker:{type:"loopitem",lst:y},body:a},{type:"looprear",lst:y,value:g.value}]}]}if(Array.isArray(v)&&v.length>1){let n=new Map(t),o=[];for(let t=0;t<v.length;t++){const r=v[t],i=w.variable.name;n.set(i,r);const[a,s]=e(n,w.body);n=a,o.push({type:"lambda",variable:w.variable,body:s,binding:[{type:"constant",value:r},r],marker:{type:"loopitem",lst:y}})}let i=o[o.length-1];for(let e=o.length-2;e>=0;e--){let t=o[e];i={type:"lambda",variable:t.variable,binding:t.binding,marker:t.marker,body:{type:"seq",nodes:[t.body,r.separator,i]}}}return[n,{type:"seq",nodes:[{type:"loopfront",lst:y,value:h.value,body:w,separator:b},i,{type:"looprear",lst:y,value:g.value}]}]}throw new Error("Unsupported type or value for e_arr: "+typeof v);case"nop":return[t,{type:"nop"}];case"bot":return[t,{type:"bot"}];case"end":return[t,{type:"end"}];default:throw new Error(`Unhandled term node type: ${r.type}`)}},t.evaluateExpr=n,t.flatten=function e(t){return"seq"===t.type?{type:"seq",nodes:t.nodes.map((t=>e(t))).flatMap((e=>"seq"===e.type?e.nodes:[e]))}:"lambda"===t.type?Object.assign(Object.assign({},t),{body:e(t.body)}):t}},887:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.unPartialEval=l,t.flatten=function e(t){if("seq"===t.type){const n=t.nodes.map((t=>e(t)));return 1==n.length?n[0]:{type:"seq",nodes:n.flatMap((e=>"seq"===e.type?e.nodes:[e]))}}return"ite"===t.type?Object.assign(Object.assign({},t),{trueBranch:e(t.trueBranch),falseBranch:e(t.falseBranch)}):"loop"===t.type?Object.assign(Object.assign({},t),{body:Object.assign(Object.assign({},t.body),{body:e(t.body.body)})}):t};const a=i(n(454));function s(e){return e[0]}function l(e){switch(e.type){case"const":return a.constNode(e.value);case"space":return a.space(e.width);case"declare":return a.declare(e.name,s(e.value));case"assign":return a.assign(e.name,s(e.value));case"exp":return a.exp(s(e.binding));case"nop":return a.nop();case"end":return a.end();case"bot":return a.bot();case"seq":return c(e);case"lambda":return p(e);case"sep":return e;default:throw new Error(`Unknown node type: ${e.type}`)}}function c(e){const t=e.nodes,n=[];for(let e=0;e<t.length;e++){const o=t[e];if("branchstart"===o.type){const r=o.condition[0],i=o.condition[1],s=o.trueBranch,l=o.falseBranch;let p=1;const u=[];for(let n=e+1;n<t.length;n++){const e=t[n];if("branchstart"===e.type&&p++,"branchend"===e.type){if(1==p)break;p--,u.push(t[n])}else u.push(t[n])}i?n.push(a.ite(r,c({type:"seq",nodes:u}),l)):n.push(a.ite(r,s,c({type:"seq",nodes:u}))),e+=u.length+1}else if("loopfront"===o.type){let i=o,s=null,l=null,c=null,p=t[++e];if("looprear"===p.type)s=p;else{let n=p;if([l,c]=u(n),p=t[++e],"looprear"!==p.type)throw new Error("Shall end with looprear node");s=p}if(""===i.value&&""===s.value&&null===l&&null===c)n.push(a.bot());else if(null===l||d(l))n.push(a.loop(i.lst,i.separator,{type:"front",value:o.value},{type:"rear",value:s.value},i.body));else{if("bot"===l.type)throw new Error("lambda is bot node");if("lambdawithexpr"!==l.type)throw new Error("Must be lambda in loopitem");n.push(a.loop(i.lst,null===c?i.separator:c,{type:"front",value:o.value},{type:"rear",value:s.value},{type:"lambda",variable:(r=l).variable,body:r.body}))}}else n.push(l(o))}var r;return a.seq(...n)}function p(e){let{variable:t,body:n,binding:r,marker:o}=e;return{type:"lambdawithexpr",variable:t,body:l(n),exp:(i=r[1],Array.isArray(i)?{type:"array",elements:i.map(f)}:{type:"array",elements:[f(i)]})};var i}function u(e){let t=e,n=e.body,r=null,o=[],i=null;for(let e of n.nodes)"sep"===e.type?i=e:"lambda"===e.type?r=e:o.push(e);if(t.body=o.every(d)?{type:"bot"}:{type:"seq",nodes:o},null==r)return"bot"===t.body.type?[{type:"bot"},null]:[p(t),null];{let[e,n]=u(r);if("bot"===e.type&&null===n)return[p(t),i];if("bot"===t.body.type&&null===i)return[e,n];if("bot"===t.body.type&&""===(null==i?void 0:i.value))return[e,n];{let r=p(t);if(s=e,y((a=r).variable,s.variable)&&h(a.body,s.body)){if(null===n)return[r,i];if(null!=i&&n.value===i.value)return[r,i];throw new Error("sepNode not equal")}throw new Error("lambda body not equal")}}var a,s}function d(e){return"bot"===e.type}function f(e){if("number"==typeof e||"boolean"==typeof e||"string"==typeof e||null===e)return{type:"constant",value:e};if(Array.isArray(e))return{type:"array",elements:e.map(f)};if("object"===e.type){const t={};for(const n in e.fields)if(e.fields.hasOwnProperty(n)){const r=e.fields[n];t[n]={type:"constant",value:r}}return{type:"object",fields:t}}throw new Error("Unsupported Value type")}function y(e,t){return e.name===t.name}function b(e,t){if(e.type!==t.type)return!1;switch(e.type){case"constant":const n=e,r=t;return"object"==typeof n.value&&"object"==typeof r.value?b(n.value,r.value):n.value===r.value;case"variable":return e.name===t.name;case"binary":const o=e,i=t;return o.operator===i.operator&&b(o.left,i.left)&&b(o.right,i.right);case"unary":const a=e,s=t;return a.operator===s.operator&&b(a.operand,s.operand);case"field":const l=e,c=t;return l.field===c.field&&b(l.object,c.object);case"array":const p=e,u=t;return p.elements.length===u.elements.length&&p.elements.every(((e,t)=>b(e,u.elements[t])));case"object":const d=e,f=t,y=Object.keys(d.fields),h=Object.keys(f.fields);return y.length===h.length&&y.every((e=>f.fields.hasOwnProperty(e)&&b(d.fields[e],f.fields[e])));case"freeze":return b(e.expression,t.expression);default:return!1}}function h(e,t){if(e.type!==t.type)return!1;switch(e.type){case"const":return e.value===t.value;case"space":return e.width===t.width;case"declare":const n=e,r=t;return y(n.name,r.name)&&b(n.value,r.value);case"assign":const o=e,i=t;return y(o.name,i.name)&&b(o.value,i.value);case"exp":return b(e.expression,t.expression);case"seq":const a=e.nodes.filter((e=>!("const"===e.type&&""===e.value))),s=t.nodes.filter((e=>!("const"===e.type&&""===e.value)));return a.length===s.length&&a.every(((e,t)=>h(e,s[t])));case"ite":const l=e,c=t;return b(l.condition,c.condition)&&h(l.trueBranch,c.trueBranch)&&h(l.falseBranch,c.falseBranch);case"loop":const p=e,u=t;return b(p.lst,u.lst)&&p.separator.value===u.separator.value&&p.front.value===u.front.value&&p.rear.value===u.rear.value&&function(e,t){return y(e.variable,t.variable)&&h(e.body,t.body)}(p.body,u.body);case"nop":case"bot":case"end":return!0;case"call":const d=e,f=t;return h(d.func,f.func)&&Object.keys(d.args).length===Object.keys(f.args).length&&Object.keys(d.args).every((e=>b(d.args[e],f.args[e])));case"lambdawithexpr":const g=e,w=t;return y(g.variable,w.variable)&&h(g.body,w.body);default:return!1}}},661:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.scopelize=function e(t){switch(t.type){case"declare":return{type:"seq",nodes:[t,{type:"declareend",name:t.name}]};case"seq":return function(t){let n=new a.Stack,r=[];for(let o of t.nodes){if("declare"===o.type){let e={type:"declareend",name:o.name};n.push(e)}"declare"===o.type?r.push(o):r.push(e(o))}if(0==r.length)return t;{let e=r.pop();for("end"!==e.type&&r.push(e);!n.isEmpty();){let e=n.pop();if(!e)throw new Error("endNode is undefined.");r.push(e)}"end"===(null==e?void 0:e.type)&&r.push(e)}return Object.assign(Object.assign({},t),{nodes:r})}(t);case"ite":let n=t.trueBranch,r=t.falseBranch;return{type:"ite",condition:t.condition,trueBranch:e(n),falseBranch:e(r)};case"loop":let o=t.body,i=o.body;return Object.assign(Object.assign({},t),{body:Object.assign(Object.assign({},o),{body:e(i)})});case"call":case"const":case"space":case"declareend":case"assign":case"exp":case"nop":case"bot":case"end":case"lambdawithexpr":case"sep":return t}return{type:"bot"}};const a=i(n(694))},577:(e,t)=>{function n(e){const t=/«[^»]*»/g;let n;const r=[];let o=0;for(;null!==(n=t.exec(e));)o<n.index&&r.push(e.slice(o,n.index)),r.push(n[0]),o=t.lastIndex;return o<e.length&&r.push(e.slice(o)),r}function r(e){var t,n,i;const a=[];let s=0;for(;s<e.length;){const l=e[s];if("string"==typeof l&&l.startsWith("«")&&l.endsWith("»")){const c=l.slice(1,-1).trim(),[p]=c.split(" ");switch(p.toUpperCase()){case"VAR":const l=c.split("=");a.push({type:"Directive",content:{type:"declare",name:l[0].split(" ")[1].trim(),expr:o(l[1].trim())}});break;case"IF":const p=c.match(/^(IF)\s+/i);if(p){const t=c.slice(p[0].length).trim(),n=[],i=[];let l=1,u=s+1;for(;u<e.length;){let t=e[u];if("string"==typeof t&&t.startsWith("«"))if(t.includes("ENDIF")||t.includes("endif")){if(l--,l<=0)break}else if((t.includes("else")||t.includes("ELSE"))&&1===l){if(l=0,!t.includes("if")&&!t.includes("IF")){u++;continue}t=t.replace(/else(?=if)/,"")}l>0?n.push(t):i.push(t),u++}const d=r(n),f=r(i);s=u,a.push({type:"Directive",content:{type:"if",expr:o(t),thenBranch:d,elseBranch:f}})}break;case"FOR":const u=/(\bfor\b|\bin\b|\bseparator\b|\bfront\b|\brear\b|[\[\],])|(\w+)|"([^"]*)"|\[([^\]]*)\]/gi,d=[];let f;for(;null!==(f=u.exec(c));)f[1]&&d.push(f[1]),f[2]&&d.push(f[2]),f[3]&&d.push(f[3]),f[4]&&d.push(f[4]);const y=c.match(/separator\s*"([^"]*)"/i);y&&y[1]&&d.push("separator",y[1]);const b=c.match(/front\s*(?:\[([^\]]*)\])?/i);b&&b[1]&&d.push("front",b[1]);const h=c.match(/rear\s*(?:\[([^\]]*)\])?/i);if(h&&h[1]&&d.push("rear",h[1]),d){const l=[];let c=1,p=s+1;for(;p<e.length&&c>0;){const t=e[p];if("string"==typeof t&&t.startsWith("«"))if(t.includes("ENDFOR")||t.includes("endfor")){if(c--,0===c)break}else(t.includes("FOR")||t.includes("for"))&&c++;c>0&&l.push(t),p++}const u=r(l);s=p,a.push({type:"Directive",content:{type:"for",name:d[1].trim(),expr:o(d[3].trim()),separator:{type:"separator",value:(null===(t=d[5])||void 0===t?void 0:t.trim())||""},front:{type:"front",value:(null===(n=d[7])||void 0===n?void 0:n.trim())||""},rear:{type:"rear",value:(null===(i=d[9])||void 0===i?void 0:i.trim())||""},fragment:u}})}break;default:if(c.includes("=")){const e=c.split("=");a.push({type:"Directive",content:{type:"assign",name:e[0].split(" ")[0].trim(),expr:o(e[1].trim())}})}else a.push({type:"Directive",content:{type:"exp",expr:o(c)}})}}else a.push(l);s++}return 1===a.length?a[0]:{type:"fragmentList",fragments:a}}function o(e){const t=function(e){const t=/"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|(&&|\|\||[<>!=]=|[{}[\](),.:;!+\-*/<>])|(\s+)|([^\s{}[\](),.:;!+\-*/<>!=&|]+)/g,n=[];let r;for(;null!==(r=t.exec(e));)void 0!==r[1]?n.push(`"${r[1]}"`):void 0!==r[2]?n.push(`'${r[2]}'`):void 0!==r[3]?n.push(r[3]):void 0!==r[5]&&n.push(r[5]);return n}(e);let n=0;function r(){if("("===t[n]){n++;const e=o();if(")"===t[n])return n++,e;throw new Error("Mismatched parentheses")}if("["===t[n]){n++;const e=[];for(;"]"!==t[n];)","===t[n]?n++:e.push(o());return n++,{type:"array",elements:e}}if("{"===t[n]){n++;const e={};for(;"}"!==t[n];)if(","===t[n])n++;else{const r=t[n++];if(":"!==t[n])throw new Error("Expected colon in object literal");{n++;const t=o();e[r]=t}}return n++,{type:"object",fields:e}}if("-"===t[n]&&!isNaN(Number(t[n+1])))return n++,{type:"constant",value:-Number(t[n++])};if(!isNaN(Number(t[n])))return{type:"constant",value:Number(t[n++])};if(/^['"].*['"]$/.test(t[n]))return{type:"constant",value:t[n++].slice(1,-1)};if(/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(t[n])){let e={type:"variable",name:t[n++]};for(;"."===t[n];){if(n++,/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(t[n]))return{type:"field",object:e,field:t[n++]};throw new Error("Expected property name after '.'")}return e}if("!"===t[n])return n++,{type:"freeze",expression:r()};throw new Error(`Unexpected token: ${t[n]}`)}function o(){let e=r();for(;["+","-","*","/","&&","||",">","<",">=","<=","!=","==","=="].includes(t[n]);)e={type:"binary",operator:t[n++],left:e,right:r()};return e}return o()}Object.defineProperty(t,"__esModule",{value:!0}),t.parse=function(e){return r(n(e))},t.tokenize=n,t.parseTokens=r},775:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.translate=function(e){const t=s(e);return a.seq(...t)};const a=i(n(454));function s(e){if("string"==typeof e)return function(e){const t=[];let n="";for(let r of e)if(" "===r){if(n.length>0){if(n.endsWith("\n")){const e=n.split(/(\n)/);for(const n of e)""!=n&&t.push(a.constNode(n))}else t.push(a.constNode(n));n=""}t.push(a.space(1))}else n+=r;if(n.length>0)if(n.endsWith("\n")){const e=n.split(/(\n)/);for(const n of e)""!=n&&t.push(a.constNode(n))}else t.push(a.constNode(n));return t}(e);switch(e.type){case"Directive":switch(e.content.type){case"declare":return[a.declare({type:"variable",name:e.content.name},e.content.expr)];case"assign":return[a.assign({type:"variable",name:e.content.name},e.content.expr)];case"exp":return[a.exp(e.content.expr)];case"if":return[a.ite(e.content.expr,{type:"seq",nodes:s(e.content.thenBranch)},l(e.content.elseBranch))];case"for":return[a.loop(e.content.expr,{type:"sep",value:e.content.separator.value},{type:"front",value:e.content.front.value},{type:"rear",value:e.content.rear.value},{type:"lambda",variable:{type:"variable",name:e.content.name},body:{type:"seq",nodes:s(e.content.fragment)}})];default:throw new Error(`Unknown directive: ${e}`)}case"fragmentList":return e.fragments.flatMap(s);default:throw new Error(`Unknown fragment: ${e}`)}}function l(e){if((t=e)&&"if"===t.type){const t=e;return a.ite(t.expr,{type:"seq",nodes:s(t.thenBranch)},l(t.elseBranch))}return function(e){return e&&"bot"===e.type}(e)?a.bot():{type:"seq",nodes:s(e)};var t}},694:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Stack=void 0,t.Stack=class{constructor(){this.items=[]}push(e){this.items.push(e)}pop(){return this.items.pop()}peek(){return this.items[this.items.length-1]}isEmpty(){return 0===this.items.length}size(){return this.items.length}clear(){this.items=[]}print(){console.log(this.items.toString())}}},63:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.isWhitespace=function(e){return/^( +)$/.test(e)},t.containsNewlineOrSpace=function(e){return/[\n\s]/.test(e)}}},t={};return function n(r){var o=t[r];if(void 0!==o)return o.exports;var i=t[r]={exports:{}};return e[r].call(i.exports,i,i.exports,n),i.exports}(612)})()));
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.BiEval=t():e.BiEval=t()}(this,(()=>(()=>{"use strict";var e={612:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.evaluateToLambdaAST=h,t.forward=function(e){let t=u.parse(e),n=d.translate(t);return c.evaluateTermNode(h(n))},t.backward=function(e,t){let n=u.parse(e),r=h(d.translate(n)),o=(0,y.fuse)({},t,r).map((({newTermNode:e,remainingOperation:t})=>{let n=(0,s.flatten)((0,b.unLambdalize)(e));try{let e=p.unPartialEval(n),t=p.flatten(e),r=f.printToSurface(t);return console.log("biEval backward, success:\n",r),r}catch(e){const t=e;return console.log("biEval backward, fail:"+t.message),""}}));return console.log("solution list:",o),o=o.filter((e=>""!=e)),Array.from(new Set(o))};const a=i(n(661)),s=n(446),l=n(619),c=i(n(238)),p=i(n(887)),u=i(n(577)),d=i(n(775)),f=i(n(69)),y=n(963),b=n(784);function h(e){const t=new Map,n=a.scopelize(e),[r,o]=(0,s.partialEval)(t,n),i=(0,s.flatten)(o);return(0,l.lambdalize)(i)}},75:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.findVariablesAndFields=function(e){let t=[],n=[];return function e(r){switch(r.type){case"variable":t.push(r);break;case"field":"variable"===r.object.type?n.push({variable:r.object,field:r.field}):e(r.object);break;case"binary":e(r.left),e(r.right);break;case"unary":e(r.operand);break;case"array":r.elements.forEach(e);break;case"object":Object.values(r.fields).forEach(e);break;case"freeze":e(r.expression)}}(e),{variables:t,fields:n}}},143:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.prettyPrint=function e(t){switch(t.type){case"constant":let n=t.value;if("string"==typeof n)return'"'+String(n)+'"';if("object"!=typeof n||null===n)return String(n);if(!Array.isArray(n))return e(n);if(0===n.length)return"[]";if(0!==n.length)return"["+n.map(e).join(", ")+"]";case"variable":return t.name;case"binary":return e(t.left)+t.operator+e(t.right);case"unary":return t.operator+e(t.operand);case"field":return e(t.object)+"."+t.field;case"array":return"["+t.elements.map((t=>e(t))).join(",")+"]";case"freeze":return"!"+e(t.expression);case"object":let r=t.fields;return"{"+Object.keys(r).map((t=>t+":"+e(r[t]))).join(", ")+"}";default:return""}},t.prettyPrintBinaryOperator=function(e){return e},t.prettyPrintUnaryOperator=function(e){return e}},235:(e,t)=>{function n(e,t){if(null===e)console.log(`${t}null`);else if("number"==typeof e||"boolean"==typeof e||"string"==typeof e)console.log(`${t}${e}`);else if("object"==typeof e){console.log(`${t}{`);const r=e;for(const e in r)if(r.hasOwnProperty(e)){const o=t+" ";console.log(`${o}${e}:`),n(r[e],o+" ")}console.log(`${t}}`)}else{if("constant"!=typeof e)throw new Error("Unhandled value type: "+typeof e);n(e.value,t+" ")}}Object.defineProperty(t,"__esModule",{value:!0}),t.printExpression=function e(t,r=""){switch(t.type){case"constant":n(t.value,r+" ");break;case"variable":console.log(`${r}Variable: ${t.name}`);break;case"binary":console.log(`${r}BinaryOperation: ${t.operator}`),e(t.left,r+" "),e(t.right,r+" ");break;case"unary":console.log(`${r}UnaryOperation: ${t.operator}`),e(t.operand,r+" ");break;case"field":console.log(`${r}FieldAccess: ${t.field}`),e(t.object,r+" ");break;case"array":console.log(`${r}ArrayLiteral:`),t.elements.forEach((t=>e(t,r+" ")));break;case"object":console.log(`${r}ObjectLiteral:`),Object.keys(t.fields).forEach((n=>{console.log(`${r} ${n} = `),e(t.fields[n],r+" ")}));break;case"freeze":console.log(`${r}Freeze:`),e(t.expression,r+" ")}},t.printConstantValue=n},454:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.seq=function(...e){return{type:"seq",nodes:e}},t.constNode=function(e){return{type:"const",value:e}},t.space=function(e){return{type:"space",width:e}},t.declare=function(e,t){return{type:"declare",name:e,value:t}},t.exp=function(e){return{type:"exp",expression:e}},t.loop=function(e,t,n,r,o){return{type:"loop",lst:e,separator:t,front:n,rear:r,body:o}},t.ite=function(e,t,n){return{type:"ite",condition:e,trueBranch:t,falseBranch:n}},t.assign=function(e,t){return{type:"assign",name:e,value:t}},t.sep=function(e){return{type:"sep",value:e}},t.nop=function(){return{type:"nop"}},t.bot=function(){return{type:"bot"}},t.end=function(){return{type:"end"}}},69:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.printToSurface=function e(t){let n="";switch(t.type){case"const":return t.value;case"space":return" ".repeat(t.width);case"declare":return n="«",n+="var ",n+=a.prettyPrint(t.name),n+="=",n+=a.prettyPrint(t.value),n+="»",n;case"assign":return n="«",n+=a.prettyPrint(t.name),n+="=",n+=a.prettyPrint(t.value),n+="»",n;case"exp":return n="«",n+=a.prettyPrint(t.expression),n+="»",n;case"seq":return t.nodes.map((t=>e(t))).join("");case"ite":n="«if "+a.prettyPrint(t.condition)+"»",n+=e(t.trueBranch);let r=t.falseBranch;return"ite"===r.type||"seq"===r.type&&1==r.nodes.length?n+="«elseif"+e(r).slice(3):"bot"!==r.type&&"seq"===r.type&&0!=r.nodes.length&&(n+="«else»",n+=e(r)),n.endsWith("«endif»")||(n+="«endif»"),n;case"loop":return n="«for "+a.prettyPrint(t.body.variable)+" in "+a.prettyPrint(t.lst),t.separator.value&&(n+=' separator "'+t.separator.value+'"'),t.front.value&&(n+=' front "'+t.front.value+'"'),t.rear.value&&(n+=' rear "'+t.rear.value+'"'),n+="»",n+=e(t.body.body),n+="«endfor»",n;default:return""}return""};const a=i(n(143))},963:function(e,t,n){var r=this&&this.__rest||function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n};Object.defineProperty(t,"__esModule",{value:!0}),t.fuse=c,t.fuseExp=p,t.valueToConstantExpr=w,t.printEnvironment=function(e){console.log("{\n");for(const t in e)if(e.hasOwnProperty(t)){const[n,r]=e[t];console.log(`${t}: {`),console.log(" val: "),(0,s.printValue)(n," "),console.log(",marks: ["),r.forEach(((e,t)=>{(0,s.printValue)(e," "),t<r.length-1&&console.log(", ")})),console.log("]\n },\n")}console.log("}")},t.fuseBulk=m;const o=n(63),i=n(75),a=n(446),s=n(893),l=n(238);function c(e,t,n){if("const"!==n.type&&"sep"!==n.type&&"loopfront"!==n.type&&"looprear"!==n.type||"string"!=typeof n.value)if("space"===n.type)switch(t.type){case"insert":const{str:r,position:i}=t;if(0==n.width)return[{newEnv:E(e),newTermNode:n,remainingOperation:t},{newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:r},n]},remainingOperation:{type:"id"}}];if(0==i){let t=[{newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:r},n]},remainingOperation:{type:"id"}}];return(0,o.isWhitespace)(r)&&t.push({newEnv:E(e),newTermNode:{type:"space",width:n.width+r.length},remainingOperation:{type:"id"}}),t}if(i<n.width){if((0,o.isWhitespace)(r))return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:r.length+n.width}),remainingOperation:{type:"id"}}];throw new Error(`Cannot insert non-space between space term: ${t}`)}if(i===n.width){let t=[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"insert",str:r,position:i-n.width}}];return(0,o.isWhitespace)(r)&&t.push({newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:n.width+r.length}),remainingOperation:{type:"id"}}),t}{const t=i-n.width;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"insert",str:r,position:t}}]}case"delete":const{str:a,position:s}=t;if(0!==s){if(s<n.width&&s+a.length<=n.width){const t=n.width-a.length;return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:t}),remainingOperation:{type:"id"}}]}if(s<n.width&&s+a.length>n.width){const t=s,r=a.substring(n.width-s);return[{newEnv:E(e),newTermNode:{type:"space",width:t},remainingOperation:{type:"delete",str:r,position:0}}]}{const t=s-n.width;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"delete",str:a,position:t}}]}}if((0,o.isWhitespace)(a)&&a.length<=n.width){const t=n.width-a.length;return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:t}),remainingOperation:{type:"id"}}]}if(a.length>n.width){const t=a.slice(n.width);return[{newEnv:E(e),newTermNode:{type:"bot"},remainingOperation:{type:"delete",str:t,position:0}},{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:0}),remainingOperation:{type:"delete",str:t,position:0}}]}case"replace":const{str1:l,str2:c,position:p}=t;if(0!==p){if(p<n.width&&p+l.length<=n.width){if((0,o.isWhitespace)(c))return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:c.length+n.width-l.length}),remainingOperation:{type:"id"}}];{const t=" ".repeat(p)+c+" ".repeat(n.width-(p+l.length));return[{newEnv:E(e),newTermNode:{type:"const",value:t},remainingOperation:{type:"id"}}]}}if(p<n.width&&p+l.length>n.width){const t=" ".repeat(p),r=n.width-p,o=c.substring(0,r),i={type:"replace",str1:l.substring(r),str2:c.substring(r),position:0},a={type:"const",value:t+o};return[{newEnv:E(e),newTermNode:a,remainingOperation:i}]}{const t=p-n.width;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"replace",str1:l,str2:c,position:t}}]}}if(l.length<=n.width){if((0,o.isWhitespace)(c)){const t=n.width-l.length+c.length;return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{width:t}),remainingOperation:{type:"id"}}]}return[{newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:c},Object.assign(Object.assign({},n),{width:n.width-l.length})]},remainingOperation:{type:"id"}}]}if(l.length>n.width){const t=c.slice(0,n.width),r=l.slice(n.width),o=c.slice(n.width);return[{newEnv:E(e),newTermNode:{type:"const",value:t},remainingOperation:{type:"replace",str1:r,str2:o,position:0}}]}case"bulk":return m(e,t,n);case"id":return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"id"}}];default:throw new Error(`Unhandled operation type: ${t}`)}else if("exp"===n.type){const a=n.binding,l=a[0],u=a[1],d="number"==typeof(s=u)||"boolean"==typeof s||"string"==typeof s?String(s):"Unknown";switch(t.type){case"insert":const{str:a,position:s}=t;if(0===s){let t=[];if(t.push({newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:a},n]},remainingOperation:{type:"id"}}),!(0,o.containsNewlineOrSpace)(a)){let r=function(e,t){try{if(v(JSON.parse(t)))return t+e}catch(e){return}}(d,a);if(void 0!==r)try{let o=y(r,u),{newEnv:i,newExp:a}=p(e,o,l);t.push({newEnv:E(i),newTermNode:Object.assign(Object.assign({},n),{binding:[a,o]}),remainingOperation:{type:"id"}})}catch(e){}}return t}if(s<d.length){const t=y(d.slice(0,s)+a+d.slice(s),u);let{newEnv:r,newExp:o}=p(e,t,l);return[{newEnv:E(r),newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(s==d.length){let r=[];if(!(0,o.containsNewlineOrSpace)(a)){const t=function(e,t){try{if(v(JSON.parse(t)))return e+t}catch(e){return}}(d,a);if(void 0!==t)try{const o=y(t,u);let{newEnv:i,newExp:a}=p(e,o,l);r.push({newEnv:E(i),newTermNode:Object.assign(Object.assign({},n),{binding:[a,o]}),remainingOperation:{type:"id"}})}catch(e){console.error(e)}}let i=E(e);return l.name&&(i[l.name]=[u,[u]]),r.push({newEnv:i,newTermNode:n,remainingOperation:Object.assign(Object.assign({},t),{position:0})}),r}{const r=s-d.length;if(l.name){let t=l.name;e[t]=[u,[u]]}return[{newEnv:E(e),newTermNode:n,remainingOperation:Object.assign(Object.assign({},t),{position:r})}]}case"delete":const{str:f,position:g}=t;if(0===g){if(f.length<d.length){const t=y(d.slice(f.length),u);let{newEnv:r,newExp:o}=p(e,t,l);return[{newEnv:E(r),newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(f.length==d.length){if(l.name){let t=l.name;if("string"==typeof u){let{newEnv:r,newExp:o}=p(e,"",l);return[{newEnv:E(r),newTermNode:Object.assign(Object.assign({},n),{binding:[o,""]}),remainingOperation:{type:"id"}},{newEnv:b(E(e),t),newTermNode:{type:"bot"},remainingOperation:{type:"id"}},{newEnv:E(e),newTermNode:{type:"bot"},remainingOperation:{type:"id"}}]}return[{newEnv:b(E(e),t),newTermNode:{type:"bot"},remainingOperation:{type:"id"}}]}if(l){let t=l.object.name,n=l.field,r=e[t][0],o=e[t][1][0];if(0==o.fields[n].length){let i=h(r,n),a=h(o,n),s=b(E(e),t);return s[t]=[i,[a]],[{newEnv:E(s),newTermNode:{type:"bot"},remainingOperation:{type:"id"}}]}throw new Error(`field has been updaeted, cannot be remvoed: ${n}$`)}}else if(f.length>d.length){let t=f.slice(0,d.length),r=f.slice(d.length),o={type:"delete",str:t,position:0},i=c(E(e),o,n),a={type:"delete",str:r,position:0};return i.forEach((e=>{e.remainingOperation=a})),i}}else{if(g+f.length<=d.length){const t=y(d.slice(0,g)+d.slice(g+f.length),u);let{newEnv:r,newExp:o}=p(e,t,l);return[{newEnv:E(r),newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(g<=d.length&&g+f.length>d.length){const t=d.slice(0,g),r=f.slice(d.length-g),o=y(t,u);let{newEnv:i,newExp:a}=p(e,o,l);return[{newEnv:E(i),newTermNode:Object.assign(Object.assign({},n),{binding:[a,o]}),remainingOperation:{type:"delete",str:r,position:0}}]}if(g>d.length){const t=g-d.length;let[{newEnv:r,newTermNode:o,remainingOperation:i}]=c(E(e),{type:"id"},n);return[{newEnv:E(r),newTermNode:o,remainingOperation:{type:"delete",str:f,position:t}}]}}case"replace":const{str1:w,str2:O,position:j}=t;if(0===j){if(d.startsWith(w)){const t=y(O+d.slice(w.length),u);let{newEnv:r,newExp:o}=p(E(e),t,l);return[{newEnv:r,newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(w.startsWith(d)){const t=y(O.slice(0,d.length),u);let{newEnv:r,newExp:o}=p(E(e),t,l);const i=w.slice(d.length),a=O.slice(d.length);return[{newEnv:r,newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"replace",str1:i,str2:a,position:0}}]}throw new Error(`unsupported replacement: ${w}; ${d}`)}if(j<d.length){const t=y(d.slice(0,j)+O+d.slice(j+w.length),u);let{newEnv:r,newExp:o}=p(E(e),t,l);return[{newEnv:r,newTermNode:Object.assign(Object.assign({},n),{binding:[o,t]}),remainingOperation:{type:"id"}}]}if(j>=d.length){const r=j-d.length;let[{newEnv:o,newTermNode:i,remainingOperation:a}]=c(E(e),{type:"id"},n);return[{newEnv:E(o),newTermNode:i,remainingOperation:Object.assign(Object.assign({},t),{position:r})}]}throw new Error("Unsupported replacement");case"bulk":return m(e,t,n);case"id":let{variables:N,fields:x}=(0,i.findVariablesAndFields)(l);return N.forEach((t=>{e=function(e,t){if(!(e.name in t))throw new Error(`Variable ${e.name} not found in environment.`);let n=t[e.name][0],r=b(t,e.name);return r[e.name]=[n,[n]],r}(t,e)})),x.forEach((({variable:t,field:n})=>{e=function(e,t,n){let o=t.name;if(!(o in n))throw new Error(`Variable ${o} not found in environment.`);let i=n[o][0],a=function(e,t,n){const o=e.fields,i=t,a=(o[i],r(o,["symbol"==typeof i?i:i+""]));return a[t]=n.fields[t],{type:"object",fields:a}}(n[o][1][0],e,i),s=b(n,o);return s[o]=[i,[a]],s}(n,t,e)})),[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"id"}}];default:throw new Error(`Unhandled operation type: ${t}`)}}else{if("lambda"===n.type){let r=n.marker;if("loopitem"===r.type){let o=[],i=n.variable.name,a=n.binding[0],s=n.binding[1],l=E(e);l=j(l,i,s);let u="";if(!r.lst)throw new Error("exp is not a Variable with a name property in loopitem's marker");{let e=r.lst.name;u=e+"_new"}let d=c(E(l),t,n.body).map((({newEnv:t,newTermNode:r,remainingOperation:o})=>{let s=E(t);if(i in s){let t=s[i][0],l=s[u][0];"object"==typeof t&&null!==t&&"type"in t&&"object"===t.type&&0===Object.keys(t.fields).length||l.push(t),s[u]=[l,[l]],s=b(E(s),i);let{newEnv:c,newExp:d}=p(s,t,a);return e[i]&&(c[i]=e[i]),{newEnv:c,newTermNode:{type:"lambda",variable:n.variable,body:"seq"!=r.type?{type:"seq",nodes:[r]}:r,binding:[d,t],marker:n.marker},remainingOperation:o}}return e[i]&&(s[i]=e[i]),{newEnv:s,newTermNode:{type:"lambda",variable:n.variable,body:"seq"!=r.type?{type:"seq",nodes:[r]}:r,binding:[{type:"constant",value:null},null],marker:n.marker},remainingOperation:o}}));return o.concat(d)}{let r=[];if("bulk"===t.type&&t.operations.length>0){let o=t.operations;const[i,...a]=o;if("insert"===i.type&&0===i.position){let t=i.str.length,o={type:"bulk",operations:a.map((e=>{if("id"===e.type)return e;if("position"in e)return Object.assign(Object.assign({},e),{position:e.position-t});throw new Error("All operations must have positions")}))},s=c(E(e),o,n);s.map((e=>(e.newTermNode={type:"seq",nodes:[{type:"const",value:i.str},e.newTermNode]},e))),r=r.concat(s)}}else if("insert"===t.type&&0===t.position){let o=c(E(e),{type:"id"},n);o.map((e=>(e.newTermNode={type:"seq",nodes:[{type:"const",value:t.str},e.newTermNode]},e))),r=r.concat(o)}let o=n.variable.name,i=n.binding[0],a=n.binding[1],s=E(e);s=j(s,o,a);let l=c(E(s),t,n.body).map((({newEnv:t,newTermNode:r,remainingOperation:a})=>{let s=t[o][0];delete t[o];let l=function(e,t){let n=E(e);for(const e in t)t.hasOwnProperty(e)&&(n[e]=t[e]);return n}(e,t),{newEnv:c,newExp:u}=p(l,s,i);return{newEnv:c,newTermNode:{type:"lambda",variable:n.variable,body:r,binding:[u,s],marker:n.marker},remainingOperation:a}}));return r=r.concat(l),r}}if("branchstart"!==n.type&&"branchend"!==n.type&&"nop"!==n.type){if("seq"===n.type){if("bulk"===t.type)return m(e,t,n);const r=n.nodes;let o=[{newEnv:e,newTermNode:{type:"seq",nodes:[]},remainingOperation:t}];for(const e of r){const t=[];for(const n of o){const r=c(E(n.newEnv),n.remainingOperation,e);for(const e of r){const r=("seq"===n.newTermNode.type?n.newTermNode.nodes:[n.newTermNode]).concat("seq"===e.newTermNode.type?e.newTermNode.nodes:[e.newTermNode]);t.push({newEnv:e.newEnv,newTermNode:{type:"seq",nodes:r},remainingOperation:e.remainingOperation})}}o=t}return o.map((e=>e))}if("end"===n.type)return[{newEnv:E(e),newTermNode:n,remainingOperation:t}];throw new Error("Operation can only be applied to ConstNode with string value")}{if("branchend"===n.type){let t=n.condition[0],r=n.condition[1],[o,i]=(0,a.evaluateExpr)(g(e),t);if(i!==r)throw new Error("Violate BX properties: updates change if-then-else branch.")}let r=[];switch(r.push({newEnv:E(e),newTermNode:n,remainingOperation:t}),t.type){case"insert":const{str:o,position:i}=t;0===i&&r.push({newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:o},n]},remainingOperation:{type:"id"}});case"delete":case"replace":case"id":return r;case"bulk":return m(e,t,n);default:throw new Error(`Unhandled operation type: ${t}`)}}}else{if("loopfront"===n.type){let t=n.lst;if(!t.name)throw new Error("exp is not a Variable with a name property in loopfront");{let n=t.name;e[n+"_new"]=[[],[]]}}else if("looprear"===n.type){let t=n.lst;if(!t.name)throw new Error("exp is not a Variable with a name property in loopfront");{let n=t.name,r=e[n+"_new"][0];r=r.reverse(),e[n]=[r,[r]]}}"sep"===n.type&&console.log("break");const r=n.value;switch(t.type){case"insert":const{str:o,position:i}=t;if(0===i){let i=[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:o+r}),remainingOperation:{type:"id"}},{newEnv:E(e),newTermNode:{type:"seq",nodes:[{type:"const",value:o},n]},remainingOperation:{type:"id"}}];return 0===r.length&&i.push({newEnv:E(e),newTermNode:n,remainingOperation:t}),i}if(i<r.length){const t=r.slice(0,i)+o+r.slice(i);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}]}if(i===r.length)return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:r+o}),remainingOperation:{type:"id"}},{newEnv:E(e),newTermNode:n,remainingOperation:{type:"insert",str:o,position:i-r.length}}];{const t=i-r.length;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"insert",str:o,position:t}}]}case"delete":const{str:a,position:s}=t;if(0===s){if(r.startsWith(a)&&a.length<=r.length){const t=r.slice(a.length);let o=[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}];return a.length===r.length&&o.push({newEnv:E(e),newTermNode:{type:"bot"},remainingOperation:{type:"id"}}),o}if(a.startsWith(r)&&a.length>r.length){const t=a.slice(r.length);return[{newEnv:E(e),newTermNode:{type:"bot"},remainingOperation:{type:"delete",str:t,position:0}},{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:""}),remainingOperation:{type:"delete",str:t,position:0}}]}throw new Error("cannot delete, not match. delStr: "+a+", s_c: "+r)}if(s<r.length&&s+a.length<=r.length){const t=r.slice(0,s)+r.slice(s+a.length);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}]}if(s<r.length&&s+a.length>r.length){const t=r.substring(0,s),n=a.substring(r.length-s);return[{newEnv:E(e),newTermNode:{type:"const",value:t},remainingOperation:{type:"delete",str:n,position:0}}]}{const t=s-r.length;return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"delete",str:a,position:t}}]}case"replace":const{str1:l,str2:c,position:p}=t;if(0!==p){if(p<r.length&&p+l.length<=r.length){const t=r.slice(0,p)+c+r.slice(p+l.length);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}]}if(p<r.length&&p+l.length>r.length){const t=r.substring(0,p),n=r.substring(p),o=c.substring(0,n.length),i={type:"replace",str1:l.substring(n.length),str2:c.substring(n.length),position:0},a={type:"const",value:t+o};return[{newEnv:E(e),newTermNode:a,remainingOperation:i}]}{const t=p-r.length;return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:r}),remainingOperation:{type:"replace",str1:l,str2:c,position:t}}]}}if(r.startsWith(l)&&l.length<=r.length){const t=c+r.slice(l.length);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"id"}}]}if(l.startsWith(r)&&l.length>r.length){const t=c.slice(0,r.length),o=l.slice(r.length),i=c.slice(r.length);return[{newEnv:E(e),newTermNode:Object.assign(Object.assign({},n),{value:t}),remainingOperation:{type:"replace",str1:o,str2:i,position:0}}]}case"bulk":return m(e,t,n);case"id":return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"id"}}];default:throw new Error(`Unhandled operation type: ${t}`)}}var s}function p(e,t,n){switch(n.type){case"constant":return{newEnv:e,newExp:w(t)};case"variable":if(!(n.name in e))throw new Error(`Variable ${n.name} not found in environment.`);const[o,i]=e[n.name];if(0===i.length){const r=E(e);return r[n.name]=[t,[t]],{newEnv:r,newExp:n}}if(1===i.length&&i[0]===t)return{newEnv:e,newExp:n};throw new Error(`Fail, variable cannot be updated to different value ${n.name}, previous: ${i[0]}, new: ${t}`);case"freeze":let[s,l]=(0,a.evaluateExpr)(g(e),n.expression);if(l!==t)throw new Error(`Fail, freezed expression cannot be changed, old: ${t}, new: ${l}`);return{newEnv:e,newExp:n};case"field":const c=n.object;if("variable"===c.type&&c.name in e){const[o,i]=e[c.name];if("object"===o.type){const a={type:"object",fields:Object.assign(Object.assign({},o.fields),{[n.field]:t})},s=function(e,t,n){const o=e.fields,i=t,a=(o[i],r(o,["symbol"==typeof i?i:i+""]));return a[t]=[n],{type:"object",fields:a}}(i[0],n.field,t),l=E(e);return l[c.name]=[a,[s]],{newEnv:l,newExp:n}}if("object"==typeof o)return{newEnv:E(e),newExp:n};throw new Error("Field access's value is not an object")}throw new Error("Field access not start with variable.");case"binary":let d=g(e),[y,b]=(0,a.evaluateExpr)(d,n.left);if("number"==typeof t){if("number"!=typeof t||null===t)throw new Error("Value must be a non-null number");switch(n.operator){case"+":let r=p(e,t-b,n.right);return{newEnv:r.newEnv,newExp:Object.assign(Object.assign({},n),{right:r.newExp})};case"-":let o=p(e,b-t,n.right);return{newEnv:o.newEnv,newExp:Object.assign(Object.assign({},n),{right:o.newExp})};case"*":let i=p(e,t/b,n.right);return{newEnv:i.newEnv,newExp:Object.assign(Object.assign({},n),{right:i.newExp})};case"/":let a=p(e,b/t,n.right);return{newEnv:a.newEnv,newExp:Object.assign(Object.assign({},n),{right:a.newExp})}}}case"unary":return{newEnv:e,newExp:n};case"array":return{newEnv:e,newExp:{type:"array",elements:u(n.elements,t)}};case"object":let h={},m=t.fields;for(const e in m)if(m.hasOwnProperty(e)){const t=m[e];h[e]=f(t)}return{newEnv:e,newExp:{type:"object",fields:h}};default:throw new Error(`Unsupported expression type: ${n.type}`)}}function u(e,t){const n=[];for(let r=0;r<t.length;r++)r<e.length?n.push(d(e[r],t[r])):n.push(f(t[r]));return n}function d(e,t){switch(e.type){case"constant":return Object.assign(Object.assign({},e),{value:t});case"array":return Object.assign(Object.assign({},e),{elements:u(e.elements,t)});case"object":return Object.assign(Object.assign({},e),{fields:Object.keys(e.fields).reduce(((n,r)=>(n[r]=Object.assign(Object.assign({},e.fields[r]),{value:t.fields[r]}),n)),{})});default:throw new Error(`Unsupported expression type: ${e.type}`)}}function f(e){if("number"==typeof e||"boolean"==typeof e||"string"==typeof e||null===e)return{type:"constant",value:e};if(Array.isArray(e))return{type:"array",elements:e.map(f)};if("object"==typeof e&&null!==e)return{type:"object",fields:Object.keys(e.fields).reduce(((t,n)=>(t[n]={type:"constant",value:e.fields[n]},t)),{})};throw new Error("Unsupported value type: "+typeof e)}function y(e,t){if("number"==typeof t){const t=parseFloat(e);if(isNaN(t))throw new Error("convert to number fail: "+e);return t}if("boolean"==typeof t){if("true"===e)return!0;if("false"===e)return!1;throw new Error("convert to boolean fail: ${s}")}if("string"==typeof t)return e;throw new Error("Unsupported type: "+typeof t)}function b(e,t){const n=E(e);return delete n[t],n}function h(e,t){const n=e.fields,o=t;return n[o],{type:"object",fields:r(n,["symbol"==typeof o?o:o+""])}}function g(e){const t=new Map;for(const n in e)e.hasOwnProperty(n)&&t.set(n,e[n][0]);return t}function w(e){if(null===e||"number"==typeof e||"boolean"==typeof e||"string"==typeof e)return{type:"constant",value:e};if(Array.isArray(e))return{type:"constant",value:e.map((e=>w(e)))};if("object"===e.type){const t=e,n={};for(const e in t.fields)t.fields.hasOwnProperty(e)&&(n[e]=w(t.fields[e]));return{type:"constant",value:{type:"object",fields:n}}}throw new Error("Unhandled value type: "+typeof e)}function m(e,t,n){if("id"===t.type)return c(E(e),t,n);if("bulk"!==t.type||!t.operations)throw new Error("Invalid bulk operation");if(0==t.operations.length)return[{newEnv:E(e),newTermNode:n,remainingOperation:{type:"id"}}];if("seq"===n.type&&0==n.nodes.length)return[{newEnv:E(e),newTermNode:n,remainingOperation:t}];const[r,...o]=t.operations;if("seq"===n.type){const r=n.nodes[0],o=n.nodes.slice(1),i=m(e,t,r),a=[];for(const e of i){const t=m(e.newEnv,e.remainingOperation,{type:"seq",nodes:o});for(const n of t){let t=[];"seq"!=n.newTermNode.type?t.push(n.newTermNode):t=n.newTermNode.nodes,a.push({newEnv:n.newEnv,newTermNode:{type:"seq",nodes:[e.newTermNode,...t]},remainingOperation:n.remainingOperation})}}return a}if("lambda"===n.type){let r=N(t);return c(E(e),r,n)}return"insert"===r.type||"delete"===r.type||"replace"===r.type?c(E(e),r,n).map((e=>{let t=e.remainingOperation;if("id"===t.type||"insert"===t.type||"delete"===t.type||"replace"===t.type){let t=(0,l.evaluateTermNode)(e.newTermNode).length;const n=o.map((e=>{if("id"===e.type)return e;if("position"in e)return Object.assign(Object.assign({},e),{position:e.position-t});throw new Error("All operations must have positions")}));let r={type:"bulk",operations:[e.remainingOperation,...n]};return[{newEnv:e.newEnv,newTermNode:e.newTermNode,remainingOperation:r}]}throw new Error("nested bulk current not supported")})).reduce(((e,t)=>e.concat(t)),[]):"id"===r.type?0==o.length?c(E(e),r,n):m(e,{type:"bulk",operations:o},n):[]}function v(e){return!!("number"==typeof e||"boolean"==typeof e||"string"==typeof e||null===e||Array.isArray(e)&&e.every(v)||"object"==typeof e&&null!==e&&"object"===e.type&&"object"==typeof e.fields)}function O(e){if(null===e||"object"!=typeof e)return e;if(Array.isArray(e))return e.map(O);if(function(e){return e&&"object"==typeof e&&"object"===e.type&&"fields"in e}(e)){const t={};for(const n in e.fields)e.fields.hasOwnProperty(n)&&(t[n]=O(e.fields[n]));return{type:"object",fields:t}}const t={};for(const n in e)e.hasOwnProperty(n)&&(t[n]=O(e[n]));return t}function E(e){const t={};for(const n in e)if(e.hasOwnProperty(n)){const[r,o]=e[n];t[n]=[O(r),O(o)]}return t}function j(e,t,n){if("number"==typeof n||"boolean"==typeof n||"string"==typeof n||null===n)e[t]=[n,[]];else if(Array.isArray(n))e[t]=[n,[]];else if("object"==typeof n&&null!==n&&"fields"in n){const r={type:"object",fields:{}};for(const e in n.fields)r.fields[e]=[];e[t]=[n,[r]]}return e}function N(e){if("bulk"===e.type){const t=e.operations.map(N);return Object.assign(Object.assign({},e),{operations:t})}return Object.assign({},e)}},238:(e,t)=>{function n(e){if(null===e)return"null";if("number"==typeof e||"boolean"==typeof e||"string"==typeof e)return e.toString();if("object"===e.type){const t=e;let r="{";for(const e in t.fields)t.fields.hasOwnProperty(e)&&(r+=`${e}: ${n(t.fields[e])}, `);return r.length>1&&(r=r.slice(0,-2)),r+="}",r}if(Array.isArray(e)){const t=e;let r="[";for(const e of t)r+=`${n(e)}, `;return r.length>1&&(r=r.slice(0,-2)),r+="]",r}throw new Error("Unhandled value type: "+typeof e)}Object.defineProperty(t,"__esModule",{value:!0}),t.evaluateTermNode=function e(t){switch(t.type){case"const":case"sep":case"loopfront":case"looprear":return t.value;case"space":return" ".repeat(t.width);case"exp":return String(n(t.binding[1]));case"seq":return t.nodes.map((t=>e(t))).join("");case"lambda":return e(t.body);case"branchstart":case"branchend":case"end":case"bot":case"nop":return"";default:throw new Error(`Unhandled term node type: ${t.type}`)}},t.printValue=n},619:(e,t)=>{function n(e){let t=e.type;if("seq"===t)return r(e);if("declare"===t||"assign"===t)throw new Error("declare|assign node shall not be visible here.");if("lambda"===t){let t=e,r=n(t.body);return{type:"lambda",variable:t.variable,body:r,binding:t.binding,marker:t.marker}}return e}function r(e){let t=e.nodes,i=[];for(let e=0;e<t.length;e++){let a=t[e];if("declare"===a.type||"assign"===a.type){let n=a,s=n.name,[l,c]=o(t,e+1,s),{nodes:p}=r({type:"seq",nodes:l}),u=p.pop();if(void 0===u)throw new Error("lastNode should be declareend or others");if("declareend"!=u.type){p.push(u);let e={type:"lambda",variable:n.name,body:{type:"seq",nodes:p},binding:n.value,marker:{type:a.type}};i.push(e)}else{let e={type:"lambda",variable:n.name,body:{type:"seq",nodes:p},binding:n.value,marker:{type:a.type}};i.push(e),"declare"!=a.type&&i.push(u)}e=c}else i.push(n(a))}return{type:"seq",nodes:i}}function o(e,t,n){let r=[],o=t;for(;o<e.length;o++){if("declareend"===e[o].type&&e[o].name.name===n.name)return r.push(e[o]),[r,o];r.push(e[o])}return[r,o]}Object.defineProperty(t,"__esModule",{value:!0}),t.lambdalize=n},784:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.unLambdalize=function e(t){switch(t.type){case"lambda":{const{variable:n,body:r,binding:o,marker:i}=t;let a;switch(i.type){case"declare":return a=e(r),{type:"seq",nodes:[{type:"declare",name:n,value:o},a]};case"assign":return a=e(r),{type:"seq",nodes:[{type:"assign",name:n,value:o},a]};default:return{type:"lambda",variable:n,body:e(r),binding:o,marker:i}}}case"seq":return{type:"seq",nodes:t.nodes.map(e)};default:return t}}},893:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.printNode=function e(t,n=""){switch(console.log(n+t.type),t.type){case"const":case"sep":console.log(n+" Value: "+t.value);break;case"space":console.log(n+" Width: "+t.width);break;case"declare":case"assign":console.log(n+" Name: "+t.name.name),o(t.value,n+" ");break;case"exp":o(t.binding,n+" ");break;case"seq":t.nodes.forEach((t=>e(t,n+" ")));break;case"branchstart":o(t.condition,n+" ");break;case"loopfront":console.log(n+" front: "+t.value);break;case"looprear":console.log(n+" rear: "+t.value);break;case"lambda":console.log(n+" Variable: "+t.variable.name),e(t.body,n+" "),o(t.binding,n+" "),a(t.marker,n+" ");break;case"callstart":console.log(n+" Name: "+t.name);break;case"end":console.log(`${n} End`)}},t.printBinding=o,t.printValue=i,t.printMarker=a;const r=n(235);function o(e,t){(0,r.printExpression)(e[0],t),i(e[1],t)}function i(e,t){if(null===e)console.log(`${t}null`);else if("number"==typeof e||"boolean"==typeof e||"string"==typeof e)console.log(`${t}${e}`);else{if("object"!=typeof e)throw new Error("Unhandled value type: "+typeof e);{console.log(`${t}{`);const n=e;for(const e in n)if(n.hasOwnProperty(e)){const r=t+" ";console.log(`${r}${e}:`),i(n[e],r+" ")}console.log(`${t}}`)}}}function a(e,t=""){console.log(t+e.type)}},446:(e,t)=>{function n(e,t){switch(t.type){case"constant":return[e,t.value];case"variable":const r=e.get(t.name);if(void 0===r)throw new Error(`Variable '${t.name}' not found in environment`);return[e,r];case"binary":let[o,i]=n(e,t.left),[a,s]=n(o,t.right);switch(t.operator){case"+":return[a,i+s];case"-":return[a,i-s];case"*":return[a,i*s];case"/":return[a,i/s];case"&&":return[a,i&&s];case"||":return[a,i||s];case">":return[a,i>s];case"<":return[a,i<s];case">=":return[a,i>=s];case"<=":return[a,i<=s];case"==":return[a,i==s];case"!=":return[a,i!=s];default:throw new Error("Unsupported binary operator")}case"unary":let[l,c]=n(e,t.operand);if("not"===t.operator)return[l,!c];throw new Error("Unsupported unary operator");case"field":let p,[u,d]=n(e,t.object);if("string"!=typeof d&&("object"!=typeof d||null===d))throw new Error(`Cannot access field '${t.field}' from non-object`);if("string"==typeof d?p=d[t.field]:Array.isArray(d)?"length"===t.field&&(p=d.length):p=d.fields[t.field],void 0===p)throw new Error(`Field '${t.field}' not found in object`);return[u,p];case"array":let f=[],y=e;for(const e of t.elements){let[t,r]=n(y,e);f.push(r),y=t}return[y,f];case"object":let b={type:"object",fields:{}},h=e;for(const e in t.fields)if(t.fields.hasOwnProperty(e)){let[r,o]=n(h,t.fields[e]);b.fields[e]=o,h=r}return[h,b];case"freeze":return n(e,t.expression);default:throw new Error("Unhandled expression type")}}Object.defineProperty(t,"__esModule",{value:!0}),t.partialEval=function e(t,r){switch(r.type){case"const":return[t,{type:"const",value:r.value}];case"space":return[t,{type:"space",width:r.width}];case"declare":const[o,i]=n(t,r.value);return o.set(r.name.name,i),[o,{type:"declare",name:r.name,value:[r.value,i]}];case"declareend":return[t,r];case"assign":const[a,s]=n(t,r.value);return a.set(r.name.name,s),[a,{type:"assign",name:r.name,value:[r.value,s]}];case"exp":const[l,c]=n(t,r.expression);return[l,{type:"exp",binding:[r.expression,c]}];case"seq":let p=t;const u=[];for(const t of r.nodes){const[n,r]=e(p,t);u.push(r),p=n}return[p,{type:"seq",nodes:u}];case"ite":const[d,f]=n(t,r.condition);if(!0===f){const[t,n]=e(d,r.trueBranch);return[t,{type:"seq",nodes:[{type:"branchstart",condition:[r.condition,f],trueBranch:r.trueBranch,falseBranch:r.falseBranch},n,{type:"branchend",condition:[r.condition,f]}]}]}if(!1===f){const[t,n]=e(d,r.falseBranch);return[t,{type:"seq",nodes:[{type:"branchstart",condition:[r.condition,f],trueBranch:r.trueBranch,falseBranch:r.falseBranch},n,{type:"branchend",condition:[r.condition,f]}]}]}throw new Error(`Condition does not evaluate to boolean: ${f}`);case"loop":const{lst:y,separator:b,front:h,rear:g,body:w}=r,[m,v]=n(t,y);if(Array.isArray(v)&&0===v.length)return[m,{type:"seq",nodes:[{type:"loopfront",lst:y,value:h.value,body:w,separator:b},{type:"looprear",lst:y,value:g.value}]}];if(Array.isArray(v)&&1===v.length){const n=v[0],r=w.variable.name,o=new Map(t);o.set(r,n);const[i,a]=e(o,w.body);return[i,{type:"seq",nodes:[{type:"loopfront",lst:y,value:h.value,body:w,separator:b},{type:"lambda",variable:w.variable,binding:[{type:"constant",value:n},n],marker:{type:"loopitem",lst:y},body:a},{type:"looprear",lst:y,value:g.value}]}]}if(Array.isArray(v)&&v.length>1){let n=new Map(t),o=[];for(let t=0;t<v.length;t++){const r=v[t],i=w.variable.name;n.set(i,r);const[a,s]=e(n,w.body);n=a,o.push({type:"lambda",variable:w.variable,body:s,binding:[{type:"constant",value:r},r],marker:{type:"loopitem",lst:y}})}let i=o[o.length-1];for(let e=o.length-2;e>=0;e--){let t=o[e];i={type:"lambda",variable:t.variable,binding:t.binding,marker:t.marker,body:{type:"seq",nodes:[t.body,r.separator,i]}}}return[n,{type:"seq",nodes:[{type:"loopfront",lst:y,value:h.value,body:w,separator:b},i,{type:"looprear",lst:y,value:g.value}]}]}throw new Error("Unsupported type or value for e_arr: "+typeof v);case"nop":return[t,{type:"nop"}];case"bot":return[t,{type:"bot"}];case"end":return[t,{type:"end"}];default:throw new Error(`Unhandled term node type: ${r.type}`)}},t.evaluateExpr=n,t.flatten=function e(t){return"seq"===t.type?{type:"seq",nodes:t.nodes.map((t=>e(t))).flatMap((e=>"seq"===e.type?e.nodes:[e]))}:"lambda"===t.type?Object.assign(Object.assign({},t),{body:e(t.body)}):t}},887:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.unPartialEval=l,t.flatten=function e(t){if("seq"===t.type){const n=t.nodes.map((t=>e(t)));return 1==n.length?n[0]:{type:"seq",nodes:n.flatMap((e=>"seq"===e.type?e.nodes:[e]))}}return"ite"===t.type?Object.assign(Object.assign({},t),{trueBranch:e(t.trueBranch),falseBranch:e(t.falseBranch)}):"loop"===t.type?Object.assign(Object.assign({},t),{body:Object.assign(Object.assign({},t.body),{body:e(t.body.body)})}):t};const a=i(n(454));function s(e){return e[0]}function l(e){switch(e.type){case"const":return a.constNode(e.value);case"space":return a.space(e.width);case"declare":return a.declare(e.name,s(e.value));case"assign":return a.assign(e.name,s(e.value));case"exp":return a.exp(s(e.binding));case"nop":return a.nop();case"end":return a.end();case"bot":return a.bot();case"seq":return c(e);case"lambda":return p(e);case"sep":return e;default:throw new Error(`Unknown node type: ${e.type}`)}}function c(e){const t=e.nodes,n=[];for(let e=0;e<t.length;e++){const o=t[e];if("branchstart"===o.type){const r=o.condition[0],i=o.condition[1],s=o.trueBranch,l=o.falseBranch;let p=1;const u=[];for(let n=e+1;n<t.length;n++){const e=t[n];if("branchstart"===e.type&&p++,"branchend"===e.type){if(1==p)break;p--,u.push(t[n])}else u.push(t[n])}i?n.push(a.ite(r,c({type:"seq",nodes:u}),l)):n.push(a.ite(r,s,c({type:"seq",nodes:u}))),e+=u.length+1}else if("loopfront"===o.type){let i=o,s=null,l=null,c=null,p=t[++e];if("looprear"===p.type)s=p;else{let n=p;if([l,c]=u(n),p=t[++e],"looprear"!==p.type)throw new Error("Shall end with looprear node");s=p}if(""===i.value&&""===s.value&&null===l&&null===c)n.push(a.bot());else if(null===l||d(l))n.push(a.loop(i.lst,i.separator,{type:"front",value:o.value},{type:"rear",value:s.value},i.body));else{if("bot"===l.type)throw new Error("lambda is bot node");if("lambdawithexpr"!==l.type)throw new Error("Must be lambda in loopitem");n.push(a.loop(i.lst,null===c?i.separator:c,{type:"front",value:o.value},{type:"rear",value:s.value},{type:"lambda",variable:(r=l).variable,body:r.body}))}}else n.push(l(o))}var r;return a.seq(...n)}function p(e){let{variable:t,body:n,binding:r,marker:o}=e;return{type:"lambdawithexpr",variable:t,body:l(n),exp:(i=r[1],Array.isArray(i)?{type:"array",elements:i.map(f)}:{type:"array",elements:[f(i)]})};var i}function u(e){let t=e,n=e.body,r=null,o=[],i=null;for(let e of n.nodes)"sep"===e.type?i=e:"lambda"===e.type?r=e:o.push(e);if(t.body=o.every(d)?{type:"bot"}:{type:"seq",nodes:o},null==r)return"bot"===t.body.type?[{type:"bot"},null]:[p(t),null];{let[e,n]=u(r);if("bot"===e.type&&null===n)return[p(t),i];if("bot"===t.body.type&&null===i)return[e,n];if("bot"===t.body.type&&""===(null==i?void 0:i.value))return[e,n];{let r=p(t);if(s=e,y((a=r).variable,s.variable)&&h(a.body,s.body)){if(null===n)return[r,i];if(null!=i&&n.value===i.value)return[r,i];throw new Error("sepNode not equal")}throw new Error("lambda body not equal")}}var a,s}function d(e){return"bot"===e.type}function f(e){if("number"==typeof e||"boolean"==typeof e||"string"==typeof e||null===e)return{type:"constant",value:e};if(Array.isArray(e))return{type:"array",elements:e.map(f)};if("object"===e.type){const t={};for(const n in e.fields)if(e.fields.hasOwnProperty(n)){const r=e.fields[n];t[n]={type:"constant",value:r}}return{type:"object",fields:t}}throw new Error("Unsupported Value type")}function y(e,t){return e.name===t.name}function b(e,t){if(e.type!==t.type)return!1;switch(e.type){case"constant":const n=e,r=t;return null===n.value&&null===r.value||("object"==typeof n.value&&"object"==typeof r.value?b(n.value,r.value):n.value===r.value);case"variable":return e.name===t.name;case"binary":const o=e,i=t;return o.operator===i.operator&&b(o.left,i.left)&&b(o.right,i.right);case"unary":const a=e,s=t;return a.operator===s.operator&&b(a.operand,s.operand);case"field":const l=e,c=t;return l.field===c.field&&b(l.object,c.object);case"array":const p=e,u=t;return p.elements.length===u.elements.length&&p.elements.every(((e,t)=>b(e,u.elements[t])));case"object":const d=e,f=t,y=Object.keys(d.fields),h=Object.keys(f.fields);return y.length===h.length&&y.every((e=>f.fields.hasOwnProperty(e)&&b(d.fields[e],f.fields[e])));case"freeze":return b(e.expression,t.expression);default:return!1}}function h(e,t){if(e.type!==t.type)return!1;switch(e.type){case"const":return e.value===t.value;case"space":return e.width===t.width;case"declare":const n=e,r=t;return y(n.name,r.name)&&b(n.value,r.value);case"assign":const o=e,i=t;return y(o.name,i.name)&&b(o.value,i.value);case"exp":return b(e.expression,t.expression);case"seq":const a=e.nodes.filter((e=>!("const"===e.type&&""===e.value))),s=t.nodes.filter((e=>!("const"===e.type&&""===e.value)));return a.length===s.length&&a.every(((e,t)=>h(e,s[t])));case"ite":const l=e,c=t;return b(l.condition,c.condition)&&h(l.trueBranch,c.trueBranch)&&h(l.falseBranch,c.falseBranch);case"loop":const p=e,u=t;return b(p.lst,u.lst)&&p.separator.value===u.separator.value&&p.front.value===u.front.value&&p.rear.value===u.rear.value&&function(e,t){return y(e.variable,t.variable)&&h(e.body,t.body)}(p.body,u.body);case"nop":case"bot":case"end":return!0;case"call":const d=e,f=t;return h(d.func,f.func)&&Object.keys(d.args).length===Object.keys(f.args).length&&Object.keys(d.args).every((e=>b(d.args[e],f.args[e])));case"lambdawithexpr":const g=e,w=t;return y(g.variable,w.variable)&&h(g.body,w.body);default:return!1}}},661:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.scopelize=function e(t){switch(t.type){case"declare":return{type:"seq",nodes:[t,{type:"declareend",name:t.name}]};case"seq":return function(t){let n=new a.Stack,r=[];for(let o of t.nodes){if("declare"===o.type){let e={type:"declareend",name:o.name};n.push(e)}"declare"===o.type?r.push(o):r.push(e(o))}if(0==r.length)return t;{let e=r.pop();for("end"!==e.type&&r.push(e);!n.isEmpty();){let e=n.pop();if(!e)throw new Error("endNode is undefined.");r.push(e)}"end"===(null==e?void 0:e.type)&&r.push(e)}return Object.assign(Object.assign({},t),{nodes:r})}(t);case"ite":let n=t.trueBranch,r=t.falseBranch;return{type:"ite",condition:t.condition,trueBranch:e(n),falseBranch:e(r)};case"loop":let o=t.body,i=o.body;return Object.assign(Object.assign({},t),{body:Object.assign(Object.assign({},o),{body:e(i)})});case"call":case"const":case"space":case"declareend":case"assign":case"exp":case"nop":case"bot":case"end":case"lambdawithexpr":case"sep":return t}return{type:"bot"}};const a=i(n(694))},577:(e,t)=>{function n(e){const t=/«[^»]*»/g;let n;const r=[];let o=0;for(;null!==(n=t.exec(e));)o<n.index&&r.push(e.slice(o,n.index)),r.push(n[0]),o=t.lastIndex;return o<e.length&&r.push(e.slice(o)),r}function r(e){var t,n,i;const a=[];let s=0;for(;s<e.length;){const l=e[s];if("string"==typeof l&&l.startsWith("«")&&l.endsWith("»")){const c=l.slice(1,-1).trim(),[p]=c.split(" ");switch(p.toUpperCase()){case"VAR":const l=c.split("=");a.push({type:"Directive",content:{type:"declare",name:l[0].split(" ")[1].trim(),expr:o(l[1].trim())}});break;case"IF":const p=c.match(/^(IF)\s+/i);if(p){const t=c.slice(p[0].length).trim(),n=[],i=[];let l=1,u=s+1;for(;u<e.length;){let t=e[u];if("string"==typeof t&&t.startsWith("«"))if(t.includes("ENDIF")||t.includes("endif")){if(l--,l<=0)break}else if((t.includes("else")||t.includes("ELSE"))&&1===l){if(l=0,!t.includes("if")&&!t.includes("IF")){u++;continue}t=t.replace(/else(?=if)/,"")}l>0?n.push(t):i.push(t),u++}const d=r(n),f=r(i);s=u,a.push({type:"Directive",content:{type:"if",expr:o(t),thenBranch:d,elseBranch:f}})}break;case"FOR":const u=/(\bfor\b|\bin\b|\bseparator\b|\bfront\b|\brear\b|[\[\],])|((?:\w+(?:\.\w*)?)+)|"([^"]*)"|\[([^\]]*)\]/gi,d=[];let f;for(;null!==(f=u.exec(c))&&"separator"!==f[0]&&"front"!==f[0]&&"rear"!==f[0];)f[1]&&d.push(f[1]),f[2]&&d.push(f[2]),f[3]&&d.push(f[3]),f[4]&&d.push(f[4]);const y=c.match(/separator\s*"([^"]*)"/i);y&&y[1]&&d.push("separator",y[1]);const b=c.match(/front\s*"([^"]*)"/i);b&&b[1]&&d.push("front",b[1]);const h=c.match(/rear\s*"([^"]*)"/i);if(h&&h[1]&&d.push("rear",h[1]),d){const l=[];let c=1,p=s+1;for(;p<e.length&&c>0;){const t=e[p];if("string"==typeof t&&t.startsWith("«"))if(t.includes("ENDFOR")||t.includes("endfor")){if(c--,0===c)break}else(t.includes("FOR")||t.includes("for"))&&c++;c>0&&l.push(t),p++}const u=r(l);s=p,a.push({type:"Directive",content:{type:"for",name:d[1].trim(),expr:o(d[3].trim()),separator:{type:"separator",value:(null===(t=d[5])||void 0===t?void 0:t.trim())||""},front:{type:"front",value:(null===(n=d[7])||void 0===n?void 0:n.trim())||""},rear:{type:"rear",value:(null===(i=d[9])||void 0===i?void 0:i.trim())||""},fragment:u}})}break;default:if(c.includes("=")){const e=c.split("=");a.push({type:"Directive",content:{type:"assign",name:e[0].split(" ")[0].trim(),expr:o(e[1].trim())}})}else a.push({type:"Directive",content:{type:"exp",expr:o(c)}})}}else a.push(l);s++}return 1===a.length?a[0]:{type:"fragmentList",fragments:a}}function o(e){const t=function(e){const t=/"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|(&&|\|\||[<>!=]=|[{}[\](),.:;!+\-*/<>])|(\s+)|([^\s{}[\](),.:;!+\-*/<>!=&|]+)/g,n=[];let r;for(;null!==(r=t.exec(e));)void 0!==r[1]?n.push(`"${r[1]}"`):void 0!==r[2]?n.push(`'${r[2]}'`):void 0!==r[3]?n.push(r[3]):void 0!==r[5]&&n.push(r[5]);return n}(e);let n=0;function r(){if("("===t[n]){n++;const e=o();if(")"===t[n])return n++,e;throw new Error("Mismatched parentheses")}if("["===t[n]){n++;const e=[];for(;"]"!==t[n];)","===t[n]?n++:e.push(o());return n++,{type:"array",elements:e}}if("{"===t[n]){n++;const e={};for(;"}"!==t[n];)if(","===t[n])n++;else{const r=t[n++];if(":"!==t[n])throw new Error("Expected colon in object literal");{n++;const t=o();e[r]=t}}return n++,{type:"object",fields:e}}if("-"===t[n]&&!isNaN(Number(t[n+1])))return n++,{type:"constant",value:-Number(t[n++])};if(!isNaN(Number(t[n])))return{type:"constant",value:Number(t[n++])};if(/^['"].*['"]$/.test(t[n]))return{type:"constant",value:t[n++].slice(1,-1)};switch(t[n]){case"null":return n++,{type:"constant",value:null};case"true":return n++,{type:"constant",value:!0};case"false":return n++,{type:"constant",value:!1}}if(/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(t[n])){let e={type:"variable",name:t[n++]};for(;"."===t[n];){if(n++,/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(t[n]))return{type:"field",object:e,field:t[n++]};throw new Error("Expected property name after '.'")}return e}if("!"===t[n])return n++,{type:"freeze",expression:r()};throw new Error(`Unexpected token: ${t[n]}`)}function o(){let e=r();for(;["+","-","*","/","&&","||",">","<",">=","<=","!=","==","=="].includes(t[n]);)e={type:"binary",operator:t[n++],left:e,right:r()};return e}return o()}Object.defineProperty(t,"__esModule",{value:!0}),t.parse=function(e){return r(n(e))},t.tokenize=n,t.parseTokens=r},775:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.translate=function(e){const t=s(e);return a.seq(...t)};const a=i(n(454));function s(e){if("string"==typeof e)return function(e){const t=[];let n="";for(let r of e)if(" "===r){if(n.length>0){if(n.endsWith("\n")){const e=n.split(/(\n)/);for(const n of e)""!=n&&t.push(a.constNode(n))}else t.push(a.constNode(n));n=""}t.push(a.space(1))}else n+=r;if(n.length>0)if(n.endsWith("\n")){const e=n.split(/(\n)/);for(const n of e)""!=n&&t.push(a.constNode(n))}else t.push(a.constNode(n));return t}(e);switch(e.type){case"Directive":switch(e.content.type){case"declare":return[a.declare({type:"variable",name:e.content.name},e.content.expr)];case"assign":return[a.assign({type:"variable",name:e.content.name},e.content.expr)];case"exp":return[a.exp(e.content.expr)];case"if":return[a.ite(e.content.expr,{type:"seq",nodes:s(e.content.thenBranch)},l(e.content.elseBranch))];case"for":return[a.loop(e.content.expr,{type:"sep",value:e.content.separator.value},{type:"front",value:e.content.front.value},{type:"rear",value:e.content.rear.value},{type:"lambda",variable:{type:"variable",name:e.content.name},body:{type:"seq",nodes:s(e.content.fragment)}})];default:throw new Error(`Unknown directive: ${e}`)}case"fragmentList":return e.fragments.flatMap(s);default:throw new Error(`Unknown fragment: ${e}`)}}function l(e){if((t=e)&&"if"===t.type){const t=e;return a.ite(t.expr,{type:"seq",nodes:s(t.thenBranch)},l(t.elseBranch))}return function(e){return e&&"bot"===e.type}(e)?a.bot():{type:"seq",nodes:s(e)};var t}},694:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Stack=void 0,t.Stack=class{constructor(){this.items=[]}push(e){this.items.push(e)}pop(){return this.items.pop()}peek(){return this.items[this.items.length-1]}isEmpty(){return 0===this.items.length}size(){return this.items.length}clear(){this.items=[]}print(){console.log(this.items.toString())}}},63:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.isWhitespace=function(e){return/^( +)$/.test(e)},t.containsNewlineOrSpace=function(e){return/[\n\s]/.test(e)}}},t={};return function n(r){var o=t[r];if(void 0!==o)return o.exports;var i=t[r]={exports:{}};return e[r].call(i.exports,i,i.exports,n),i.exports}(612)})()));
|
@@ -4,7 +4,7 @@ export type Literal = string;
|
|
4
4
|
export type Fragment = Literal | Directive | FragmentList;
|
5
5
|
export interface Directive {
|
6
6
|
type: 'Directive';
|
7
|
-
content: DeclareDirective | AssignDirective | ExprDirective | IfDirective | ForDirective;
|
7
|
+
content: DeclareDirective | AssignDirective | ExprDirective | IfDirective | ForDirective | TemplateDefDirective | TemplateCallDirective;
|
8
8
|
}
|
9
9
|
export interface DeclareDirective {
|
10
10
|
type: 'declare';
|
@@ -55,3 +55,14 @@ export interface FragmentList {
|
|
55
55
|
type: 'fragmentList';
|
56
56
|
fragments: Fragment[];
|
57
57
|
}
|
58
|
+
export interface TemplateDefDirective {
|
59
|
+
type: 'template';
|
60
|
+
name: string;
|
61
|
+
parameters: string[];
|
62
|
+
fragment: Fragment;
|
63
|
+
}
|
64
|
+
export interface TemplateCallDirective {
|
65
|
+
type: 'templateCall';
|
66
|
+
name: string;
|
67
|
+
arguments: Expr[];
|
68
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "bit2",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.2",
|
4
4
|
"main": "dist/bit2.js",
|
5
5
|
"types": "dist/bit2.d.ts",
|
6
6
|
"dependencies": {
|
@@ -53,7 +53,9 @@
|
|
53
53
|
"example.small.9": "node dist/test/examples/small.9.test.js",
|
54
54
|
"example.small.10": "node dist/test/examples/small.10.test.js",
|
55
55
|
"example.small.13": "node dist/test/examples/small.13.test.js",
|
56
|
-
"example.small.17": "node dist/test/examples/small.17.test.js"
|
56
|
+
"example.small.17": "node dist/test/examples/small.17.test.js",
|
57
|
+
"example.test": "node dist/test/examples/Test.test.js",
|
58
|
+
"example.simple": "node dist/test/examples/Test.simple.test.js"
|
57
59
|
},
|
58
60
|
"files": [
|
59
61
|
"dist/bit2.js",
|
@@ -61,5 +63,10 @@
|
|
61
63
|
"dist/src/**/*.d.ts"
|
62
64
|
],
|
63
65
|
"author": "steanzan",
|
64
|
-
"keywords": [
|
66
|
+
"keywords": [
|
67
|
+
"bx",
|
68
|
+
"live programming",
|
69
|
+
"bidirectional live programming",
|
70
|
+
"template language"
|
71
|
+
]
|
65
72
|
}
|