Skip to content

Commit 82ecdcc

Browse files
committed
重构类型转换相关的方法
1 parent d231968 commit 82ecdcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1122
-291
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
372372
override fun visitCastExpression(ctx: mcfppParser.CastExpressionContext): Var<*> {
373373
Project.ctx = ctx
374374
val a: Var<*>? = visit(ctx.unaryExpression())
375-
return a!!.cast(MCFPPType.parseFromIdentifier(ctx.type().text, Function.currFunction.field))
375+
return a!!.explicitCast(MCFPPType.parseFromIdentifier(ctx.type().text, Function.currFunction.field))
376376
}
377377

378378
/**

src/main/kotlin/top/mcfpp/lang/ClassPointer.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,9 @@ class ClassPointer : Var<Int>{
132132
}
133133

134134
@Override
135-
override fun cast(type: MCFPPType): Var<*> {
135+
override fun explicitCast(type: MCFPPType): Var<*> {
136136
if(MCFPPType.baseType.contains(type)){
137-
LogProcessor.error("Cannot cast [${this.type}] to [$type]")
138-
throw VariableConverseException()
137+
buildCastErrorVar(type)
139138
}
140139
//TODO: 这里有问题,class类型的问题
141140
val namespace = StringHelper.splitNamespaceID(type.typeName)
@@ -145,8 +144,7 @@ class ClassPointer : Var<Int>{
145144
return UnknownVar("${type}_ptr" + UUID.randomUUID())
146145
}
147146
if (!this.clazz.canCastTo(c)) {
148-
LogProcessor.error("Cannot cast [${this.type}] to [$type]")
149-
throw VariableConverseException()
147+
buildCastErrorVar(type)
150148
}
151149
val re = ClassPointer(this)
152150
return re

src/main/kotlin/top/mcfpp/lang/DataTemplateObject.kt

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ open class DataTemplateObject : Var<CompoundTag> {
110110
}
111111
}
112112

113-
override fun cast(type: MCFPPType): Var<*> {
113+
override fun explicitCast(type: MCFPPType): Var<*> {
114+
val r = super.explicitCast(type)
115+
if(!r.isError) return r
114116
when(type){
115117
MCFPPNBTType.NBT -> {
116118
val re = NBTBasedData<CompoundTag>(this.identifier)
@@ -123,14 +125,30 @@ open class DataTemplateObject : Var<CompoundTag> {
123125
val re = DataTemplateObject(type.template, this.identifier)
124126
re.nbtPath = nbtPath
125127
return re
128+
}else{
129+
return buildCastErrorVar(type)
126130
}
127-
LogProcessor.error("Cannot cast [${this.type}] to [$type]")
128-
throw VariableConverseException()
129131
}
130132

131-
else -> {
132-
throw VariableConverseException()
133+
else -> return r
134+
}
135+
}
136+
137+
override fun implicitCast(type: MCFPPType): Var<*> {
138+
val r = super.implicitCast(type)
139+
if(!r.isError) return r
140+
when(type){
141+
is MCFPPDataTemplateType -> {
142+
if(type.template.canCastTo(templateType)){
143+
val re = DataTemplateObject(type.template, this.identifier)
144+
re.nbtPath = nbtPath
145+
return re
146+
}else{
147+
return buildCastErrorVar(type)
148+
}
133149
}
150+
151+
else -> return r
134152
}
135153
}
136154

src/main/kotlin/top/mcfpp/lang/EntityVar.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,6 @@ open class EntityVar : NBTBasedData<IntArrayTag>{
8585
return this
8686
}
8787

88-
/**
89-
* 将这个变量强制转换为一个类型
90-
* @param type 要转换到的目标类型
91-
*/
92-
override fun cast(type: MCFPPType): Var<*> {
93-
return when(type){
94-
BaseEntity -> this
95-
MCFPPNBTType.NBT -> this
96-
MCFPPBaseType.Any -> MCAny(this.identifier)
97-
else -> {
98-
LogProcessor.error("Cannot cast [${this.type}] to [$type]")
99-
UnknownVar(this.identifier)
100-
}
101-
}
102-
}
103-
10488
companion object {
10589
val data = CompoundData("entity","mcfpp")
10690
}

src/main/kotlin/top/mcfpp/lang/EnumVar.kt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ open class EnumVar : Var<Int> {
7272
}
7373
}
7474

75-
override fun cast(type: MCFPPType): Var<*> {
76-
return when (type) {
77-
this.type -> this
78-
MCFPPBaseType.Any -> MCAnyConcrete(this)
79-
else -> {
80-
LogProcessor.error("Cannot cast [${this.type}] to [$type]")
81-
throw VariableConverseException()
82-
}
83-
}
84-
}
85-
8675
override fun clone(): Var<*> {
8776
return EnumVar(this)
8877
}
@@ -211,7 +200,7 @@ class EnumVarConcrete : EnumVar, MCFPPValue<Int>{
211200
}
212201

213202
@Override
214-
override fun cast(type: MCFPPType): Var<*> {
203+
override fun explicitCast(type: MCFPPType): Var<*> {
215204
//TODO 类支持
216205
return when (type) {
217206
this.type -> this

src/main/kotlin/top/mcfpp/lang/JavaVar.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,6 @@ class JavaVar : Var<Any>, MCFPPValue<Any?>{
8282
return this
8383
}
8484

85-
/**
86-
* 将这个变量强制转换为一个类型
87-
* @param type 要转换到的目标类型
88-
*/
89-
override fun cast(type: MCFPPType): Var<*> {
90-
return when(type){
91-
MCFPPBaseType.JavaVar -> this
92-
MCFPPBaseType.Any -> MCAnyConcrete(this)
93-
else -> throw VariableConverseException()
94-
}
95-
}
96-
9785
override fun clone(): JavaVar {
9886
return JavaVar(this)
9987
}

src/main/kotlin/top/mcfpp/lang/JsonText.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,6 @@ open class JsonText : NBTBasedData<CompoundTag> {
5959
return this
6060
}
6161

62-
override fun cast(type: MCFPPType): Var<*> {
63-
return when(type){
64-
MCFPPBaseType.JText -> this
65-
MCFPPNBTType.NBT -> NBTBasedData(this)
66-
MCFPPBaseType.Any -> MCAnyConcrete(this)
67-
else -> {
68-
LogProcessor.error("Cannot cast [${this.type}] to [$type]")
69-
UnknownVar(this.identifier)
70-
}
71-
}
72-
}
73-
7462
override fun clone(): NBTBasedData<CompoundTag> {
7563
return JsonText(this)
7664
}

src/main/kotlin/top/mcfpp/lang/MCAny.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ open class MCAny : Var<Var<*>> {
117117
* 将这个变量强制转换为一个类型
118118
* @param type 要转换到的目标类型
119119
*/
120-
override fun cast(type: MCFPPType): Var<*> {
120+
override fun explicitCast(type: MCFPPType): Var<*> {
121121
when(type){
122122
MCFPPBaseType.Any -> return this
123123
else -> {
124124
LogProcessor.warn("Try to cast any to ${type.typeName}")
125-
return Var.build(this.identifier, type, parentClass()?:parentTemplate()?:Function.currFunction)
125+
return build(this.identifier, type, parentClass()?:parentTemplate()?:Function.currFunction)
126126
}
127127
}
128128
}
@@ -251,8 +251,8 @@ class MCAnyConcrete : MCAny, MCFPPValue<Var<*>>{
251251
LogProcessor.warn("Try to assign ${b.value.type.typeName} to ${this.value.type.typeName}")
252252
}
253253
//构造假设变量
254-
val t = Var.build(this.identifier, b.value.type, parentClass()?:parentTemplate()?:Function.currFunction)
255-
val v = Var.build(b.identifier, b.value.type, b.parentClass()?:b.parentTemplate()?:Function.currFunction)
254+
val t = build(this.identifier, b.value.type, parentClass()?:parentTemplate()?:Function.currFunction)
255+
val v = build(b.identifier, b.value.type, b.parentClass()?:b.parentTemplate()?:Function.currFunction)
256256
t.assign(v)
257257
this.value = b.value
258258
return this
@@ -263,7 +263,7 @@ class MCAnyConcrete : MCAny, MCFPPValue<Var<*>>{
263263
}
264264
else -> {
265265
this.value = b
266-
val t = Var.build(this.identifier, b.type, parentClass()?:parentTemplate()?:Function.currFunction)
266+
val t = build(this.identifier, b.type, parentClass()?:parentTemplate()?:Function.currFunction)
267267
t.assign(b)
268268
return this
269269
}

src/main/kotlin/top/mcfpp/lang/MCBool.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,6 @@ open class MCBool : Var<Boolean>, OnScoreboard {
5858
}
5959
}
6060

61-
@Override
62-
override fun cast(type: MCFPPType): Var<*> {
63-
return when(type){
64-
MCFPPBaseType.Bool -> this
65-
MCFPPBaseType.Any -> MCAnyConcrete(this)
66-
else -> throw VariableConverseException()
67-
}
68-
}
69-
7061
@InsertCommand
7162
open fun equalCommand(a: MCBool): MCBool {
7263
//re = t == a

src/main/kotlin/top/mcfpp/lang/MCFPPTypeVar.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ open class MCFPPTypeVar : Var<MCFPPType>, MCFPPValue<MCFPPType> {
2828
return this
2929
}
3030

31-
override fun cast(type: MCFPPType): Var<*> {
32-
if(type.typeName == "type"){
33-
return this
34-
}else{
35-
throw Exception("Cannot cast a ${type.typeName} to a MCFPPTypeVar")
36-
}
37-
}
38-
3931
override fun clone(): Var<*> {
4032
return this
4133
}

0 commit comments

Comments
 (0)