-(()=>{"use strict";class e{constructor(t){let r=e.get_root(t.operators);if(this.root=r[0],this.left=void 0,this.right=void 0,-1==r[1])console.error("Something went wrong during sytax tree creation");else if("Binary"==r[0][0]){let n=t.formula.substring(0,r[1]),i=t.formula.substring(r[1]+1);"("==n.charAt(0)&&(n=n.substring(1,r[1]-1)),"("==i.charAt(0)&&(i=i.substring(1,i.length-1)),this.left=new e(new s(n)),this.right=new e(new s(i))}else if("Unary"==r[0][0]){let r=t.formula.substring(1);"("==r.charAt(0)&&(r=r.substring(1,r.length-1)),this.left=void 0,this.right=new e(new s(r))}}toString(e){let t="";return null==e&&(e=this),null!=e.left&&null!=e.right?t+=`(${e.root[1]}, ${e.left}, ${e.right})`:null!=e.right?t+=`(${e.root[1]}, ${e.right})`:t=e.root[1],t}static get_root(e){if(1==e.length)return[e[0],0];switch(e[0][0]){case"Atom":return[e[1],1];case"Unary":return[e[0],0];case"NestOpen":{let t=0;for(let s=0;s<e.length;s++)if("NestOpen"==e[s][0])t++;else if("NestClose"==e[s][0]&&0==--t)return[e[s+1],s+1]}}return[["Atom",""],-1]}}class t{constructor(e){this.name=e,this.value=!1}setState(e){this.value="0"!=e}}class s{constructor(r){let n=s.is_fbf(r),[i,a,l]=n;this.variables=[];for(let e of l){let s=!1;for(let t of this.variables)t.name==e&&(s=!0);s||this.variables.push(new t(e))}this.formula=r,this.fbf=i,this.operators=a,this.models=[],this.countermodels=[],this.tree=i?new e(this):void 0}evaluate(){let e=[],t="0".repeat(2**this.variables.length);for(let s=0;s<2**this.variables.length;s++){t=s.toString(2).padStart(this.variables.length,"0");for(let e=0;e<t.length;e++)this.variables[e].setState(t[e]);e.push([t,this.evaluateAssignment()])}return e}evaluateAssignment(e){let t=!1;if(null==e&&(e=this.tree),"Atom"==e.root[0])this.variables.forEach((s=>{s.name==e.root[1]&&(t=s.value)}));else if("Unary"==e.root[0])t=!this.evaluateAssignment(e.right);else switch(e.root[1]){case"&":t=this.evaluateAssignment(e.left)&&this.evaluateAssignment(e.right);break;case"|":t=this.evaluateAssignment(e.left)||this.evaluateAssignment(e.right);break;case">":t=!this.evaluateAssignment(e.left)||this.evaluateAssignment(e.right);break;case"-":t=this.evaluateAssignment(e.left)==this.evaluateAssignment(e.right)}return t}static is_fbf(e){let t=[],s=[],r=!1,n=!1;for(let i of e)switch(i){case"!":t.push(["Unary","!"]),r=!0;break;case"&":case"|":case">":case"-":if(r||n)return[!1,[],[]];t.push(["Binary",i]);break;case"(":case"[":case"{":t.push(["NestOpen"]),n=!0;break;case")":case"]":case"}":if(t.push(["NestClose"]),n||r)return[!1,[],[]];break;default:t.push(["Atom",i]),s.push(i),r=!1,n=!1}if(r||n)return[!1,[],[]];let i=Object.assign([],s),a=Object.assign([],t);for(;0!=a.length;)switch(a.pop()[0]){case"Unary":if(void 0===i.pop())return console.error("Unary operation requires one and only one operator"),[!1,[],[]];i.push("_");break;case"Binary":if(void 0===i.pop())return console.error("Binary operation requires exactly 2 operators"),[!1,[],[]];if(void 0===i.pop())return console.error("Binary operation requires exactly 2 operators"),[!1,[],[]];i.push("_");break;case"NestOpen":return console.error("Invalid parentesis"),[!1,[],[]];case"NestClose":{let e=a.reverse().findIndex((e=>"NestOpen"===e[0]));if(-1==e)return console.error("Missing closing parentesis"),[!1,[],[]];a.splice(e,1),a.reverse()}}return 1!=i.length?(console.error("Invalid number of variables"),[!1,[],[]]):[!0,t,s]}}window.checkFormula=function(){let t=document.getElementById("formulaInput").value,r=new s(t.trim());if(r.fbf){document.getElementById("invalidFormula").setAttribute("style","display: none");let t=new e(r),s=document.getElementById("treeString");s.setAttribute("style","display: block"),s.innerHTML=`Syntax tree: </br><b style="padding-left: 20px">${t}</b>`;let n=document.getElementById("formulaTable");n.setAttribute("style","display: block"),n.innerHTML="<h2 class='font-medium leading-tight text-3xl mt-0 mb-2'>Truth table:</h2>"+function(e){let t="<table class='table table-compact'><thead><tr><th>Assignment</th><th>Value</th></tr></thead><tbody>";for(const[s,r]of e)t+=`\n <tr>\n <td>${s}</td>\n <td>${r}</td>\n </tr>\n `;return t+="</table>",t}(r.evaluate())}else document.getElementById("invalidFormula").setAttribute("style","display: block")}})();
\ No newline at end of file
+(()=>{"use strict";class e{constructor(t){let r=e.get_root(t.operators);if(this.root=r[0],this.left=void 0,this.right=void 0,-1==r[1])console.error("Something went wrong during sytax tree creation");else if("Binary"==r[0][0]){let a=t.formula.substring(0,r[1]),i=t.formula.substring(r[1]+1);"("==a.charAt(0)&&(a=a.substring(1,r[1]-1)),"("==i.charAt(0)&&(i=i.substring(1,i.length-1)),this.left=new e(new s(a)),this.right=new e(new s(i))}else if("Unary"==r[0][0]){let r=t.formula.substring(1);"("==r.charAt(0)&&(r=r.substring(1,r.length-1)),this.left=void 0,this.right=new e(new s(r))}}toString(e){let t="";return null==e&&(e=this),null!=e.left&&null!=e.right?t+=`(${e.root[1]}, ${e.left}, ${e.right})`:null!=e.right?t+=`(${e.root[1]}, ${e.right})`:t=e.root[1],t}static get_root(e){if(1==e.length)return[e[0],0];switch(e[0][0]){case"Atom":return[e[1],1];case"Unary":return[e[0],0];case"NestOpen":{let t=0;for(let s=0;s<e.length;s++)if("NestOpen"==e[s][0])t++;else if("NestClose"==e[s][0]&&0==--t)return[e[s+1],s+1]}}return[["Atom",""],-1]}}class t{constructor(e){this.name=e,this.value=!1}setState(e){this.value="0"!=e}}class s{constructor(r){let a=s.is_fbf(r),[i,n,l]=a;this.variables=[];for(let e of l){let s=!1;for(let t of this.variables)t.name==e&&(s=!0);s||this.variables.push(new t(e))}this.formula=r,this.fbf=i,this.operators=n,this.models=[],this.countermodels=[],this.tree=i?new e(this):void 0}evaluate(){let e=[],t="0".repeat(2**this.variables.length);for(let s=0;s<2**this.variables.length;s++){t=s.toString(2).padStart(this.variables.length,"0");for(let e=0;e<t.length;e++)this.variables[e].setState(t[e]);e.push([t,this.evaluateAssignment()])}return e}evaluateAssignment(e){let t=!1;if(null==e&&(e=this.tree),"Atom"==e.root[0])this.variables.forEach((s=>{s.name==e.root[1]&&(t=s.value)}));else if("Unary"==e.root[0])t=!this.evaluateAssignment(e.right);else switch(e.root[1]){case"&":t=this.evaluateAssignment(e.left)&&this.evaluateAssignment(e.right);break;case"|":t=this.evaluateAssignment(e.left)||this.evaluateAssignment(e.right);break;case">":t=!this.evaluateAssignment(e.left)||this.evaluateAssignment(e.right);break;case"-":t=this.evaluateAssignment(e.left)==this.evaluateAssignment(e.right)}return t}static is_fbf(e){let t=[],s=[],r=!1,a=!1;for(let i of e)switch(i){case"!":t.push(["Unary","!"]),r=!0;break;case"&":case"|":case">":case"-":if(r||a)return[!1,[],[]];t.push(["Binary",i]);break;case"(":case"[":case"{":t.push(["NestOpen"]),a=!0;break;case")":case"]":case"}":if(t.push(["NestClose"]),a||r)return[!1,[],[]];break;default:t.push(["Atom",i]),s.push(i),r=!1,a=!1}if(r||a)return[!1,[],[]];let i=Object.assign([],s),n=Object.assign([],t);for(;0!=n.length;)switch(n.pop()[0]){case"Unary":if(void 0===i.pop())return console.error("Unary operation requires one and only one operator"),[!1,[],[]];i.push("_");break;case"Binary":if(void 0===i.pop())return console.error("Binary operation requires exactly 2 operators"),[!1,[],[]];if(void 0===i.pop())return console.error("Binary operation requires exactly 2 operators"),[!1,[],[]];i.push("_");break;case"NestOpen":return console.error("Invalid parentesis"),[!1,[],[]];case"NestClose":{let e=n.reverse().findIndex((e=>"NestOpen"===e[0]));if(-1==e)return console.error("Missing closing parentesis"),[!1,[],[]];n.splice(e,1),n.reverse()}}return 1!=i.length?(console.error("Invalid number of variables"),[!1,[],[]]):[!0,t,s]}}window.checkFormula=function(){let e=document.getElementById("formulaInput").value,t=new s(e.trim());if(t.fbf){document.getElementById("invalidFormula").setAttribute("style","display: none");let e=document.getElementById("treeString");e.setAttribute("style","display: block"),e.innerHTML=`Syntax tree: </br><b style="padding-left: 20px">${t.tree}</b>`;let s=document.getElementById("formulaTable");s.setAttribute("style","display: block"),s.innerHTML="<h2 class='font-medium leading-tight text-3xl mt-0 mb-2'>Truth table:</h2>"+function(e,t){let s="<table class='table table-compact'><thead><tr>";for(const t of e)s+=`<th>${t.name}</th>`;s+="<th>Value</th></tr></thead><tbody>";for(const[e,r]of t){s+="<tr>";for(const t of e)s+=`<td>${t}</td>`;s+=`<td>${r}</td>`,s+="</tr>"}return s+="</table>",s}(t.variables,t.evaluate())}else document.getElementById("invalidFormula").setAttribute("style","display: block")}})();
\ No newline at end of file