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

add astToLaTeX function and test for expression

parent b364dc71
No related branches found
No related tags found
No related merge requests found
...@@ -62,6 +62,50 @@ function astToString(node, parentPrecedence = 0) { ...@@ -62,6 +62,50 @@ function astToString(node, parentPrecedence = 0) {
return result; return result;
} }
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;
}
var ast = {}; var ast = {};
ast.Not = Node.bind(null, 'not'); ast.Not = Node.bind(null, 'not');
...@@ -72,5 +116,6 @@ ast.Symbol = Node.bind(null, 'symbol'); ...@@ -72,5 +116,6 @@ ast.Symbol = Node.bind(null, 'symbol');
ast.True = Node.bind(null, 'true'); ast.True = Node.bind(null, 'true');
ast.False = Node.bind(null, 'false'); ast.False = Node.bind(null, 'false');
ast.astToString = astToString; ast.astToString = astToString;
ast.astToLaTeX = astToLaTeX;
module.exports = ast; // Export the AST module module.exports = ast; // Export the AST module
\ No newline at end of file
...@@ -105,3 +105,54 @@ test('astToString could work correctly for complex expression', () => { ...@@ -105,3 +105,54 @@ test('astToString could work correctly for complex expression', () => {
expect(output).toEqual(inputString); expect(output).toEqual(inputString);
}); });
// astToLaTeX test for expression
test('astToLaTeX could work correctly for and expression', () => {
const inputString = "A & B";
const input = parse(inputString);
const output = ast.astToLaTeX(input);
expect(output).toEqual("\\mathit{A} \\land \\mathit{B}");
});
test('astToLaTeX could work correctly for or expression', () => {
const inputString = "A || B";
const input = parse(inputString);
const output = ast.astToLaTeX(input);
expect(output).toEqual("\\mathit{A} \\lor \\mathit{B}");
});
test('astToLaTeX could work correctly for negation expression', () => {
const inputString = "!A";
const input = parse(inputString);
const output = ast.astToLaTeX(input);
expect(output).toEqual("\\neg \\mathit{A}");
});
test('astToLaTeX could work correctly for implication expression', () => {
const inputString = "A -> B";
const input = parse(inputString);
const output = ast.astToLaTeX(input);
expect(output).toEqual("\\mathit{A} \\rightarrow \\mathit{B}");
});
test('astToLaTeX could work correctly for true and false expression', () => {
const inputString = "true & false";
const input = parse(inputString);
const output = ast.astToLaTeX(input);
expect(output).toEqual("\\top \\land \\bot");
});
test('astToLaTeX could work correctly for mixed logical expression', () => {
const inputString = "P -> Q || !R";
const input = parse(inputString);
const output = ast.astToLaTeX(input);
expect(output).toEqual("\\mathit{P} \\rightarrow \\mathit{Q} \\lor \\neg \\mathit{R}");
});
test('astToLaTeX could work correctly for complex expression', () => {
const inputString = "X & (Y -> Z)";
const input = parse(inputString);
const output = ast.astToLaTeX(input);
expect(output).toEqual("\\mathit{X} \\land (\\mathit{Y} \\rightarrow \\mathit{Z})");
});
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