| 1 | #! /usr/bin/env crystal run |
| 2 | |
| 3 | require "readline" |
| 4 | require "./reader" |
| 5 | require "./printer" |
| 6 | require "./types" |
| 7 | require "./env" |
| 8 | require "./core" |
| 9 | require "./error" |
| 10 | |
| 11 | # Note: |
| 12 | # Employed downcase names because Crystal prohibits uppercase names for methods |
| 13 | |
| 14 | module Mal |
| 15 | extend self |
| 16 | |
| 17 | def func_of(env, binds, body) |
| 18 | ->(args : Array(Mal::Type)) { |
| 19 | new_env = Mal::Env.new(env, binds, args) |
| 20 | eval(body, new_env) |
| 21 | }.as(Mal::Func) |
| 22 | end |
| 23 | |
| 24 | def eval_ast(ast, env) |
| 25 | return ast.map { |n| eval(n, env).as(Mal::Type) } if ast.is_a? Mal::List |
| 26 | |
| 27 | val = ast.unwrap |
| 28 | |
| 29 | Mal::Type.new case val |
| 30 | when Mal::Symbol |
| 31 | if e = env.get(val.str) |
| 32 | e |
| 33 | else |
| 34 | eval_error "'#{val.str}' not found" |
| 35 | end |
| 36 | when Mal::List |
| 37 | val.each_with_object(Mal::List.new) { |n, l| l << eval(n, env) } |
| 38 | when Mal::Vector |
| 39 | val.each_with_object(Mal::Vector.new) { |n, l| l << eval(n, env) } |
| 40 | when Mal::HashMap |
| 41 | val.each { |k, v| val[k] = eval(v, env) } |
| 42 | val |
| 43 | else |
| 44 | val |
| 45 | end |
| 46 | end |
| 47 | |
| 48 | def eval_invocation(list, env) |
| 49 | f = eval(list.first, env).unwrap |
| 50 | eval_error "expected function symbol as the first symbol of list" unless f.is_a? Mal::Func |
| 51 | f.call eval_ast(list[1..-1].each_with_object(Mal::List.new) { |i, l| l << i }, env) |
| 52 | end |
| 53 | |
| 54 | def read(str) |
| 55 | read_str str |
| 56 | end |
| 57 | |
| 58 | def eval(ast, env) |
| 59 | list = ast.unwrap |
| 60 | |
| 61 | return eval_ast(ast, env) unless list.is_a? Mal::List |
| 62 | return gen_type Mal::List if list.empty? |
| 63 | |
| 64 | head = list.first.unwrap |
| 65 | |
| 66 | Mal::Type.new case head |
| 67 | when Mal::Symbol |
| 68 | case head.str |
| 69 | when "def!" |
| 70 | eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
| 71 | a1 = list[1].unwrap |
| 72 | eval_error "1st argument of 'def!' must be symbol" unless a1.is_a? Mal::Symbol |
| 73 | env.set(a1.str, eval(list[2], env)) |
| 74 | when "let*" |
| 75 | eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
| 76 | |
| 77 | bindings = list[1].unwrap |
| 78 | eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array |
| 79 | eval_error "size of binding list must be even" unless bindings.size.even? |
| 80 | |
| 81 | new_env = Mal::Env.new env |
| 82 | bindings.each_slice(2) do |binding| |
| 83 | key, value = binding |
| 84 | name = key.unwrap |
| 85 | eval_error "name of binding must be specified as symbol" unless name.is_a? Mal::Symbol |
| 86 | new_env.set(name.str, eval(value, new_env)) |
| 87 | end |
| 88 | |
| 89 | eval(list[2], new_env) |
| 90 | when "do" |
| 91 | list.shift 1 |
| 92 | eval_ast(list, env).last |
| 93 | when "if" |
| 94 | cond = eval(list[1], env).unwrap |
| 95 | case cond |
| 96 | when Nil |
| 97 | list.size >= 4 ? eval(list[3], env) : nil |
| 98 | when false |
| 99 | list.size >= 4 ? eval(list[3], env) : nil |
| 100 | else |
| 101 | eval(list[2], env) |
| 102 | end |
| 103 | when "fn*" |
| 104 | # Note: |
| 105 | # If writing lambda expression here directly, compiler will fail to infer type of 'list'. (Error 'Nil for empty?') |
| 106 | func_of(env, list[1].unwrap, list[2]) |
| 107 | else |
| 108 | eval_invocation(list, env) |
| 109 | end |
| 110 | else |
| 111 | eval_invocation(list, env) |
| 112 | end |
| 113 | end |
| 114 | |
| 115 | def print(result) |
| 116 | pr_str(result, true) |
| 117 | end |
| 118 | |
| 119 | def rep(str) |
| 120 | print(eval(read(str), REPL_ENV)) |
| 121 | end |
| 122 | end |
| 123 | |
| 124 | REPL_ENV = Mal::Env.new nil |
| 125 | Mal::NS.each { |k, v| REPL_ENV.set(k, Mal::Type.new(v)) } |
| 126 | Mal.rep "(def! not (fn* (a) (if a false true)))" |
| 127 | |
| 128 | while line = Readline.readline("user> ") |
| 129 | begin |
| 130 | puts Mal.rep(line) |
| 131 | rescue e : Mal::RuntimeException |
| 132 | STDERR.puts "Error: #{pr_str(e.thrown, true)}" |
| 133 | rescue e |
| 134 | STDERR.puts "Error: #{e}" |
| 135 | end |
| 136 | end |