|
@ -40,90 +40,90 @@ module Crisp |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def func_of(env, binds, body) |
|
|
def func_of(env, binds, body) |
|
|
-> (args : Array(Crisp::Type)) { |
|
|
|
|
|
new_env = Crisp::Env.new(env, binds, args) |
|
|
|
|
|
eval(body, new_env) |
|
|
|
|
|
} as Crisp::Func |
|
|
|
|
|
|
|
|
-> (args : Array(Crisp::Type)) { |
|
|
|
|
|
new_env = Crisp::Env.new(env, binds, args) |
|
|
|
|
|
eval(body, new_env) |
|
|
|
|
|
} 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::Type} if ast.is_a? Array |
|
|
|
|
|
|
|
|
val = ast.unwrap |
|
|
|
|
|
|
|
|
val = ast.unwrap |
|
|
|
|
|
|
|
|
Crisp::Type.new case val |
|
|
|
|
|
when Crisp::Symbol |
|
|
|
|
|
|
|
|
Crisp::Type.new case val |
|
|
|
|
|
when Crisp::Symbol |
|
|
if e = env.get(val.str) |
|
|
if e = env.get(val.str) |
|
|
e |
|
|
e |
|
|
else |
|
|
else |
|
|
eval_error "'#{val.str}' not found" |
|
|
|
|
|
|
|
|
Crisp.eval_error "'#{val.str}' not found" |
|
|
end |
|
|
end |
|
|
when Crisp::List |
|
|
|
|
|
|
|
|
when Crisp::List |
|
|
val.each_with_object(Crisp::List.new){|n, l| l << eval(n, env)} |
|
|
val.each_with_object(Crisp::List.new){|n, l| l << eval(n, env)} |
|
|
when Crisp::Vector |
|
|
|
|
|
|
|
|
when Crisp::Vector |
|
|
val.each_with_object(Crisp::Vector.new){|n, l| l << eval(n, env)} |
|
|
val.each_with_object(Crisp::Vector.new){|n, l| l << eval(n, env)} |
|
|
when Crisp::HashMap |
|
|
|
|
|
|
|
|
when Crisp::HashMap |
|
|
new_map = Crisp::HashMap.new |
|
|
new_map = Crisp::HashMap.new |
|
|
val.each{|k, v| new_map[k] = eval(v, env)} |
|
|
val.each{|k, v| new_map[k] = eval(v, env)} |
|
|
new_map |
|
|
new_map |
|
|
else |
|
|
|
|
|
|
|
|
else |
|
|
val |
|
|
val |
|
|
end |
|
|
|
|
|
|
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def read(str) |
|
|
def read(str) |
|
|
read_str str |
|
|
|
|
|
|
|
|
read_str str |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
macro pair?(list) |
|
|
macro pair?(list) |
|
|
{{list}}.is_a?(Array) && !{{list}}.empty? |
|
|
|
|
|
|
|
|
{{list}}.is_a?(Array) && !{{list}}.empty? |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def quasiquote(ast) |
|
|
def quasiquote(ast) |
|
|
list = ast.unwrap |
|
|
|
|
|
|
|
|
list = ast.unwrap |
|
|
|
|
|
|
|
|
unless pair?(list) |
|
|
|
|
|
return Crisp::Type.new( |
|
|
|
|
|
Crisp::List.new << gen_type(Crisp::Symbol, "quote") << ast |
|
|
|
|
|
) |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
unless pair?(list) |
|
|
|
|
|
return Crisp::Type.new( |
|
|
|
|
|
Crisp::List.new << gen_type(Crisp::Symbol, "quote") << ast |
|
|
|
|
|
) |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
head = list.first.unwrap |
|
|
|
|
|
|
|
|
head = list.first.unwrap |
|
|
|
|
|
|
|
|
case |
|
|
|
|
|
# ("unquote" ...) |
|
|
|
|
|
when head.is_a?(Crisp::Symbol) && head.str == "unquote" |
|
|
|
|
|
|
|
|
case |
|
|
|
|
|
# ("unquote" ...) |
|
|
|
|
|
when head.is_a?(Crisp::Symbol) && head.str == "unquote" |
|
|
list[1] |
|
|
list[1] |
|
|
# (("splice-unquote" ...) ...) |
|
|
|
|
|
when pair?(head) && (arg0 = head.first.unwrap).is_a?(Crisp::Symbol) && arg0.str == "splice-unquote" |
|
|
|
|
|
|
|
|
# (("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 |
|
|
tail = Crisp::Type.new list[1..-1].to_crisp_value |
|
|
Crisp::Type.new( |
|
|
Crisp::Type.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 |
|
|
tail = Crisp::Type.new list[1..-1].to_crisp_value |
|
|
Crisp::Type.new( |
|
|
Crisp::Type.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 |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def macro_call?(ast, env) |
|
|
def macro_call?(ast, env) |
|
|
list = ast.unwrap |
|
|
|
|
|
return false unless list.is_a? Crisp::List |
|
|
|
|
|
return false if list.empty? |
|
|
|
|
|
|
|
|
list = ast.unwrap |
|
|
|
|
|
return false unless list.is_a? Crisp::List |
|
|
|
|
|
return false if list.empty? |
|
|
|
|
|
|
|
|
sym = list.first.unwrap |
|
|
|
|
|
return false unless sym.is_a? Crisp::Symbol |
|
|
|
|
|
|
|
|
sym = list.first.unwrap |
|
|
|
|
|
return false unless sym.is_a? Crisp::Symbol |
|
|
|
|
|
|
|
|
func = env.find(sym.str).try(&.data[sym.str]) |
|
|
|
|
|
return false unless func && func.macro? |
|
|
|
|
|
|
|
|
func = env.find(sym.str).try(&.data[sym.str]) |
|
|
|
|
|
return false unless func && func.macro? |
|
|
|
|
|
|
|
|
true |
|
|
|
|
|
|
|
|
true |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def macroexpand(ast, env) |
|
|
def macroexpand(ast, env) |
|
|
while macro_call?(ast, env) |
|
|
|
|
|
|
|
|
while macro_call?(ast, env) |
|
|
|
|
|
|
|
|
# Already checked in macro_call? |
|
|
# Already checked in macro_call? |
|
|
list = ast.unwrap as Crisp::List |
|
|
list = ast.unwrap as Crisp::List |
|
@ -132,41 +132,41 @@ module Crisp |
|
|
|
|
|
|
|
|
case func |
|
|
case func |
|
|
when Crisp::Func |
|
|
when Crisp::Func |
|
|
ast = func.call(list[1..-1]) |
|
|
|
|
|
|
|
|
ast = func.call(list[1..-1]) |
|
|
when Crisp::Closure |
|
|
when Crisp::Closure |
|
|
ast = func.fn.call(list[1..-1]) |
|
|
|
|
|
|
|
|
ast = func.fn.call(list[1..-1]) |
|
|
else |
|
|
else |
|
|
eval_error "macro '#{func_sym.str}' must be function: #{ast}" |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
Crisp.eval_error "macro '#{func_sym.str}' must be function: #{ast}" |
|
|
end |
|
|
end |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
ast |
|
|
|
|
|
|
|
|
ast |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
macro invoke_list(l, env) |
|
|
macro invoke_list(l, env) |
|
|
f = eval({{l}}.first, {{env}}).unwrap |
|
|
|
|
|
args = eval_ast({{l}}[1..-1], {{env}}) as Array |
|
|
|
|
|
|
|
|
f = eval({{l}}.first, {{env}}).unwrap |
|
|
|
|
|
args = eval_ast({{l}}[1..-1], {{env}}) as Array |
|
|
|
|
|
|
|
|
case f |
|
|
|
|
|
when Crisp::Closure |
|
|
|
|
|
|
|
|
case f |
|
|
|
|
|
when Crisp::Closure |
|
|
ast = f.ast |
|
|
ast = f.ast |
|
|
{{env}} = Crisp::Env.new(f.env, f.params, args) |
|
|
{{env}} = Crisp::Env.new(f.env, f.params, args) |
|
|
next # TCO |
|
|
next # TCO |
|
|
when Crisp::Func |
|
|
|
|
|
|
|
|
when Crisp::Func |
|
|
return f.call args |
|
|
return f.call args |
|
|
else |
|
|
|
|
|
eval_error "expected function as the first argument: #{f}" |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
else |
|
|
|
|
|
Crisp.eval_error "expected function as the first argument: #{f}" |
|
|
|
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def debug(ast) |
|
|
def debug(ast) |
|
|
puts print(ast).colorize.red |
|
|
|
|
|
|
|
|
puts print(ast).colorize.red |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def eval(ast, env) |
|
|
def eval(ast, env) |
|
|
# 'next' in 'do...end' has a bug in crystal 0.7.1 |
|
|
|
|
|
# https://github.com/manastech/crystal/issues/659 |
|
|
|
|
|
while true |
|
|
|
|
|
|
|
|
# 'next' in 'do...end' has a bug in crystal 0.7.1 |
|
|
|
|
|
# https://github.com/manastech/crystal/issues/659 |
|
|
|
|
|
while true |
|
|
return eval_ast(ast, env) unless ast.unwrap.is_a? Crisp::List |
|
|
return eval_ast(ast, env) unless ast.unwrap.is_a? Crisp::List |
|
|
|
|
|
|
|
|
ast = macroexpand(ast, env) |
|
|
ast = macroexpand(ast, env) |
|
@ -181,63 +181,63 @@ 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::Type.new case head.str |
|
|
when "def!" |
|
|
|
|
|
eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
|
|
|
|
|
|
|
|
when "def!" |
|
|
|
|
|
Crisp.eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
|
|
a1 = list[1].unwrap |
|
|
a1 = list[1].unwrap |
|
|
eval_error "1st argument of 'def!' must be symbol: #{a1}" unless a1.is_a? Crisp::Symbol |
|
|
|
|
|
|
|
|
Crisp.eval_error "1st argument of 'def!' must be symbol: #{a1}" unless a1.is_a? Crisp::Symbol |
|
|
env.set(a1.str, eval(list[2], env)) |
|
|
env.set(a1.str, eval(list[2], env)) |
|
|
when "let*" |
|
|
|
|
|
eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
|
|
|
|
|
|
|
|
when "let*" |
|
|
|
|
|
Crisp.eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
|
|
|
|
|
|
|
|
bindings = list[1].unwrap |
|
|
bindings = list[1].unwrap |
|
|
eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array |
|
|
|
|
|
eval_error "size of binding list must be even" unless bindings.size.even? |
|
|
|
|
|
|
|
|
Crisp.eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array |
|
|
|
|
|
Crisp.eval_error "size of binding list must be even" unless bindings.size.even? |
|
|
|
|
|
|
|
|
new_env = Crisp::Env.new env |
|
|
new_env = Crisp::Env.new env |
|
|
bindings.each_slice(2) do |binding| |
|
|
bindings.each_slice(2) do |binding| |
|
|
key, value = binding |
|
|
|
|
|
name = key.unwrap |
|
|
|
|
|
eval_error "name of binding must be specified as symbol #{name}" unless name.is_a? Crisp::Symbol |
|
|
|
|
|
new_env.set(name.str, eval(value, new_env)) |
|
|
|
|
|
|
|
|
key, value = binding |
|
|
|
|
|
name = key.unwrap |
|
|
|
|
|
class="n">Crisp class="o">.eval_error "name of binding must be specified as symbol #{name}" unless name.is_a? Crisp::Symbol |
|
|
|
|
|
new_env.set(name.str, eval(value, new_env)) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
ast, env = list[2], new_env |
|
|
ast, env = list[2], new_env |
|
|
next # TCO |
|
|
next # TCO |
|
|
when "do" |
|
|
|
|
|
|
|
|
when "do" |
|
|
if list.empty? |
|
|
if list.empty? |
|
|
ast = Crisp::Type.new nil |
|
|
|
|
|
next |
|
|
|
|
|
|
|
|
ast = Crisp::Type.new nil |
|
|
|
|
|
next |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
eval_ast(list[1..-2].to_crisp_value, env) |
|
|
eval_ast(list[1..-2].to_crisp_value, env) |
|
|
ast = list.last |
|
|
ast = list.last |
|
|
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::Type.new(nil) |
|
|
else |
|
|
else |
|
|
list[2] |
|
|
|
|
|
|
|
|
list[2] |
|
|
end |
|
|
end |
|
|
next # TCO |
|
|
next # TCO |
|
|
when "fn*" |
|
|
|
|
|
|
|
|
when "fn*" |
|
|
params = list[1].unwrap |
|
|
params = list[1].unwrap |
|
|
unless params.is_a? Array |
|
|
unless params.is_a? Array |
|
|
eval_error "'fn*' parameters must be list or vector: #{params}" |
|
|
|
|
|
|
|
|
class="n">Crisp class="o">.eval_error "'fn*' parameters must be list or vector: #{params}" |
|
|
end |
|
|
end |
|
|
Crisp::Closure.new(list[2], params, env, func_of(env, params, list[2])) |
|
|
Crisp::Closure.new(list[2], params, env, func_of(env, params, list[2])) |
|
|
when "quote" |
|
|
|
|
|
|
|
|
when "quote" |
|
|
list[1] |
|
|
list[1] |
|
|
when "quasiquote" |
|
|
|
|
|
|
|
|
when "quasiquote" |
|
|
ast = quasiquote list[1] |
|
|
ast = quasiquote list[1] |
|
|
next # TCO |
|
|
next # TCO |
|
|
when "defmacro!" |
|
|
|
|
|
eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3 |
|
|
|
|
|
|
|
|
when "defmacro!" |
|
|
|
|
|
Crisp.eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3 |
|
|
a1 = list[1].unwrap |
|
|
a1 = list[1].unwrap |
|
|
eval_error "1st argument of 'defmacro!' must be symbol: #{a1}" unless a1.is_a? Crisp::Symbol |
|
|
|
|
|
|
|
|
Crisp.eval_error "1st argument of 'defmacro!' must be symbol: #{a1}" unless a1.is_a? Crisp::Symbol |
|
|
env.set(a1.str, eval(list[2], env).tap{|n| n.is_macro = true}) |
|
|
env.set(a1.str, eval(list[2], env).tap{|n| n.is_macro = true}) |
|
|
when "macroexpand" |
|
|
|
|
|
|
|
|
when "macroexpand" |
|
|
macroexpand(list[1], env) |
|
|
macroexpand(list[1], env) |
|
|
when "try*" |
|
|
|
|
|
|
|
|
when "try*" |
|
|
catch_list = list[2].unwrap |
|
|
catch_list = list[2].unwrap |
|
|
return eval(list[1], env) unless catch_list.is_a? Crisp::List |
|
|
return eval(list[1], env) unless catch_list.is_a? Crisp::List |
|
|
|
|
|
|
|
@ -246,22 +246,22 @@ module Crisp |
|
|
return eval(list[1], env) unless catch_head.str == "catch*" |
|
|
return eval(list[1], env) unless catch_head.str == "catch*" |
|
|
|
|
|
|
|
|
begin |
|
|
begin |
|
|
eval(list[1], env) |
|
|
|
|
|
|
|
|
eval(list[1], env) |
|
|
rescue e : Crisp::RuntimeException |
|
|
rescue e : Crisp::RuntimeException |
|
|
new_env = Crisp::Env.new(env, [catch_list[1]], [e.thrown]) |
|
|
|
|
|
eval(catch_list[2], new_env) |
|
|
|
|
|
|
|
|
new_env = Crisp::Env.new(env, [catch_list[1]], [e.thrown]) |
|
|
|
|
|
eval(catch_list[2], new_env) |
|
|
rescue e |
|
|
rescue e |
|
|
new_env = Crisp::Env.new(env, [catch_list[1]], [Crisp::Type.new e.message]) |
|
|
|
|
|
eval(catch_list[2], new_env) |
|
|
|
|
|
|
|
|
new_env = Crisp::Env.new(env, [catch_list[1]], [Crisp::Type.new e.message]) |
|
|
|
|
|
eval(catch_list[2], new_env) |
|
|
end |
|
|
end |
|
|
else |
|
|
|
|
|
|
|
|
else |
|
|
invoke_list(list, env) |
|
|
invoke_list(list, env) |
|
|
end |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
end |
|
|
|
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def print(result) |
|
|
def print(result) |
|
|
@printer.print(result) |
|
|
|
|
|
|
|
|
@printer.print(result) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def eval_string(str) |
|
|
def eval_string(str) |
|
|