java2ib
Version:
TypeScript library that converts Java code into IB Computer Science pseudocode format
127 lines • 4.74 kB
JavaScript
;
/**
* Pseudocode Generation System for converting PseudocodeNode AST to formatted pseudocode strings
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PseudocodeGenerator = void 0;
exports.createGeneratorFromConversionOptions = createGeneratorFromConversionOptions;
const transformer_1 = require("./transformer");
class PseudocodeGenerator {
constructor(options = {}) {
this.options = {
indentSize: options.indentSize ?? 2,
preserveComments: options.preserveComments ?? true,
indentChar: options.indentChar ?? ' ',
};
}
/**
* Generate formatted pseudocode from PseudocodeNode AST
* @param nodes - Array of PseudocodeNode objects to convert
* @returns Formatted pseudocode string
*/
generate(nodes) {
const lines = [];
for (const node of nodes) {
const generatedLines = this.generateNode(node);
lines.push(...generatedLines);
}
return lines.join('\n');
}
/**
* Generate lines for a single PseudocodeNode
* @param node - The PseudocodeNode to generate
* @returns Array of formatted lines
*/
generateNode(node) {
const lines = [];
switch (node.type) {
case transformer_1.PseudocodeNodeType.STATEMENT:
lines.push(this.formatLine(node.content, node.indentLevel));
break;
case transformer_1.PseudocodeNodeType.BLOCK:
// For block nodes, generate the block header and then children
if (node.content) {
lines.push(this.formatLine(node.content, node.indentLevel));
}
if (node.children) {
for (const child of node.children) {
lines.push(...this.generateNode(child));
}
}
break;
case transformer_1.PseudocodeNodeType.EXPRESSION:
// Expressions are typically inline and don't generate separate lines
// They are handled as part of statements
lines.push(this.formatLine(node.content, node.indentLevel));
break;
case transformer_1.PseudocodeNodeType.COMMENT:
if (this.options.preserveComments) {
lines.push(this.formatLine(node.content, node.indentLevel));
}
break;
default:
// Fallback for unknown node types
lines.push(this.formatLine(node.content, node.indentLevel));
break;
}
// Generate children if they exist and haven't been handled above
if (node.children && node.type !== transformer_1.PseudocodeNodeType.BLOCK) {
for (const child of node.children) {
lines.push(...this.generateNode(child));
}
}
return lines;
}
/**
* Format a single line with proper indentation
* @param content - The content to format
* @param indentLevel - The indentation level
* @returns Formatted line with indentation
*/
formatLine(content, indentLevel) {
const indent = this.generateIndent(indentLevel);
return `${indent}${content}`;
}
/**
* Generate indentation string for the given level
* @param level - The indentation level
* @returns Indentation string
*/
generateIndent(level) {
const totalSpaces = level * this.options.indentSize;
return this.options.indentChar.repeat(totalSpaces);
}
/**
* Update generation options
* @param options - New options to merge
*/
updateOptions(options) {
this.options = { ...this.options, ...options };
}
/**
* Get current generation options
* @returns Current options
*/
getOptions() {
return { ...this.options };
}
}
exports.PseudocodeGenerator = PseudocodeGenerator;
/**
* Utility function to create a PseudocodeGenerator from ConversionOptions
* @param conversionOptions - ConversionOptions from the main API
* @returns Configured PseudocodeGenerator
*/
function createGeneratorFromConversionOptions(conversionOptions) {
const generatorOptions = {};
if (conversionOptions) {
if (conversionOptions.indentSize !== undefined) {
generatorOptions.indentSize = conversionOptions.indentSize;
}
if (conversionOptions.preserveComments !== undefined) {
generatorOptions.preserveComments = conversionOptions.preserveComments;
}
}
return new PseudocodeGenerator(generatorOptions);
}
//# sourceMappingURL=code-generator.js.map