Merge pull request #156 from omarrayward/explain-regexp-tokenizer
[jackhill/mal.git] / julia / types.jl
1 module types
2
3 export MalException, MalFunc, sequential_Q, equal_Q, hash_map, Atom
4
5 import Base.copy
6
7 type MalException <: Exception
8 malval
9 end
10
11 type MalFunc
12 fn::Function
13 ast
14 env
15 params
16 ismacro
17 meta
18 end
19
20 # ismacro default to false
21 function MalFunc(fn, ast, env, params)
22 MalFunc(fn, ast, env, params, false, nothing)
23 end
24
25 function copy(f::MalFunc)
26 MalFunc(f.fn, f.ast, f.env, f.params, f.ismacro, f.meta)
27 end
28
29 function sequential_Q(obj)
30 isa(obj, Array) || isa(obj, Tuple)
31 end
32
33 function 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 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
50 elseif isa(a,AbstractString)
51 a == b
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
62 else
63 a === b
64 end
65 end
66
67 function hash_map(lst...)
68 hm = Dict()
69 for i = 1:2:length(lst)
70 hm[lst[i]] = lst[i+1]
71 end
72 hm
73 end
74
75 type Atom
76 val
77 end
78
79 end
80
81