Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed function definitions

  • Loading branch information...
commit 02e61a8ef0233cf65e30c164b4b1fb494de9d7ce 1 parent d719927
Chris Parker authored February 27, 2012
18  lib/kalc/ast.rb
@@ -159,10 +159,6 @@ def eval(context)
159 159
     end
160 160
 
161 161
     class FunctionDefinition
162  
-      attr_reader :name
163  
-      attr_reader :argument_list
164  
-      attr_reader :body
165  
-
166 162
       def initialize(name, argument_list, body)
167 163
         @name = name
168 164
         @argument_list = argument_list
@@ -170,18 +166,16 @@ def initialize(name, argument_list, body)
170 166
       end
171 167
 
172 168
       def eval(context)
173  
-        context.add_function(@name.to_sym, lambda { |parent_context, *argument_list|
  169
+        context.add_function(@name.to_sym, lambda { |parent_context, *args|
  170
+          dup_body = @body.dup
174 171
           cxt = Environment.new(parent_context)
175  
-
176  
-          argument_list.each_with_index do |arg, idx|
177  
-            cxt.add_variable(argument_list[idx].value, arg.eval(cxt))
  172
+          args.each_with_index do |arg, idx|
  173
+            cxt.add_variable(@argument_list[idx].value, arg.eval(cxt))
178 174
           end
179  
-
180  
-          @body.eval(cxt)
  175
+          dup_body.eval(cxt)
181 176
         })
182  
-        context.get_function(@name)
  177
+        nil
183 178
       end
184 179
     end
185  
-
186 180
   end
187 181
 end
6  lib/kalc/grammar.rb
@@ -246,9 +246,9 @@ def self.operators(operators={})
246 246
   }
247 247
 
248 248
   rule(:function_definition_expression) {
249  
-    (str('DEF ') >> identifier.as(:name) >>
250  
-      paren_argument_list.as(:argument_list) >> 
251  
-      left_brace >> function_body.as(:body) >> right_brace).as(:function_definition) |
  249
+    (str('DEFINE') >> spaces? >> identifier.as(:name) >>
  250
+         paren_argument_list.as(:argument_list) >>
  251
+         left_brace >> function_body.as(:body) >> right_brace).as(:function_definition) |
252 252
     expressions.as(:expressions)
253 253
   }
254 254
 
20  lib/kalc/interpreter.rb
... ...
@@ -1,14 +1,13 @@
1 1
 # Code inspired by https://github.com/txus/schemer
2 2
 
  3
+require 'pp'
  4
+
3 5
 module Kalc
4 6
   class Interpreter
5 7
 
6 8
     attr_reader :env
7 9
 
8  
-    def initialize(ast = nil)
9  
-
10  
-      @ast = ast
11  
-
  10
+    def initialize
12 11
       @env = Environment.new do |env|
13 12
 
14 13
         env.add_function(:IF, lambda { |cxt, cond, if_true, if_false|
@@ -85,12 +84,19 @@ def initialize(ast = nil)
85 84
           })
86 85
         end
87 86
 
  87
+        env.add_function(:P, lambda { |cxt, *output|  
  88
+          p output
  89
+        })
  90
+
  91
+        env.add_function(:PP, lambda { |cxt, *output|  
  92
+          pp output
  93
+        })
  94
+
88 95
       end
89 96
     end
90 97
 
91  
-    def run(ast = nil)
92  
-      @ast = ast if ast
93  
-      @ast.eval(@env)
  98
+    def run(ast)
  99
+      ast.eval(@env)
94 100
     end
95 101
   end
96 102
 end

0 notes on commit 02e61a8

Please sign in to comment.
Something went wrong with that request. Please try again.