import { ASTNode } from "./index"; import symbol_tree from './index'; import { Expression, ValueNode, AttributionNode, FunctionCallNode } from './index'; var variable_id = 0; let results: string[] = []; function nextVar () { return 'var' + variable_id++; } function writeThreeAddressCode (node) { if (node.id === 'StatementsNode') { for (var statement of node.statements) { writeThreeAddressCode(statement); } } else if (node instanceof FunctionCallNode) { /**TODO: * generate the three address code for each parameter of the node.parameters array * write on the screen the three address code for each parameter * node.result will be nextVar() * write on the screen the three address code for function call */ } else if (node instanceof ValueNode) { // the result for a number is the number itself node.result = node.value; } else if (node instanceof AttributionNode) { /** TODO: * generate the three address code for node.value * write on the screen the three address code for an attribution*/ } else if (node instanceof Expression) { if (node.left !== undefined && node.right !== undefined) { writeThreeAddressCode (node.left); writeThreeAddressCode (node.right); // node.left.result is the result of node.left // node.right.result is the result of node.right /** TODO: * node.result will be nextVar() * write on the screen the three address code based on result, left result, right result and operator*/ } } } var ast = parser.parse (str); console.log (JSON.stringify(ast, null, 4)); writeThreeAddressCode(ast);