Skip to content

Commit fd3fc8e

Browse files
committed
NBT类型补充
1 parent 9faafa0 commit fd3fc8e

File tree

13 files changed

+164
-29
lines changed

13 files changed

+164
-29
lines changed

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/antlr/mcfppLexer.g4

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ VEC: 'vec';
120120
INT: 'int';
121121
ENTITY: 'entity';
122122
BOOL: 'bool';
123+
BYTE: 'byte';
124+
SHORT: 'short';
125+
LONG: 'long';
123126
FLOAT: 'float';
124127
DOUBLE: 'double';
125128
SELECTOR: 'selector';
@@ -188,7 +191,7 @@ NBTByte: IntConstant NBTByteSuffix;
188191
NBTShort: IntConstant NBTShortSuffix;
189192
NBTInt: IntConstant;
190193
NBTLong: IntConstant NBTLongSuffix;
191-
NBTFloat: FractionalConstant ExponentPart?;
194+
NBTFloat: (FractionalConstant ExponentPart? NBTFloatSuffix?) | (IntConstant NBTFloatSuffix?);
192195
NBTDouble: (DigitSequence|FractionalConstant) ExponentPart? NBTDoubleSuffix;
193196
NBTBool: BooleanConstant;
194197

src/main/antlr/mcfppParser.g4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,9 @@ normalType
593593
: INT
594594
| ENTITY
595595
| BOOL
596+
| BYTE
597+
| SHORT
598+
| LONG
596599
| FLOAT
597600
| DOUBLE
598601
| SELECTOR

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import top.mcfpp.type.MCFPPEnumType
99
import top.mcfpp.type.MCFPPGenericClassType
1010
import top.mcfpp.type.MCFPPType
1111
import top.mcfpp.core.lang.MCFPPValue
12-
import top.mcfpp.core.lang.nbt.MCStringConcrete
13-
import top.mcfpp.core.lang.nbt.NBTBasedDataConcrete
14-
import top.mcfpp.core.lang.nbt.NBTDictionaryConcrete
15-
import top.mcfpp.core.lang.nbt.NBTListConcrete
12+
import top.mcfpp.core.lang.nbt.*
1613
import top.mcfpp.lib.NBTPath
1714
import top.mcfpp.model.Class
1815
import top.mcfpp.model.DataTemplate
@@ -27,6 +24,11 @@ import top.mcfpp.model.generic.GenericClass
2724
import top.mcfpp.type.MCFPPBaseType
2825
import top.mcfpp.util.BoolTag
2926
import top.mcfpp.util.LogProcessor
27+
import top.mcfpp.util.NBTUtil.toNBTByte
28+
import top.mcfpp.util.NBTUtil.toNBTDouble
29+
import top.mcfpp.util.NBTUtil.toNBTFloat
30+
import top.mcfpp.util.NBTUtil.toNBTLong
31+
import top.mcfpp.util.NBTUtil.toNBTShort
3032
import top.mcfpp.util.StringHelper
3133
import top.mcfpp.util.TextTranslator
3234
import top.mcfpp.util.TextTranslator.translate
@@ -639,23 +641,25 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
639641

640642
private lateinit var path : NBTPath
641643

644+
642645
override fun visitNbtValue(ctx: mcfppParser.NbtValueContext): Var<*> {
646+
val qwq: Byte = 1;
643647
if(ctx.LineString() != null) {
644648
return NBTBasedDataConcrete(StringTag(ctx.LineString().text))
645649
}else if(ctx.nbtBool() != null){
646650
return NBTBasedDataConcrete(BoolTag(ctx.nbtBool().text == "true"))
647651
}else if(ctx.nbtByte() != null){
648-
return NBTBasedDataConcrete(ByteTag(ctx.nbtByte().text.toByte()))
652+
return MCByteConcrete(ctx.nbtByte().text.toNBTByte())
649653
}else if(ctx.nbtShort() != null){
650-
return NBTBasedDataConcrete(ShortTag(ctx.nbtShort().text.toShort()))
654+
return MCShortConcrete(ctx.nbtShort().text.toNBTShort())
651655
}else if(ctx.nbtInt() != null) {
652-
return NBTBasedDataConcrete(IntTag(ctx.nbtInt().text.toInt()))
656+
return MCIntConcrete(ctx.nbtInt().text.toInt())
653657
}else if(ctx.nbtLong() != null){
654-
return NBTBasedDataConcrete(LongTag(ctx.nbtLong().text.toLong()))
658+
return MCLongConcrete(LongTag(ctx.nbtLong().text.toNBTLong()))
655659
}else if(ctx.nbtFloat() != null){
656-
return NBTBasedDataConcrete(FloatTag(ctx.nbtFloat().text.toFloat()))
660+
return MCFloatConcrete(ctx.nbtFloat().text.toNBTFloat())
657661
}else if(ctx.nbtDouble() != null) {
658-
return NBTBasedDataConcrete(DoubleTag(ctx.nbtDouble().text.toDouble()))
662+
return MCDoubleConcrete(DoubleTag(ctx.nbtDouble().text.toNBTDouble()))
659663
}else if(ctx.nbtCompound() != null){
660664
val compound = NBTDictionaryConcrete(HashMap())
661665
for (kv in ctx.nbtCompound().nbtKeyValuePair()){

src/main/kotlin/top/mcfpp/core/lang/nbt/MCByte.kt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ open class MCByte: MCInt {
3333
override fun doAssignedBy(b: Var<*>) : MCByte {
3434
return when (b) {
3535
is MCByte -> {
36-
MCByte(assignCommand(b))
36+
assignCommand(b)
3737
}
3838

3939
is CommandReturn -> {
@@ -54,6 +54,44 @@ open class MCByte: MCInt {
5454
}
5555
}
5656

57+
//this = a
58+
@InsertCommand
59+
override fun assignCommand(a: MCNumber<*>) : MCByte {
60+
return assignCommandLambda(a,
61+
ifThisIsClassMemberAndAIsConcrete = { b, final ->
62+
//对类中的成员的值进行修改
63+
if(final.size == 2){
64+
Function.addCommand(final[0])
65+
}
66+
Function.addCommand(final.last().build(Commands.sbPlayerSet(this, (b as MCIntConcrete).value)))
67+
this
68+
},
69+
ifThisIsClassMemberAndAIsNotConcrete = { b, final ->
70+
//对类中的成员的值进行修改
71+
if(final.size == 2){
72+
Function.addCommand(final[0])
73+
}
74+
Function.addCommand(final.last().build(Commands.sbPlayerOperation(this,"=",b as MCInt)))
75+
this
76+
},
77+
ifThisIsNormalVarAndAIsConcrete = { b, _ ->
78+
MCByteConcrete(this, (b as MCByteConcrete).value)
79+
},
80+
ifThisIsNormalVarAndAIsClassMember = { c, cmd ->
81+
if(cmd.size == 2){
82+
Function.addCommand(cmd[0])
83+
}
84+
Function.addCommand(cmd.last().build(Commands.sbPlayerOperation(this, "=", c as MCInt)))
85+
MCByte(this)
86+
},
87+
ifThisIsNormalVarAndAIsNotConcrete = { c, _ ->
88+
//变量进栈
89+
Function.addCommand(Commands.sbPlayerOperation(this, "=", c as MCInt))
90+
MCByte(this)
91+
}
92+
) as MCByte
93+
}
94+
5795
override fun canAssignedBy(b: Var<*>): Boolean {
5896
if(!b.implicitCast(type).isError) return true
5997
return when(b){
@@ -88,7 +126,7 @@ class MCByteConcrete: MCByte, MCFPPValue<Byte> {
88126
curr: FieldContainer,
89127
value: Byte,
90128
identifier: String = UUID.randomUUID().toString()
91-
) : super(curr.prefix + identifier) {
129+
) : super(curr, identifier) {
92130
this.value = value
93131
}
94132

src/main/kotlin/top/mcfpp/core/lang/nbt/MCDouble.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ open class MCDouble: NBTBasedData {
3030
constructor(
3131
curr: FieldContainer,
3232
identifier: String = UUID.randomUUID().toString()
33-
) : super(curr.prefix + identifier) {
33+
) : super(curr, identifier) {
3434
this.identifier = identifier
3535
}
3636

src/main/kotlin/top/mcfpp/core/lang/nbt/MCLong.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class MCLongConcrete: MCLong, MCFPPValue<LongTag> {
200200
curr: FieldContainer,
201201
value: LongTag,
202202
identifier: String = UUID.randomUUID().toString()
203-
) : super(curr.prefix + identifier) {
203+
) : super(curr, identifier) {
204204
this.value = value
205205
}
206206

src/main/kotlin/top/mcfpp/core/lang/nbt/MCShort.kt

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package top.mcfpp.core.lang.nbt
22

33
import net.querz.nbt.tag.ByteTag
44
import net.querz.nbt.tag.ShortTag
5+
import top.mcfpp.annotations.InsertCommand
56
import top.mcfpp.command.Commands
67
import top.mcfpp.core.lang.*
78
import top.mcfpp.core.lang.bool.ScoreBoolConcrete
@@ -54,6 +55,45 @@ open class MCShort: MCInt {
5455
}
5556
}
5657

58+
59+
//this = a
60+
@InsertCommand
61+
override fun assignCommand(a: MCNumber<*>) : MCShort {
62+
return assignCommandLambda(a,
63+
ifThisIsClassMemberAndAIsConcrete = { b, final ->
64+
//对类中的成员的值进行修改
65+
if(final.size == 2){
66+
Function.addCommand(final[0])
67+
}
68+
Function.addCommand(final.last().build(Commands.sbPlayerSet(this, (b as MCIntConcrete).value)))
69+
this
70+
},
71+
ifThisIsClassMemberAndAIsNotConcrete = { b, final ->
72+
//对类中的成员的值进行修改
73+
if(final.size == 2){
74+
Function.addCommand(final[0])
75+
}
76+
Function.addCommand(final.last().build(Commands.sbPlayerOperation(this,"=",b as MCInt)))
77+
this
78+
},
79+
ifThisIsNormalVarAndAIsConcrete = { b, _ ->
80+
MCShortConcrete(this, (b as MCShortConcrete).value)
81+
},
82+
ifThisIsNormalVarAndAIsClassMember = { c, cmd ->
83+
if(cmd.size == 2){
84+
Function.addCommand(cmd[0])
85+
}
86+
Function.addCommand(cmd.last().build(Commands.sbPlayerOperation(this, "=", c as MCInt)))
87+
MCByte(this)
88+
},
89+
ifThisIsNormalVarAndAIsNotConcrete = { c, _ ->
90+
//变量进栈
91+
Function.addCommand(Commands.sbPlayerOperation(this, "=", c as MCInt))
92+
MCByte(this)
93+
}
94+
) as MCShort
95+
}
96+
5797
override fun canAssignedBy(b: Var<*>): Boolean {
5898
if(!b.implicitCast(type).isError) return true
5999
return when(b){
@@ -78,7 +118,7 @@ class MCShortConcrete: MCShort, MCFPPValue<Short> {
78118
curr: FieldContainer,
79119
value: Short,
80120
identifier: String = UUID.randomUUID().toString()
81-
) : super(curr.prefix + identifier) {
121+
) : super(curr, identifier) {
82122
this.value = value
83123
}
84124

@@ -91,7 +131,7 @@ class MCShortConcrete: MCShort, MCFPPValue<Short> {
91131
this.value = value
92132
}
93133

94-
constructor(v: MCByte, value: Short) : super(v) {
134+
constructor(v: MCShort, value: Short) : super(v) {
95135
this.value = value
96136
}
97137

@@ -105,7 +145,7 @@ class MCShortConcrete: MCShort, MCFPPValue<Short> {
105145

106146
override fun toDynamic(replace: Boolean): Var<*> {
107147
MCIntConcrete(this, value.toInt()).toDynamic(replace)
108-
return MCByte(this)
148+
return MCShort(this)
109149
}
110150

111151
override fun plus(a: Var<*>): Var<*>? {

src/main/kotlin/top/mcfpp/type/MCFPPType.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,14 @@ open class MCFPPType(
222222
MCFPPBaseType.Void.typeName to MCFPPBaseType.Void,
223223
MCFPPEntityType.Entity.typeName to MCFPPEntityType.Entity,
224224
MCFPPConcreteType.Type.typeName to MCFPPConcreteType.Type,
225+
MCFPPNBTType.Byte.typeName to MCFPPNBTType.Byte,
226+
MCFPPNBTType.Short.typeName to MCFPPNBTType.Short,
225227
MCFPPBaseType.Int.typeName to MCFPPBaseType.Int,
228+
MCFPPNBTType.Long.typeName to MCFPPNBTType.Long,
229+
MCFPPNBTType.Double.typeName to MCFPPNBTType.Double,
230+
MCFPPBaseType.Float.typeName to MCFPPBaseType.Float,
226231
MCFPPBaseType.Bool.typeName to MCFPPBaseType.Bool,
227232
MCFPPBaseType.String.typeName to MCFPPBaseType.String,
228-
MCFPPBaseType.Float.typeName to MCFPPBaseType.Float,
229233
MCFPPBaseType.Any.typeName to MCFPPBaseType.Any,
230234
MCFPPEntityType.Selector.typeName to MCFPPEntityType.Selector,
231235
MCFPPConcreteType.JavaVar.typeName to MCFPPConcreteType.JavaVar,

src/main/kotlin/top/mcfpp/util/NBTUtil.kt

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import top.mcfpp.exception.VariableConverseException
55
import top.mcfpp.core.lang.*
66
import top.mcfpp.core.lang.MCFPPValue
77
import top.mcfpp.core.lang.bool.ScoreBoolConcrete
8-
import top.mcfpp.core.lang.nbt.MCStringConcrete
9-
import top.mcfpp.core.lang.nbt.NBTBasedDataConcrete
8+
import top.mcfpp.core.lang.nbt.*
109

1110
object NBTUtil {
1211

@@ -19,9 +18,13 @@ object NBTUtil {
1918
//is JsonString -> TODO()
2019
is MCAnyConcrete -> varToNBT(v.value)
2120
is ScoreBoolConcrete -> ByteTag(v.value)
21+
is MCByteConcrete -> ByteTag(v.value)
22+
is MCShortConcrete -> ShortTag(v.value)
23+
is MCIntConcrete -> IntTag(v.value)
24+
is MCLongConcrete -> v.value
2225
is MCFloatConcrete -> FloatTag(v.value)
26+
is MCDoubleConcrete -> v.value
2327
is MCFPPTypeVar -> TODO()
24-
is MCIntConcrete -> IntTag(v.value)
2528
is MCStringConcrete -> v.value
2629
is NBTBasedDataConcrete -> v.value
2730
is UnionTypeVarConcrete -> valueToNBT(v.value)
@@ -132,4 +135,44 @@ object NBTUtil {
132135
else -> throw VariableConverseException()
133136
}
134137
}
138+
139+
fun String.toNBTByte(): Byte{
140+
return if(endsWith("b") || endsWith("B")){
141+
substring(0, length - 1).toByte()
142+
}else{
143+
toByte()
144+
}
145+
}
146+
147+
fun String.toNBTShort(): Short{
148+
return if(endsWith("s") || endsWith("S")){
149+
substring(0, length - 1).toShort()
150+
}else{
151+
toShort()
152+
}
153+
}
154+
155+
fun String.toNBTLong(): Long{
156+
return if(endsWith("l") || endsWith("L")){
157+
substring(0, length - 1).toLong()
158+
}else{
159+
toLong()
160+
}
161+
}
162+
163+
fun String.toNBTFloat(): Float{
164+
return if(endsWith("f") || endsWith("F")){
165+
substring(0, length - 1).toFloat()
166+
}else{
167+
toFloat()
168+
}
169+
}
170+
171+
fun String.toNBTDouble(): Double{
172+
return if(endsWith("d") || endsWith("D")){
173+
substring(0, length - 1).toDouble()
174+
}else{
175+
toDouble()
176+
}
177+
}
135178
}

0 commit comments

Comments
 (0)