Permalink
Browse files

Fixed function definitions

  • Loading branch information...
1 parent d719927 commit 02e61a8ef0233cf65e30c164b4b1fb494de9d7ce @mrcsparker committed Feb 27, 2012
Showing with 22 additions and 22 deletions.
  1. +6 −12 lib/kalc/ast.rb
  2. +3 −3 lib/kalc/grammar.rb
  3. +13 −7 lib/kalc/interpreter.rb
View
18 lib/kalc/ast.rb
@@ -159,29 +159,23 @@ def eval(context)
end
class FunctionDefinition
- attr_reader :name
- attr_reader :argument_list
- attr_reader :body
-
def initialize(name, argument_list, body)
@name = name
@argument_list = argument_list
@body = body
end
def eval(context)
- context.add_function(@name.to_sym, lambda { |parent_context, *argument_list|
+ context.add_function(@name.to_sym, lambda { |parent_context, *args|
+ dup_body = @body.dup
cxt = Environment.new(parent_context)
-
- argument_list.each_with_index do |arg, idx|
- cxt.add_variable(argument_list[idx].value, arg.eval(cxt))
+ args.each_with_index do |arg, idx|
+ cxt.add_variable(@argument_list[idx].value, arg.eval(cxt))
end
-
- @body.eval(cxt)
+ dup_body.eval(cxt)
})
- context.get_function(@name)
+ nil
end
end
-
end
end
View
6 lib/kalc/grammar.rb
@@ -246,9 +246,9 @@ def self.operators(operators={})
}
rule(:function_definition_expression) {
- (str('DEF ') >> identifier.as(:name) >>
- paren_argument_list.as(:argument_list) >>
- left_brace >> function_body.as(:body) >> right_brace).as(:function_definition) |
+ (str('DEFINE') >> spaces? >> identifier.as(:name) >>
+ paren_argument_list.as(:argument_list) >>
+ left_brace >> function_body.as(:body) >> right_brace).as(:function_definition) |
expressions.as(:expressions)
}
View
20 lib/kalc/interpreter.rb
@@ -1,14 +1,13 @@
# Code inspired by https://github.com/txus/schemer
+require 'pp'
+
module Kalc
class Interpreter
attr_reader :env
- def initialize(ast = nil)
-
- @ast = ast
-
+ def initialize
@env = Environment.new do |env|
env.add_function(:IF, lambda { |cxt, cond, if_true, if_false|
@@ -85,12 +84,19 @@ def initialize(ast = nil)
})
end
+ env.add_function(:P, lambda { |cxt, *output|
+ p output
+ })
+
+ env.add_function(:PP, lambda { |cxt, *output|
+ pp output
+ })
+
end
end
- def run(ast = nil)
- @ast = ast if ast
- @ast.eval(@env)
+ def run(ast)
+ ast.eval(@env)
end
end
end

0 comments on commit 02e61a8

Please sign in to comment.