Browse Source

Move core functions to their own module

master
Dylan Baker 4 years ago
parent
commit
0abc70adbd
2 changed files with 18 additions and 16 deletions
  1. 14
    0
      src/core.js
  2. 4
    16
      src/env.js

+ 14
- 0
src/core.js View File

@@ -0,0 +1,14 @@
1
+const AST = require('./ast')
2
+
3
+module.exports = {
4
+  '+': (a, b) => new AST.Number({ value: a.value + b.value }),
5
+  '-': (a, b) => new AST.Number({ value: a.value - b.value }),
6
+  '*': (a, b) => new AST.Number({ value: a.value * b.value }),
7
+  '/': (a, b) => new AST.Number({ value: a.value / b.value }),
8
+  '=': (a, b) => new AST.Boolean({ value: a.value === b.value }),
9
+  '>': (a, b) => new AST.Boolean({ value: a.value > b.value }),
10
+  '<': (a, b) => new AST.Boolean({ value: a.value < b.value }),
11
+  '>=': (a, b) => new AST.Boolean({ value: a.value >= b.value }),
12
+  '<=': (a, b) => new AST.Boolean({ value: a.value <= b.value }),
13
+  'list': (...args) => { return new AST.List({ elements: args }) },
14
+}

+ 4
- 16
src/env.js View File

@@ -1,4 +1,5 @@
1 1
 const AST = require('./ast')
2
+const core = require('./core')
2 3
 const OsloError = require('./osloError')
3 4
 
4 5
 module.exports = class Env {
@@ -10,22 +11,9 @@ module.exports = class Env {
10 11
         this.data[key] = parent.data[key]
11 12
       })
12 13
     } else {
13
-      this.data = {
14
-        '+': (a, b) => new AST.Number({ value: a.value + b.value }),
15
-        '-': (a, b) => new AST.Number({ value: a.value - b.value }),
16
-        '*': (a, b) => new AST.Number({ value: a.value * b.value }),
17
-        '/': (a, b) => new AST.Number({ value: a.value / b.value }),
18
-        '=': (a, b) => new AST.Boolean({ value: a.value === b.value }),
19
-        '>': (a, b) => new AST.Boolean({ value: a.value > b.value }),
20
-        '<': (a, b) => new AST.Boolean({ value: a.value < b.value }),
21
-        '>=': (a, b) => new AST.Boolean({ value: a.value >= b.value }),
22
-        '<=': (a, b) => new AST.Boolean({ value: a.value <= b.value }),
23
-        'list': function () {
24
-          return new AST.List({
25
-            elements: Object.keys(arguments).map(arg => arguments[arg])
26
-          })
27
-        }
28
-      }
14
+      Object.keys(core).forEach(key => {
15
+        this.data[key] = core[key]
16
+      })
29 17
     }
30 18
   }
31 19
 

Loading…
Cancel
Save