Browse Source

Core functions should return Oslo types, not JS types

master
Dylan Baker 5 years ago
parent
commit
4d957c60a0
3 changed files with 19 additions and 15 deletions
  1. 3
    1
      src/compiler.js
  2. 11
    9
      src/env.js
  3. 5
    5
      test/coreTest.js

+ 3
- 1
src/compiler.js View File

@@ -25,6 +25,8 @@ module.exports = class Compiler {
25 25
       case AST.Number:
26 26
       case AST.String:
27 27
         return node.value
28
+      case AST.Boolean:
29
+        return node.value === true ? '#t' : '#f'
28 30
       case AST.Identifier:
29 31
         return this.compileNode(this.env.get(node.name))
30 32
       case AST.Conditional:
@@ -38,7 +40,7 @@ module.exports = class Compiler {
38 40
       case AST.Application:
39 41
         if (node.function.constructor === AST.Identifier) {
40 42
           let f = this.env.get(node.function.name)
41
-          return f(...node.args.map(arg => this.compileNode(arg)))
43
+          return this.compileNode(f(...node.args.map(arg => this.compileNode(arg))))
42 44
         } else if (node.function.constructor === AST.Lambda) {
43 45
           let env = new Env(this.env)
44 46
           node.function.parameters.forEach((param, index) => {

+ 11
- 9
src/env.js View File

@@ -1,18 +1,20 @@
1
+const AST = require('../src/ast')
2
+
1 3
 module.exports = class Env {
2 4
   constructor(parent = null) {
3 5
     if (parent) {
4 6
       this.data = parent.data
5 7
     } else {
6 8
       this.data = {
7
-        '+': (a, b) => a + b,
8
-        '-': (a, b) => a - b,
9
-        '*': (a, b) => a * b,
10
-        '/': (a, b) => a / b,
11
-        '=': (a, b)  => a === b,
12
-        '>': (a, b)  => a > b,
13
-        '<': (a, b)  => a < b,
14
-        '>=': (a, b) => a >= b,
15
-        '<=': (a, b) => a <= b,
9
+        '+': (a, b) => new AST.Number({ value: a + b }),
10
+        '-': (a, b) => new AST.Number({ value: a - b }),
11
+        '*': (a, b) => new AST.Number({ value: a * b }),
12
+        '/': (a, b) => new AST.Number({ value: a / b }),
13
+        '=': (a, b) => new AST.Boolean({ value: a === b }),
14
+        '>': (a, b) => new AST.Boolean({ value: a > b }),
15
+        '<': (a, b) => new AST.Boolean({ value: a < b }),
16
+        '>=': (a, b) => new AST.Boolean({ value: a >= b }),
17
+        '<=': (a, b) => new AST.Boolean({ value: a <= b }),
16 18
       }
17 19
     }
18 20
   }

+ 5
- 5
test/coreTest.js View File

@@ -5,17 +5,17 @@ test('comparison functions', t => {
5 5
   t.plan(5)
6 6
 
7 7
   let result = helpers.compile('(= 1 2)')
8
-  t.equal(result, 'false')
8
+  t.equal(result, '#f')
9 9
 
10 10
   result = helpers.compile('(> 5 4)')
11
-  t.equal(result, 'true')
11
+  t.equal(result, '#t')
12 12
 
13 13
   result = helpers.compile('(< 58 10)')
14
-  t.equal(result, 'false')
14
+  t.equal(result, '#f')
15 15
 
16 16
   result = helpers.compile('(>= 5 5)')
17
-  t.equal(result, 'true')
17
+  t.equal(result, '#t')
18 18
 
19 19
   result = helpers.compile('(<= 10 10)')
20
-  t.equal(result, 'true')
20
+  t.equal(result, '#t')
21 21
 })

Loading…
Cancel
Save