forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.jl
137 lines (117 loc) · 3.66 KB
/
core.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module core
import types
import reader
using printer
import readline_mod
export ns
function string_Q(obj)
isa(obj,AbstractString) && (length(obj) == 0 || obj[1] != '\u029e')
end
function keyword_Q(obj)
isa(obj,AbstractString) && (length(obj) > 0 && obj[1] == '\u029e')
end
function concat(args...)
res = []
for a=args
res = [res; Any[a...]]
end
res
end
function do_apply(f, all_args...)
fn = isa(f,types.MalFunc) ? f.fn : f
args = concat(all_args[1:end-1], all_args[end])
fn(args...)
end
function do_map(a,b)
# map and convert to array/list
if isa(a,types.MalFunc)
collect(map(a.fn,b))
else
collect(map(a,b))
end
end
function conj(seq, args...)
if isa(seq,Array)
concat(reverse(args), seq)
else
tuple(concat(seq, args)...)
end
end
function do_seq(obj)
if isa(obj,Array)
length(obj) > 0 ? obj : nothing
elseif isa(obj,Tuple)
length(obj) > 0 ? Any[obj...] : nothing
elseif isa(obj,AbstractString)
length(obj) > 0 ? [string(c) for c=obj] : nothing
elseif obj == nothing
nothing
else
error("seq: called on non-sequence")
end
end
function with_meta(obj, meta)
new_obj = types.copy(obj)
new_obj.meta = meta
new_obj
end
ns = Dict{Any,Any}(
symbol("=") => (a,b) -> types.equal_Q(a, b),
:throw => (a) -> throw(types.MalException(a)),
symbol("nil?") => (a) -> a === nothing,
symbol("true?") => (a) -> a === true,
symbol("false?") => (a) -> a === false,
symbol("string?") => string_Q,
symbol("symbol") => (a) -> symbol(a),
symbol("symbol?") => (a) -> typeof(a) === Symbol,
symbol("keyword") => (a) -> a[1] == '\u029e' ? a : "\u029e$(a)",
symbol("keyword?") => keyword_Q,
symbol("pr-str") => (a...) -> join(map((e)->pr_str(e, true),a)," "),
:str => (a...) -> join(map((e)->pr_str(e, false),a),""),
:prn => (a...) -> println(join(map((e)->pr_str(e, true),a)," ")),
:println => (a...) -> println(join(map((e)->pr_str(e, false),a)," ")),
symbol("read-string") => (a) -> reader.read_str(a),
:readline => readline_mod.do_readline,
:slurp => (a) -> readall(open(a)),
:< => <,
:<= => <=,
:> => >,
:>= => >=,
:+ => +,
:- => -,
symbol("*") => *,
:/ => div,
symbol("time-ms") => () -> round(Int, time()*1000),
:list => (a...) -> Any[a...],
symbol("list?") => (a) -> isa(a, Array),
:vector => (a...) -> tuple(a...),
symbol("vector?") => (a) -> isa(a, Tuple),
symbol("hash-map") => types.hash_map,
symbol("map?") => (a) -> isa(a, Dict),
:assoc => (a, b...) -> merge(a, types.hash_map(b...)),
:dissoc => (a, b...) -> foldl((x,y) -> delete!(x,y),copy(a), b),
:get => (a,b) -> a === nothing ? nothing : get(a,b,nothing),
symbol("contains?") => haskey,
:keys => (a) -> [keys(a)...],
:vals => (a) -> [values(a)...],
symbol("sequential?") => types.sequential_Q,
:cons => (a,b) -> [Any[a]; Any[b...]],
:concat => concat,
:nth => (a,b) -> b+1 > length(a) ? error("nth: index out of range") : a[b+1],
:first => (a) -> a === nothing || isempty(a) ? nothing : first(a),
:rest => (a) -> a === nothing ? Any[] : Any[a[2:end]...],
symbol("empty?") => isempty,
:count => (a) -> a == nothing ? 0 : length(a),
:apply => do_apply,
:map => do_map,
:conj => conj,
:seq => do_seq,
:meta => (a) -> isa(a,types.MalFunc) ? a.meta : nothing,
symbol("with-meta") => with_meta,
:atom => (a) -> types.Atom(a),
symbol("atom?") => (a) -> isa(a,types.Atom),
:deref => (a) -> a.val,
:reset! => (a,b) -> a.val = b,
:swap! => (a,b,c...) -> a.val = do_apply(b, a.val, c),
)
end