What are all the features of pypp: const fs = require('fs'); const { spawn } =…
- User
What are all the features of pypp: const fs = require('fs'); const { spawn } = require('child_process');
const config = 'CONFIG'; let mainName = 'app.pypp';
if (fs.existsSync(config)) { fs.readFile(config, 'utf8', (err, data) => { if (err) throw err; const match = data.match(/APP_NAME=(.+).pypp/); if (match) { mainName = match[1] + '.pypp'; } else { formatError(
Couldn't find the main file name in the config file.); process.exit(1); }if (fs.existsSync(mainName)) { translate(mainName); } else { formatError(`Couldn't find the file '${mainName}' in directory.`); process.exit(1); } });} else { if (fs.existsSync(mainName)) { translate(mainName); } else { formatError(
Couldn't find the file '${mainName}' in directory.); process.exit(1); } }function formatError(error, color = '\x1b[31m') { console.error(
${color}ERROR: ${error}\x1b[37m); }function translate(fileName) { fs.readFile(fileName, 'utf8', (err, data) => { if (err) throw err;
const formattedData = processCode(data); if (!formattedData) return; // Error has already been handled fs.writeFile('app.py', formattedData, err => { if (err) throw err; const pythonProcess = spawn('python', ['app.py'], { stdio: 'inherit' }); pythonProcess.on('close', () => { fs.unlink('app.py', err => { if (err) throw err; }); }); }); });}
function processCode(code) { let functionCounter = 0; let output = ''; let functionsCode = ''; let i = 0;
let readonlyVars = new Set(); let imports = new Set(); // State variables for tracking strings and comments let inSingleQuote = false; let inDoubleQuote = false; let inTripleSingleQuote = false; let inTripleDoubleQuote = false; let inSingleLineComment = false; let inMultiLineComment = false; while (i < code.length) { let char = code[i]; let nextChars = code.substr(i, 3); // Handle comments and strings if (inSingleLineComment) { // In single-line comment output += char; if (char === '\n') { inSingleLineComment = false; } i++; continue; } else if (inMultiLineComment) { // In multi-line comment output += char; if (code.substr(i, 2) === '*/') { inMultiLineComment = false; output += '*'; i++; } i++; continue; } else if (inSingleQuote) { // In single-quoted string output += char; if (char === "'" && code[i - 1] !== '\\') { inSingleQuote = false; } i++; continue; } else if (inDoubleQuote) { // In double-quoted string output += char; if (char === '"' && code[i - 1] !== '\\') { inDoubleQuote = false; } i++; continue; } else if (inTripleSingleQuote) { // In triple-single-quoted string output += char; if (code.substr(i, 3) === "'''") { inTripleSingleQuote = false; output += "''"; i += 2; } i++; continue; } else if (inTripleDoubleQuote) { // In triple-double-quoted string output += char; if (code.substr(i, 3) === '"""') { inTripleDoubleQuote = false; output += '""'; i += 2; } i++; continue; } else if (code.substr(i, 2) === '//') { // Start of single-line comment inSingleLineComment = true; output += '//'; i += 2; continue; } else if (code.substr(i, 2) === '/*') { // Start of multi-line comment inMultiLineComment = true; output += '/*'; i += 2; continue; } else if (char === "'") { inSingleQuote = true; output += char; i++; continue; } else if (char === '"') { inDoubleQuote = true; output += char; i++; continue; } else if (code.substr(i, 3) === "'''") { inTripleSingleQuote = true; output += "'''"; i += 3; continue; } else if (code.substr(i, 3) === '"""') { inTripleDoubleQuote = true; output += '"""'; i += 3; continue; } else if (/^\s$/.test(char)) { // Whitespace output += char; i++; continue; } // Now, we are not in a string or comment // Check for 'def' if (code.substr(i, 3) === 'def' && (/\s|\(/.test(code[i + 3] || ''))) { let j = i + 3; // Skip any whitespace after 'def' while (j < code.length && /\s/.test(code[j])) j++; if (code[j] === '(') { // Anonymous function let { funcCode, funcName, newIndex } = extractAnonymousFunction(code, i, functionCounter++); functionsCode += funcCode + '\n'; output += funcName; i = newIndex; continue; } else if (/[a-zA-Z_]/.test(code[j])) { // Regular function let { funcCode, newIndex } = extractRegularFunction(code, i); output += funcCode; i = newIndex; continue; } else { formatError('Invalid function definition after def'); return ''; } } // Check for 'readonly' if (code.substr(i, 8) === 'readonly' && (/\s/.test(code[i + 8] || ''))) { let j = i + 'readonly'.length; // Skip any whitespace after 'readonly' while (j < code.length && /\s/.test(code[j])) j++; // Now extract variable name let varNameMatch = code.substr(j).match(/^([a-zA-Z_][a-zA-Z0-9_]*)/); if (varNameMatch) { let varName = varNameMatch[1]; j += varName.length; // Skip whitespace while (j < code.length && /\s/.test(code[j])) j++; if (code[j] === '=') { j++; // Skip '=' // Skip whitespace while (j < code.length && /\s/.test(code[j])) j++; // Extract value let valueStart = j; while (j < code.length && code[j] !== '\n') j++; let value = code.substring(valueStart, j).trim(); // Add to readonlyVars readonlyVars.add(varName); // Replace 'readonly x = y' with 'x = y' output += `${varName} = ${value}\n`; i = j + 1; // Move to next character after the newline } else { formatError('Expected "=" after readonly variable name'); return ''; } continue; } else { formatError('Invalid readonly variable declaration'); return ''; } } // Check for 'enum' if (code.substr(i, 4) === 'enum' && (/\s|\{/.test(code[i + 4] || ''))) { let j = i + 4; // skip 'enum' // Skip any whitespace while (j < code.length && /\s/.test(code[j])) j++; // Now extract enum name let enumNameMatch = code.substr(j).match(/^([a-zA-Z_][a-zA-Z0-9_]*)/); if (enumNameMatch) { let enumName = enumNameMatch[1]; j += enumName.length; // Skip whitespace while (j < code.length && /\s/.test(code[j])) j++; // Expect '{' if (code[j] === '{') { j++; // Skip '{' // Now extract enum members until '}' let enumMembers = []; let currentValue = 0; while (j < code.length) { // Skip whitespace while (j < code.length && /\s/.test(code[j])) j++; if (code[j] === '}') { j++; // Skip '}' break; } // Now extract member name let memberNameMatch = code.substr(j).match(/^([a-zA-Z_][a-zA-Z0-9_]*)/); if (memberNameMatch) { let memberName = memberNameMatch[1]; j += memberName.length; // Skip whitespace while (j < code.length && /\s/.test(code[j])) j++; let memberValue = null; // Check for '=' if (code[j] === '=') { j++; // Skip '=' // Skip whitespace while (j < code.length && /\s/.test(code[j])) j++; // Extract value let valueStart = j; while (j < code.length && /[^\s,}]/.test(code[j])) j++; let valueStr = code.substring(valueStart, j).trim(); // Try to parse memberValue as integer let parsedValue = parseInt(valueStr, 10); if (!isNaN(parsedValue)) { memberValue = parsedValue; currentValue = memberValue; } else { formatError(`Invalid enum value '${valueStr}' for member '${memberName}'`); return ''; } } else { memberValue = currentValue; } enumMembers.push({ name: memberName, value: memberValue }); currentValue++; // Increment for next member if no value specified // Skip whitespace while (j < code.length && /\s/.test(code[j])) j++; // Check for ',' or '}' if (code[j] === ',') { j++; // Skip ',' continue; } else if (code[j] === '}') { // j++; // Skip '}' continue; } else if (code[j] === '/') { // Handle comments if (code[j + 1] === '/') { // Single-line comment, skip to end of line j += 2; while (j < code.length && code[j] !== '\n') j++; } else if (code[j + 1] === '*') { // Multi-line comment j += 2; while (j < code.length && !(code[j] === '*' && code[j + 1] === '/')) j++; if (code[j] === '*' && code[j + 1] === '/') j += 2; } continue; } else { // For safety, skip to next ',' or '}' while (j < code.length && code[j] !== ',' && code[j] !== '}') { j++; } if (code[j] === ',') { j++; continue; } else if (code[j] === '}') { j++; break; } } } else { formatError('Invalid enum member declaration'); return ''; } } // Now generate Python code for the enum imports.add('from enum import Enum'); let enumCode = `class ${enumName}(Enum):\n`; enumMembers.forEach(member => { enumCode += ` ${member.name} = ${member.value}\n`; }); // Add to output output += enumCode + '\n'; i = j; continue; } else { formatError('Expected "{" after enum name'); return ''; } } else { formatError('Invalid enum declaration, expected enum name'); return ''; } } // Handle assignments let assignmentMatch = code.substr(i).match(/^([a-zA-Z_][a-zA-Z0-9_]*)\s*=/); if (assignmentMatch) { let varName = assignmentMatch[1]; if (readonlyVars.has(varName)) { // Inject code to raise exception output += `raise Exception("Cannot reassign readonly variable '${varName}'")\n`; // Skip the assignment line while (i < code.length && code[i] !== '\n') i++; i++; // Skip '\n' continue; } else { // Copy the assignment line while (i < code.length && code[i] !== '\n') { output += code[i++]; } output += '\n'; i++; // Skip '\n' continue; } } // Copy the character output += char; i++; } // Combine the function definitions, imports, and the rest of the code const importsCode = Array.from(imports).join('\n'); const finalOutput = importsCode + '\n' + functionsCode + output; return replaceCurlyBrackets(finalOutput); - Assistant