1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 10:37:58 +03:00
mal/crystal/step8_macros.cr

259 lines
7.0 KiB
Crystal
Raw Normal View History

2015-05-24 21:49:53 +03:00
#! /usr/bin/env crystal run
2018-10-27 23:20:36 +03:00
require "readline"
2015-05-24 21:49:53 +03:00
require "./reader"
require "./printer"
require "./types"
require "./env"
require "./core"
require "./error"
# 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-24 21:49:53 +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-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
val = ast.unwrap
2015-05-24 21:49:53 +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 Array(Mal::Type)
2018-10-27 23:20:36 +03:00
val.map { |n| eval(n, env).as(Mal::Type) }
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-24 21:49:53 +03:00
else
2015-06-03 19:59:10 +03:00
val
2015-05-24 21:49:53 +03:00
end
end
2015-06-03 19:59:10 +03:00
def read(str)
read_str str
2015-05-24 21:49:53 +03:00
end
2015-06-03 19:59:10 +03:00
macro pair?(list)
{{list}}.is_a?(Array) && !{{list}}.empty?
2015-05-24 21:49:53 +03:00
end
2015-06-03 19:59:10 +03:00
def quasiquote(ast)
list = ast.unwrap
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
unless pair?(list)
return Mal::Type.new(
Mal::List.new << gen_type(Mal::Symbol, "quote") << ast
)
end
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
head = list.first.unwrap
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
case
# ("unquote" ...)
when head.is_a?(Mal::Symbol) && head.str == "unquote"
list[1]
2018-10-27 23:20:36 +03:00
# (("splice-unquote" ...) ...)
2015-06-03 19:59:10 +03:00
when pair?(head) && (arg0 = head.first.unwrap).is_a?(Mal::Symbol) && arg0.str == "splice-unquote"
2018-10-27 23:20:36 +03:00
tail = Mal::Type.new list[1..-1].each_with_object(Mal::List.new) { |e, l| l << e }
2015-06-03 19:59:10 +03:00
Mal::Type.new(
Mal::List.new << gen_type(Mal::Symbol, "concat") << head[1] << quasiquote(tail)
)
2015-05-24 21:49:53 +03:00
else
2018-10-27 23:20:36 +03:00
tail = Mal::Type.new list[1..-1].each_with_object(Mal::List.new) { |e, l| l << e }
2015-06-03 19:59:10 +03:00
Mal::Type.new(
Mal::List.new << gen_type(Mal::Symbol, "cons") << quasiquote(list.first) << quasiquote(tail)
)
2015-05-24 21:49:53 +03:00
end
end
2015-06-03 19:59:10 +03:00
def macro_call?(ast, env)
list = ast.unwrap
return false unless list.is_a? Mal::List
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
sym = list.first.unwrap
return false unless sym.is_a? Mal::Symbol
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
func = env.find(sym.str).try(&.data[sym.str])
return false unless func && func.macro?
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
true
end
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
def macroexpand(ast, env)
while macro_call?(ast, env)
# Already checked in macro_call?
2018-10-27 23:20:36 +03:00
list = ast.unwrap.as(Mal::List)
func_sym = list[0].unwrap.as(Mal::Symbol)
2015-06-03 19:59:10 +03:00
func = env.get(func_sym.str).unwrap
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
case func
when Mal::Func
ast = func.call(list[1..-1])
when Mal::Closure
ast = func.fn.call(list[1..-1])
else
eval_error "macro '#{func_sym.str}' must be function: #{ast}"
end
end
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
ast
end
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
macro invoke_list(l, env)
f = eval({{l}}.first, {{env}}).unwrap
args = eval_ast({{l}}[1..-1].each_with_object(Mal::List.new){|i, l| l << i}, {{env}})
case f
when Mal::Closure
ast = f.ast
{{env}} = Mal::Env.new(f.env, f.params, args)
next # TCO
when Mal::Func
return f.call args
else
eval_error "expected function as the first argument: #{f}"
end
end
2015-05-24 21:49:53 +03:00
2015-06-03 19:59:10 +03:00
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
return eval_ast(ast, env) unless ast.unwrap.is_a? Mal::List
ast = macroexpand(ast, env)
list = ast.unwrap
return eval_ast(ast, env) unless list.is_a? Mal::List
2015-06-03 19:59:10 +03:00
return ast if list.empty?
head = list.first.unwrap
return invoke_list(list, env) unless head.is_a? Mal::Symbol
return Mal::Type.new case head.str
2018-10-27 23:20:36 +03:00
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: #{a1}" 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 #{name}" unless name.is_a? Mal::Symbol
new_env.set(name.str, eval(value, new_env))
end
ast, env = list[2], new_env
next # TCO
when "do"
if list.empty?
ast = Mal::Type.new nil
next
end
eval_ast(list[1..-2].each_with_object(Mal::List.new) { |i, l| l << i }, env)
ast = list.last
next # TCO
when "if"
ast = unless eval(list[1], env).unwrap
list.size >= 4 ? list[3] : Mal::Type.new(nil)
2015-05-24 21:49:53 +03:00
else
2018-10-27 23:20:36 +03:00
list[2]
end
next # TCO
when "fn*"
params = list[1].unwrap
unless params.is_a? Array
eval_error "'fn*' parameters must be list or vector: #{params}"
2015-05-24 21:49:53 +03:00
end
2018-10-27 23:20:36 +03:00
Mal::Closure.new(list[2], params, env, func_of(env, params, list[2]))
when "quote"
list[1]
when "quasiquote"
ast = quasiquote list[1]
next # TCO
when "defmacro!"
eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3
a1 = list[1].unwrap
eval_error "1st argument of 'defmacro!' must be symbol: #{a1}" unless a1.is_a? Mal::Symbol
env.set(a1.str, eval(list[2], env).tap { |n| n.is_macro = true })
when "macroexpand"
macroexpand(list[1], env)
else
invoke_list(list, env)
end
2015-06-03 19:59:10 +03:00
end
2015-05-24 21:49:53 +03:00
end
2015-06-03 19:59:10 +03:00
def print(result)
pr_str(result, true)
end
2015-05-24 21:49:53 +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-24 21:49:53 +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)) }
REPL_ENV.set("eval", Mal::Type.new ->(args : Array(Mal::Type)) { Mal.eval(args[0], REPL_ENV) })
2015-06-03 19:59:10 +03:00
Mal.rep "(def! not (fn* (a) (if a false true)))"
Mal.rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
Mal.rep "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"
Mal.rep "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"
2015-05-24 21:49:53 +03:00
2018-10-27 23:20:36 +03:00
argv = Mal::List.new
REPL_ENV.set("*ARGV*", Mal::Type.new argv)
2015-05-24 21:49:53 +03:00
unless ARGV.empty?
if ARGV.size > 1
ARGV[1..-1].each do |a|
2018-10-27 23:20:36 +03:00
argv << Mal::Type.new(a)
2015-05-24 21:49:53 +03:00
end
end
begin
2015-06-03 19:59:10 +03:00
Mal.rep "(load-file \"#{ARGV[0]}\")"
2015-05-24 21:49:53 +03:00
rescue e
STDERR.puts e
end
exit
end
2018-10-27 23:20:36 +03:00
while line = Readline.readline("user> ", true)
2015-05-24 21:49:53 +03:00
begin
2015-06-03 19:59:10 +03:00
puts Mal.rep(line)
rescue e : Mal::RuntimeException
STDERR.puts "Error: #{pr_str(e.thrown, true)}"
2015-05-24 21:49:53 +03:00
rescue e
STDERR.puts "Error: #{e}"
2015-05-24 21:49:53 +03:00
end
end