Skip to content
Snippets Groups Projects
Commit 4ea66a3f authored by Jenny Zhang's avatar Jenny Zhang
Browse files

redefined Node by constructor function

parent f8506705
No related branches found
No related tags found
No related merge requests found
...@@ -2,14 +2,13 @@ ...@@ -2,14 +2,13 @@
// Function to create a new AST node // Function to create a new AST node
function Node(type, value, left, right) { function Node(type, value, left, right) {
return { this.type = type;
type: type, this.value = value;
value: value, this.left = left;
left: left, this.right = right;
right: right
};
} }
const precedence = { const precedence = {
'imply': 1, 'imply': 1,
'or': 2, 'or': 2,
...@@ -20,7 +19,12 @@ const precedence = { ...@@ -20,7 +19,12 @@ const precedence = {
'false': 5 'false': 5
}; };
function astsToString(nodes){ function astsToString(nodes) {
if (console.log(typeof nodes) instanceof Node) {
throw new Error("Invalid input. Only sequent is accepted, not expression.");
}
const leftList = nodes[0]; const leftList = nodes[0];
const rightList = nodes[1]; const rightList = nodes[1];
...@@ -36,49 +40,56 @@ function astsToString(nodes){ ...@@ -36,49 +40,56 @@ function astsToString(nodes){
} }
function astToString(node, parentPrecedence = 0) { function astToString(node, parentPrecedence = 0) {
if (!node) return "";
if (!(console.log(typeof nodes) instanceof Node)) {
let currentPrecedence = precedence[node.type]; throw new Error("Invalid input. Only expression is accepted.");
}
let left = astToString(node.left, currentPrecedence);
let right = astToString(node.right, currentPrecedence); if (!node) return "";
let result; let currentPrecedence = precedence[node.type];
switch (node.type) {
case 'not': let left = astToString(node.left, currentPrecedence);
result = `!${left}`; let right = astToString(node.right, currentPrecedence);
break;
case 'and': let result;
result = `${left}&${right}`; switch (node.type) {
break; case 'not':
case 'or': result = `!${left}`;
result = `${left}||${right}`; break;
break; case 'and':
case 'imply': result = `${left}&${right}`;
result = `${left}->${right}`; break;
break; case 'or':
case 'symbol': result = `${left}||${right}`;
result = node.value; break;
break; case 'imply':
case 'true': result = `${left}->${right}`;
result = 'true'; break;
break; case 'symbol':
case 'false': result = node.value;
result = 'false'; break;
break; case 'true':
default: result = 'true';
throw new Error(`Unknown node type: ${node.type}`); break;
} case 'false':
result = 'false';
if (currentPrecedence < parentPrecedence) { break;
result = `(${result})`; default:
} throw new Error(`Unknown node type: ${node.type}`);
}
return result;
if (currentPrecedence < parentPrecedence) {
result = `(${result})`;
}
return result;
} }
// nodes are [[ast],[ast]] // nodes are [[ast],[ast]]
function astsToLateX(nodes) { function astsToLateX(nodes) {
const leftList = nodes[0]; const leftList = nodes[0];
const rightList = nodes[1]; const rightList = nodes[1];
...@@ -139,13 +150,13 @@ function astToLaTeX(node, parentPrecedence = 0) { ...@@ -139,13 +150,13 @@ function astToLaTeX(node, parentPrecedence = 0) {
var ast = {}; var ast = {};
ast.Not = Node.bind(null, 'not'); ast.Not = function (value, left, right) { return new Node('not', value, left, right); };
ast.And = Node.bind(null, 'and'); ast.And = function (value, left, right) { return new Node('and', value, left, right); };
ast.Or = Node.bind(null, 'or'); ast.Or = function (value, left, right) { return new Node('or', value, left, right); };
ast.Imply = Node.bind(null, 'imply'); ast.Imply = function (value, left, right) { return new Node('imply', value, left, right); };
ast.Symbol = Node.bind(null, 'symbol'); ast.Symbol = function (value, left, right) { return new Node('symbol', value, left, right); };
ast.True = Node.bind(null, 'true'); ast.True = function () { return new Node('true'); };
ast.False = Node.bind(null, 'false'); ast.False = function () { return new Node('false'); }
ast.astToString = astToString; ast.astToString = astToString;
ast.astToLaTeX = astToLaTeX; ast.astToLaTeX = astToLaTeX;
ast.astsToLateX = astsToLateX; ast.astsToLateX = astsToLateX;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment