A fork of Crisp for HARP
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

280 行
7.2 KiB

  1. #! /usr/bin/env crystal run
  2. require "colorize"
  3. require "./readline"
  4. require "./reader"
  5. require "./printer"
  6. require "./types"
  7. require "./env"
  8. require "./core"
  9. require "./error"
  10. # Note:
  11. # Employed downcase names because Crystal prohibits uppercase names for methods
  12. def func_of(env, binds, body)
  13. -> (args : Array(Mal::Type)) {
  14. new_env = Mal::Env.new(env, binds, args)
  15. eval(body, new_env)
  16. } as Mal::Func
  17. end
  18. def eval_ast(ast, env)
  19. return ast.map{|n| eval(n, env) as Mal::Type} if ast.is_a? Array
  20. val = ast.unwrap
  21. Mal::Type.new case val
  22. when Mal::Symbol
  23. if e = env.get(val.str)
  24. e
  25. else
  26. eval_error "'#{val.str}' not found"
  27. end
  28. when Mal::List
  29. val.each_with_object(Mal::List.new){|n, l| l << eval(n, env)}
  30. when Mal::Vector
  31. val.each_with_object(Mal::Vector.new){|n, l| l << eval(n, env)}
  32. when Mal::HashMap
  33. new_map = Mal::HashMap.new
  34. val.each{|k, v| new_map[k] = eval(v, env)}
  35. new_map
  36. else
  37. val
  38. end
  39. end
  40. def read(str)
  41. read_str str
  42. end
  43. macro pair?(list)
  44. {{list}}.is_a?(Array) && !{{list}}.empty?
  45. end
  46. def quasiquote(ast)
  47. list = ast.unwrap
  48. unless pair?(list)
  49. return Mal::Type.new(
  50. Mal::List.new << gen_type(Mal::Symbol, "quote") << ast
  51. )
  52. end
  53. head = list.first.unwrap
  54. case
  55. # ("unquote" ...)
  56. when head.is_a?(Mal::Symbol) && head.str == "unquote"
  57. list[1]
  58. # (("splice-unquote" ...) ...)
  59. when pair?(head) && (arg0 = head.first.unwrap).is_a?(Mal::Symbol) && arg0.str == "splice-unquote"
  60. tail = Mal::Type.new list[1..-1].to_mal
  61. Mal::Type.new(
  62. Mal::List.new << gen_type(Mal::Symbol, "concat") << head[1] << quasiquote(tail)
  63. )
  64. else
  65. tail = Mal::Type.new list[1..-1].to_mal
  66. Mal::Type.new(
  67. Mal::List.new << gen_type(Mal::Symbol, "cons") << quasiquote(list.first) << quasiquote(tail)
  68. )
  69. end
  70. end
  71. def macro_call?(ast, env)
  72. list = ast.unwrap
  73. return false unless list.is_a? Mal::List
  74. return false if list.empty?
  75. sym = list.first.unwrap
  76. return false unless sym.is_a? Mal::Symbol
  77. func = env.find(sym.str).try(&.data[sym.str])
  78. return false unless func && func.macro?
  79. true
  80. end
  81. def macroexpand(ast, env)
  82. while macro_call?(ast, env)
  83. # Already checked in macro_call?
  84. list = ast.unwrap as Mal::List
  85. func_sym = list[0].unwrap as Mal::Symbol
  86. func = env.get(func_sym.str).unwrap
  87. case func
  88. when Mal::Func
  89. ast = func.call(list[1..-1])
  90. when Mal::Closure
  91. ast = func.fn.call(list[1..-1])
  92. else
  93. eval_error "macro '#{func_sym.str}' must be function: #{ast}"
  94. end
  95. end
  96. ast
  97. end
  98. macro invoke_list(l, env)
  99. f = eval({{l}}.first, {{env}}).unwrap
  100. args = eval_ast({{l}}[1..-1], {{env}}) as Array
  101. case f
  102. when Mal::Closure
  103. ast = f.ast
  104. {{env}} = Mal::Env.new(f.env, f.params, args)
  105. next # TCO
  106. when Mal::Func
  107. return f.call args
  108. else
  109. eval_error "expected function as the first argument: #{f}"
  110. end
  111. end
  112. def debug(ast)
  113. puts print(ast).colorize.red
  114. end
  115. def eval(ast, env)
  116. # 'next' in 'do...end' has a bug in crystal 0.7.1
  117. # https://github.com/manastech/crystal/issues/659
  118. while true
  119. return eval_ast(ast, env) unless ast.unwrap.is_a? Mal::List
  120. ast = macroexpand(ast, env)
  121. list = ast.unwrap
  122. return ast unless list.is_a? Mal::List
  123. return ast if list.empty?
  124. head = list.first.unwrap
  125. return invoke_list(list, env) unless head.is_a? Mal::Symbol
  126. return Mal::Type.new case head.str
  127. when "def!"
  128. eval_error "wrong number of argument for 'def!'" unless list.size == 3
  129. a1 = list[1].unwrap
  130. eval_error "1st argument of 'def!' must be symbol: #{a1}" unless a1.is_a? Mal::Symbol
  131. env.set(a1.str, eval(list[2], env))
  132. when "let*"
  133. eval_error "wrong number of argument for 'def!'" unless list.size == 3
  134. bindings = list[1].unwrap
  135. eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array
  136. eval_error "size of binding list must be even" unless bindings.size.even?
  137. new_env = Mal::Env.new env
  138. bindings.each_slice(2) do |binding|
  139. key, value = binding
  140. name = key.unwrap
  141. eval_error "name of binding must be specified as symbol #{name}" unless name.is_a? Mal::Symbol
  142. new_env.set(name.str, eval(value, new_env))
  143. end
  144. ast, env = list[2], new_env
  145. next # TCO
  146. when "do"
  147. if list.empty?
  148. ast = Mal::Type.new nil
  149. next
  150. end
  151. eval_ast(list[1..-2].to_mal, env)
  152. ast = list.last
  153. next # TCO
  154. when "if"
  155. ast = unless eval(list[1], env).unwrap
  156. list.size >= 4 ? list[3] : Mal::Type.new(nil)
  157. else
  158. list[2]
  159. end
  160. next # TCO
  161. when "fn*"
  162. params = list[1].unwrap
  163. unless params.is_a? Array
  164. eval_error "'fn*' parameters must be list or vector: #{params}"
  165. end
  166. Mal::Closure.new(list[2], params, env, func_of(env, params, list[2]))
  167. when "quote"
  168. list[1]
  169. when "quasiquote"
  170. ast = quasiquote list[1]
  171. next # TCO
  172. when "defmacro!"
  173. eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3
  174. a1 = list[1].unwrap
  175. eval_error "1st argument of 'defmacro!' must be symbol: #{a1}" unless a1.is_a? Mal::Symbol
  176. env.set(a1.str, eval(list[2], env).tap{|n| n.is_macro = true})
  177. when "macroexpand"
  178. macroexpand(list[1], env)
  179. when "try*"
  180. catch_list = list[2].unwrap
  181. return eval(list[1], env) unless catch_list.is_a? Mal::List
  182. catch_head = catch_list.first.unwrap
  183. return eval(list[1], env) unless catch_head.is_a? Mal::Symbol
  184. return eval(list[1], env) unless catch_head.str == "catch*"
  185. begin
  186. eval(list[1], env)
  187. rescue e : Mal::RuntimeException
  188. new_env = Mal::Env.new(env, [catch_list[1]], [e.thrown])
  189. eval(catch_list[2], new_env)
  190. rescue e
  191. new_env = Mal::Env.new(env, [catch_list[1]], [Mal::Type.new e.message])
  192. eval(catch_list[2], new_env)
  193. end
  194. else
  195. invoke_list(list, env)
  196. end
  197. end
  198. end
  199. def print(result)
  200. pr_str(result, true)
  201. end
  202. def rep(str)
  203. print(eval(read(str), $repl_env))
  204. end
  205. $repl_env = Mal::Env.new nil
  206. Mal::NS.each{|k,v| $repl_env.set(k, Mal::Type.new(v))}
  207. $repl_env.set("eval", Mal::Type.new -> (args: Array(Mal::Type)){ eval(args[0], $repl_env) })
  208. rep "(def! not (fn* (a) (if a false true)))"
  209. rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
  210. 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)))))))"
  211. 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))))))))"
  212. rep("(def! *host-language* \"crystal\")")
  213. $argv = Mal::List.new
  214. $repl_env.set("*ARGV*", Mal::Type.new $argv)
  215. unless ARGV.empty?
  216. if ARGV.size > 1
  217. ARGV[1..-1].each do |a|
  218. $argv << Mal::Type.new(a)
  219. end
  220. end
  221. begin
  222. rep "(load-file \"#{ARGV[0]}\")"
  223. rescue e
  224. STDERR.puts e
  225. end
  226. exit
  227. end
  228. rep("(println (str \"Mal [\" *host-language* \"]\"))")
  229. while line = my_readline("user> ")
  230. begin
  231. puts rep(line)
  232. rescue e
  233. STDERR.puts e
  234. end
  235. end