Browse Source

Make all functions lambdas for now

master
Dylan Baker 5 years ago
parent
commit
b83126524d
5 changed files with 10 additions and 42 deletions
  1. 3
    4
      src/ast/index.js
  2. 1
    1
      src/ast/lambda.js
  3. 1
    1
      src/compiler.js
  4. 4
    18
      src/parser.js
  5. 1
    18
      test/parserTest.js

+ 3
- 4
src/ast/index.js View File

@@ -2,7 +2,7 @@ const Application = require("./application");
2 2
 const Attribute = require("./attribute");
3 3
 const Boolean = require("./boolean");
4 4
 const Identifier = require("./identifier");
5
-const FunctionDefinition = require("./functionDefinition");
5
+const Lambda = require("./lambda");
6 6
 const List = require("./list");
7 7
 const Number = require("./number");
8 8
 const String = require("./string");
@@ -13,10 +13,9 @@ module.exports = {
13 13
   Attribute: Attribute,
14 14
   Boolean: Boolean,
15 15
   Identifier: Identifier,
16
-  FunctionDefinition: FunctionDefinition,
16
+  Lambda: Lambda,
17 17
   List: List,
18 18
   Number: Number,
19 19
   String: String,
20
-  Symbol,
21
-  Symbol
20
+  Symbol: Symbol,
22 21
 };

src/ast/functionDefinition.js → src/ast/lambda.js View File

@@ -1,6 +1,6 @@
1 1
 const Node = require("./node");
2 2
 
3
-module.exports = class FunctionDefinition extends Node {
3
+module.exports = class Lambda extends Node {
4 4
   constructor(opts) {
5 5
     super(opts);
6 6
   }

+ 1
- 1
src/compiler.js View File

@@ -32,7 +32,7 @@ module.exports = class Compiler {
32 32
         if (node.function.constructor === AST.Identifier) {
33 33
           let f = this.env.get(node.function.name)
34 34
           return f(...node.args.map(arg => this.compileNode(arg)))
35
-        } else if (node.function.constructor === AST.FunctionDefinition) {
35
+        } else if (node.function.constructor === AST.Lambda) {
36 36
           let env = new Env(this.env)
37 37
           node.function.parameters.forEach((param, index) => {
38 38
             env.set(param.name, node.args[index])

+ 4
- 18
src/parser.js View File

@@ -51,12 +51,9 @@ module.exports = class Parser {
51 51
 
52 52
     let node
53 53
 
54
-    if (this.tokenStream.peek().value === 'define') {
54
+    if (this.tokenStream.peek().value === 'lambda') {
55 55
       this.tokenStream.eat(tokenTypes.IDENTIFIER)
56
-      node = this.functionDefinition()
57
-    } else if (this.tokenStream.peek().value === 'lambda') {
58
-      this.tokenStream.eat(tokenTypes.IDENTIFIER)
59
-      node = this.functionDefinition(true)
56
+      node = this.lambda(true)
60 57
     } else {
61 58
       node = new AST.Application()
62 59
 
@@ -87,17 +84,7 @@ module.exports = class Parser {
87 84
     })
88 85
   }
89 86
 
90
-  functionDefinition(anonymous = false) {
91
-    let name
92
-
93
-    if (anonymous) {
94
-      name = false
95
-    } else {
96
-      name = new AST.Identifier({
97
-        name: this.tokenStream.eat(tokenTypes.IDENTIFIER).value,
98
-      })
99
-    }
100
-
87
+  lambda(anonymous = false) {
101 88
     let parameters = []
102 89
 
103 90
     this.tokenStream.eat(tokenTypes.OPAREN)
@@ -116,8 +103,7 @@ module.exports = class Parser {
116 103
 
117 104
     this.tokenStream.eat(tokenTypes.CPAREN)
118 105
 
119
-    return new AST.FunctionDefinition({
120
-      name: name,
106
+    return new AST.Lambda({
121 107
       parameters: parameters,
122 108
       body: this.form(),
123 109
     })

+ 1
- 18
test/parserTest.js View File

@@ -52,30 +52,13 @@ test('allow empty strings', t => {
52 52
   ])
53 53
 })
54 54
 
55
-test('parses calls to define into a function definition', t => {
56
-  t.plan(1)
57
-  const tree = helpers.parse('(define plusOne (n) (+ n 1))')
58
-
59
-  t.deepEqual(tree, [
60
-    new AST.FunctionDefinition({
61
-      name: new AST.Identifier({ name: 'plusOne' }),
62
-      parameters: [new AST.Identifier({ name: 'n' })],
63
-      body: new AST.Application({
64
-        function: new AST.Identifier({ name: '+' }),
65
-        args: [new AST.Identifier({ name: 'n' }), new AST.Number({ value: 1 })],
66
-      }),
67
-    }),
68
-  ])
69
-})
70
-
71 55
 test('parse lambdas and expressions in function position', t => {
72 56
   t.plan(1)
73 57
   const tree = helpers.parse('((lambda (n) (+ n 1)) 5)')
74 58
 
75 59
   t.deepEqual(tree, [
76 60
     new AST.Application({
77
-      function: new AST.FunctionDefinition({
78
-        name: false,
61
+      function: new AST.Lambda({
79 62
         parameters: [new AST.Identifier({ name: 'n' })],
80 63
         body: new AST.Application({
81 64
           function: new AST.Identifier({ name: '+' }),

Loading…
Cancel
Save