Skip to content

Commit dcce6fa

Browse files
committed
NBTAnyDataTypeTest.kt测试通过
1 parent 6a775a9 commit dcce6fa

32 files changed

+267
-276
lines changed

src/main/antlr/mcfppLexer.g4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ DOC_COMMENT
229229
: '#{' .*? '}#'
230230
;
231231

232+
SIMPLE_DOC_COMMENT
233+
: '#>'.*? '\n'
234+
;
235+
232236
BLOCK_COMMENT
233237
: '##' .*? '##' -> skip
234238
;

src/main/java/top/mcfpp/mni/MCBoolConcreteData.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ public static void or(BaseBool a, BaseBool caller, ValueWrapper<BaseBool> re){
2525
public static void and(BaseBool a, BaseBool caller, ValueWrapper<BaseBool> re){
2626
re.setValue((BaseBool) caller.and(a));
2727
}
28+
29+
@SuppressWarnings("DataFlowIssue")
30+
@MNIOperator(operator = "!", returnType = "bool")
31+
public static void negation(BaseBool caller, ValueWrapper<BaseBool> re){
32+
re.setValue((BaseBool) caller.negation());
33+
}
2834
}

src/main/java/top/mcfpp/mni/NBTListConcreteData.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void prepend(Var<?> e, NBTListConcrete caller) throws IOException
5151
}
5252

5353
@InsertCommand
54-
@MNIFunction(normalParams = {"list<E> list"}, caller = "list", genericType = "E")
54+
@MNIFunction(normalParams = {"list<E>"}, caller = "list", genericType = "E")
5555
public static void prependAll(NBTList list, NBTListConcrete caller){
5656
if(list instanceof NBTListConcrete ec){
5757
for (var e : ec.getValue()){
@@ -63,7 +63,7 @@ public static void prependAll(NBTList list, NBTListConcrete caller){
6363
}
6464

6565
@InsertCommand
66-
@MNIFunction(normalParams = {"int index", "E e"}, caller = "list", genericType = "E")
66+
@MNIFunction(normalParams = {"int", "E"}, caller = "list", genericType = "E")
6767
public static void insert(MCInt index, Var<?> e, NBTListConcrete caller) throws IOException {
6868
if(e instanceof MCFPPValue<?> && index instanceof MCIntConcrete indexC){
6969
caller.getValue().add(indexC.getValue(), e);
@@ -73,7 +73,7 @@ public static void insert(MCInt index, Var<?> e, NBTListConcrete caller) throws
7373
}
7474

7575
@InsertCommand
76-
@MNIFunction(normalParams = {"int index"}, caller = "list", genericType = "E")
76+
@MNIFunction(normalParams = {"int"}, caller = "list", genericType = "E")
7777
public static void removeAt(MCInt index, NBTListConcrete caller){
7878
if(index instanceof MCIntConcrete indexC){
7979
//确定的
@@ -84,7 +84,7 @@ public static void removeAt(MCInt index, NBTListConcrete caller){
8484
}
8585
}
8686

87-
@MNIFunction(normalParams = {"E e"}, caller = "list", genericType = "E")
87+
@MNIFunction(normalParams = {"E"}, caller = "list", genericType = "E")
8888
public static void remove(@NotNull Var<?> var, NBTListConcrete caller){
8989
if(var instanceof MCFPPValue<?> vC){
9090
for (var e : caller.getValue()){
@@ -105,7 +105,7 @@ public static void remove(@NotNull Var<?> var, NBTListConcrete caller){
105105
}
106106

107107
@InsertCommand
108-
@MNIFunction(normalParams = {"E e"}, caller = "list", genericType = "E", returnType = "int")
108+
@MNIFunction(normalParams = {"E"}, caller = "list", genericType = "E", returnType = "int")
109109
public static void indexOf(Var<?> e, NBTListConcrete caller, ValueWrapper<MCInt> returnVar){
110110
if(e instanceof MCFPPValue<?>){
111111
//确定的
@@ -117,7 +117,7 @@ public static void indexOf(Var<?> e, NBTListConcrete caller, ValueWrapper<MCInt>
117117
}
118118

119119
@InsertCommand
120-
@MNIFunction(normalParams = {"E e"}, caller = "list", genericType = "E", returnType = "int")
120+
@MNIFunction(normalParams = {"E"}, caller = "list", genericType = "E", returnType = "int")
121121
public static void lastIndexOf(Var<?> e, NBTListConcrete caller, ValueWrapper<MCInt> returnVar){
122122
if(e instanceof MCFPPValue<?>){
123123
//确定的
@@ -133,7 +133,7 @@ public static void lastIndexOf(Var<?> e, NBTListConcrete caller, ValueWrapper<MC
133133
}
134134
}
135135

136-
@MNIFunction(normalParams = {"E e"}, caller = "list", genericType = "E", returnType = "bool")
136+
@MNIFunction(normalParams = {"E"}, caller = "list", genericType = "E", returnType = "bool")
137137
public static void contains(Var<?> e, NBTListConcrete caller, ValueWrapper<BaseBool> returnVar){
138138
if(e instanceof MCFPPValue eC){
139139
boolean contains = false;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ class MCFPPExprVisitor(
430430
if(p.first != null){
431431
LogProcessor.warn("Invalid namespace usage ${p.first} in function call ")
432432
}
433-
MCFPPFuncGetter().getFunction(currSelector!!,p.second, readOnlyArgs, normalArgs)
433+
MCFPPFuncGetter.getFunction(currSelector!!,p.second, readOnlyArgs, normalArgs)
434434
}
435435
//调用函数
436436
if (func !is UnknownFunction) {
@@ -517,12 +517,12 @@ class MCFPPExprVisitor(
517517
re.first!!
518518
}
519519
}
520-
if(re is UnknownVar && currSelector != null){
520+
if(re is UnknownVar && currSelector == null){
521521
//从类型获取
522522
val typeStr = ctx.Identifier().text
523523
val type = MCFPPType.parseFromString(typeStr, Function.currFunction.field)
524524
if(type == null){
525-
LogProcessor.error(TextTranslator.SYMBOL_NOT_DEFINED.translate(currSelector!!.identifier))
525+
LogProcessor.error(TextTranslator.SYMBOL_NOT_DEFINED.translate(ctx.text))
526526
}else{
527527
re = ObjectVar(type)
528528
}

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

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package top.mcfpp.antlr
33
import top.mcfpp.Project
44
import top.mcfpp.Project.withCompilationContext
55
import top.mcfpp.annotations.InsertCommand
6-
import top.mcfpp.annotations.MNIOperator
76
import top.mcfpp.annotations.MNIFunction
7+
import top.mcfpp.annotations.MNIOperator
88
import top.mcfpp.antlr.mcfppParser.ClassDeclarationContext
99
import top.mcfpp.antlr.mcfppParser.TemplateDeclarationContext
1010
import top.mcfpp.compiletime.CompileTimeFunction
@@ -586,7 +586,7 @@ open class MCFPPFieldVisitor : mcfppParserBaseVisitor<Any?>() {
586586
val op = ctx.supportOperator().text
587587
//创建函数对象
588588
val f = Function(
589-
ctx.supportOperator().text,
589+
op,
590590
Class.currClass!!,
591591
ctx.functionBody()
592592
)
@@ -600,15 +600,22 @@ open class MCFPPFieldVisitor : mcfppParserBaseVisitor<Any?>() {
600600
//解析参数
601601
f.addParamsFromContext(ctx.functionParams())
602602
//参数数量检查
603-
if(f.normalParams.size != 1){
603+
if(Var.unaryOp.contains(op) && f.normalParams.size != 0){
604+
LogProcessor.error("Unary operator $op must have no parameter: ${ctx.text}")
605+
return null
606+
}else if(Var.binaryOp.contains(op) && f.normalParams.size!= 1){
607+
LogProcessor.error("Binary operator $op must have only one parameter: ${ctx.text}")
608+
return null
609+
}else if(f.normalParams.size != 1){
604610
LogProcessor.error("Operator $op must have only one parameter: ${ctx.text}")
605611
return null
606612
}
613+
val type = f.normalParams.firstOrNull()?.type
607614
//注册函数
608-
if (Class.currClass!!.field.hasOperator(op, f.normalParams[0].type)) {
609-
LogProcessor.error("Already defined operator: $op(${f.normalParams[0].type}) in class " + Class.currClass!!.identifier)
615+
if (Class.currClass!!.field.hasOperator(op, type)) {
616+
LogProcessor.error("Already defined operator: $op($type) in class " + Class.currClass!!.identifier)
610617
} else {
611-
Class.currClass!!.field.addOperator(op, f.normalParams[0].type, f)
618+
Class.currClass!!.field.addOperator(op, type, f)
612619
}
613620
f.ast = null
614621
return f
@@ -618,7 +625,7 @@ open class MCFPPFieldVisitor : mcfppParserBaseVisitor<Any?>() {
618625
val op = ctx.supportOperator().text
619626
//创建函数对象
620627
val f = Function(
621-
ctx.supportOperator().text,
628+
op,
622629
DataTemplate.currTemplate!!,
623630
ctx.functionBody()
624631
)
@@ -632,15 +639,22 @@ open class MCFPPFieldVisitor : mcfppParserBaseVisitor<Any?>() {
632639
//解析参数
633640
f.addParamsFromContext(ctx.functionParams())
634641
//参数数量检查
635-
if(f.normalParams.size != 1){
642+
if(Var.unaryOp.contains(op) && f.normalParams.size != 0){
643+
LogProcessor.error("Unary operator $op must have no parameter: ${ctx.text}")
644+
return null
645+
}else if(Var.binaryOp.contains(op) && f.normalParams.size!= 1){
646+
LogProcessor.error("Binary operator $op must have only one parameter: ${ctx.text}")
647+
return null
648+
}else if(f.normalParams.size != 1){
636649
LogProcessor.error("Operator $op must have only one parameter: ${ctx.text}")
637650
return null
638651
}
652+
val type = f.normalParams.firstOrNull()?.type
639653
//注册函数
640-
if (DataTemplate.currTemplate!!.field.hasOperator(op, f.normalParams[0].type)) {
641-
LogProcessor.error("Already defined operator: $op(${f.normalParams[0].type}) in class " + DataTemplate.currTemplate!!.identifier)
654+
if (DataTemplate.currTemplate!!.field.hasOperator(op, type)) {
655+
LogProcessor.error("Already defined operator: $op($type) in class " + DataTemplate.currTemplate!!.identifier)
642656
} else {
643-
DataTemplate.currTemplate!!.field.addOperator(op, f.normalParams[0].type, f)
657+
DataTemplate.currTemplate!!.field.addOperator(op, type, f)
644658
}
645659
f.ast = null
646660
return f
@@ -664,10 +678,17 @@ open class MCFPPFieldVisitor : mcfppParserBaseVisitor<Any?>() {
664678
}
665679
nf.addParamsFromContext(ctx.functionParams())
666680
//参数数量检查
667-
if(nf.normalParams.size != 1){
681+
if(Var.unaryOp.contains(op) && nf.normalParams.size != 0){
682+
LogProcessor.error("Unary operator $op must have no parameter: ${ctx.text}")
683+
return null
684+
}else if(Var.binaryOp.contains(op) && nf.normalParams.size!= 1){
685+
LogProcessor.error("Binary operator $op must have only one parameter: ${ctx.text}")
686+
return null
687+
}else if(nf.normalParams.size != 1){
668688
LogProcessor.error("Operator $op must have only one parameter: ${ctx.text}")
669689
return null
670690
}
691+
val type = nf.normalParams.firstOrNull()?.type
671692
try {
672693
//根据JavaRefer找到类
673694
val refer = ctx.javaRefer().text
@@ -678,24 +699,24 @@ open class MCFPPFieldVisitor : mcfppParserBaseVisitor<Any?>() {
678699
for(method in methods){
679700
val mniRegister = method.getAnnotation(MNIOperator::class.java) ?: continue
680701
//比对
681-
if(nf.normalParams[0].type.typeName == mniRegister.paramType){
702+
if((type?.typeName ?: "") == mniRegister.paramType){
682703
hasFind = true
683704
nf.javaMethod = method
684705
break
685706
}
686707
}
687708
if(!hasFind){
688-
throw NoSuchMethodException("Cannot find operator $op(${nf.normalParams[0].type}) in jvm class $clsName")
709+
throw NoSuchMethodException("Cannot find operator $op($type) in jvm class $clsName")
689710
}
690711
} catch (e: ClassNotFoundException) {
691712
LogProcessor.error("Cannot find java class: " + e.message)
692713
return null
693714
}
694715
//注册函数
695-
if (Class.currClass!!.field.hasOperator(op, nf.normalParams[0].type)) {
716+
if (Class.currClass!!.field.hasOperator(op, type)) {
696717
LogProcessor.error("Already defined operator: $op(${nf.normalParams[0].type}) in template " + Class.currClass!!.identifier)
697718
} else {
698-
Class.currClass!!.field.addOperator(op, nf.normalParams[0].type, nf)
719+
Class.currClass!!.field.addOperator(op, type, nf)
699720
}
700721
return nf
701722
}
@@ -710,10 +731,17 @@ open class MCFPPFieldVisitor : mcfppParserBaseVisitor<Any?>() {
710731
}
711732
nf.addParamsFromContext(ctx.functionParams())
712733
//参数数量检查
713-
if(nf.normalParams.size != 1){
734+
if(Var.unaryOp.contains(op) && nf.normalParams.size != 0){
735+
LogProcessor.error("Unary operator $op must have no parameter: ${ctx.text}")
736+
return null
737+
}else if(Var.binaryOp.contains(op) && nf.normalParams.size!= 1){
738+
LogProcessor.error("Binary operator $op must have only one parameter: ${ctx.text}")
739+
return null
740+
}else if(nf.normalParams.size != 1){
714741
LogProcessor.error("Operator $op must have only one parameter: ${ctx.text}")
715742
return null
716743
}
744+
val type = nf.normalParams.firstOrNull()?.type
717745
try {
718746
//根据JavaRefer找到类
719747
val refer = ctx.javaRefer().text
@@ -724,24 +752,24 @@ open class MCFPPFieldVisitor : mcfppParserBaseVisitor<Any?>() {
724752
for(method in methods){
725753
val mniRegister = method.getAnnotation(MNIOperator::class.java) ?: continue
726754
//比对
727-
if(nf.normalParams[0].type.typeName == mniRegister.paramType){
755+
if((type?.typeName ?: "") == mniRegister.paramType){
728756
hasFind = true
729757
nf.javaMethod = method
730758
break
731759
}
732760
}
733761
if(!hasFind){
734-
throw NoSuchMethodException("Cannot find operator $op(${nf.normalParams[0].type}) in jvm class $clsName")
762+
throw NoSuchMethodException("Cannot find operator $op($type) in jvm class $clsName")
735763
}
736764
} catch (e: ClassNotFoundException) {
737765
LogProcessor.error("Cannot find java class: " + e.message)
738766
return null
739767
}
740768
//注册函数
741-
if (DataTemplate.currTemplate!!.field.hasOperator(op, nf.normalParams[0].type)) {
742-
LogProcessor.error("Already defined operator: $op(${nf.normalParams[0].type}) in template " + DataTemplate.currTemplate!!.identifier)
769+
if (DataTemplate.currTemplate!!.field.hasOperator(op, type)) {
770+
LogProcessor.error("Already defined operator: $op($type) in template " + DataTemplate.currTemplate!!.identifier)
743771
} else {
744-
DataTemplate.currTemplate!!.field.addOperator(op, nf.normalParams[0].type, nf)
772+
DataTemplate.currTemplate!!.field.addOperator(op, type, nf)
745773
}
746774
return nf
747775
}

src/main/kotlin/top/mcfpp/compiletime/CompileTimeFunctionField.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import top.mcfpp.core.lang.Var
44
import top.mcfpp.model.field.FunctionField
55
import top.mcfpp.model.field.IField
66

7-
class CompileTimeFunctionField(parent: IField?): FunctionField(parent) {
7+
class CompileTimeFunctionField: FunctionField {
8+
9+
constructor(parent: IField?) : super(parent)
10+
11+
constructor(parents: List<IField?>): super(parents)
12+
813
override fun clone(): CompileTimeFunctionField {
914
return clone(this)
1015
}

src/main/kotlin/top/mcfpp/core/lang/Var.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,5 +634,8 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember{
634634
}
635635
}
636636

637+
val binaryOp = arrayOf("+", "-", "*", "/", "==", "!=", "<", ">", "<=", ">=", "||", "&&", "|")
638+
val unaryOp = arrayOf("!", "&")
639+
637640
}
638641
}

src/main/kotlin/top/mcfpp/model/Namespace.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import top.mcfpp.core.lang.obj.DataTemplateObject
88
import top.mcfpp.model.compound.UnsolvedClass
99
import top.mcfpp.model.compound.UnsolvedTemplate
1010
import top.mcfpp.model.field.GlobalField
11+
import top.mcfpp.model.field.IField
1112
import top.mcfpp.model.field.NamespaceField
12-
import top.mcfpp.model.field.SimpleFieldWithTypeWithParent
13+
import top.mcfpp.model.field.SimpleFieldWithType
1314
import top.mcfpp.model.function.Function
14-
import top.mcfpp.model.function.NativeFunction
1515
import top.mcfpp.model.function.GenericFunction
16+
import top.mcfpp.model.function.NativeFunction
1617
import top.mcfpp.type.MCFPPBaseType
1718
import top.mcfpp.type.MCFPPGenericParamType
1819
import top.mcfpp.type.MCFPPPrivateType
@@ -104,7 +105,7 @@ class Namespace(val identifier: String): Serializable, FieldContainer {
104105
Project.currNamespace = this.identifier
105106
val methods = cls.methods
106107
//获取import方法
107-
val simpleFieldWithType = SimpleFieldWithTypeWithParent(field)
108+
val simpleFieldWithType = SimpleFieldWithType.getTypeScope()
108109
for(method in methods){
109110
if(method.name == "importToMNI"){
110111
if(method.parameterCount != 0){

src/main/kotlin/top/mcfpp/model/compound/CompoundData.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ open class CompoundData : FieldContainer, Serializable, WithDocument {
272272
return
273273
}
274274
//检查method的参数
275-
if(method.parameterCount != 3){
275+
if((paramType != null && method.parameterCount != 3) || (paramType == null && method.parameterCount != 2)){
276276
LogProcessor.error("Method ${method.name} in class ${method.declaringClass.name} has wrong parameter count")
277277
return
278278
}

0 commit comments

Comments
 (0)