UNPKG

python2igcse

Version:

Convert Python code to IGCSE Pseudocode format

201 lines 7.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserConverter = void 0; exports.convertPythonToIGCSE = convertPythonToIGCSE; // Browser-compatible converter implementation const parser_1 = require("./parser"); const emitter_1 = require("./emitter"); /** * Browser-compatible converter class */ class BrowserConverter { constructor(options = {}) { this.options = this.mergeDefaultOptions(options); // Initialize parser with basic options 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 emitter with basic options const emitterOptions = { format: this.options.outputFormat ?? 'plain', indentSize: this.options.indentSize ?? 2, lineEnding: this.options.lineEnding ?? '\n', indentChar: ' ', includeComments: this.options.includeComments ?? true, includeLineNumbers: this.options.includeLineNumbers ?? false, includeDebugInfo: false, beautify: this.options.beautify ?? true, }; this.emitter = new emitter_1.TextEmitter(emitterOptions); } /** * Convert Python code to IGCSE Pseudocode */ convertCode(pythonCode) { const startTime = Date.now(); try { // Parse Python code const parseResult = this.parser.parse(pythonCode); if (!parseResult.success || parseResult.errors.length > 0) { return this.createErrorResult('Parse failed', parseResult.errors, parseResult.warnings || [], startTime); } // Create program IR node from parsed statements const programIR = { kind: 'program', text: '', children: parseResult.ir, meta: { lineNumber: 1, }, }; // Emit IGCSE pseudocode const emitResult = this.emitter.emit(programIR); if (!emitResult.success || emitResult.errors.length > 0) { return this.createErrorResult('Emit failed', emitResult.errors, emitResult.warnings || [], startTime); } const endTime = Date.now(); return { code: emitResult.code, parseResult, emitResult, success: true, stats: { inputLines: pythonCode.split('\n').length, outputLines: emitResult.code.split('\n').length, conversionTime: endTime - startTime, parseTime: parseResult.parseTime || 0, emitTime: emitResult.emitTime || 0, errorCount: parseResult.errors.length + emitResult.errors.length, warningCount: parseResult.warnings.length + emitResult.warnings.length, totalTime: endTime - startTime, }, }; } catch (error) { return this.createErrorResult(`Conversion failed: ${error instanceof Error ? error.message : 'Unknown error'}`, [], [], startTime); } } /** * Validate Python syntax */ validateSyntax(pythonCode) { try { const parseResult = this.parser.parse(pythonCode); return { isValid: parseResult.success && parseResult.errors.length === 0, errors: parseResult.errors.map((e) => e.message), warnings: parseResult.warnings.map((w) => w.message), }; } catch (error) { return { isValid: false, errors: [error instanceof Error ? error.message : 'Unknown error'], warnings: [], }; } } /** * Get current options */ getOptions() { return { ...this.options }; } /** * Update options */ updateOptions(newOptions) { this.options = { ...this.options, ...newOptions }; } // Error logging is handled by the parent converter mergeDefaultOptions(options) { return { debug: false, strictMode: false, includeComments: true, preserveWhitespace: false, maxErrors: 100, timeout: 30000, outputFormat: 'plain', indentSize: 2, indentType: 'spaces', lineEnding: '\n', maxLineLength: 80, beautify: true, includeLineNumbers: false, uppercaseKeywords: true, spaceAroundOperators: true, spaceAfterCommas: true, allowExperimentalSyntax: false, maxNestingDepth: 50, ...options, }; } createErrorResult(_message, errors, warnings, startTime) { const endTime = Date.now(); return { code: '', success: false, parseResult: { ir: [], errors: errors.map((e) => typeof e === 'string' ? { message: e, type: 'syntax_error', severity: 'error' } : e), warnings: warnings.map((w) => typeof w === 'string' ? { message: w, type: 'style_suggestion' } : w), stats: { linesProcessed: 0, nodesGenerated: 0, parseTime: 0, functionsFound: 0, classesFound: 0, variablesFound: 0, }, success: false, parseTime: 0, }, emitResult: { code: '', errors: [], warnings: [], stats: { linesGenerated: 0, lineCount: 0, charactersGenerated: 0, characterCount: 0, nodesProcessed: 0, emitTime: 0, processingTime: 0, maxNestingDepth: 0, maxLineLength: 0, }, success: false, emitTime: 0, output: '', }, stats: { inputLines: 0, outputLines: 0, conversionTime: endTime - startTime, parseTime: 0, emitTime: 0, errorCount: errors.length, warningCount: warnings.length, totalTime: endTime - startTime, }, }; } } exports.BrowserConverter = BrowserConverter; /** * Utility function for browser conversion */ function convertPythonToIGCSE(pythonCode, options = {}) { const converter = new BrowserConverter(options); return converter.convertCode(pythonCode); } //# sourceMappingURL=browser-converter.js.map