Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed issue with parsing end parens, fixed non-atomic symbol definitions

  • Loading branch information...
commit cdcf5658413ce6febb29f3b614b437a717dfe04b 1 parent 278d0f4
@cjhveal authored
Showing with 17 additions and 8 deletions.
  1. +17 −8 lispr.rb
View
25 lispr.rb
@@ -17,7 +17,8 @@ def set! var, val
end
def find var
- @dict[var] if @dict.has_key? var else @outer.find var rescue raise "Symbol '#{var}' not defined"
+ return @dict[var] if @dict.has_key? var
+ @outer.find var rescue raise "Symbol '#{var}' not defined"
end
end
GLOBALS = {:+ => lambda(&:+), :- => lambda(&:-), :* => lambda(&:*), :/ => lambda(&:/), :% => lambda(&:%),
@@ -29,7 +30,7 @@ def find var
@@global_env= Env.new GLOBALS
def run x, env=@@global_env
- if x.class == String
+ if x.class == Symbol
return env.find x
elsif x.class != Array
return x
@@ -40,10 +41,10 @@ def run x, env=@@global_env
return run (eval(test,env) ? conseq : alt), env
elsif x.first == :set!
(_, var, expr) = x
- env.set! var, expr
+ env.set! var, run(expr)
elsif x.first == :define
(_, var, expr) = x
- env.define var, expr
+ env.define var, run(expr)
elsif x.first == :lambda
(_, params, expr) = x
return lambda { |*args| run expr, (Env.new Hash[params.zip args], env) }
@@ -51,7 +52,7 @@ def run x, env=@@global_env
x[1..-1].map {|expr| run expr, env}.last
else
exprs = x.map {|expr| run expr, env}
- procedure = exprs.pop
+ procedure = exprs.shift
return procedure.call(*exprs)
end
end
@@ -71,12 +72,12 @@ def read_next tokens
if token == '('
branch = []
branch.push read_next tokens until tokens.first == ')'
- tokens.pop
- return branch
+ tokens.shift
+ branch
elsif token == ')'
raise "unexpected ')' token"
else
- return atomize(token);
+ atomize(token);
end
end
@@ -84,4 +85,12 @@ def atomize token
Integer(token) rescue Float(token) rescue token.to_sym
end
+ def repl
+ loop do
+ val = run read gets.chomp
+ puts val unless val.nil?
+ end
+ end
+
end
+include Lispr
Please sign in to comment.
Something went wrong with that request. Please try again.