UNPKG

python2igcse

Version:

Convert Python code to IGCSE Pseudocode format

48 lines 1.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createIR = createIR; exports.getIRDepth = getIRDepth; exports.countIRNodes = countIRNodes; exports.findIRNodes = findIRNodes; /** * Helper function for creating IR nodes */ function createIR(kind, text, children = [], meta) { const ir = { kind, text, children }; if (meta !== undefined) ir.meta = meta; return ir; } /** * Calculate IR tree depth */ function getIRDepth(ir) { if (ir.children.length === 0) { return 1; } return 1 + Math.max(...ir.children.map(child => getIRDepth(child))); } /** * Calculate number of nodes in IR tree */ function countIRNodes(ir) { return 1 + ir.children.reduce((sum, child) => sum + countIRNodes(child), 0); } /** * Search for IR nodes of specific type */ function findIRNodes(ir, kind) { const result = []; if (ir.kind === kind) { result.push(ir); } for (const child of ir.children) { result.push(...findIRNodes(child, kind)); } return result; } //# sourceMappingURL=ir.js.map