Skip to content

Commit 19b581c

Browse files
committed
类初始化器
1 parent 6a95bfd commit 19b581c

File tree

6 files changed

+64
-31
lines changed

6 files changed

+64
-31
lines changed

src/main/antlr/mcfppParser.g4

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,11 @@ var
367367

368368
identifierSuffix
369369
: '[' conditionalExpression ']'
370-
| '[' Identifier '=' expression (',' basicExpression '=' expression)* ']'
370+
| '[' objectInitializer (',' objectInitializer)* ']'
371+
;
372+
373+
objectInitializer
374+
: Identifier '=' expression
371375
;
372376

373377
selector

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import top.mcfpp.lang.type.MCFPPEnumType
99
import top.mcfpp.lang.type.MCFPPGenericClassType
1010
import top.mcfpp.lang.type.MCFPPType
1111
import top.mcfpp.lang.value.MCFPPValue
12-
import top.mcfpp.model.*
13-
import top.mcfpp.model.function.Function
12+
import top.mcfpp.model.CanSelectMember
13+
import top.mcfpp.model.Class
14+
import top.mcfpp.model.CompoundDataCompanion
15+
import top.mcfpp.model.Namespace
1416
import top.mcfpp.model.field.GlobalField
17+
import top.mcfpp.model.function.Function
1518
import top.mcfpp.model.function.FunctionParam
1619
import top.mcfpp.model.function.NoStackFunction
1720
import top.mcfpp.model.function.UnknownFunction
@@ -20,11 +23,7 @@ import top.mcfpp.model.generic.GenericClass
2023
import top.mcfpp.util.LogProcessor
2124
import top.mcfpp.util.StringHelper
2225
import top.mcfpp.util.Utils
23-
import java.lang.StringBuilder
2426
import java.util.*
25-
import kotlin.IllegalArgumentException
26-
import kotlin.collections.ArrayList
27-
import kotlin.collections.HashMap
2827
import kotlin.system.exitProcess
2928

3029
/**
@@ -535,19 +534,32 @@ class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
535534
if (ctx.identifierSuffix() == null || ctx.identifierSuffix().size == 0) {
536535
return re
537536
} else {
538-
if(re is Indexable<*>){
539-
for (value in ctx.identifierSuffix()) {
540-
if(value.conditionalExpression() != null){
541-
//索引
542-
val index = visit(value.conditionalExpression())!!
543-
re = (re as Indexable<*>).getByIndex(index)
544-
}else{
545-
//初始化
546-
TODO()
537+
for (value in ctx.identifierSuffix()) {
538+
if(value.conditionalExpression() != null){
539+
if(re !is Indexable<*>){
540+
LogProcessor.error("Cannot index ${re.type}")
541+
return UnknownVar("${re.identifier}_index_" + UUID.randomUUID())
542+
}
543+
//索引
544+
val index = visit(value.conditionalExpression())!!
545+
re = (re as Indexable<*>).getByIndex(index)
546+
}else{
547+
if(!re.isTemp) re = re.getTempVar()
548+
//初始化
549+
for (initializer in value.objectInitializer()){
550+
val id = initializer.Identifier().text
551+
val v = visit(initializer.expression())
552+
val (m, b) = re.getMemberVar(id, re.getAccess(Function.currFunction))
553+
if(!b){
554+
LogProcessor.error("Cannot access member $id")
555+
}
556+
if(m == null) {
557+
LogProcessor.error("Member $id not found")
558+
continue
559+
}
560+
m.replacedBy(m.assign(v))
547561
}
548562
}
549-
}else{
550-
throw IllegalArgumentException("Cannot index ${re.type}")
551563
}
552564
return re
553565
}

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import net.querz.nbt.io.SNBTUtil
44
import net.querz.nbt.tag.CompoundTag
55
import top.mcfpp.command.Command
66
import top.mcfpp.command.Commands
7-
import top.mcfpp.lang.type.MCFPPDataTemplateType
87
import top.mcfpp.lang.type.MCFPPClassType
8+
import top.mcfpp.lang.type.MCFPPDataTemplateType
99
import top.mcfpp.lang.type.MCFPPNBTType
1010
import top.mcfpp.lang.type.MCFPPType
1111
import top.mcfpp.lang.value.MCFPPValue
@@ -195,7 +195,7 @@ open class DataTemplateObject : Var<DataTemplateObject> {
195195
}
196196
}
197197

198-
override fun onMemberChanged(member: Member) {
198+
override fun onMemberVarChanged(member: Var<*>) {
199199
if(member is MCFPPValue<*> && isConcrete()){
200200
this.replacedBy(this.toConcrete())
201201
}
@@ -264,6 +264,10 @@ class DataTemplateObjectConcrete: DataTemplateObject, MCFPPValue<CompoundTag>{
264264
return DataTemplateObject(this)
265265
}
266266

267+
override fun getTempVar(): DataTemplateObjectConcrete {
268+
return DataTemplateObjectConcrete(super.getTempVar(), this.value.clone())
269+
}
270+
267271
override fun toDynamic(replace: Boolean): Var<*> {
268272
val parent = this.parent
269273

@@ -292,15 +296,18 @@ class DataTemplateObjectConcrete: DataTemplateObject, MCFPPValue<CompoundTag>{
292296
return re
293297
}
294298

295-
override fun getMemberVar(key: String, accessModifier: Member.AccessModifier): Pair<Var<*>?, Boolean> {
296-
val re = super.getMemberVar(key, accessModifier)
297-
var qwq = re.first
298-
if(re.first != null) {
299-
//re是已知的
300-
val data = value.get(key)
301-
qwq = (re.first as Var<*>).assign(NBTBasedDataConcrete(data))
299+
override fun toString(): String {
300+
return "[$type,value=${SNBTUtil.toSNBT(value)}]"
301+
}
302+
303+
override fun onMemberVarChanged(member: Var<*>) {
304+
if(member !is MCFPPValue<*>){
305+
toDynamic(true)
306+
}else{
307+
val key = member.identifier
308+
val data = NBTUtil.toNBT(member)
309+
value.put(key, data)
302310
}
303-
return qwq to re.second
304311
}
305312

306313
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
438438
if(this.parent != other.parent) return false
439439
if(this.name != other.name) return false
440440
if(this is MCFPPValue<*> != other is MCFPPValue<*>) return false
441+
if(this is MCFPPValue<*> && other is MCFPPValue<*> && this.value != other.value) return false
441442
return true
442443
}
443444

@@ -455,7 +456,9 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
455456
fun replacedBy(v : Var<*>){
456457
if(v == this) return
457458
if(parent == null){
458-
Function.currFunction.field.putVar(identifier, v, true)
459+
if(Function.currFunction.field.containVar(identifier)){
460+
Function.currFunction.field.putVar(identifier, v, true)
461+
}
459462
}else{
460463
v.parent = this.parent
461464
when (val parent = parent){
@@ -478,7 +481,7 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
478481
}
479482
else -> {}
480483
}
481-
parent?.onMemberChanged(v)
484+
parent?.onMemberVarChanged(v)
482485
}
483486
}
484487

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@ interface CanSelectMember{
3333

3434
fun getAccess(function: Function): Member.AccessModifier
3535

36-
fun onMemberChanged(member: Member) {}
36+
/**
37+
* @param member 更改之后的成员变量
38+
*
39+
* 当成员变量发生变化时,会调用此方法
40+
*/
41+
fun onMemberVarChanged(member: Var<*>) {}
3742
}

test/test.mcfpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ data Test{
99

1010
func main(){
1111
Test t = {a:1, b:2, c:3, d:4};
12+
t = t[a = 100];
13+
var a = @a[tag="qwq", distance=1..2]
1214
print(t.toText());
1315
}

0 commit comments

Comments
 (0)