Browse Source

Store function name on the function node

master
Dylan Baker 5 years ago
parent
commit
48b94af6f6
4 changed files with 8 additions and 5 deletions
  1. 5
    3
      lib/chervil/ast/function.rb
  2. 1
    1
      lib/chervil/parser.rb
  3. 1
    1
      spec/ast/function_spec.rb
  4. 1
    0
      spec/parser_spec.rb

+ 5
- 3
lib/chervil/ast/function.rb View File

@@ -1,15 +1,17 @@
1 1
 module Chervil::AST
2 2
   class Function
3
+    attr_reader :name
3 4
     attr_reader :params
4 5
     attr_reader :body
5 6
 
6
-    def initialize(params, body)
7
+    def initialize(name, params, body)
8
+      @name = name
7 9
       @params = params
8 10
       @body = body
9 11
     end
10 12
 
11 13
     def ==(other)
12
-      @params == other.params && @body == other.body
14
+      @name == other.name && @params == other.params && @body == other.body
13 15
     end
14 16
 
15 17
     def evaluate(env)
@@ -35,7 +37,7 @@ module Chervil::AST
35 37
     end
36 38
 
37 39
     def to_s
38
-      "<function>"
40
+      "<function: #{@name.name}(#{@params.map(&:name).join(' ')})>"
39 41
     end
40 42
   end
41 43
 end

+ 1
- 1
lib/chervil/parser.rb View File

@@ -66,7 +66,7 @@ module Chervil
66 66
 
67 67
         eat(:rparen)
68 68
 
69
-        AST::Definition.new(name, AST::Function.new(params, body))
69
+        AST::Definition.new(name, AST::Function.new(name, params, body))
70 70
       end
71 71
     end
72 72
 

+ 1
- 1
spec/ast/function_spec.rb View File

@@ -1,9 +1,9 @@
1 1
 module Chervil
2 2
   RSpec.describe AST::Definition do
3 3
     it 'evaluates its body with its arguments when called' do
4
-      env = Env.new
5 4
       expect(
6 5
         AST::Function.new(
6
+          AST::Identifier.new("square"),
7 7
           [
8 8
             AST::Identifier.new("x")
9 9
           ],

+ 1
- 0
spec/parser_spec.rb View File

@@ -71,6 +71,7 @@ module Chervil
71 71
         AST::Definition.new(
72 72
           AST::Identifier.new("square"),
73 73
           AST::Function.new(
74
+            AST::Identifier.new("square"),
74 75
             [
75 76
               AST::Identifier.new("x")
76 77
             ],

Loading…
Cancel
Save