Browse Source

Make sure all arguments get evaluated

master
Dylan Baker 4 years ago
parent
commit
8821f2368e
2 changed files with 18 additions and 5 deletions
  1. 3
    5
      src/evaluator.js
  2. 15
    0
      test/evaluatorTest.js

+ 3
- 5
src/evaluator.js View File

@@ -10,7 +10,7 @@ module.exports = class Evaluator {
10 10
     let evaluatedTree = []
11 11
 
12 12
     tree.forEach(node => {
13
-      let evaluatedNode = this.evalNode(node)
13
+      let evaluatedNode = this.evalNode(node, env)
14 14
 
15 15
       if (evaluatedNode) {
16 16
         if (evaluatedNode.constructor === OsloError) {
@@ -63,7 +63,7 @@ module.exports = class Evaluator {
63 63
             return this.evalNode(
64 64
               new AST.Application({
65 65
                 function: env.get(node.function),
66
-                args: node.args,
66
+                args: this.eval(node.args, env),
67 67
               }),
68 68
               env,
69 69
             )
@@ -76,9 +76,7 @@ module.exports = class Evaluator {
76 76
 
77 77
             return this.evalNode(node.function.body, innerEnv)
78 78
           case Function:
79
-            let args = node.args.map(arg => {
80
-              return this.evalNode(arg, env)
81
-            })
79
+            let args = this.eval(node.args, env)
82 80
             args.unshift(this)
83 81
             return node.function.call(...args)
84 82
         }

+ 15
- 0
test/evaluatorTest.js View File

@@ -58,3 +58,18 @@ test('let bindings go out of scope after the let block', t => {
58 58
     new OsloError({ line: 1, message: "Symbol 'x' is not bound" }),
59 59
   )
60 60
 })
61
+
62
+test('factorial', t => {
63
+  t.plan(1)
64
+
65
+  const tree = helpers.evaluate(`
66
+    (define factorial
67
+      (lambda (x)
68
+        (if (= x 0)
69
+          1
70
+          (* x (factorial (- x 1))))))
71
+    (factorial 5)
72
+
73
+  `)
74
+  t.deepEqual(tree[0], new AST.Number({ value: 120 }))
75
+})

Loading…
Cancel
Save