Skip to content

Commit 2d2800f

Browse files
committed
重构布尔值的运算
1 parent 6028d61 commit 2d2800f

File tree

15 files changed

+169
-45
lines changed

15 files changed

+169
-45
lines changed

src/main/antlr/mcfppParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ primary
387387
| var
388388
| THIS
389389
| SUPER
390+
| type
390391
;
391392

392393
varWithSelector
@@ -590,7 +591,6 @@ value
590591
| LineString
591592
| multiLineStringLiteral
592593
| nbtValue
593-
| type
594594
| TargetSelector
595595
;
596596

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import top.mcfpp.command.Command;
88
import top.mcfpp.command.Commands;
99
import top.mcfpp.core.lang.*;
10+
import top.mcfpp.core.lang.bool.BaseBool;
11+
import top.mcfpp.core.lang.bool.ScoreBool;
12+
import top.mcfpp.core.lang.bool.ScoreBoolConcrete;
1013
import top.mcfpp.lib.ScoreChatComponent;
1114
import top.mcfpp.model.function.Function;
1215
import top.mcfpp.util.LogProcessor;
@@ -91,6 +94,26 @@ public static void print(@NotNull DataTemplateObject object) throws IOException
9194
}
9295
}
9396

97+
@InsertCommand
98+
@MNIFunction(normalParams = {"bool b"})
99+
public static void print(BaseBool bool){
100+
ScoreBool b;
101+
if(bool instanceof ScoreBool){
102+
b = (ScoreBool) bool;
103+
}else {
104+
b = bool.toScoreBool();
105+
}
106+
if(b instanceof ScoreBoolConcrete bC){
107+
Function.Companion.addCommand("tellraw @a " + bC.getValue());
108+
}else {
109+
if(b.getParent() != null){
110+
Function.Companion.addCommands(Commands.INSTANCE.selectRun(b.getParent(), "tellraw @a " + new ScoreChatComponent(b.asIntVar()).toCommandPart(), true));
111+
}else {
112+
Function.Companion.addCommand("tellraw @a " + new ScoreChatComponent(b.asIntVar()).toCommandPart());
113+
}
114+
}
115+
}
116+
94117
@MNIFunction
95118
public static void debug(){
96119
//噢,在这里断点,这样就可以断点编译了

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import top.mcfpp.model.function.UnknownFunction
2525
import top.mcfpp.model.generic.Generic
2626
import top.mcfpp.model.generic.GenericClass
2727
import top.mcfpp.type.MCFPPBaseType
28+
import top.mcfpp.util.BoolTag
2829
import top.mcfpp.util.LogProcessor
2930
import top.mcfpp.util.StringHelper
3031
import top.mcfpp.util.TextTranslator
@@ -57,9 +58,6 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
5758
Function.currFunction = f
5859
return if(ctx.primary() != null){
5960
val q = visit(ctx.primary())
60-
if(q is UnknownVar){
61-
LogProcessor.error(TextTranslator.VARIABLE_NOT_DEFINED.translate(q.identifier))
62-
}
6361
Function.currFunction = l
6462
l.commands.addAll(f.commands)
6563
q
@@ -109,11 +107,7 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
109107
visitConditionalAndExpressionRe = visit(ctx.equalityExpression(0))
110108
processVarCache.add(visitConditionalAndExpressionRe!!)
111109
for (i in 1 until ctx.equalityExpression().size) {
112-
var b: Var<*>? = visit(ctx.equalityExpression(i))
113-
if(b is MCFloat) b = b.toTempEntity()
114-
if(visitConditionalAndExpressionRe!! != MCFloat.ssObj){
115-
visitConditionalAndExpressionRe = visitConditionalAndExpressionRe!!.getTempVar()
116-
}
110+
val b: Var<*>? = visit(ctx.equalityExpression(i))
117111
visitConditionalAndExpressionRe = visitConditionalAndExpressionRe!!.binaryComputation(b!!, "&&")
118112
processVarCache[processVarCache.size - 1] = visitConditionalAndExpressionRe!!
119113
}
@@ -322,7 +316,11 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
322316
Project.ctx = ctx
323317
if (ctx.`var`() != null) {
324318
//变量
325-
return visit(ctx.`var`())
319+
val qwq = visit(ctx.`var`())
320+
if(qwq is UnknownVar){
321+
LogProcessor.error(TextTranslator.VARIABLE_NOT_DEFINED.translate(qwq.identifier))
322+
}
323+
return qwq
326324
} else if (ctx.value() != null) {
327325
return visit(ctx.value())
328326
} else if (ctx.range() != null){
@@ -350,6 +348,11 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
350348
LogProcessor.error("Range sides should be a number: ${left?.type} and ${right?.type}")
351349
return UnknownVar("range_" + UUID.randomUUID())
352350
}
351+
} else if (ctx.type() != null){
352+
return MCFPPTypeVar(MCFPPType.parseFromIdentifier(ctx.type().text, Function.currFunction.field)?: run {
353+
LogProcessor.error(TextTranslator.INVALID_TYPE_ERROR.translate(ctx.type().text))
354+
MCFPPBaseType.Any
355+
})
353356
} else {
354357
//this或者super
355358
val re: Var<*>? = Function.field.getVar(ctx.text)
@@ -576,15 +579,8 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
576579
stringArray.add(tailQuote.substring(3,tailQuote.length))
577580
}
578581
return MCStringConcrete(StringTag(stringArray.joinToString("")) ) //没有解析值就变不了MCString了
579-
} else if (ctx.boolValue() != null){
580-
return ScoreBoolConcrete(ctx.boolValue()!!.text.toBoolean())
581582
} else if (ctx.nbtValue() != null){
582583
return visit(ctx.nbtValue())
583-
} else if (ctx.type() != null){
584-
return MCFPPTypeVar(MCFPPType.parseFromIdentifier(ctx.type().text, Function.currFunction.field)?: run {
585-
LogProcessor.error(TextTranslator.INVALID_TYPE_ERROR.translate(ctx.type().text))
586-
MCFPPBaseType.Any
587-
})
588584
} else if (ctx.TargetSelector() != null){
589585
TODO()
590586
} else if(ctx.coordinate() != null){
@@ -635,8 +631,10 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
635631
private lateinit var path : NBTPath
636632

637633
override fun visitNbtValue(ctx: mcfppParser.NbtValueContext): Var<*> {
638-
if(ctx.LineString() != null){
634+
if(ctx.LineString() != null) {
639635
return NBTBasedDataConcrete(StringTag(ctx.LineString().text))
636+
}else if(ctx.nbtBool() != null){
637+
return NBTBasedDataConcrete(BoolTag(ctx.nbtBool().text == "true"))
640638
}else if(ctx.nbtByte() != null){
641639
return NBTBasedDataConcrete(ByteTag(ctx.nbtByte().text.toByte()))
642640
}else if(ctx.nbtShort() != null){
@@ -673,7 +671,7 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
673671
return NBTBasedDataConcrete(SNBTUtil.fromSNBT(ctx.nbtByteArray().text))
674672
}else if(ctx.nbtIntArray() != null) {
675673
return NBTBasedDataConcrete(SNBTUtil.fromSNBT(ctx.nbtIntArray().text))
676-
}else if(ctx.nbtLongArray() != null){
674+
}else if(ctx.nbtLongArray() != null) {
677675
return NBTBasedDataConcrete(SNBTUtil.fromSNBT(ctx.nbtLongArray().text))
678676
}else {
679677
LogProcessor.error("Invalid NBT value")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ open class MCFPPImVisitor: mcfppParserBaseVisitor<Any?>() {
194194
}
195195
"dynamic" -> {
196196
if(`var` is MCFPPValue<*> && `var`.hasAssigned){
197-
`var`.toDynamic(true)
197+
`var` = `var`.toDynamic(false)
198198
}else if(`var` is MCFPPValue<*>){
199199
`var` = type.buildUnConcrete(c.Identifier().text, Function.currFunction)
200200
}

src/main/kotlin/top/mcfpp/command/Commands.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ object Commands {
8585
.build(b.sbObject.toString(),b.sbObject.toString())
8686
}
8787

88+
fun sbPlayerOperation(a: MCInt, operation: String, b: ScoreBool): Command {
89+
return Command.build("scoreboard players operation")
90+
.build(a.name,a.name)
91+
.build(a.sbObject.toString(),a.sbObject.toString())
92+
.build(operation,"operation")
93+
.build(b.identifier,b.identifier)
94+
.build(b.boolObject.toString(),b.boolObject.toString())
95+
}
96+
97+
fun sbPlayerOperation(a: ScoreBool, operation: String, b: ScoreBool): Command {
98+
return Command.build("scoreboard players operation")
99+
.build(a.identifier,a.identifier)
100+
.build(a.boolObject.toString(),a.boolObject.toString())
101+
.build(operation,"operation")
102+
.build(b.identifier,b.identifier)
103+
.build(b.boolObject.toString(),b.boolObject.toString())
104+
}
105+
88106
/**
89107
* `scoreboard players remove <target.name> <target.object.toString()> value`
90108
*

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,7 @@ class MCIntConcrete : MCInt, MCFPPValue<Int> {
469469
val cmd = Commands.selectRun(parent!!, "scoreboard players set @s $sbObject $value")
470470
Function.addCommands(cmd)
471471
} else {
472-
val cmd = if (!isTemp)
473-
Command("execute store result").build(nbtPath.toCommandPart()).build("int 1 run ")
474-
else
475-
Command("")
476-
Function.addCommand(cmd.build("scoreboard players set $name $sbObject $value", false))
472+
Function.addCommand("scoreboard players set $name $sbObject $value")
477473
}
478474
val re = MCInt(this)
479475
if(replace){

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import net.querz.nbt.tag.*
44
import top.mcfpp.annotations.InsertCommand
55
import top.mcfpp.command.Command
66
import top.mcfpp.command.Commands
7+
import top.mcfpp.core.lang.bool.BaseBool
8+
import top.mcfpp.core.lang.bool.ScoreBoolConcrete
79
import top.mcfpp.lib.*
810
import top.mcfpp.model.*
911
import top.mcfpp.model.function.Function
@@ -173,9 +175,9 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
173175
if(v.isError){
174176
v = b
175177
}
176-
hasAssigned = true
177178
val re = doAssignedBy(v)
178179
re.isDynamic = isDynamic
180+
re.hasAssigned = true
179181
if(stackIndex != 0) trackLost = true
180182
return if(re is MCFPPValue<*> && re.isDynamic){
181183
re.toDynamic(false) as Self
@@ -222,7 +224,7 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
222224
return when(type){
223225
MCFPPBaseType.Any -> MCAnyConcrete(this)
224226
MCFPPNBTType.NBT -> {
225-
if(this is MCFPPValue<*>){
227+
if(this is MCFPPValue<*> && (this is ScoreBoolConcrete || this !is BaseBool)){
226228
NBTBasedDataConcrete(this.toNBTVar(), NBTUtil.varToNBT(this)!!)
227229
}else{
228230
this.toNBTVar()

src/main/kotlin/top/mcfpp/core/lang/bool/BaseBool.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package top.mcfpp.core.lang.bool
22

33
import top.mcfpp.command.Command
4+
import top.mcfpp.core.lang.NBTBasedData
45
import top.mcfpp.core.lang.Var
56
import top.mcfpp.model.Member
67
import top.mcfpp.model.function.Function
@@ -13,6 +14,12 @@ abstract class BaseBool : Var<BaseBool> {
1314

1415
override var type: MCFPPType = MCFPPBaseType.Bool
1516

17+
/**
18+
* 创建一个bool值。它的标识符和mc名相同。
19+
* @param identifier identifier
20+
*/
21+
constructor(identifier: String = UUID.randomUUID().toString()) : super(identifier)
22+
1623
abstract override fun negation(): Var<*>?
1724

1825
abstract override fun and(a: Var<*>): Var<*>?
@@ -21,18 +28,18 @@ abstract class BaseBool : Var<BaseBool> {
2128

2229
abstract fun toCommandPart(): Command
2330

24-
/**
25-
* 创建一个bool值。它的标识符和mc名相同。
26-
* @param identifier identifier
27-
*/
28-
constructor(identifier: String = UUID.randomUUID().toString()) : super(identifier)
31+
override fun toNBTVar(): NBTBasedData {
32+
return toScoreBool().toNBTVar()
33+
}
2934

3035
/**
3136
* 复制一个bool
3237
* @param b 被复制的int值
3338
*/
3439
constructor(b: ScoreBool) : super(b)
3540

41+
abstract fun toScoreBool(): ScoreBool
42+
3643
override fun canAssignedBy(b: Var<*>): Boolean {
3744
return b is BaseBool
3845
}
@@ -50,6 +57,4 @@ abstract class BaseBool : Var<BaseBool> {
5057
return UnknownFunction(key) to true
5158
}
5259

53-
abstract fun toScoreBool(): ScoreBool
54-
5560
}

src/main/kotlin/top/mcfpp/core/lang/bool/ScoreBool.kt

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,25 @@ open class ScoreBool : BaseBool, OnScoreboard {
177177

178178
@InsertCommand
179179
override fun or(a: Var<*>): Var<*>? {
180-
if(a is ScoreBoolConcrete && a.value) return ScoreBoolConcrete(this, true)
180+
if(a is ScoreBoolConcrete){
181+
return if(a.value){
182+
ScoreBoolConcrete(this, true)
183+
}else{
184+
this
185+
}
186+
}
181187
return ExecuteBool(this).or(a)
182188
}
183189

184190
@InsertCommand
185191
override fun and(a: Var<*>): Var<*>? {
186-
if(a is ScoreBoolConcrete && !a.value) return ScoreBoolConcrete(this, false)
192+
if(a is ScoreBoolConcrete ){
193+
return if(!a.value){
194+
ScoreBoolConcrete(this, false)
195+
}else{
196+
this
197+
}
198+
}
187199
return ExecuteBool(this).and(a)
188200
}
189201

@@ -203,7 +215,7 @@ open class ScoreBool : BaseBool, OnScoreboard {
203215
if(final.size == 2){
204216
Function.addCommand(final[0])
205217
}
206-
Function.addCommand(final.last().build(Commands.sbPlayerOperation(this,"=",b as MCInt)))
218+
Function.addCommand(final.last().build(Commands.sbPlayerOperation(this,"=",b as ScoreBool)))
207219
this
208220
},
209221
ifThisIsNormalVarAndAIsConcrete = { b, _ ->
@@ -213,12 +225,12 @@ open class ScoreBool : BaseBool, OnScoreboard {
213225
if(cmd.size == 2){
214226
Function.addCommand(cmd[0])
215227
}
216-
Function.addCommand(cmd.last().build(Commands.sbPlayerOperation(this, "=", c as MCInt)))
228+
Function.addCommand(cmd.last().build(Commands.sbPlayerOperation(this, "=", c as ScoreBool)))
217229
ScoreBool(this)
218230
},
219231
ifThisIsNormalVarAndAIsNotConcrete = { c, _ ->
220232
//变量进栈
221-
Function.addCommand(Commands.sbPlayerOperation(this, "=", c as MCInt))
233+
Function.addCommand(Commands.sbPlayerOperation(this, "=", c as ScoreBool))
222234
ScoreBool(this)
223235
}
224236
) as ScoreBool
@@ -262,6 +274,33 @@ open class ScoreBool : BaseBool, OnScoreboard {
262274

263275
override fun toScoreBool(): ScoreBool = this
264276

277+
override fun toNBTVar(): NBTBasedData {
278+
val n = NBTBasedData()
279+
n.name = name
280+
n.identifier = identifier
281+
n.isStatic = isStatic
282+
n.accessModifier = accessModifier
283+
n.isTemp = isTemp
284+
n.stackIndex = stackIndex
285+
n.isConst = isConst
286+
n.nbtPath = nbtPath.clone()
287+
return n
288+
}
289+
290+
open fun asIntVar(): MCInt{
291+
val n = MCInt()
292+
n.name = name
293+
n.identifier = identifier
294+
n.isStatic = isStatic
295+
n.accessModifier = accessModifier
296+
n.isTemp = isTemp
297+
n.stackIndex = stackIndex
298+
n.isConst = isConst
299+
n.nbtPath = nbtPath.clone()
300+
n.setObj(boolObject)
301+
return n
302+
}
303+
265304
companion object{
266305
val data = CompoundData("bool","mcfpp")
267306
}
@@ -278,7 +317,7 @@ class ScoreBoolConcrete : ScoreBool, MCFPPValue<Boolean> {
278317
* @param curr 域容器
279318
* @param value 值
280319
*/
281-
constructor(curr: FieldContainer, value: Boolean, identifier: String = UUID.randomUUID().toString()) : super(curr.prefix + identifier) {
320+
constructor(curr: FieldContainer, value: Boolean, identifier: String = UUID.randomUUID().toString()) : super(curr, identifier) {
282321
this.value = value
283322
}
284323

@@ -398,16 +437,17 @@ class ScoreBoolConcrete : ScoreBool, MCFPPValue<Boolean> {
398437
val cmd = Commands.selectRun(parent!!, "scoreboard players set @s $boolObject $value")
399438
Function.addCommands(cmd)
400439
} else {
401-
val cmd = if (!isTemp)
402-
Command("execute store result").build(nbtPath.toCommandPart()).build("int 1 run ")
403-
else
404-
Command("")
405-
Function.addCommand(cmd.build("scoreboard players set $name $boolObject $value", false))
440+
Function.addCommand("scoreboard players set $name $boolObject $value")
406441
}
407442
val re = ScoreBool(this)
408443
if(replace){
409444
replacedBy(re)
410445
}
411446
return re
412447
}
448+
449+
override fun asIntVar(): MCInt {
450+
val n = MCIntConcrete(super.asIntVar() , if(value) 1 else 0)
451+
return n
452+
}
413453
}

0 commit comments

Comments
 (0)