Finished additional core functions for step 9
[jackhill/mal.git] / kotlin / src / mal / types.kt
1 package mal
2
3 import java.util.*
4
5 // TODO clean up exception hierarchy
6 open class MalException(message: String?) : Exception(message), MalType { }
7 class MalContinue() : MalException("continue") { }
8 class MalReaderException(message: String) : MalException(message) { }
9 class MalPrinterException(message: String) : MalException(message) { }
10
11 class MalCoreException(message: String, val value: MalType) : MalException(message) // TODO rename
12
13 interface MalType {
14 }
15
16 open class MalConstant(val value: String) : MalType {
17 override fun equals(other: Any?): Boolean = other is MalConstant && value.equals(other.value)
18 override fun hashCode(): Int = value.hashCode()
19 }
20
21 class MalInteger(val value: Int) : MalType {
22 operator fun plus(a: MalInteger): MalInteger = MalInteger(value + a.value)
23 operator fun minus(a: MalInteger): MalInteger = MalInteger(value - a.value)
24 operator fun times(a: MalInteger): MalInteger = MalInteger(value * a.value)
25 operator fun div(a: MalInteger): MalInteger = MalInteger(value / a.value)
26 operator fun compareTo(a: MalInteger): Int = value.compareTo(a.value)
27
28 override fun equals(other: Any?): Boolean = other is MalInteger && value.equals(other.value)
29 }
30
31 class MalSymbol(val value: String) : MalType {
32 override fun equals(other: Any?): Boolean = other is MalSymbol && value.equals(other.value)
33 }
34
35 open class MalString(value: String) : MalConstant(value)
36
37 class MalKeyword(value: String) : MalString("\u029E" + value)
38
39 interface ILambda : MalType {
40 fun apply(seq: ISeq): MalType
41 }
42
43 open class MalFunction(val lambda: (ISeq) -> MalType) : MalType, ILambda {
44 var is_macro: Boolean = false
45
46 override fun apply(seq: ISeq): MalType = lambda(seq)
47 }
48
49 class MalFnFunction(val ast: MalType, val params: Sequence<MalSymbol>, val env: Env, lambda: (ISeq) -> MalType) : MalFunction(lambda)
50
51 interface ISeq : MalType {
52 fun seq(): Sequence<MalType>
53 fun first(): MalType
54 fun rest(): ISeq
55 fun nth(n: Int): MalType
56 fun slice(fromIndex: Int, toIndex: Int): ISeq
57 }
58
59 interface IMutableSeq : ISeq {
60 fun conj_BANG(form: MalType)
61 }
62
63 class MalSequence(val elements : Sequence<MalType>) : MalType, ISeq {
64 override fun seq(): Sequence<MalType> = elements
65 override fun first(): MalType = elements.first()
66 override fun rest(): ISeq = MalSequence(elements.drop(1))
67 override fun nth(n: Int): MalType = elements.elementAt(n)
68
69 override fun slice(fromIndex: Int, toIndex: Int): MalList =
70 MalList(elements.toLinkedList().subList(fromIndex, toIndex))
71 }
72
73 class MalList(val elements: MutableList<MalType>) : MalType, IMutableSeq {
74 constructor() : this(LinkedList<MalType>())
75 constructor(s: ISeq) : this(s.seq().toLinkedList())
76
77 override fun seq(): Sequence<MalType> = elements.asSequence()
78 override fun first(): MalType = elements.first()
79 override fun rest(): ISeq = MalSequence(elements.drop(1).asSequence())
80 override fun nth(n: Int): MalType = elements.elementAt(n)
81
82 override fun conj_BANG(form: MalType) {
83 elements.add(form)
84 }
85
86 override fun equals(other: Any?): Boolean =
87 (other is ISeq)
88 && elements.size == other.seq().count() // TODO optimize counting?
89 && elements.asSequence().zip(other.seq()).all({ it -> it.first == it.second })
90
91 override fun slice(fromIndex: Int, toIndex: Int): MalList =
92 MalList(elements.subList(fromIndex, toIndex))
93 }
94
95 class MalVector(val elements: MutableList<MalType>) : MalType, IMutableSeq {
96 constructor() : this(ArrayList<MalType>())
97 constructor(s: ISeq) : this(s.seq().toArrayList())
98
99 override fun seq(): Sequence<MalType> = elements.asSequence()
100 override fun first(): MalType = elements.first()
101 override fun rest(): ISeq = MalSequence(elements.drop(1).asSequence())
102 override fun nth(n: Int): MalType = elements.elementAt(n)
103
104 override fun conj_BANG(form: MalType) {
105 elements.add(form)
106 }
107
108 override fun equals(other: Any?): Boolean =
109 (other is ISeq)
110 && elements.size == other.seq().count() // TODO optimize counting?
111 && elements.asSequence().zip(other.seq()).all({ it -> it.first == it.second })
112
113 override fun slice(fromIndex: Int, toIndex: Int): MalVector =
114 MalVector(elements.subList(fromIndex, toIndex))
115 }
116
117 class MalHashMap() : MalType {
118 val elements = HashMap<MalString, MalType>()
119
120 constructor(other: MalHashMap) : this() {
121 other.elements.forEach({ it -> assoc_BANG(it.key, it.value) })
122 }
123
124 fun assoc_BANG(key: MalString, value: MalType) = elements.put(key, value)
125 fun dissoc_BANG(key: MalString) {
126 elements.remove(key)
127 }
128 }
129
130 // TODO add truthiness checking
131 val NIL = MalConstant("nil")
132 val TRUE = MalConstant("true")
133 val FALSE = MalConstant("false")
134 val ZERO = MalInteger(0)