Skip to content

Instantly share code, notes, and snippets.

@titouancreach
Last active December 12, 2017 17:58
Show Gist options
  • Save titouancreach/95a2f6e3b3da397f599f05fb4bff72f2 to your computer and use it in GitHub Desktop.
Save titouancreach/95a2f6e3b3da397f599f05fb4bff72f2 to your computer and use it in GitHub Desktop.
const expr = ["min", ["max", 0.5, ["toto", 1, 2]],1];
const env = {
'eegle:min': Math.min,
'eegle:max': Math.max,
'UI:COMPONENT': function (x) {
console.log('Component called', x);
return 1;
}
}
function templateLangVisu(expr) {
if (typeof expr === 'number') {
return expr;
} else {
const [x, ...xs] = expr;
return x + '(' + xs.map(x => templateLangVisu(x)) + ')'
}
}
function templateLangEvaluator(expr, env) {
console.log(expr);
if (typeof expr === 'number') {
return expr;
} else {
const [x, ...xs] = expr;
if (x in env) {
return env[x](...xs.map(x => templateLangEvaluator(x, env)));
} else {
console.log(x, ...xs.map(x => templateLangEvaluator(x, env)))
return [x, ...xs.map(x => templateLangEvaluator(x, env))]
}
}
}
console.log('Expression visualiser');
console.log(templateLangVisu(expr))
console.log('evaluation')
console.log(templateLangEvaluator(expr, env))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment