Browse Source

clean up and updates to the code

master
wes 7 years ago
parent
commit
ecb6d9b617
  1. 18
      environments.js
  2. 7
      parse.js
  3. 2
      representation.js
  4. 8
      vm.js

18
environments.js

@ -6,23 +6,6 @@
var errors = require("./errors.js");
var rep = require("./representation.js");
/*
* returns the new environment after mutating it
* values = [(identifier, JLambda expression)]
*/
function extend(env, values) {
var new_env = {};
var env_keys = Object.keys(env);
for (var i = 0; i < env_keys.length; i++) {
new_env[env_keys[i]] = env[env_keys[i]];
}
for (i = 0; i < values.length; i++) {
new_env[values[i][0].val] = values[i][1];
}
return new_env;
}
// creates a new environment initialized with the pairs in values
function makeEnv(name, values) {
var env = {};
@ -46,6 +29,5 @@ function lookup(name, env) {
module.exports = {
lookup : lookup,
extend : extend,
makeEnv : makeEnv
};

7
parse.js

@ -29,10 +29,12 @@ function addSrcPos(stx, tokens, linenum, charnum) {
return stx;
}
/* Gets the first token from the right side of the stack */
function fst(ts) {
return ts[ts.length-1];
}
/* Gets the second token from the right side of the stack */
function snd(ts) {
return ts[ts.length-2];
}
@ -814,7 +816,10 @@ function parseFull(tokenized) {
current = closure.annotate_fvs(desugarer.desugar(parse(tokenized), typeBindings));
ast.push(current);
}
return [ast, typeBindings];
return {
"ast" : ast,
"types" : typeBindings
};
} catch (e) {
if (e.stxerror !== undefined) {
e.stxerror();

2
representation.js

@ -345,7 +345,7 @@ function DataType(name, params, type) {
}
//Applies the function ``name'' to the list of parameters
/* Applies the function ``name'' to the list of parameters */
function makeApp(name, parameters) {
if (parameters) {
return parameters.slice(1).reduce(function(f, ident) {

8
vm.js

@ -8,8 +8,6 @@ var env = require("./environments.js");
var fs = require("fs");
var istr = fs.readFileSync('/dev/stdin').toString();
//var istr = "if true then (+ 6 (a+a*b)) else 1";
//var istr = "def (f a) (a + b)"
var ast = parse.parseFull(tokenizer.tokenize(istr));
function apply(func, p) {
@ -46,6 +44,7 @@ function evaluate(ast, environment) {
}
}
else if (ast.exprType === "Definition") {
console.log("XXX");
console.log(ast);
}
else {
@ -59,8 +58,9 @@ var testenv = env.makeEnv("toplevel",
["a", 2],
["b", 3]]);
var all = evaluate(ast[0][ast[0].length - 1], testenv);
console.log(all);
console.log(ast.types);
//var all = evaluate(ast, testenv);
//console.log(all);
//console.log("%j", testenv);
//console.log("%j", ast[0][ast[0].length - 1]);
//console.log("%j", ast[0][ast[0].length - 1]);

Loading…
Cancel
Save