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) { }
11 class MalCoreException(message: String, val value: MalType) : MalException(message) // TODO rename
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()
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)
28 override fun equals(other: Any?): Boolean = other is MalInteger && value.equals(other.value)
31 class MalSymbol(val value: String) : MalType {
32 override fun equals(other: Any?): Boolean = other is MalSymbol && value.equals(other.value)
35 open class MalString(value: String) : MalConstant(value)
37 class MalKeyword(value: String) : MalString("\u029E" + value)
39 interface ILambda : MalType {
40 fun apply(seq: ISeq): MalType
43 open class MalFunction(val lambda: (ISeq) -> MalType) : MalType, ILambda {
44 var is_macro: Boolean = false
46 override fun apply(seq: ISeq): MalType = lambda(seq)
49 class MalFnFunction(val ast: MalType, val params: Sequence<MalSymbol>, val env: Env, lambda: (ISeq) -> MalType) : MalFunction(lambda)
51 interface ISeq : MalType {
52 fun seq(): Sequence<MalType>
55 fun nth(n: Int): MalType
56 fun slice(fromIndex: Int, toIndex: Int): ISeq
59 interface IMutableSeq : ISeq {
60 fun conj_BANG(form: MalType)
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)
69 override fun slice(fromIndex: Int, toIndex: Int): MalList =
70 MalList(elements.toLinkedList().subList(fromIndex, toIndex))
73 class MalList(val elements: MutableList<MalType>) : MalType, IMutableSeq {
74 constructor() : this(LinkedList<MalType>())
75 constructor(s: ISeq) : this(s.seq().toLinkedList())
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)
82 override fun conj_BANG(form: MalType) {
86 override fun equals(other: Any?): Boolean =
88 && elements.size == other.seq().count() // TODO optimize counting?
89 && elements.asSequence().zip(other.seq()).all({ it -> it.first == it.second })
91 override fun slice(fromIndex: Int, toIndex: Int): MalList =
92 MalList(elements.subList(fromIndex, toIndex))
95 class MalVector(val elements: MutableList<MalType>) : MalType, IMutableSeq {
96 constructor() : this(ArrayList<MalType>())
97 constructor(s: ISeq) : this(s.seq().toArrayList())
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)
104 override fun conj_BANG(form: MalType) {
108 override fun equals(other: Any?): Boolean =
110 && elements.size == other.seq().count() // TODO optimize counting?
111 && elements.asSequence().zip(other.seq()).all({ it -> it.first == it.second })
113 override fun slice(fromIndex: Int, toIndex: Int): MalVector =
114 MalVector(elements.subList(fromIndex, toIndex))
117 class MalHashMap() : MalType {
118 val elements = HashMap<MalString, MalType>()
120 constructor(other: MalHashMap) : this() {
121 other.elements.forEach({ it -> assoc_BANG(it.key, it.value) })
124 fun assoc_BANG(key: MalString, value: MalType) = elements.put(key, value)
125 fun dissoc_BANG(key: MalString) {
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)