Browse Source

Make sure all arguments get evaluated

master
Dylan Baker 5 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
     let evaluatedTree = []
10
     let evaluatedTree = []
11
 
11
 
12
     tree.forEach(node => {
12
     tree.forEach(node => {
13
-      let evaluatedNode = this.evalNode(node)
13
+      let evaluatedNode = this.evalNode(node, env)
14
 
14
 
15
       if (evaluatedNode) {
15
       if (evaluatedNode) {
16
         if (evaluatedNode.constructor === OsloError) {
16
         if (evaluatedNode.constructor === OsloError) {
63
             return this.evalNode(
63
             return this.evalNode(
64
               new AST.Application({
64
               new AST.Application({
65
                 function: env.get(node.function),
65
                 function: env.get(node.function),
66
-                args: node.args,
66
+                args: this.eval(node.args, env),
67
               }),
67
               }),
68
               env,
68
               env,
69
             )
69
             )
76
 
76
 
77
             return this.evalNode(node.function.body, innerEnv)
77
             return this.evalNode(node.function.body, innerEnv)
78
           case Function:
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
             args.unshift(this)
80
             args.unshift(this)
83
             return node.function.call(...args)
81
             return node.function.call(...args)
84
         }
82
         }

+ 15
- 0
test/evaluatorTest.js View File

58
     new OsloError({ line: 1, message: "Symbol 'x' is not bound" }),
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