yip-core
Version:
basic acces tp the yip (yet-interpreted-programming) c & dr models (Yip compiler & direct-runner)
34 lines (28 loc) • 880 B
JavaScript
const { Yip } = require('../yip');
const y = new Yip([]);
// Runtime command: print!("...")
y.register('print', 'Print to console', `print!("hello");`, function () {
this.semicolonEnding(() => {
const val = this.parseParen();
console.log(this.resolve(val));
});
});
// Runtime command: let! x = 42;
y.register('let', 'Declare one or more variables', `let! x = 10, y = 20;`, function () {
this.semicolonEnding(() => {
const name = this.next();
this.expect('=');
const value = this.next();
this.vars[name] = this.resolve(value);
});
});
// Compiler: print!("hello");
y.registerCompiler('print', 'Compile print to JS', `print!("hello");`, function () {
let out = '';
this.semicolonEnding(() => {
const val = this.parseParen();
out += `console.log(${JSON.stringify(this.resolve(val))});\n`;
});
return out;
});
module.exports = y;