Commit | Line | Data |
---|---|---|
93a8c65d DM |
1 | MalTypes := Object clone |
2 | ||
3 | nil malPrint := method(readable, self asString) | |
4 | true malPrint := method(readable, self asString) | |
5 | false malPrint := method(readable, self asString) | |
6 | Number malPrint := method(readable, self asString) | |
7 | ||
8 | // Io strings are of type Sequence | |
9 | Sequence malPrint := method(readable, | |
748df6f7 DM |
10 | if(readable, |
11 | "\"" .. (self asString asMutable replaceSeq("\\", "\\\\") replaceSeq("\"", "\\\"") replaceSeq("\n", "\\n")) .. "\"", | |
12 | self asString) | |
93a8c65d DM |
13 | ) |
14 | ||
55acffa6 DM |
15 | MalMeta := Object clone do( |
16 | meta ::= nil | |
17 | ) | |
18 | ||
9f78a1b9 | 19 | MalSymbol := Object clone appendProto(MalMeta) do ( |
93a8c65d | 20 | val ::= nil |
fd308353 | 21 | with := method(str, self clone setVal(if(str ?val, str val, str))) |
93a8c65d | 22 | malPrint := method(readable, val) |
9f78a1b9 | 23 | == := method(other, (self type == other type) and (val == other val)) |
93a8c65d DM |
24 | ) |
25 | ||
26 | MalKeyword := Object clone do ( | |
27 | val ::= nil | |
fd308353 | 28 | with := method(str, self clone setVal(if(str ?val, str val, str))) |
93a8c65d | 29 | malPrint := method(readable, ":" .. val) |
9f78a1b9 | 30 | == := method(other, (self type == other type) and (val == other val)) |
93a8c65d DM |
31 | ) |
32 | ||
8f22ab1a DM |
33 | MalSequential := Object clone do( |
34 | isSequential := method(true) | |
8e4de1ed DM |
35 | equalSequence := method(other, |
36 | if((other ?isSequential) not, return false) | |
37 | if(self size != other size, return false) | |
38 | unequalElement := self detect(i, valA, | |
39 | (valA == (other at(i))) not | |
40 | ) | |
41 | if(unequalElement, false, true) | |
42 | ) | |
8f22ab1a DM |
43 | ) |
44 | ||
9f78a1b9 DM |
45 | MalList := List clone appendProto(MalSequential) appendProto(MalMeta) do ( |
46 | with := method(lst, self clone copy(lst)) | |
93a8c65d DM |
47 | malPrint := method(readable, |
48 | "(" .. (self map(e, e malPrint(readable)) join(" ")) .. ")" | |
49 | ) | |
bea8cb29 DM |
50 | rest := method(MalList with(resend)) |
51 | slice := method(MalList with(resend)) | |
8e4de1ed | 52 | == := method(other, equalSequence(other)) |
93a8c65d DM |
53 | ) |
54 | ||
9f78a1b9 DM |
55 | MalVector := List clone appendProto(MalSequential) appendProto(MalMeta) do ( |
56 | with := method(lst, self clone copy(lst)) | |
93a8c65d DM |
57 | malPrint := method(readable, |
58 | "[" .. (self map(e, e malPrint(readable)) join(" ")) .. "]" | |
59 | ) | |
8f22ab1a DM |
60 | rest := method(MalList with(resend)) |
61 | slice := method(MalList with(resend)) | |
8e4de1ed | 62 | == := method(other, equalSequence(other)) |
93a8c65d DM |
63 | ) |
64 | ||
9f78a1b9 | 65 | MalMap := Map clone appendProto(MalMeta) do ( |
93a8c65d DM |
66 | withList := method(lst, |
67 | obj := self clone | |
68 | k := nil | |
69 | lst foreach(i, e, | |
70 | if(i % 2 == 0, | |
71 | k := e, | |
ec6abf6f | 72 | obj atPut(objToKey(k), e) |
93a8c65d DM |
73 | ) |
74 | ) | |
75 | obj | |
76 | ) | |
9f78a1b9 | 77 | withMap := method(aMap, self clone merge(aMap)) |
ec6abf6f | 78 | objToKey := method(obj, |
9f78a1b9 | 79 | if(obj type == "MalKeyword", "K_" .. (obj val), "S_" .. obj) |
93a8c65d | 80 | ) |
ec6abf6f | 81 | keyToObj := method(s, |
93a8c65d DM |
82 | if(s beginsWithSeq("K_"), |
83 | MalKeyword with(s exSlice(2)), | |
84 | s exSlice(2) | |
85 | ) | |
86 | ) | |
87 | malPrint := method(readable, | |
bea8cb29 DM |
88 | "{" .. |
89 | (self map(k, v, | |
90 | (keyToObj(k) malPrint(readable)) .. " " .. (v malPrint(readable)) | |
93a8c65d DM |
91 | ) join(" ")) .. "}" |
92 | ) | |
a7353735 DM |
93 | contains := method(obj, hasKey(objToKey(obj))) |
94 | get := method(obj, at(objToKey(obj))) | |
95 | malKeys := method(MalList with(keys map(k, keyToObj(k)))) | |
96 | malVals := method(MalList with(values)) | |
97 | removeKey := method(obj, removeAt(objToKey(obj))) | |
98 | == := method(other, | |
99 | if(self type != other type, return false) | |
100 | if(keys size != other keys size, return false) | |
101 | unequalElement := self detect(k, valA, | |
102 | (valA == (other at(k))) not | |
103 | ) | |
104 | if(unequalElement, false, true) | |
105 | ) | |
93a8c65d | 106 | ) |
3da6f492 DM |
107 | |
108 | Block malPrint := method(readable, "#<NativeFunction>") | |
55acffa6 | 109 | Block appendProto(MalMeta) |
3da6f492 | 110 | |
9f78a1b9 | 111 | MalFunc := Object clone appendProto(MalMeta) do ( |
3da6f492 DM |
112 | ast ::= nil |
113 | params ::= nil | |
114 | env ::= nil | |
115 | blk ::= nil | |
15f8f2d0 | 116 | isMacro ::= false |
3da6f492 DM |
117 | with := method(aAst, aParams, aEnv, aBlk, |
118 | self clone setAst(aAst) setParams(aParams) setEnv(aEnv) setBlk(aBlk) | |
119 | ) | |
120 | malPrint := method(readable, "#<Function:params=" .. (params malPrint(true)) .. ">") | |
a7353735 | 121 | call := method(args, blk call(args)) |
3da6f492 | 122 | ) |
e4f737c3 | 123 | |
0862f64d | 124 | MalAtom := Object clone appendProto(MalMeta) do ( |
e4f737c3 | 125 | val ::= nil |
9f78a1b9 | 126 | with := method(str, self clone setVal(str)) |
e4f737c3 | 127 | malPrint := method(readable, "(atom " .. (val malPrint(true)) .. ")") |
9f78a1b9 | 128 | == := method(other, (self type == other type) and (val == other val)) |
e4f737c3 | 129 | ) |
a7353735 DM |
130 | |
131 | MalException := Exception clone do ( | |
132 | val ::= nil | |
9f78a1b9 | 133 | with := method(str, self clone setVal(str)) |
a7353735 | 134 | ) |