|
@ -4,21 +4,23 @@ var typ = require("./representation.js"); |
|
|
var parse = require("./parse.js"); |
|
|
var parse = require("./parse.js"); |
|
|
var tokenizer = require("./tokenize.js"); |
|
|
var tokenizer = require("./tokenize.js"); |
|
|
var pprint = require("./pprint.js"); |
|
|
var pprint = require("./pprint.js"); |
|
|
|
|
|
var env = require("./environments.js"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//var istr = fs.readFileSync('/dev/stdin').toString();
|
|
|
//var istr = fs.readFileSync('/dev/stdin').toString();
|
|
|
var istr = "(a + b)"; |
|
|
//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)); |
|
|
var ast = parse.parseFull(tokenizer.tokenize(istr)); |
|
|
|
|
|
|
|
|
function apply(func, p, environment) { |
|
|
function apply(func, p) { |
|
|
var full_func = evaluate(func, environment); |
|
|
return func(p); |
|
|
return [full_func, evaluate(p, environment)]; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function evaluateAll(ast, environment) { |
|
|
function evaluateAll(ast, environment) { |
|
|
var l = ast.length; |
|
|
var l = ast.length; |
|
|
var evaled = []; |
|
|
var evaled = []; |
|
|
for (var i = 0; i < l; i++) { |
|
|
for (var i = 0; i < l; i++) { |
|
|
|
|
|
// should look for closures?
|
|
|
evaled.push(evaluate(ast[i], environment)); |
|
|
evaled.push(evaluate(ast[i], environment)); |
|
|
} |
|
|
} |
|
|
return evaled; |
|
|
return evaled; |
|
@ -26,17 +28,39 @@ function evaluateAll(ast, environment) { |
|
|
|
|
|
|
|
|
function evaluate(ast, environment) { |
|
|
function evaluate(ast, environment) { |
|
|
if (ast.exprType == "Application") { |
|
|
if (ast.exprType == "Application") { |
|
|
return apply(ast.func, ast.p, environment); |
|
|
return apply(evaluate(ast.func, environment), evaluate(ast.p, environment)); |
|
|
} |
|
|
} |
|
|
else if (ast.exprType === "Unary") { /* Unary function application */ |
|
|
else if (ast.exprType === "Unary") { /* Unary function application */ |
|
|
return apply(ast.op, ast.val, environment); |
|
|
return apply(evaluate(ast.op, environment), evaluate(ast.val, environment)); |
|
|
} |
|
|
} |
|
|
else if (ast.exprType === "Name") { |
|
|
else if (ast.exprType === "Name") { |
|
|
return ast.ident; |
|
|
//console.log(env.lookup(ast.ident, environment));
|
|
|
|
|
|
return env.lookup(ast.ident, environment); |
|
|
|
|
|
} |
|
|
|
|
|
else if (ast.exprType === "If") { |
|
|
|
|
|
if (evaluate(ast.condition, environment)) { |
|
|
|
|
|
return evaluate(ast.thenexp, environment); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
return evaluate(ast.elseexp, environment); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else if (ast.exprType === "Definition") { |
|
|
|
|
|
console.log(ast); |
|
|
} |
|
|
} |
|
|
else { |
|
|
else { |
|
|
return ast.val; |
|
|
return ast.val; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
console.log("%j", evaluateAll(ast[0], false)); |
|
|
var testenv = env.makeEnv("toplevel", |
|
|
|
|
|
[ |
|
|
|
|
|
["+", function(a) { return function(b) { return a + b; } }], |
|
|
|
|
|
["*", function(a) { return function(b) { return a * b; } }], |
|
|
|
|
|
["a", 2], |
|
|
|
|
|
["b", 3]]); |
|
|
|
|
|
|
|
|
|
|
|
var all = evaluate(ast[0][ast[0].length - 1], 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]);
|
|
|