Last active
August 13, 2021 11:03
-
-
Save divs1210/69d92d6f3598ded2a38bdb66bec12624 to your computer and use it in GitHub Desktop.
A TCOd implementation of https://gist.github.com/divs1210/cfc17976a0ad85faa0099e4fd0a502fb
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<div> | |
<center> | |
<h3>Lisp.js</h3> | |
<h4>Use the browser console to try it out!</h4> | |
</center> | |
<pre style="margin-left: 44%;"> | |
let env = baseEnv(); | |
let code = ['+', 1, 2]; | |
lispEval(code, env); | |
</pre> | |
</div> | |
<script type="text/javascript"> | |
let parentEnvKey = '__parent_env__'; | |
function getTopLevel(env) { | |
if(parentEnvKey in env) | |
return getTopLevel(env[parentEnvKey]); | |
return env; | |
} | |
function lookup(env, symbol) { | |
if(symbol in env) | |
return env[symbol]; | |
if(parentEnvKey in env) | |
return lookup(env[parentEnvKey], symbol); | |
throw 'symbol ' +symbol+ ' is not defined!'; | |
} | |
function makeFunction(params, body, env) { | |
return { | |
isLispFn: true, | |
params: params, | |
body: body, | |
env: env | |
}; | |
} | |
function zipmap(keys, vals) { | |
var result = {}; | |
keys.forEach((key, i) => { | |
result[key] = vals[i]; | |
}); | |
return result; | |
} | |
function lispEval(_exp, _env) { | |
let [exp, env] = [_exp, _env]; | |
while(true) { | |
if(typeof exp == 'number') { // numbers | |
return exp; | |
} else if(typeof exp == 'string') { // symbols | |
return lookup(env, exp); | |
} else if(Array.isArray(exp)) { // lists | |
let [operator, ...operands] = exp; | |
if(operator == 'do') { | |
let ops = operands; | |
exp = ops.pop(); | |
for(let op of ops) { | |
lispEval(op, env); | |
} | |
} else if(operator == 'def') { | |
let [symbol, exp] = operands; | |
let topLevelEnv = getTopLevel(env); | |
topLevelEnv[symbol] = lispEval(exp, env); | |
return; | |
} else if(operator == 'if') { | |
let [testExp, thenExp, elseExp] = operands; | |
exp = lispEval(testExp, env)? thenExp : elseExp; | |
} else if(operator == 'fn') { | |
let [params, body] = operands; | |
return makeFunction(params, body, env); | |
} else if(operator == 'quote') { | |
let [quoted] = operands; | |
return quoted; | |
} else if(operator == 'js*') { | |
let [jsCode] = operands; | |
return eval(jsCode); | |
} else { | |
let fn = lispEval(operator, env); | |
let args = operands.map((o) => lispEval(o, env)); | |
if(fn.isLispFn) { | |
exp = fn.body; | |
env = zipmap(fn.params, args); | |
env[parentEnvKey] = fn.env; | |
} else { | |
return fn(...args); | |
} | |
} | |
} | |
} | |
} | |
function baseEnv() { | |
return { | |
'+' : (a, b) => a + b, | |
'-' : (a, b) => a - b, | |
'*' : (a, b) => a * b, | |
'/' : (a, b) => a / b, | |
'^' : (a, b) => Math.pow(a, b), | |
'=' : (a, b) => a === b, | |
'<' : (a, b) => a < b, | |
'>' : (a, b) => a > b, | |
'<=': (a, b) => a <= b, | |
'>=': (a, b) => a >= b, | |
'not': (a) => !a | |
}; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try it out