mirror of
https://github.com/kanaka/mal.git
synced 2024-09-19 17:47:53 +03:00
crystal: fix early steps broken because of later refactoring
This commit is contained in:
parent
b9e204b19c
commit
725ee0bd91
1
Makefile
1
Makefile
@ -43,6 +43,7 @@ EXCLUDE_TESTS += test^ruby^step5 # test completes, even at 100,000
|
||||
EXCLUDE_TESTS += test^rust^step5 # no catching stack overflows
|
||||
EXCLUDE_TESTS += test^ocaml^step5 # test completes, even at 1,000,000
|
||||
EXCLUDE_TESTS += test^vb^step5 # completes at 10,000
|
||||
EXCLUDE_TESTS += test^crystal^step5 # test completes, even at 1,000,000
|
||||
|
||||
EXCLUDE_PERFS = perf^mal # TODO: fix this
|
||||
|
||||
|
@ -11,7 +11,8 @@ def pr_str(value, print_readably = true)
|
||||
when Mal::Func then "<function>"
|
||||
when Mal::Closure then "<closure>"
|
||||
when Mal::HashMap
|
||||
"{#{value.map{|k, v| "#{pr_str(k, print_readably)} #{pr_str(v, print_readably)}"}.join(" ")}}"
|
||||
# step1_read_print.cr requires specifying type
|
||||
"{#{value.map{|k, v| "#{pr_str(k, print_readably)} #{pr_str(v, print_readably)}" as String}.join(" ")}}"
|
||||
when String
|
||||
case
|
||||
when value.empty?()
|
||||
|
@ -14,10 +14,10 @@ end
|
||||
|
||||
def num_func(func)
|
||||
-> (args : Array(Mal::Type)) {
|
||||
x, y = args[0], args[1]
|
||||
x, y = args[0].unwrap, args[1].unwrap
|
||||
eval_error "invalid arguments" unless x.is_a?(Int32) && y.is_a?(Int32)
|
||||
func.call(x, y) as Mal::Type
|
||||
} as Mal::Func
|
||||
Mal::Type.new func.call(x, y)
|
||||
}
|
||||
end
|
||||
|
||||
$repl_env = {
|
||||
@ -25,19 +25,22 @@ $repl_env = {
|
||||
"-" => num_func(->(x : Int32, y : Int32){ x - y }),
|
||||
"*" => num_func(->(x : Int32, y : Int32){ x * y }),
|
||||
"/" => num_func(->(x : Int32, y : Int32){ x / y }),
|
||||
} of String => Mal::Type
|
||||
} of String => Mal::Func
|
||||
|
||||
def eval_ast(ast, env)
|
||||
def eval_ast(a, env)
|
||||
return a.map{|n| eval(n, env) as Mal::Type} if a.is_a? Mal::List
|
||||
return a unless a
|
||||
|
||||
ast = a.unwrap
|
||||
case ast
|
||||
when Mal::Symbol
|
||||
if env.has_key? ast.val
|
||||
env[ast.val]
|
||||
if env.has_key? ast.str
|
||||
env[ast.str]
|
||||
else
|
||||
eval_error "'#{ast.val}' not found"
|
||||
eval_error "'#{ast.str}' not found"
|
||||
end
|
||||
when Mal::List
|
||||
# ast.each_with_object(Mal::List.new){|n, l| l << eval(n, env) as Mal::Type}
|
||||
ast.map{|n| eval(n, env) as Mal::Type}
|
||||
ast.each_with_object(Mal::List.new){|n, l| l << eval(n, env)}
|
||||
when Mal::Vector
|
||||
ast.each_with_object(Mal::Vector.new){|n, l| l << eval(n, env)}
|
||||
when Mal::HashMap
|
||||
@ -51,23 +54,23 @@ def read(str)
|
||||
read_str str
|
||||
end
|
||||
|
||||
def eval(ast, env)
|
||||
case ast
|
||||
when Mal::List
|
||||
eval_error "empty list" if ast.empty?
|
||||
def eval(t, env)
|
||||
Mal::Type.new case ast = t.unwrap
|
||||
when Mal::List
|
||||
eval_error "empty list" if ast.empty?
|
||||
|
||||
f = eval_ast(ast[0], env)
|
||||
ast.shift(1)
|
||||
args = eval_ast(ast, env).each_with_object([] of Mal::Type){|e, a| a << e}
|
||||
f = eval_ast(ast.first, env)
|
||||
ast.shift(1)
|
||||
args = eval_ast(ast, env)
|
||||
|
||||
if f.is_a?(Mal::Func)
|
||||
f.call(args)
|
||||
else
|
||||
eval_error "expected function symbol as the first symbol of list"
|
||||
end
|
||||
if f.is_a?(Mal::Func)
|
||||
f.call(args)
|
||||
else
|
||||
eval_ast(ast, env)
|
||||
eval_error "expected function symbol as the first symbol of list"
|
||||
end
|
||||
else
|
||||
eval_ast(t, env)
|
||||
end
|
||||
end
|
||||
|
||||
def print(result)
|
||||
|
@ -15,33 +15,36 @@ end
|
||||
|
||||
def num_func(func)
|
||||
-> (args : Array(Mal::Type)) {
|
||||
x, y = args[0], args[1]
|
||||
x, y = args[0].unwrap, args[1].unwrap
|
||||
eval_error "invalid arguments" unless x.is_a?(Int32) && y.is_a?(Int32)
|
||||
func.call(x, y) as Mal::Type
|
||||
} as Mal::Func
|
||||
Mal::Type.new func.call(x, y)
|
||||
}
|
||||
end
|
||||
|
||||
$repl_env = Mal::Env.new nil
|
||||
$repl_env.set("+", num_func(->(x : Int32, y : Int32){ x + y }))
|
||||
$repl_env.set("-", num_func(->(x : Int32, y : Int32){ x - y }))
|
||||
$repl_env.set("*", num_func(->(x : Int32, y : Int32){ x * y }))
|
||||
$repl_env.set("/", num_func(->(x : Int32, y : Int32){ x / y }))
|
||||
$repl_env.set("+", Mal::Type.new num_func(->(x : Int32, y : Int32){ x + y }))
|
||||
$repl_env.set("-", Mal::Type.new num_func(->(x : Int32, y : Int32){ x - y }))
|
||||
$repl_env.set("*", Mal::Type.new num_func(->(x : Int32, y : Int32){ x * y }))
|
||||
$repl_env.set("/", Mal::Type.new num_func(->(x : Int32, y : Int32){ x / y }))
|
||||
|
||||
def eval_ast(ast, env)
|
||||
case ast
|
||||
def eval_ast(a, env)
|
||||
return a.map{|n| eval(n, env) } if a.is_a? Array
|
||||
|
||||
Mal::Type.new case ast = a.unwrap
|
||||
when Mal::Symbol
|
||||
if e = env.get(ast.val)
|
||||
if e = env.get(ast.str)
|
||||
e
|
||||
else
|
||||
eval_error "'#{ast.val}' not found"
|
||||
eval_error "'#{ast.str}' not found"
|
||||
end
|
||||
when Mal::List
|
||||
# ast.each_with_object(Mal::List.new){|n, l| l << eval(n, env) as Mal::Type}
|
||||
ast.map{|n| eval(n, env) as Mal::Type}
|
||||
ast.each_with_object(Mal::List.new){|n, l| l << eval(n, env)}
|
||||
when Mal::Vector
|
||||
ast.each_with_object(Mal::Vector.new){|n, l| l << eval(n, env)}
|
||||
when Mal::HashMap
|
||||
ast.each{|k, v| ast[k] = eval(v, env)}
|
||||
new_map = Mal::HashMap.new
|
||||
ast.each{|k, v| new_map[k] = eval(v, env)}
|
||||
new_map
|
||||
else
|
||||
ast
|
||||
end
|
||||
@ -51,42 +54,44 @@ def read(str)
|
||||
read_str str
|
||||
end
|
||||
|
||||
def eval(ast, env)
|
||||
return eval_ast(ast, env) unless ast.is_a?(Mal::List)
|
||||
def eval(t, env)
|
||||
ast = t.unwrap
|
||||
|
||||
return eval_ast(t, env) unless ast.is_a?(Mal::List)
|
||||
|
||||
eval_error "empty list" if ast.empty?
|
||||
|
||||
sym = ast.first
|
||||
sym = ast.first.unwrap
|
||||
eval_error "first element of list must be a symbol" unless sym.is_a?(Mal::Symbol)
|
||||
|
||||
case sym.val
|
||||
Mal::Type.new case sym.str
|
||||
when "def!"
|
||||
eval_error "wrong number of argument for 'def!'" unless ast.size == 3
|
||||
a1 = ast[1]
|
||||
a1 = ast[1].unwrap
|
||||
eval_error "1st argument of 'def!' must be symbol" unless a1.is_a?(Mal::Symbol)
|
||||
env.set(a1.val, eval(ast[2], env) as Mal::Type)
|
||||
env.set(a1.str, eval(ast[2], env) as Mal::Type)
|
||||
when "let*"
|
||||
eval_error "wrong number of argument for 'def!'" unless ast.size == 3
|
||||
|
||||
bindings = ast[1]
|
||||
bindings = ast[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?
|
||||
|
||||
new_env = Mal::Env.new env
|
||||
bindings.each_slice(2) do |binding|
|
||||
name, value = binding
|
||||
name, value = binding[0].unwrap, binding[1]
|
||||
eval_error "name of binding must be specified as symbol" unless name.is_a?(Mal::Symbol)
|
||||
new_env.set(name.val, eval(value, new_env))
|
||||
new_env.set(name.str, eval(value, new_env))
|
||||
end
|
||||
|
||||
eval(ast[2], new_env)
|
||||
else
|
||||
f = eval_ast(sym, env)
|
||||
f = eval_ast(ast.first, env)
|
||||
ast.shift(1)
|
||||
args = eval_ast(ast, env).each_with_object([] of Mal::Type){|e, a| a << e}
|
||||
args = eval_ast(ast, env)
|
||||
|
||||
if f.is_a?(Mal::Func)
|
||||
f.call(args)
|
||||
if f.is_a?(Mal::Type) && (f2 = f.unwrap).is_a?(Mal::Func)
|
||||
f2.call(args as Array(Mal::Type))
|
||||
else
|
||||
eval_error "expected function symbol as the first symbol of list"
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user