All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.skillw.asahi.internal.namespacing.prefix.lang.structure.Condition.kt Maven / Gradle / Ivy

There is a newer version: 1.6.7-beta-6
Show newest version
package com.skillw.asahi.internal.namespacing.prefix.lang.structure

import com.skillw.asahi.api.annotation.AsahiPrefix
import com.skillw.asahi.api.member.quest.Quester
import com.skillw.asahi.api.prefixParser
import com.skillw.asahi.api.quest
import com.skillw.asahi.api.quester

/**
 * @className Condition
 *
 * @author Glom
 * @date 2023/1/14 0:21 Copyright 2024 Glom. 
 */

@AsahiPrefix(["condition"], "lang")
private fun condition() = prefixParser {
    val condition = questCondition(";")
    result {
        condition.get()
    }
}

//单路
@AsahiPrefix(["if"], "lang")
private fun `if`() = prefixParser {
    val condition = questCondition("then")
    expect("then")
    val ifTrue = quest()
    val ifFalse = if (expect("else")) quest() else quester { }
    result {
        if (condition.get()) ifTrue.run()
        else ifFalse.run()
    }
}

//多路
@AsahiPrefix(["when", "switch"], "lang")
private fun `when`() = prefixParser {
    val value = if (expect("of")) quest() else null
    val pairs = ArrayList, Quester>>()
    expect("{")
    while (expect("case", "when")) {
        value?.let {
            val condition = questCondition("->") {
                val symbol = next()
                val other = quest()
                quester { com.skillw.asahi.util.check(value.get(), symbol, other) }
            }
            expect("->")
            pairs.add(condition to quest())
        } ?: kotlin.run {
            val condition = questCondition("->")
            expect("->")
            pairs.add(condition to quest())
        }
    }
    if (expect("else", "default")) {
        expect("->")
        pairs.add(quester { true } to quest())
    }
    expect("}")
    result {
        pairs.forEach {
            if (it.first.get()) {
                return@result it.second.get()
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy