summaryrefslogtreecommitdiff
path: root/src/LispAst.kt
blob: 6ac70320f9c0c4e28739659ac80fea8321751231 (plain)
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
package moe.nea.lisp

sealed class LispAst : HasLispPosition {


    abstract fun toSource(): String


    data class Program(override val position: LispPosition, val nodes: List<LispNode>) : LispAst() {
        override fun toSource(): String {
            return nodes.joinToString("\n") {
                it.toSource()
            }
        }
    }

    sealed class LispNode : LispAst()
    data class Atom(override val position: LispPosition, val label: String) : LispNode() {
        override fun toSource(): String {
            return ":$label"
        }
    }

    data class Reference(override val position: LispPosition, val label: String) : LispNode() {
        override fun toSource(): String {
            return label
        }
    }

    data class Parenthesis(override val position: LispPosition, val items: List<LispNode>) : LispNode() {
        override fun toSource(): String {
            return items.joinToString(" ", "(", ")") { it.toSource() }
        }
    }

    data class NumberLiteral(override val position: LispPosition, val numberValue: Double) : LispNode() {
        override fun toSource(): String {
            return numberValue.toString()
        }
    }

    data class StringLiteral(override val position: LispPosition, val parsedString: String) : LispNode() {
        override fun toSource(): String {
            return "\"${parsedString.replace("\\", "\\\\").replace("\"", "\\\"")}\"" // TODO: better escaping
        }
    }
}