Permalink
Browse files

more tweaks for correctness,

added more examples to make sure proper results are being interpreted
  • Loading branch information...
1 parent 09b6d96 commit ad316bbe61c6fa3262669df9bcd3ffeb04a52e3f Ezra Zygmuntowicz committed Nov 24, 2010
Showing with 23 additions and 20 deletions.
  1. +1 −1 bin/ez-scheme
  2. +10 −5 lib/ez-scheme/builtins.rb
  3. +2 −1 lib/ez-scheme/environment.rb
  4. +4 −3 lib/ez-scheme/expr.rb
  5. +6 −10 lib/ez-scheme/interpreter.rb
View
@@ -2,7 +2,7 @@ require 'ez-scheme/interpreter'
if file = ARGV[0]
inp = IO.read(file)
- interpret_code(inp)
+ interactive_interpreter(interpret_code(inp))
else
interactive_interpreter()
end
View
@@ -90,7 +90,7 @@ def builtin_eqv(args)
#
left, right = args[0], args[1]
- if left.instance_of(Pair) and right.instance_of(Pair)
+ if left.instance_of?(Pair) and right.instance_of?(Pair)
Boolean.new(left.object_id == right.object_id)
else
Boolean.new(left == right)
@@ -134,16 +134,21 @@ def builtin_or(args)
end
def make_comparison_operator_builtin(op)
- lambda{ |args|
+ Proc.new{ |args|
a = args[0]
+ res = nil
for b in args[1..-1]
- if send(op, a.value, b.value)
+ if a.value.send(op, b.value)
a = b
else
- return Boolean.new(false)
+ break res = Boolean.new(false)
end
end
- Boolean.new(true)
+ if res
+ res
+ else
+ Boolean.new(true)
+ end
}
end
@@ -30,7 +30,8 @@ def lookup_var(var)
elsif ! @parent.nil?
return @parent.lookup_var(var)
else
- raise Unbound.new("unbound variable '#{var}'")
+ return nil
+ #raise Unbound.new("unbound variable '#{var}'")
end
end
View
@@ -111,7 +111,8 @@ def repr_rec(obj)
elsif obj.instance_of?(Array)
obj.map {|o| repr_rec(o) }.join("\n")
else
- raise ExprError.new("Unexpected type: #{obj.class}")
+ obj.class.to_s
+ #raise ExprError.new("Unexpected type: #{obj.class}")
end
end
@@ -408,8 +409,8 @@ def convert_let_to_application(exp)
bindings = let_bindings(exp)
while ! bindings.nil?
- vars.append(bindings.first.first)
- vals.append(bindings.first.second.first)
+ vars << bindings.first.first
+ vals << bindings.first.second.first
bindings = bindings.second
end
lambda_expr = make_lambda(make_nested_pairs(*vars), let_body(exp))
@@ -4,7 +4,7 @@
require File.dirname(__FILE__) + '/builtins'
require 'pp'
-DEBUG = true
+DEBUG = ENV['DEBUG']
class Procedure
@@ -87,7 +87,6 @@ def _eval(expr, env)
elsif is_begin(expr)
return _eval_sequence(begin_actions(expr), env)
elsif is_application(expr)
- puts "eval apply"
return _apply(_eval(application_operator(expr), env),
_list_of_values(application_operands(expr), env))
else
@@ -123,22 +122,18 @@ def _list_of_values(exprs, env)
def _apply(proc, args)
# Standard Scheme apply (SICP 4.1.1)
#
- if DEBUG
- puts("~~~~ Applying procedure #{proc}")
- puts(" with args #{expr_repr(args)}")
- end
-
if proc.instance_of?(BuiltinProcedure)
if DEBUG
puts("~~~~ Applying builtin procedure: #{proc.name}")
+ puts(" with args #{expr_repr(args)}")
end
# The '' builtin gets the current output stream as a custom
# argument
#
return proc.apply(expand_nested_pairs(args))
elsif proc.instance_of?(Procedure)
if DEBUG
- puts("~~~~ Applying procedure with args: #{proc.args}")
+ puts("~~~~ Applying procedure with args: #{expr_repr(proc.args)}")
puts(" and body:\n#{expr_repr(proc.body)}")
end
return _eval_sequence(proc.body,
@@ -201,12 +196,13 @@ def interpret_code(code_str, output_stream=nil)
for expr in parsed_exprs
interp.interpret(expr)
end
+ interp
end
-def interactive_interpreter()
+def interactive_interpreter(interp=nil)
# Interactive interpreter
- interp = ::SchemeInterpreter.new # by default output_stream is sys.stdout
+ interp ||= ::SchemeInterpreter.new # by default output_stream is sys.stdout
parser = ::SchemeParser.new
puts("Type a Scheme expression or 'quit'")

0 comments on commit ad316bb

Please sign in to comment.