babu-language
Version:
A fun programming language built on JavaScript. Created by Atikin Verse.
67 lines (51 loc) • 2.24 kB
JavaScript
const fs = require('fs');
// Function to transpile Babu code into JavaScript
function transpile(code) {
return code
// ✅ Print Statements
.replace(/^ *bol babu "(.*?)" *$/gm, 'console.log("$1")') // Static string
.replace(/^ *bol babu (.+?) *$/gm, 'console.log($1)') // Dynamic expression
// ✅ Variable Declarations
.replace(/let babu (\w+) = (.+)/g, 'let $1 = $2')
.replace(/const babu (\w+) = (.+)/g, 'const $1 = $2')
.replace(/var babu (\w+) = (.+)/g, 'var $1 = $2')
// ✅ Arrays
.replace(/let babu (\w+) = \[(.*?)\]/g, 'let $1 = [$2]')
// ✅ Objects
.replace(/let babu (\w+) = \{(.*?)\}/g, 'let $1 = {$2}')
// ✅ Conditionals
.replace(/agar babu \((.*?)\)/g, 'if ($1) {')
.replace(/nahi to babu/g, '} else {')
// ✅ Loops
.replace(/jab tak babu \((.*?)\)/g, 'while ($1) {')
// ✅ Functions
.replace(/kaam babu (\w+)\((.*?)\)/g, 'function $1($2) {') // Function declaration
.replace(/kaam khatam babu/g, '}') // End of block
.replace(/bula babu (\w+)\((.*?)\)/g, '$1($2)') // Function call with args
// ✅ Return
.replace(/wapis jao babu (.+)/g, 'return $1')
// ✅ Booleans
.replace(/\bsahi babu\b/g, 'true')
.replace(/\bgalat babu\b/g, 'false')
// ✅ File operations
.replace(/padho file "(.*?)"/g, 'fs.readFileSync("$1", "utf-8")')
.replace(/likho file "(.*?)", (.+)/g, 'fs.writeFileSync("$1", $2)')
// ✅ Error handling
.replace(/galti ho sakti hai babu/g, 'try {')
.replace(/pakdo babu \((\w+)\)/g, '} catch ($1) {')
.replace(/koi ghalti babu "(.*?)"/g, 'throw new Error("$1")')
// ✅ Event Handling (basic)
.replace(/event babu (\w+)\s*=\s*(.+)/g, '$1 = function() { $2 }')
.replace(/bula babu (\w+)\(event\)/g, '$1(event)');
}
// Function to read file, transpile it and execute the result
function run(filePath) {
const code = fs.readFileSync(filePath, 'utf-8');
const jsCode = transpile(code);
try {
eval(jsCode);
} catch (err) {
console.error('Error executing the transpiled code:', err);
}
}
module.exports = { run };