Skip to content

Commit ef5122b

Browse files
committed
enum变量在声明的时候若已知类型则可略去enum主类型名而只写成员
1 parent a1ab974 commit ef5122b

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/main/kotlin/top/mcfpp/antlr/McfppExprVisitor.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import net.querz.nbt.tag.StringTag
55
import top.mcfpp.Project
66
import top.mcfpp.annotations.InsertCommand
77
import top.mcfpp.lang.*
8+
import top.mcfpp.lang.type.MCFPPEnumType
89
import top.mcfpp.lang.type.MCFPPGenericClassType
910
import top.mcfpp.lang.type.MCFPPType
1011
import top.mcfpp.lang.value.MCFPPValue
@@ -29,7 +30,7 @@ import kotlin.system.exitProcess
2930
/**
3031
* 获取表达式结果用的visitor。解析并计算一个形如a+b*c的表达式。
3132
*/
32-
class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassType? = null): mcfppParserBaseVisitor<Var<*>>() {
33+
class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassType? = null, private var enumType: MCFPPEnumType? = null): mcfppParserBaseVisitor<Var<*>>() {
3334

3435
private val tempVarCommandCache = HashMap<Var<*>, String>()
3536

@@ -498,6 +499,9 @@ class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
498499
//变量
499500
//没有数组选取
500501
val qwq: String = ctx.Identifier().text
502+
if(enumType != null && currSelector == null){
503+
currSelector = enumType
504+
}
501505
var re = if(currSelector == null) {
502506
Function.currFunction.field.getVar(qwq) ?: UnknownVar(qwq)
503507
}else{

src/main/kotlin/top/mcfpp/antlr/McfppImVisitor.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import top.mcfpp.exception.*
1111
import top.mcfpp.io.MCFPPFile
1212
import top.mcfpp.lang.*
1313
import top.mcfpp.lang.type.MCFPPBaseType
14+
import top.mcfpp.lang.type.MCFPPEnumType
1415
import top.mcfpp.lang.type.MCFPPGenericClassType
1516
import top.mcfpp.lang.type.MCFPPType
1617
import top.mcfpp.lang.value.MCFPPValue
@@ -165,7 +166,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
165166
Function.addCommand("#field: " + ctx.type().text + " " + c.Identifier().text + if (c.expression() != null) " = " + c.expression().text else "")
166167
//变量初始化
167168
if (c.expression() != null) {
168-
val init: Var<*> = McfppExprVisitor(if(type is MCFPPGenericClassType) type else null).visit(c.expression())!!
169+
val init: Var<*> = McfppExprVisitor(if(type is MCFPPGenericClassType) type else null, if(type is MCFPPEnumType) type else null).visit(c.expression())!!
169170
try {
170171
`var` = `var`.assign(init)
171172
} catch (e: VariableConverseException) {
@@ -203,13 +204,14 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
203204
override fun visitStatementExpression(ctx: mcfppParser.StatementExpressionContext):Any? {
204205
Project.ctx = ctx
205206
Function.addCommand("#expression: " + ctx.text)
206-
val right: Var<*> = McfppExprVisitor().visit(ctx.expression())!!
207207
if(ctx.basicExpression() != null){
208208
val left: Var<*> = McfppLeftExprVisitor().visit(ctx.basicExpression())!!
209209
if (left.isConst) {
210210
LogProcessor.error("Cannot assign a constant repeatedly: " + left.identifier)
211211
return null
212212
}
213+
val type = left.type
214+
val right: Var<*> = McfppExprVisitor(if(type is MCFPPGenericClassType) type else null, if(type is MCFPPEnumType) type else null).visit(ctx.expression())!!
213215
try {
214216
left.replacedBy(left.assign(right))
215217
} catch (e: VariableConverseException) {

src/test/kotlin/top/mcfpp/test/EnumTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class EnumTest {
1313
}
1414
1515
func main(){
16-
Test qwq = Test.A;
16+
Test qwq = A;
1717
print(qwq);
1818
}
1919
""".trimIndent()

0 commit comments

Comments
 (0)