Created
December 14, 2015 21:00
-
-
Save jeremiahbiard/15f211c4fffa908ea0eb to your computer and use it in GitHub Desktop.
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
| //Node Code Here | |
| //Add more files in the file editor. | |
| //Run the code, test it and share it. | |
| //app.js is your main file which will execute. | |
| //You can add more files and directories in the file Explorer in the left. | |
| "use strict"; | |
| var assert = require('assert'); | |
| var evalScheem = function (expr, env) { | |
| // Numbers evaluate to themselves | |
| if (typeof expr === 'number') { | |
| return expr; | |
| } | |
| // Strings are variable references | |
| if (typeof expr === 'string') { | |
| return env[expr]; | |
| } | |
| // Look at head of list for operation | |
| //console.log(expr[0]); | |
| switch (expr[0]) { | |
| case '+': | |
| return evalScheem(expr[1], env) + | |
| evalScheem(expr[2], env); | |
| case 'set!': | |
| env[expr[1]] = evalScheem(expr[2], env); | |
| return 0; | |
| case 'begin': | |
| // console.log(expr); | |
| while (expr[2]) { | |
| evalScheem(expr[1], env); | |
| expr = (expr.slice(1)) | |
| } | |
| return evalScheem(expr[1], env); | |
| } | |
| }; | |
| /* var prg = ['begin', | |
| ['define', 'x', 5], | |
| ['set!', 'x', ['+', 'x', 1]], | |
| ['+', 2, 'x']]; | |
| */ | |
| assert.equal( | |
| evalScheem(['begin', ['set!', 'x', 5], | |
| ['set!', 'x', ['+', 'y', 'x']], 'x'], {x:1, y:2}), 7); | |
| assert.equal(evalScheem(['begin', ['+', 2, 2]], {}), 4); | |
| assert.equal(evalScheem(['begin', 1, 2, 3]), 3) | |
| console.log('all tests passed'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment