Skip to content

Commit a1ab974

Browse files
committed
枚举的赋值,传值
修复了编译期变量的toString方法不能正确输出值的问题
1 parent 3e40f02 commit a1ab974

File tree

18 files changed

+521
-177
lines changed

18 files changed

+521
-177
lines changed

.mclib

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,6 @@
33
{
44
"id":"default",
55
"functions":[
6-
{
7-
"id":"test",
8-
"normalParams":[
9-
{
10-
"id":"i",
11-
"type":"int",
12-
"isStatic":false
13-
}
14-
],
15-
"returnType":"int",
16-
"isAbstract":false,
17-
"tags":[
18-
19-
]
20-
},
216
{
227
"id":"main",
238
"normalParams":[

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

Lines changed: 69 additions & 64 deletions
Large diffs are not rendered by default.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ object Commands {
3535
fun sbPlayerGet(target: MCInt): Command{
3636
return Command.build("scoreboard players get")
3737
.build(target.name,target.name)
38-
.build(target.`object`.toString(),target.`object`.toString())
38+
.build(target.sbObject.toString(),target.sbObject.toString())
3939
}
4040

4141
/**
@@ -48,7 +48,7 @@ object Commands {
4848
fun sbPlayerAdd(target: MCInt, value: Int): Command {
4949
return Command.build("scoreboard players add")
5050
.build(target.name, target.name)
51-
.build(target.`object`.toString(), target.`object`.toString())
51+
.build(target.sbObject.toString(), target.sbObject.toString())
5252
.build("$value")
5353
}
5454

@@ -63,10 +63,10 @@ object Commands {
6363
fun sbPlayerOperation(a: MCInt, operation: String, b: MCInt): Command {
6464
return Command.build("scoreboard players operation")
6565
.build(a.name,a.name)
66-
.build(a.`object`.toString(),a.`object`.toString())
66+
.build(a.sbObject.toString(),a.sbObject.toString())
6767
.build(operation,"operation")
6868
.build(b.name,b.name)
69-
.build(b.`object`.toString(),b.`object`.toString())
69+
.build(b.sbObject.toString(),b.sbObject.toString())
7070
}
7171

7272
/**
@@ -79,14 +79,14 @@ object Commands {
7979
fun sbPlayerRemove(target: MCInt, value: Int): Command {
8080
return Command.build("scoreboard players remove ")
8181
.build(target.name, target.name)
82-
.build(target.`object`.toString(), target.`object`.toString())
82+
.build(target.sbObject.toString(), target.sbObject.toString())
8383
.build(value.toString())
8484
}
8585

8686
fun sbPlayerSet(a: MCInt, value: Int): Command {
8787
return Command.build("scoreboard players set ")
8888
.build(a.name,a.name)
89-
.build(a.`object`.toString(),a.`object`.toString())
89+
.build(a.sbObject.toString(),a.sbObject.toString())
9090
.build(value.toString())
9191
}
9292

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import net.querz.nbt.tag.CompoundTag
55
import top.mcfpp.command.Command
66
import top.mcfpp.command.Commands
77
import top.mcfpp.exception.VariableConverseException
8-
import top.mcfpp.lang.type.DataTemplateType
8+
import top.mcfpp.lang.type.MCFPPDataTemplateType
99
import top.mcfpp.lang.type.MCFPPClassType
1010
import top.mcfpp.lang.type.MCFPPNBTType
1111
import top.mcfpp.lang.type.MCFPPType
1212
import top.mcfpp.lang.value.MCFPPValue
13-
import top.mcfpp.model.Class
1413
import top.mcfpp.model.DataTemplate
15-
import top.mcfpp.model.FieldContainer
1614
import top.mcfpp.model.Member
1715
import top.mcfpp.model.field.CompoundDataField
1816
import top.mcfpp.model.function.Function
@@ -23,7 +21,7 @@ import java.util.*
2321
open class DataTemplateObject : Var<CompoundTag> {
2422

2523
val templateType
26-
get() = (type as DataTemplateType).template
24+
get() = (type as MCFPPDataTemplateType).template
2725

2826
var instanceField: CompoundDataField
2927

@@ -120,7 +118,7 @@ open class DataTemplateObject : Var<CompoundTag> {
120118
return re
121119
}
122120

123-
is DataTemplateType -> {
121+
is MCFPPDataTemplateType -> {
124122
if(type.template.canCastTo(templateType)){
125123
val re = DataTemplateObject(type.template, this.identifier)
126124
re.nbtPath = nbtPath
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
package top.mcfpp.lang
2+
3+
import top.mcfpp.annotations.InsertCommand
4+
import top.mcfpp.command.Command
5+
import top.mcfpp.command.Commands
6+
import top.mcfpp.exception.VariableConverseException
7+
import top.mcfpp.lang.type.MCFPPBaseType
8+
import top.mcfpp.lang.type.MCFPPClassType
9+
import top.mcfpp.lang.type.MCFPPType
10+
import top.mcfpp.lang.value.MCFPPValue
11+
import top.mcfpp.model.Enum
12+
import top.mcfpp.model.FieldContainer
13+
import top.mcfpp.model.Member
14+
import top.mcfpp.model.function.Function
15+
import top.mcfpp.util.LogProcessor
16+
import java.util.*
17+
18+
open class EnumVar : Var<Int> {
19+
20+
var sbObject: SbObject = SbObject.MCFPP_default
21+
22+
var enum: Enum
23+
24+
/**
25+
* 创建一个枚举类型的变量。它的mc名和变量所在的域容器有关。
26+
*
27+
* @param identifier 标识符。默认为
28+
*/
29+
constructor(
30+
enum: Enum,
31+
curr: FieldContainer,
32+
identifier: String = UUID.randomUUID().toString()
33+
) : this(enum, curr.prefix + identifier) {
34+
this.identifier = identifier
35+
}
36+
37+
/**
38+
* 创建一个枚举值。它的标识符和mc名相同。
39+
* @param identifier identifier
40+
*/
41+
constructor(enum: Enum, identifier: String = UUID.randomUUID().toString()) : super(identifier){
42+
this.enum = enum
43+
type = enum.getType()
44+
}
45+
46+
/**
47+
* 复制一个int
48+
* @param b 被复制的int值
49+
*/
50+
constructor(b: EnumVar) : super(b){
51+
enum = b.enum
52+
type = enum.getType()
53+
}
54+
55+
constructor(b: MCInt, enum : Enum): super(b){
56+
sbObject = b.sbObject
57+
this.enum = enum
58+
type = enum.getType()
59+
}
60+
61+
override fun assign(b: Var<*>): Var<Int> {
62+
return when(b){
63+
is EnumVar -> {
64+
val i = this.getIntVar().assign(b.getIntVar())
65+
if(i is MCIntConcrete) EnumVarConcrete(i, enum)
66+
else EnumVar(i, enum)
67+
}
68+
else -> {
69+
throw Exception("Cannot assign ${b::class.simpleName} to EnumVar")
70+
}
71+
}
72+
}
73+
74+
override fun cast(type: MCFPPType): Var<*> {
75+
return when (type) {
76+
this.type -> this
77+
MCFPPBaseType.Any -> MCAnyConcrete(this)
78+
else -> {
79+
LogProcessor.error("Cannot cast [${this.type}] to [$type]")
80+
throw VariableConverseException()
81+
}
82+
}
83+
}
84+
85+
override fun clone(): Var<*> {
86+
return EnumVar(this)
87+
}
88+
89+
override fun getTempVar(): Var<*> {
90+
if (isTemp) return this
91+
val re = EnumVar(enum)
92+
re.isTemp = true
93+
return re.assign(this)
94+
}
95+
96+
override fun storeToStack() {
97+
if(parent != null) return
98+
Function.addCommand(
99+
Command("execute store result")
100+
.build(nbtPath.toCommandPart())
101+
.build("int 1 run scoreboard players get $name ${SbObject.MCFPP_default}")
102+
)
103+
}
104+
105+
override fun getFromStack() {
106+
if(parent != null) return
107+
Function.addCommand(
108+
Command("execute store result score $name ${SbObject.MCFPP_default} run data get")
109+
.build(nbtPath.toCommandPart())
110+
)
111+
}
112+
113+
override fun getMemberVar(key: String, accessModifier: Member.AccessModifier): Pair<Var<*>?, Boolean> {
114+
TODO("Not yet implemented")
115+
}
116+
117+
override fun getMemberFunction(
118+
key: String,
119+
readOnlyParams: List<MCFPPType>,
120+
normalParams: List<MCFPPType>,
121+
accessModifier: Member.AccessModifier
122+
): Pair<Function, Boolean> {
123+
TODO("Not yet implemented")
124+
}
125+
126+
open fun getIntVar(): MCInt{
127+
return MCInt(this)
128+
}
129+
}
130+
131+
class EnumVarConcrete : EnumVar, MCFPPValue<Int>{
132+
133+
override var value: Int
134+
135+
/**
136+
* 创建一个固定的int
137+
*
138+
* @param identifier 标识符
139+
* @param curr 域容器
140+
* @param value 值
141+
*/
142+
constructor(
143+
enum: Enum,
144+
curr: FieldContainer,
145+
value: Int,
146+
identifier: String = UUID.randomUUID().toString()
147+
) : super(enum, curr.prefix + identifier) {
148+
this.value = value
149+
}
150+
151+
/**
152+
* 创建一个固定的int。它的标识符和mc名一致/
153+
* @param identifier 标识符。如不指定,则为随机uuid
154+
* @param value 值
155+
*/
156+
constructor(enum: Enum, value: Int, identifier: String = UUID.randomUUID().toString()) : super(enum ,identifier) {
157+
this.value = value
158+
}
159+
160+
constructor(enum: EnumVar, value: Int) : super(enum){
161+
this.value = value
162+
}
163+
164+
constructor(enum: EnumVarConcrete) : super(enum){
165+
this.value = enum.value
166+
}
167+
168+
constructor(b: MCIntConcrete, enum : Enum): super(b, enum){
169+
value = b.value
170+
}
171+
172+
override fun clone(): EnumVarConcrete {
173+
return EnumVarConcrete(this)
174+
}
175+
176+
/**
177+
* 动态化
178+
*
179+
*/
180+
override fun toDynamic(replace: Boolean): Var<*> {
181+
//避免错误 Smart cast to 'ClassPointer' is impossible, because 'parent' is a mutable property that could have been changed by this time
182+
val parent = parent
183+
184+
if (parent != null) {
185+
val cmd = when(parent){
186+
is ClassPointer -> {
187+
Commands.selectRun(parent)
188+
}
189+
is MCFPPClassType -> {
190+
arrayOf(Command.build("execute as ${parent.cls.uuid} run "))
191+
}
192+
else -> TODO()
193+
}
194+
if(cmd.size == 2){
195+
Function.addCommand(cmd[0])
196+
}
197+
Function.addCommand(cmd.last().build("scoreboard players set @s ${SbObject.MCFPP_default} $value"))
198+
} else {
199+
val cmd = if (!isTemp)
200+
Command("execute store result").build(nbtPath.toCommandPart()).build("int 1 run ")
201+
else
202+
Command("")
203+
Function.addCommand(cmd.build("scoreboard players set $name ${SbObject.MCFPP_default} $value", false))
204+
}
205+
val re = EnumVar(this)
206+
if(replace){
207+
Function.currFunction.field.putVar(identifier, re, true)
208+
}
209+
return re
210+
}
211+
212+
@Override
213+
override fun cast(type: MCFPPType): Var<*> {
214+
//TODO 类支持
215+
return when (type) {
216+
this.type -> this
217+
MCFPPBaseType.Any -> this
218+
else -> {
219+
LogProcessor.error("Cannot cast [${this.type}] to [$type]")
220+
throw VariableConverseException()
221+
}
222+
}
223+
}
224+
225+
/**
226+
* 获取临时变量
227+
*
228+
* @return 返回临时变量
229+
*/
230+
@Override
231+
@InsertCommand
232+
override fun getTempVar(): Var<*> {
233+
if (isTemp) return this
234+
return EnumVarConcrete(enum ,value)
235+
}
236+
237+
override fun getIntVar(): MCInt {
238+
return MCIntConcrete(this)
239+
}
240+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ class JsonTextPlain(val value: String) : ChatComponent {
5757

5858
class JsonTextNumber(val value: MCInt) : ChatComponent {
5959
override fun toJson(): String {
60-
return "{\"score\":{\"name\":\"${value.name}\",\"objective\":\"${value.`object`.name}\"}}"
60+
return "{\"score\":{\"name\":\"${value.name}\",\"objective\":\"${value.sbObject.name}\"}}"
6161
}
6262
}

0 commit comments

Comments
 (0)