Newer
Older
Jenny Zhang
committed
// src/utils/ast.js
// Function to create a new AST node
function Node(type, value, left, right) {
return {
type: type,
value: value,
left: left,
right: right
};
}
const precedence = {
'imply': 1,
'or': 2,
'and': 3,
'not': 4,
'symbol': 5,
'true': 5,
'false': 5
};
function astsToString(nodes){
const leftList = nodes[0];
const rightList = nodes[1];
if (leftList.length > 0) {
const leftFormulas = leftList.map(ast => astToString(ast)).join(",");
const rightFormulas = rightList.map(ast => astToString(ast)).join(",");
return `${leftFormulas}|-${rightFormulas}`;
}
const rightFormulas = rightList.map(ast => astToString(ast)).join(",");
return `|-${rightFormulas}`;
}
Jenny Zhang
committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function astToString(node, parentPrecedence = 0) {
if (!node) return "";
let currentPrecedence = precedence[node.type];
let left = astToString(node.left, currentPrecedence);
let right = astToString(node.right, currentPrecedence);
let result;
switch (node.type) {
case 'not':
result = `!${left}`;
break;
case 'and':
result = `${left}&${right}`;
break;
case 'or':
result = `${left}||${right}`;
break;
case 'imply':
result = `${left}->${right}`;
break;
case 'symbol':
result = node.value;
break;
case 'true':
result = 'true';
break;
case 'false':
result = 'false';
break;
default:
throw new Error(`Unknown node type: ${node.type}`);
}
if (currentPrecedence < parentPrecedence) {
result = `(${result})`;
}
return result;
}
// nodes are [[ast],[ast]]
function astsToLateX(nodes) {
const leftList = nodes[0];
const rightList = nodes[1];
if (leftList.length > 0) {
const leftFormulas = leftList.map(ast => astToLaTeX(ast)).join(", ");
const rightFormulas = rightList.map(ast => astToLaTeX(ast)).join(", ");
return `${leftFormulas} \\vdash ${rightFormulas}`;
}
const rightFormulas = rightList.map(ast => astToLaTeX(ast)).join(", ");
return `\\vdash ${rightFormulas}`;
}
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function astToLaTeX(node, parentPrecedence = 0) {
if (!node) return "";
let currentPrecedence = precedence[node.type];
let left = astToLaTeX(node.left, currentPrecedence);
let right = astToLaTeX(node.right, currentPrecedence);
let result;
switch (node.type) {
case 'not':
result = `\\neg ${left}`;
break;
case 'and':
result = `${left} \\land ${right}`;
break;
case 'or':
result = `${left} \\lor ${right}`;
break;
case 'imply':
result = `${left} \\rightarrow ${right}`;
break;
case 'symbol':
result = `\\mathit{${node.value}}`;
break;
case 'true':
result = '\\top';
break;
case 'false':
result = '\\bot';
break;
default:
throw new Error(`Unknown node type: ${node.type}`);
}
if (currentPrecedence < parentPrecedence) {
result = `(${result})`;
}
return result;
}
Jenny Zhang
committed
var ast = {};
ast.Not = Node.bind(null, 'not');
ast.And = Node.bind(null, 'and');
ast.Or = Node.bind(null, 'or');
ast.Imply = Node.bind(null, 'imply');
ast.Symbol = Node.bind(null, 'symbol');
ast.True = Node.bind(null, 'true');
ast.False = Node.bind(null, 'false');
ast.astToString = astToString;
ast.astToLaTeX = astToLaTeX;
ast.astsToLateX = astsToLateX;
ast.astsToString = astsToString;
Jenny Zhang
committed
module.exports = ast; // Export the AST module