@drincs/pixi-vn-ink
Version:
Pixi'VN gives you the ability to write your own narrative using Ink
2 lines • 3.76 kB
JavaScript
var p;(t=>(t.log=(i,...l)=>console.log(`[Pixi\u2019VN Ink] ${i}`,...l),t.warn=(i,...l)=>console.warn(`[Pixi\u2019VN Ink] ${i}`,...l),t.error=(i,...l)=>console.error(`[Pixi\u2019VN Ink] ${i}`,...l),t.info=(i,...l)=>console.info(`[Pixi\u2019VN Ink] ${i}`,...l)))(p||={});var s="_|_";var A=["+","-","/","*","%","POW","RANDOM"],V=["INT","FLOOR","FLOAT"],x=["==",">","<",">=","<=","!=","CONTAINS"];function g(n,o){if(n.endsWith(".s"))return;if(new RegExp(/.*\.[0-9]+\..*$/).test(n)&&(n=n.split(".").filter(t=>!t.match(/^[0-9]+$/)).join("."),!n.startsWith(".")))return n.replaceAll(".",s);if(!new RegExp(/^\.\^.*$/).test(n)&&n.includes("g-")){let e=n.split("g-");return u(o)+s+"g-"+e[e.length-1]}let r=0;for(;new RegExp(/^\.\^.*$/).test(n);)r++,n=n.substring(2);if(r=r-1,new RegExp(/^\..*$/).test(n)&&o){let e=n.substring(1);return u(o,r)+s+e.replaceAll(".",s)}return n.replaceAll(".",s)||u(o,r).replaceAll(".",s)}function u(n,o=0){let r=n.split(s);for(;r.length>1&&o>0;)r.pop()?.includes("g-")&&o--,o--;return r.join(s)}var d="\xA7DOUBLE_SLASH_CONVERTOR\xA7";function m(n){return n.startsWith("^")&&(n=n.substring(1)),n=n.replaceAll("\\\\",d),n=n.replaceAll("\\n",`
`),n=n.replaceAll("\\t"," "),n=n.replaceAll(" "," "),n=n.replaceAll(d,"\\\\"),n}function O(n,o,r="storage"){let e=o.indexOf(n);return e>=0?{type:"value",storageType:"params",storageOperationType:"get",key:e}:{type:"value",storageOperationType:"get",storageType:r,key:n}}function f(n,o,r){n=n.map(t=>{if(typeof t=="string"){if(t==="rnd")return "RANDOM";if(t==="?")return "CONTAINS"}return t});let e=[];return n.forEach(t=>{if(typeof t=="object"&&"CNT?"in t)if(new RegExp(/.*\.[0-9]\..*/).test(t["CNT?"])){let i=t["CNT?"].split("."),l=i.pop(),h=i.pop();if(h===void 0||l===void 0){p.error("Error parsing ink file: Conditional statement is not valid",n);return}let N=parseInt(h),a=i.join(".");if(a.includes("^.")){let c=a.split("."),T=c[c.length-1].replaceAll(".",s);c.pop(),a=c.join(".")+"."+T,l.includes("c-")&&(a=a+s+l);}else a=a.replaceAll(".",s);let y=g(a,o);if(!y)return;e.push({type:"compare",leftValue:P(y),operator:">=",rightValue:{type:"value",value:N}});}else {let i=g(t["CNT?"],o);if(!i)return;e.push(P(i));}else if(typeof t=="object"&&"VAR?"in t)e.push(O(t["VAR?"],r));else if(t==="&&"||t==="||"){if(e.length>=2){let i={type:"union",unionType:t==="&&"?"and":"or",conditions:[e[e.length-2],e[e.length-1]]};e.pop(),e.pop(),e.push(i);}}else if(t==="!")if(e.length===0)p.error("Error parsing ink file: Conditional statement is not valid",n);else {let i={type:"union",unionType:"not",condition:e[e.length-1]};e[e.length-1]=i;}else if(t&&typeof t=="string"&&x.includes(t))if(e.length<2)p.error("Error parsing ink file: Conditional statement is not valid",n);else {let i={type:"compare",operator:t,rightValue:e[e.length-1],leftValue:e[e.length-2]};e.pop(),e.pop(),e.push(i);}else if(t&&typeof t=="string"&&A.includes(t))if(e.length<2)p.error("Error parsing ink file: Conditional statement is not valid",n);else {let i={type:"arithmetic",operator:t,rightValue:e[e.length-1],leftValue:e[e.length-2]};e.pop(),e.pop(),e.push(i);}else if(t&&typeof t=="string"&&V.includes(t)){let i={type:"arithmeticsingle",operator:t,leftValue:e[e.length-1]};e.pop(),e.push(i);}else t&&typeof t=="string"?t.startsWith("^")&&e.push(m(t)):typeof t=="object"&&"^->"in t?t["^->"].includes("$r")||e.push(t["^->"]):e.push(t);}),e}function P(n){return {type:"value",storageType:"label",storageOperationType:"get",label:n}}function H(n,o,r){if(n.length===0){p.error("Error parsing ink file: Arithmetic statement is not valid",n);return}let e=f(n,o,r);if(e.length===1){let t=e[0];return t&&typeof t=="object"&&"type"in t?{type:"value",storageType:"logic",storageOperationType:"get",operation:t}:t}}export{H as arithmeticParser};