|
@ -12,18 +12,18 @@ module Crisp |
|
|
|
|
|
|
|
|
class Evaluator |
|
|
class Evaluator |
|
|
def func_of(env, binds, body) |
|
|
def func_of(env, binds, body) |
|
|
-> (args : Array(Crisp::Type)) { |
|
|
|
|
|
|
|
|
-> (args : Array(Crisp::Expr)) { |
|
|
new_env = Crisp::Env.new(env, binds, args) |
|
|
new_env = Crisp::Env.new(env, binds, args) |
|
|
eval(body, new_env) |
|
|
eval(body, new_env) |
|
|
} as Crisp::Func |
|
|
} as Crisp::Func |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def eval_ast(ast, env) |
|
|
def eval_ast(ast, env) |
|
|
return ast.map{|n| eval(n, env) as Crisp::Type} if ast.is_a? Array |
|
|
|
|
|
|
|
|
return ast.map{|n| eval(n, env) as Crisp::Expr} if ast.is_a? Array |
|
|
|
|
|
|
|
|
val = ast.unwrap |
|
|
val = ast.unwrap |
|
|
|
|
|
|
|
|
Crisp::Type.new case val |
|
|
|
|
|
|
|
|
Crisp::Expr.new case val |
|
|
when Crisp::Symbol |
|
|
when Crisp::Symbol |
|
|
if e = env.get(val.str) |
|
|
if e = env.get(val.str) |
|
|
e |
|
|
e |
|
@ -51,7 +51,7 @@ module Crisp |
|
|
list = ast.unwrap |
|
|
list = ast.unwrap |
|
|
|
|
|
|
|
|
unless pair?(list) |
|
|
unless pair?(list) |
|
|
return Crisp::Type.new( |
|
|
|
|
|
|
|
|
return Crisp::Expr.new( |
|
|
Crisp::List.new << gen_type(Crisp::Symbol, "quote") << ast |
|
|
Crisp::List.new << gen_type(Crisp::Symbol, "quote") << ast |
|
|
) |
|
|
) |
|
|
end |
|
|
end |
|
@ -64,13 +64,13 @@ module Crisp |
|
|
list[1] |
|
|
list[1] |
|
|
# (("splice-unquote" ...) ...) |
|
|
# (("splice-unquote" ...) ...) |
|
|
when pair?(head) && (arg0 = head.first.unwrap).is_a?(Crisp::Symbol) && arg0.str == "splice-unquote" |
|
|
when pair?(head) && (arg0 = head.first.unwrap).is_a?(Crisp::Symbol) && arg0.str == "splice-unquote" |
|
|
tail = Crisp::Type.new list[1..-1].to_crisp_value |
|
|
|
|
|
Crisp::Type.new( |
|
|
|
|
|
|
|
|
tail = Crisp::Expr.new list[1..-1].to_crisp_value |
|
|
|
|
|
Crisp::Expr.new( |
|
|
Crisp::List.new << gen_type(Crisp::Symbol, "concat") << head[1] << quasiquote(tail) |
|
|
Crisp::List.new << gen_type(Crisp::Symbol, "concat") << head[1] << quasiquote(tail) |
|
|
) |
|
|
) |
|
|
else |
|
|
else |
|
|
tail = Crisp::Type.new list[1..-1].to_crisp_value |
|
|
|
|
|
Crisp::Type.new( |
|
|
|
|
|
|
|
|
tail = Crisp::Expr.new list[1..-1].to_crisp_value |
|
|
|
|
|
Crisp::Expr.new( |
|
|
Crisp::List.new << gen_type(Crisp::Symbol, "cons") << quasiquote(list.first) << quasiquote(tail) |
|
|
Crisp::List.new << gen_type(Crisp::Symbol, "cons") << quasiquote(list.first) << quasiquote(tail) |
|
|
) |
|
|
) |
|
|
end |
|
|
end |
|
@ -148,7 +148,7 @@ module Crisp |
|
|
|
|
|
|
|
|
return invoke_list(list, env) unless head.is_a? Crisp::Symbol |
|
|
return invoke_list(list, env) unless head.is_a? Crisp::Symbol |
|
|
|
|
|
|
|
|
return Crisp::Type.new case head.str |
|
|
|
|
|
|
|
|
return Crisp::Expr.new case head.str |
|
|
when "def!" |
|
|
when "def!" |
|
|
Crisp.eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
|
|
Crisp.eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
|
|
a1 = list[1].unwrap |
|
|
a1 = list[1].unwrap |
|
@ -173,7 +173,7 @@ module Crisp |
|
|
next # TCO |
|
|
next # TCO |
|
|
when "do" |
|
|
when "do" |
|
|
if list.empty? |
|
|
if list.empty? |
|
|
ast = Crisp::Type.new nil |
|
|
|
|
|
|
|
|
ast = Crisp::Expr.new nil |
|
|
next |
|
|
next |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
@ -182,7 +182,7 @@ module Crisp |
|
|
next # TCO |
|
|
next # TCO |
|
|
when "if" |
|
|
when "if" |
|
|
ast = unless eval(list[1], env).unwrap |
|
|
ast = unless eval(list[1], env).unwrap |
|
|
list.size >= 4 ? list[3] : Crisp::Type.new(nil) |
|
|
|
|
|
|
|
|
list.size >= 4 ? list[3] : Crisp::Expr.new(nil) |
|
|
else |
|
|
else |
|
|
list[2] |
|
|
list[2] |
|
|
end |
|
|
end |
|
@ -219,7 +219,7 @@ module Crisp |
|
|
new_env = Crisp::Env.new(env, [catch_list[1]], [e.thrown]) |
|
|
new_env = Crisp::Env.new(env, [catch_list[1]], [e.thrown]) |
|
|
eval(catch_list[2], new_env) |
|
|
eval(catch_list[2], new_env) |
|
|
rescue e |
|
|
rescue e |
|
|
new_env = Crisp::Env.new(env, [catch_list[1]], [Crisp::Type.new e.message]) |
|
|
|
|
|
|
|
|
new_env = Crisp::Env.new(env, [catch_list[1]], [Crisp::Expr.new e.message]) |
|
|
eval(catch_list[2], new_env) |
|
|
eval(catch_list[2], new_env) |
|
|
end |
|
|
end |
|
|
else |
|
|
else |
|
|