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
 const Attribute = require("./attribute");
2
 const Attribute = require("./attribute");
3
 const Boolean = require("./boolean");
3
 const Boolean = require("./boolean");
4
 const Identifier = require("./identifier");
4
 const Identifier = require("./identifier");
5
-const FunctionDefinition = require("./functionDefinition");
5
+const Lambda = require("./lambda");
6
 const List = require("./list");
6
 const List = require("./list");
7
 const Number = require("./number");
7
 const Number = require("./number");
8
 const String = require("./string");
8
 const String = require("./string");
13
   Attribute: Attribute,
13
   Attribute: Attribute,
14
   Boolean: Boolean,
14
   Boolean: Boolean,
15
   Identifier: Identifier,
15
   Identifier: Identifier,
16
-  FunctionDefinition: FunctionDefinition,
16
+  Lambda: Lambda,
17
   List: List,
17
   List: List,
18
   Number: Number,
18
   Number: Number,
19
   String: String,
19
   String: String,
20
-  Symbol,
21
-  Symbol
20
+  Symbol: Symbol,
22
 };
21
 };

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

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

+ 1
- 1
src/compiler.js View File

32
         if (node.function.constructor === AST.Identifier) {
32
         if (node.function.constructor === AST.Identifier) {
33
           let f = this.env.get(node.function.name)
33
           let f = this.env.get(node.function.name)
34
           return f(...node.args.map(arg => this.compileNode(arg)))
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
           let env = new Env(this.env)
36
           let env = new Env(this.env)
37
           node.function.parameters.forEach((param, index) => {
37
           node.function.parameters.forEach((param, index) => {
38
             env.set(param.name, node.args[index])
38
             env.set(param.name, node.args[index])

+ 4
- 18
src/parser.js View File

51
 
51
 
52
     let node
52
     let node
53
 
53
 
54
-    if (this.tokenStream.peek().value === 'define') {
54
+    if (this.tokenStream.peek().value === 'lambda') {
55
       this.tokenStream.eat(tokenTypes.IDENTIFIER)
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
     } else {
57
     } else {
61
       node = new AST.Application()
58
       node = new AST.Application()
62
 
59
 
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
     let parameters = []
88
     let parameters = []
102
 
89
 
103
     this.tokenStream.eat(tokenTypes.OPAREN)
90
     this.tokenStream.eat(tokenTypes.OPAREN)
116
 
103
 
117
     this.tokenStream.eat(tokenTypes.CPAREN)
104
     this.tokenStream.eat(tokenTypes.CPAREN)
118
 
105
 
119
-    return new AST.FunctionDefinition({
120
-      name: name,
106
+    return new AST.Lambda({
121
       parameters: parameters,
107
       parameters: parameters,
122
       body: this.form(),
108
       body: this.form(),
123
     })
109
     })

+ 1
- 18
test/parserTest.js View File

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
 test('parse lambdas and expressions in function position', t => {
55
 test('parse lambdas and expressions in function position', t => {
72
   t.plan(1)
56
   t.plan(1)
73
   const tree = helpers.parse('((lambda (n) (+ n 1)) 5)')
57
   const tree = helpers.parse('((lambda (n) (+ n 1)) 5)')
74
 
58
 
75
   t.deepEqual(tree, [
59
   t.deepEqual(tree, [
76
     new AST.Application({
60
     new AST.Application({
77
-      function: new AST.FunctionDefinition({
78
-        name: false,
61
+      function: new AST.Lambda({
79
         parameters: [new AST.Identifier({ name: 'n' })],
62
         parameters: [new AST.Identifier({ name: 'n' })],
80
         body: new AST.Application({
63
         body: new AST.Application({
81
           function: new AST.Identifier({ name: '+' }),
64
           function: new AST.Identifier({ name: '+' }),

Loading…
Cancel
Save