hash-map equality: bash, c, coffee, cs, es6, ...
[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)
41 tuple(a...) == tuple(b...)
82484631 42 elseif isa(a,AbstractString)
4430aab9 43 a == b
a0b63ee4
JM
44 elseif isa(a,Dict)
45 if length(a) !== length(b)
46 return false
47 end
48 for (k,v) in a
49 if !equal_Q(v,b[k])
50 return false
51 end
52 end
53 return true
2aa39ccd
JM
54 else
55 a === b
56 end
421cdb5d
JM
57end
58
4430aab9
JM
59function hash_map(lst...)
60 hm = Dict()
61 for i = 1:2:length(lst)
62 hm[lst[i]] = lst[i+1]
63 end
64 hm
65end
66
85110962
JM
67type Atom
68 val
69end
70
421cdb5d
JM
71end
72
73