python2igcse
Version:
Convert Python code to IGCSE Pseudocode format
337 lines • 11.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Converter = void 0;
exports.convertPythonToIGCSE = convertPythonToIGCSE;
// Python to IGCSE Pseudocode Converter
const parser_1 = require("./parser");
const emitter_1 = require("./emitter");
/**
* Python to IGCSE Pseudocode Converter
*/
class Converter {
constructor(options = {}) {
this.options = this.mergeDefaultOptions(options);
// Initialize parser
const parserOptions = {
strictMode: this.options.strictMode ?? false,
includeComments: this.options.includeComments ?? true,
preserveWhitespace: this.options.preserveWhitespace ?? false,
maxErrors: this.options.maxErrors ?? 100,
timeout: this.options.timeout ?? 30000,
};
this.parser = new parser_1.PythonParser(parserOptions);
// Initialize emitters
const emitterOptions = {
format: this.options.outputFormat ?? 'plain',
indentSize: this.options.indentSize ?? 2,
indentChar: ' ',
indentType: this.options.indentType ?? 'spaces',
lineEnding: this.options.lineEnding ?? '\n',
maxLineLength: this.options.maxLineLength ?? 80,
beautify: this.options.beautify ?? true,
includeComments: this.options.includeComments ?? true,
includeLineNumbers: this.options.includeLineNumbers ?? false,
includeDebugInfo: false,
};
this.textEmitter = new emitter_1.TextEmitter(emitterOptions);
this.markdownEmitter = new emitter_1.MarkdownEmitter({
...emitterOptions,
format: 'markdown',
});
}
/**
* Convert Python code to IGCSE Pseudocode
*/
convert(pythonCode) {
const startTime = Date.now();
try {
// Parse process
const parseResult = this.parser.parse(pythonCode);
if (parseResult.errors.length > 0) {
return this.createErrorResult('Parse errors occurred', parseResult.errors, parseResult.warnings, startTime);
}
// Emit process
const emitter = this.options.outputFormat === 'markdown' ? this.markdownEmitter : this.textEmitter;
// Create compound IR to process all IR nodes
const compoundIR = {
kind: 'compound',
text: '',
children: parseResult.ir,
};
const emitResult = emitter.emit(compoundIR);
// Create result
const endTime = Date.now();
const stats = this.createConversionStats(parseResult, emitResult, startTime, endTime);
const result = {
code: emitResult.code,
parseResult,
emitResult,
stats,
ast: parseResult.ir,
ir: Array.isArray(parseResult.ir) ? parseResult.ir : [parseResult.ir],
success: parseResult.success && emitResult.success,
};
return result;
}
catch (error) {
const errorResult = this.createErrorResult(error instanceof Error ? error.message : 'Unknown error occurred', [], [], startTime);
return errorResult;
}
}
/**
* Batch conversion (multiple files)
*/
convertBatch(files) {
const results = [];
for (const file of files) {
try {
const result = this.convert(file.content);
results.push({ name: file.name, result });
}
catch (error) {
const errorResult = {
code: '',
parseResult: {
ir: [{ kind: 'comment', text: '', children: [] }],
errors: [
{
message: error instanceof Error ? error.message : 'Unknown error',
type: 'syntax_error',
line: 1,
column: 1,
severity: 'error',
},
],
warnings: [],
stats: {
parseTime: 0,
linesProcessed: 0,
nodesGenerated: 0,
functionsFound: 0,
classesFound: 0,
variablesFound: 0,
},
success: false,
parseTime: 0,
},
emitResult: {
code: '',
errors: [],
warnings: [],
stats: {
emitTime: 0,
linesGenerated: 0,
lineCount: 0,
charactersGenerated: 0,
characterCount: 0,
nodesProcessed: 0,
processingTime: 0,
maxNestingDepth: 0,
maxLineLength: 0,
},
success: false,
emitTime: 0,
output: '',
},
stats: {
parseTime: 0,
emitTime: 0,
conversionTime: 0,
inputLines: 0,
outputLines: 0,
errorCount: 1,
warningCount: 0,
totalTime: 0,
},
success: false,
};
results.push({ name: file.name, result: errorResult });
}
}
return results;
}
/**
* Update conversion options
*/
updateOptions(newOptions) {
this.options = this.mergeDefaultOptions({ ...this.options, ...newOptions });
// Update parser options (implement as needed)
// Update emitter options
const emitterOptions = {
format: this.options.outputFormat ?? 'plain',
indentSize: this.options.indentSize ?? 2,
indentChar: ' ',
indentType: this.options.indentType ?? 'spaces',
lineEnding: this.options.lineEnding ?? '\n',
maxLineLength: this.options.maxLineLength ?? 80,
beautify: this.options.beautify ?? true,
includeComments: this.options.includeComments ?? true,
includeLineNumbers: this.options.includeLineNumbers ?? false,
includeDebugInfo: false,
};
this.textEmitter.updateOptions(emitterOptions);
this.markdownEmitter.updateOptions({ ...emitterOptions, format: 'markdown' });
}
/**
* Get current options
*/
getOptions() {
return { ...this.options };
}
/**
* Get conversion statistics
*/
getStats() {
// Implementation should track statistics
// Currently returns dummy values
return {
totalConversions: 0,
successfulConversions: 0,
averageParseTime: 0,
averageEmitTime: 0,
averageTotalTime: 0,
};
}
/**
* Validate IR
*/
validateIR(ir) {
const errors = [];
const warnings = [];
// Basic validation
if (!ir.kind) {
errors.push('IR node missing kind property');
}
if (!ir.children) {
errors.push('IR node missing children property');
}
// Recursive validation
for (const child of ir.children || []) {
const childValidation = this.validateIR(child);
errors.push(...childValidation.errors);
warnings.push(...childValidation.warnings);
}
return {
isValid: errors.length === 0,
errors,
warnings,
};
}
/**
* Merge with default options
*/
mergeDefaultOptions(options) {
const defaults = {
outputFormat: 'plain',
indentSize: 2,
indentType: 'spaces',
lineEnding: '\n',
maxLineLength: 80,
beautify: true,
strictMode: false,
includeComments: true,
includeLineNumbers: false,
preserveWhitespace: false,
uppercaseKeywords: true,
spaceAroundOperators: true,
spaceAfterCommas: true,
maxErrors: 10,
timeout: 30000,
};
return { ...defaults, ...options };
}
/**
* Create error result
*/
createErrorResult(message, parseErrors, parseWarnings, startTime) {
const endTime = Date.now();
return {
code: '',
parseResult: {
ir: [{ kind: 'comment', text: '', children: [] }],
errors: [
{
type: 'conversion',
severity: 'error',
message,
location: { line: 1, column: 1 },
},
...parseErrors,
],
warnings: parseWarnings,
stats: {
parseTime: 0,
linesProcessed: 0,
nodesGenerated: 0,
functionsFound: 0,
classesFound: 0,
variablesFound: 0,
},
success: false,
parseTime: 0,
},
emitResult: {
code: '',
errors: [],
warnings: [],
stats: {
emitTime: 0,
linesGenerated: 0,
lineCount: 0,
charactersGenerated: 0,
characterCount: 0,
nodesProcessed: 0,
processingTime: 0,
maxNestingDepth: 0,
maxLineLength: 0,
},
success: false,
emitTime: 0,
output: '',
},
stats: {
parseTime: 0,
emitTime: 0,
conversionTime: endTime - startTime,
inputLines: 0,
outputLines: 0,
errorCount: parseErrors.length + 1,
warningCount: parseWarnings.length,
totalTime: endTime - startTime,
},
success: false,
ast: undefined,
};
}
/**
* Create conversion statistics
*/
createConversionStats(parseResult, emitResult, startTime, endTime) {
return {
parseTime: parseResult.stats.parseTime,
emitTime: emitResult.stats.emitTime,
conversionTime: endTime - startTime,
inputLines: parseResult.stats.linesProcessed,
outputLines: emitResult.stats.linesGenerated,
errorCount: parseResult.errors.length,
warningCount: parseResult.warnings.length,
totalTime: endTime - startTime,
};
}
}
exports.Converter = Converter;
/**
* Utility function: Simple conversion
*/
async function convertPythonToIGCSE(pythonCode, options = {}) {
const converter = new Converter(options);
return converter.convert(pythonCode);
}
/**
* Utility function: Convert from file
*/
// File conversion functions are available in converter-node.ts for Node.js environments
/**
* Utility function: Convert multiple files
*/
//# sourceMappingURL=converter.js.map