I'm creating a new syntax for Python to look more like C++, but still maintains the…
- User
I'm creating a new syntax for Python to look more like C++, but still maintains the simplicity of Python, I'm making it through JavaScript... here's the code:
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.); }if (fs.existsSync(mainName)) { translate(mainName); } else { formatError(`Couldn't find the file '${mainName}' in directory.`); } });} else { if (fs.existsSync(mainName)) { translate(mainName); } else { formatError(
Couldn't find the file '${mainName}' in directory.); } }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;
function replaceCurlyBrackets(code) { let inFString = false; let formattedCode = ''; let indentLevel = 0; const indentSize = 4; code = code.replace(/}\s+(.*?)/g, '}$1'); code = code.replace(/^(\s*)?\/\/(.*)$/gm, '$1#$2'); for (let i = 0; i < code.length; i++) { if (code[i] === 'f' && (code[i + 1] === '"' || code[i + 1] === "'")) { inFString = true; formattedCode += code[i]; continue; } if (inFString) { if (code[i] === code[i - 1]) { inFString = false; } formattedCode += code[i]; continue; } if (code[i] === '{') { formattedCode += ':\n' + ' '.repeat((indentLevel + 1) * indentSize); indentLevel++; } else if (code[i] === '}') { indentLevel = Math.max(indentLevel - 1, 0); formattedCode = formattedCode.trimEnd() + '\n' + ' '.repeat(indentLevel * indentSize); } else if (code[i] === '\n') { formattedCode = formattedCode.trimEnd(); formattedCode += '\n' + ' '.repeat(indentLevel * indentSize); } else if (code[i] === ';') { continue; } else { formattedCode += code[i]; } } return formattedCode.trimEnd(); } const formattedData = replaceCurlyBrackets(data); 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; }); }); }); }); - Assistant