iri-compile
Version:
36 lines (35 loc) • 1.06 kB
JavaScript
function compile(program,vars) {
let match,scope = 0;
let compiled,currentscope = ""
program.split(' ').forEach(c=>{
// I/O
if (match = c.match(/I\/OSTREAM\s*<<\s*(\$?)(\w+)/)) {
// VARIABLE PRINTING
if (match[1] === '$') {
// SCOPE CHECK
if (scope > vars[match[2]].scope || currentscope === vars[match[2]].scopeName) {
compiled += "process.stdout.write(chalk.hex('ffeb33')(vars[match[2]]))"
} else {
compiled += `process.stdout.write(chalk.bgBrightRed.white(\`
---- SCOPE ERROR ----
Could not find variable '${match[2]}' in your current scope. You may want to try this instead:
${chalk.strikethrough(`
${chalk.italic('keyword')} {
var ${match[2]} : '${vars[match[2]].value}';
}
I/OSTREAM << $${match[2]}
`)}
var ${match[2]} : ${vars[match[2]].value};
${chalk.italic('keyword')} {
${chalk.italic('...')}
}
I/OSTREAM << $${match[2]}
\`))`
}
} else {
compiled += 'process.stdout.write(match[2])'
}
}
})
}
module.exports = compile