forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMalTypes.io
122 lines (109 loc) · 3.6 KB
/
MalTypes.io
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
MalTypes := Object clone
nil malPrint := method(readable, self asString)
true malPrint := method(readable, self asString)
false malPrint := method(readable, self asString)
Number malPrint := method(readable, self asString)
// Io strings are of type Sequence
Sequence malPrint := method(readable,
if(readable, self asString asJson, self asString)
)
MalMeta := Object clone do(
meta ::= nil
)
MalSymbol := Object clone appendProto(MalMeta) do (
val ::= nil
with := method(str, self clone setVal(str))
malPrint := method(readable, val)
== := method(other, (self type == other type) and (val == other val))
)
MalKeyword := Object clone do (
val ::= nil
with := method(str, self clone setVal(str))
malPrint := method(readable, ":" .. val)
== := method(other, (self type == other type) and (val == other val))
)
MalSequential := Object clone do(
isSequential := method(true)
)
MalList := List clone appendProto(MalSequential) appendProto(MalMeta) do (
with := method(lst, self clone copy(lst))
malPrint := method(readable,
"(" .. (self map(e, e malPrint(readable)) join(" ")) .. ")"
)
rest := method(MalList with(resend))
slice := method(MalList with(resend))
)
MalVector := List clone appendProto(MalSequential) appendProto(MalMeta) do (
with := method(lst, self clone copy(lst))
malPrint := method(readable,
"[" .. (self map(e, e malPrint(readable)) join(" ")) .. "]"
)
rest := method(MalList with(resend))
slice := method(MalList with(resend))
)
MalMap := Map clone appendProto(MalMeta) do (
withList := method(lst,
obj := self clone
k := nil
lst foreach(i, e,
if(i % 2 == 0,
k := e,
obj atPut(objToKey(k), e)
)
)
obj
)
withMap := method(aMap, self clone merge(aMap))
objToKey := method(obj,
if(obj type == "MalKeyword", "K_" .. (obj val), "S_" .. obj)
)
keyToObj := method(s,
if(s beginsWithSeq("K_"),
MalKeyword with(s exSlice(2)),
s exSlice(2)
)
)
malPrint := method(readable,
"{" ..
(self map(k, v,
(keyToObj(k) malPrint(readable)) .. " " .. (v malPrint(readable))
) join(" ")) .. "}"
)
contains := method(obj, hasKey(objToKey(obj)))
get := method(obj, at(objToKey(obj)))
malKeys := method(MalList with(keys map(k, keyToObj(k))))
malVals := method(MalList with(values))
removeKey := method(obj, removeAt(objToKey(obj)))
== := method(other,
if(self type != other type, return false)
if(keys size != other keys size, return false)
unequalElement := self detect(k, valA,
(valA == (other at(k))) not
)
if(unequalElement, false, true)
)
)
Block malPrint := method(readable, "#<NativeFunction>")
Block appendProto(MalMeta)
MalFunc := Object clone appendProto(MalMeta) do (
ast ::= nil
params ::= nil
env ::= nil
blk ::= nil
isMacro ::= false
with := method(aAst, aParams, aEnv, aBlk,
self clone setAst(aAst) setParams(aParams) setEnv(aEnv) setBlk(aBlk)
)
malPrint := method(readable, "#<Function:params=" .. (params malPrint(true)) .. ">")
call := method(args, blk call(args))
)
MalAtom := Object clone do (
val ::= nil
with := method(str, self clone setVal(str))
malPrint := method(readable, "(atom " .. (val malPrint(true)) .. ")")
== := method(other, (self type == other type) and (val == other val))
)
MalException := Exception clone do (
val ::= nil
with := method(str, self clone setVal(str))
)