Last active
July 12, 2021 17:55
-
-
Save winstondu/e9f2f9b9ae9e68268ace2ec281b008a7 to your computer and use it in GitHub Desktop.
Interpret
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const fns = { | |
gt: (a, b) => a > b, | |
lt: (a, b) => a < b, | |
eq: (a, b) => a === b, | |
add: (a, b) => a + b, | |
sub: (a, b) => a - b, | |
mul: (a, b) => a * b, | |
div: (a, b) => a / b, | |
if: (cond, a, b) => (cond ? a : b), | |
}; | |
function interpret2(pgm) { | |
let isArray = Array.isArray(pgm); | |
if (isArray) { | |
let [fn, ...args] = pgm; | |
let values = args.map(interpret2); | |
return fns[fn](...values); | |
} else { | |
return pgm; | |
} | |
} | |
(async () => { | |
let isFifteenDivisibleByThreeProgram = [ | |
"if", | |
["eq", 0, ["div", 15, 3]], | |
true, | |
false | |
]; | |
console.log(interpret2(isFifteenDivisibleByThreeProgram)); | |
const shutdown = (signal) => async () => { | |
process.exit(0); | |
}; | |
process | |
.on('SIGINT', shutdown('SIGINT')) | |
.on('SIGTERM', shutdown('SIGTERM')) | |
.on('uncaughtException', shutdown('uncaughtException')); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment