2015-05-07 21:04:42 +03:00
|
|
|
#! /usr/bin/env crystal run
|
|
|
|
|
2018-10-27 23:20:36 +03:00
|
|
|
require "readline"
|
2015-05-07 21:04:42 +03:00
|
|
|
require "./reader"
|
|
|
|
require "./printer"
|
|
|
|
require "./types"
|
|
|
|
require "./env"
|
2015-05-10 22:14:17 +03:00
|
|
|
require "./core"
|
2015-05-12 05:33:03 +03:00
|
|
|
require "./error"
|
2015-05-07 21:04:42 +03:00
|
|
|
|
|
|
|
# Note:
|
|
|
|
# Employed downcase names because Crystal prohibits uppercase names for methods
|
|
|
|
|
2015-06-03 19:59:10 +03:00
|
|
|
module Mal
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def func_of(env, binds, body)
|
2018-10-27 23:20:36 +03:00
|
|
|
->(args : Array(Mal::Type)) {
|
2015-06-03 19:59:10 +03:00
|
|
|
new_env = Mal::Env.new(env, binds, args)
|
|
|
|
eval(body, new_env)
|
2018-10-27 23:20:36 +03:00
|
|
|
}.as(Mal::Func)
|
2015-06-03 19:59:10 +03:00
|
|
|
end
|
2015-05-07 21:04:42 +03:00
|
|
|
|
2015-06-03 19:59:10 +03:00
|
|
|
def eval_ast(ast, env)
|
2018-10-27 23:20:36 +03:00
|
|
|
return ast.map { |n| eval(n, env).as(Mal::Type) } if ast.is_a? Mal::List
|
2015-05-11 19:47:58 +03:00
|
|
|
|
2015-06-03 19:59:10 +03:00
|
|
|
val = ast.unwrap
|
2015-05-11 19:47:58 +03:00
|
|
|
|
2015-06-03 19:59:10 +03:00
|
|
|
Mal::Type.new case val
|
|
|
|
when Mal::Symbol
|
|
|
|
if e = env.get(val.str)
|
|
|
|
e
|
|
|
|
else
|
|
|
|
eval_error "'#{val.str}' not found"
|
|
|
|
end
|
|
|
|
when Mal::List
|
2018-10-27 23:20:36 +03:00
|
|
|
val.each_with_object(Mal::List.new) { |n, l| l << eval(n, env) }
|
2015-06-03 19:59:10 +03:00
|
|
|
when Mal::Vector
|
2018-10-27 23:20:36 +03:00
|
|
|
val.each_with_object(Mal::Vector.new) { |n, l| l << eval(n, env) }
|
2015-06-03 19:59:10 +03:00
|
|
|
when Mal::HashMap
|
2018-10-27 23:20:36 +03:00
|
|
|
val.each { |k, v| val[k] = eval(v, env) }
|
2015-06-03 19:59:10 +03:00
|
|
|
val
|
2015-05-07 21:04:42 +03:00
|
|
|
else
|
2015-06-03 19:59:10 +03:00
|
|
|
val
|
2015-05-07 21:04:42 +03:00
|
|
|
end
|
|
|
|
end
|
2015-05-13 17:03:03 +03:00
|
|
|
|
2015-06-03 19:59:10 +03:00
|
|
|
def eval_invocation(list, env)
|
|
|
|
f = eval(list.first, env).unwrap
|
|
|
|
eval_error "expected function symbol as the first symbol of list" unless f.is_a? Mal::Func
|
2018-10-27 23:20:36 +03:00
|
|
|
f.call eval_ast(list[1..-1].each_with_object(Mal::List.new) { |i, l| l << i }, env)
|
2015-06-03 19:59:10 +03:00
|
|
|
end
|
2015-05-07 21:04:42 +03:00
|
|
|
|
2015-06-03 19:59:10 +03:00
|
|
|
def read(str)
|
|
|
|
read_str str
|
|
|
|
end
|
2015-05-09 20:05:24 +03:00
|
|
|
|
2015-06-03 19:59:10 +03:00
|
|
|
def eval(ast, env)
|
|
|
|
list = ast.unwrap
|
|
|
|
|
2018-10-27 23:20:36 +03:00
|
|
|
return eval_ast(ast, env) unless list.is_a? Mal::List
|
2015-06-03 19:59:10 +03:00
|
|
|
return gen_type Mal::List if list.empty?
|
|
|
|
|
|
|
|
head = list.first.unwrap
|
|
|
|
|
|
|
|
Mal::Type.new case head
|
|
|
|
when Mal::Symbol
|
|
|
|
case head.str
|
|
|
|
when "def!"
|
|
|
|
eval_error "wrong number of argument for 'def!'" unless list.size == 3
|
|
|
|
a1 = list[1].unwrap
|
|
|
|
eval_error "1st argument of 'def!' must be symbol" unless a1.is_a? Mal::Symbol
|
|
|
|
env.set(a1.str, eval(list[2], env))
|
|
|
|
when "let*"
|
|
|
|
eval_error "wrong number of argument for 'def!'" unless list.size == 3
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
new_env = Mal::Env.new env
|
|
|
|
bindings.each_slice(2) do |binding|
|
|
|
|
key, value = binding
|
|
|
|
name = key.unwrap
|
|
|
|
eval_error "name of binding must be specified as symbol" unless name.is_a? Mal::Symbol
|
|
|
|
new_env.set(name.str, eval(value, new_env))
|
|
|
|
end
|
|
|
|
|
|
|
|
eval(list[2], new_env)
|
|
|
|
when "do"
|
|
|
|
list.shift 1
|
|
|
|
eval_ast(list, env).last
|
|
|
|
when "if"
|
|
|
|
cond = eval(list[1], env).unwrap
|
|
|
|
case cond
|
|
|
|
when Nil
|
|
|
|
list.size >= 4 ? eval(list[3], env) : nil
|
|
|
|
when false
|
2018-10-27 23:20:36 +03:00
|
|
|
list.size >= 4 ? eval(list[3], env) : nil
|
2015-06-03 19:59:10 +03:00
|
|
|
else
|
|
|
|
eval(list[2], env)
|
|
|
|
end
|
|
|
|
when "fn*"
|
|
|
|
# Note:
|
|
|
|
# If writing lambda expression here directly, compiler will fail to infer type of 'list'. (Error 'Nil for empty?')
|
|
|
|
func_of(env, list[1].unwrap, list[2])
|
2015-05-09 20:05:24 +03:00
|
|
|
else
|
2015-06-03 19:59:10 +03:00
|
|
|
eval_invocation(list, env)
|
2015-05-09 20:05:24 +03:00
|
|
|
end
|
2015-05-07 21:04:42 +03:00
|
|
|
else
|
2015-05-13 17:03:03 +03:00
|
|
|
eval_invocation(list, env)
|
2015-05-07 21:04:42 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-03 19:59:10 +03:00
|
|
|
def print(result)
|
|
|
|
pr_str(result, true)
|
|
|
|
end
|
2015-05-07 21:04:42 +03:00
|
|
|
|
2015-06-03 19:59:10 +03:00
|
|
|
def rep(str)
|
2018-10-27 23:20:36 +03:00
|
|
|
print(eval(read(str), REPL_ENV))
|
2015-06-03 19:59:10 +03:00
|
|
|
end
|
2015-05-07 21:04:42 +03:00
|
|
|
end
|
|
|
|
|
2018-10-27 23:20:36 +03:00
|
|
|
REPL_ENV = Mal::Env.new nil
|
|
|
|
Mal::NS.each { |k, v| REPL_ENV.set(k, Mal::Type.new(v)) }
|
2015-06-03 19:59:10 +03:00
|
|
|
Mal.rep "(def! not (fn* (a) (if a false true)))"
|
2015-05-10 22:14:17 +03:00
|
|
|
|
2018-10-27 23:20:36 +03:00
|
|
|
while line = Readline.readline("user> ")
|
2015-05-07 21:04:42 +03:00
|
|
|
begin
|
2015-06-03 19:59:10 +03:00
|
|
|
puts Mal.rep(line)
|
Test uncaught throw, catchless try* . Fix 46 impls.
Fixes made to: ada, c, chuck, clojure, coffee, common-lisp, cpp,
crystal, d, dart, elm, erlang, es6, factor, fsharp, gnu-smalltalk,
groovy, guile, haxe, hy, js, livescript, matlab, miniMAL, nasm, nim,
objc, objpascal, ocaml, perl, perl6, php, plsql, ps, python, r,
rpython, ruby, scheme, swift3, tcl, ts, vb, vimscript, wasm, yorick.
Catchless try* test is an optional test. Not all implementations
support catchless try* but a number were fixed so they at least don't
crash on catchless try*.
2018-12-03 22:20:44 +03:00
|
|
|
rescue e : Mal::RuntimeException
|
|
|
|
STDERR.puts "Error: #{pr_str(e.thrown, true)}"
|
2015-05-07 21:04:42 +03:00
|
|
|
rescue e
|
Test uncaught throw, catchless try* . Fix 46 impls.
Fixes made to: ada, c, chuck, clojure, coffee, common-lisp, cpp,
crystal, d, dart, elm, erlang, es6, factor, fsharp, gnu-smalltalk,
groovy, guile, haxe, hy, js, livescript, matlab, miniMAL, nasm, nim,
objc, objpascal, ocaml, perl, perl6, php, plsql, ps, python, r,
rpython, ruby, scheme, swift3, tcl, ts, vb, vimscript, wasm, yorick.
Catchless try* test is an optional test. Not all implementations
support catchless try* but a number were fixed so they at least don't
crash on catchless try*.
2018-12-03 22:20:44 +03:00
|
|
|
STDERR.puts "Error: #{e}"
|
2015-05-07 21:04:42 +03:00
|
|
|
end
|
|
|
|
end
|