I have a general question about compiler design. I...
# compiler
r
I have a general question about compiler design. I'm currently building a compiler for a simple language with Kotlin like syntax (for educational purposes). I'm writing the Compiler itself in Kotlin, and I have just finished up the first iteration of the Lexer/Scanner. I have noticed in every compiler tutorial that I have come across, that global variables are used extensively, for example to represent the next char of source code to scan. Aside from this approach being difficult for me to mentally model, it seemed that this might just be a corner which is cut for pedagogical reasons. I have instead written things differently, doing my best to avoid global variables. My current approach is to use Kotlin's Pair and Triple types to return multiple values from my functions:
Copy code
class DonkHandWrittenScanner() : IScanner {
    override fun getTokens(source: String): ScannerResult {
        var start = 0
        var current = 0
        var line = 1

        val tokenList = mutableListOf<DonkToken>()
        val scannerErrorList = mutableListOf<ScannerException>()

        while (!current.isAtEnd(source.length)) {
            val scan = scanToken(start, current, source)

            //new start position
            start = scan.first

            //new current position
            current = scan.second

            //deal with the token itself
            when (scan.third.type) {
                TokenType.SKIP -> {}

                TokenType.ERROR -> {
                    scannerErrorList.add(
                        ScannerException(
                            scan.third.literal.toString()
                        )
                    )
                }

                else -> {
                    tokenList.add(scan.third)
                }
            }
        }
        return if (scannerErrorList.isEmpty()) {
            tokenList.add(DonkToken("eof", TokenType.EOF))
            ScannerResult.Success(tokenList)
        }
        else ScannerResult.Error(scannerErrorList)
    }
Copy code
/**
     * Triple<newStart, newCurrent, Token>
     */
    internal fun scanToken(start: Int, current: Int, source: String): Triple<Int, Int, DonkToken> {

        val char = source.get(current)

        //whitespace
        when {
            char.isWhitespace() -> return Triple(start, current + 1, DonkToken(char.toString(), TokenType.SKIP))
        }

        //keywords
        when {
            listOf('a', 'e', 'f', 'n', 'o', 'r', 's', 't', 'w', 'i', 'v').contains(char) -> {
                val scanKeyword = matchKeyword(current, source)
                if (scanKeyword.second.type != TokenType.SKIP)
                    return Triple(start, current + scanKeyword.first, scanKeyword.second)
            }
        }

        //Literals
//...

        //identifiers
//...
        //Single, double, and comment tokens
        return when (char) {
            '(' -> Triple(start, current + 1, DonkToken(char.toString(), TokenType.LEFT_PAREN))
//...
            else -> Triple(start, current + 1, DonkToken("", TokenType.ERROR,"Unable to scan input $char at: $current"))
        }
    }
//...
private fun matchString(index: Int, source: String): Pair<Int, DonkToken> {
        //counter starts at 1 to maintain consistency of indexes after eating the first quotation mark
        var counter = 1
        val builder = StringBuilder()

        run loop@{
            source.filterIndexed { filter, _ -> filter >= index }
                .forEach { char ->
                    if (char == '\"') {
                        counter++
                        return@loop
                    } else {
                        counter++
                        builder.append(char)
                    }
                }
        }

        return Pair(counter, DonkToken(builder.toString(), TokenType.STRING, builder.toString()))
    }