switch to tail -f circular pipes
[jackhill/mal.git] / julia / types.jl
CommitLineData
421cdb5d
JM
1module types
2
85110962
JM
3export MalException, MalFunc, sequential_Q, equal_Q, hash_map, Atom
4
5import Base.copy
4430aab9
JM
6
7type MalException <: Exception
8 malval
9end
421cdb5d
JM
10
11type MalFunc
12 fn::Function
13 ast
14 env
15 params
2aa39ccd 16 ismacro
85110962 17 meta
2aa39ccd
JM
18end
19
20# ismacro default to false
21function MalFunc(fn, ast, env, params)
85110962
JM
22 MalFunc(fn, ast, env, params, false, nothing)
23end
24
25function copy(f::MalFunc)
26 MalFunc(f.fn, f.ast, f.env, f.params, f.ismacro, f.meta)
2aa39ccd
JM
27end
28
29function sequential_Q(obj)
30 isa(obj, Array) || isa(obj, Tuple)
31end
32
33function equal_Q(a, b)
34 ota = typeof(a)
35 otb = typeof(b)
36 if !(ota === otb || (sequential_Q(a) && sequential_Q(b)))
37 return false
38 end
39
40 if sequential_Q(a)
c4b23bf4
JM
41 if length(a) !== length(b)
42 return false
43 end
44 for (x, y) in zip(a,b)
45 if !equal_Q(x, y)
46 return false
47 end
48 end
49 return true
82484631 50 elseif isa(a,AbstractString)
4430aab9 51 a == b
a0b63ee4
JM
52 elseif isa(a,Dict)
53 if length(a) !== length(b)
54 return false
55 end
56 for (k,v) in a
57 if !equal_Q(v,b[k])
58 return false
59 end
60 end
61 return true
2aa39ccd
JM
62 else
63 a === b
64 end
421cdb5d
JM
65end
66
4430aab9
JM
67function hash_map(lst...)
68 hm = Dict()
69 for i = 1:2:length(lst)
70 hm[lst[i]] = lst[i+1]
71 end
72 hm
73end
74
85110962
JM
75type Atom
76 val
77end
78
421cdb5d
JM
79end
80
81