Skip to content

Commit 720e851

Browse files
committed
修复了数据包路径错误的问题,修复了数据模板的成员赋值时数据模板动态化时机过晚的问题
1 parent 21dd137 commit 720e851

File tree

277 files changed

+234
-175
lines changed

Some content is hidden

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

277 files changed

+234
-175
lines changed

src/main/kotlin/top/mcfpp/MCFPP.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import java.io.FileInputStream
1212
* 编译器的启动入口
1313
*/
1414
fun main(args: Array<String>) {
15+
//获取log4j2.xml配置文件
1516
val source:ConfigurationSource
1617
try {
1718
source = ConfigurationSource(FileInputStream("log4j2.xml"))

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,14 @@ class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
231231
if (re is MCInt && b is MCInt) {
232232
when (ctx.relationalOp().text) {
233233
">" -> re = re.isBigger(b)
234-
">=" -> re = re.isGreaterOrEqual(b)
234+
">=" -> re = re.isBiggerOrEqual(b)
235235
"<" -> re = re.isSmaller(b)
236236
"<=" -> re = re.isSmallerOrEqual(b)
237237
}
238238
} else if(re is MCFloat && b is MCFloat){
239239
when (ctx.relationalOp().text) {
240240
">" -> re = re.isBigger(b)
241-
">=" -> re = re.isGreaterOrEqual(b)
241+
">=" -> re = re.isBiggerOrEqual(b)
242242
"<" -> re = re.isSmaller(b)
243243
"<=" -> re = re.isSmallerOrEqual(b)
244244
}
@@ -410,7 +410,6 @@ class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
410410
@Override
411411
override fun visitVarWithSelector(ctx: mcfppParser.VarWithSelectorContext): Var<*> {
412412
Project.ctx = ctx
413-
val namespaceID : Pair<String?, String>
414413
if(ctx.primary() != null){
415414
currSelector = visit(ctx.primary())
416415
}
@@ -598,7 +597,7 @@ class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
598597
LogProcessor.error("Template constructor ${template.identifier} cannot have arguments")
599598
return UnknownVar("${template.identifier}_type_" + UUID.randomUUID())
600599
}
601-
return DataTemplateObject(template)
600+
return DataTemplateObjectConcrete(template, template.getDefaultValue())
602601
}
603602
if(cls is GenericClass){
604603
if(defaultGenericClassType != null){

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
214214
val type = left.type
215215
val right: Var<*> = McfppExprVisitor(if(type is MCFPPGenericClassType) type else null, if(type is MCFPPEnumType) type else null).visit(ctx.expression())!!
216216
try {
217+
if(right !is MCFPPValue<*> && left.parent is DataTemplateObjectConcrete){
218+
left.parent = (left.parent as DataTemplateObjectConcrete).toDynamic(true)
219+
}
217220
left.replacedBy(left.assign(right))
218221
} catch (e: VariableConverseException) {
219222
LogProcessor.error("Cannot convert " + right.javaClass + " to " + left.javaClass)

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

Lines changed: 79 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package top.mcfpp.antlr
33
import top.mcfpp.Project
44
import top.mcfpp.annotations.InsertCommand
55
import top.mcfpp.lang.*
6+
import top.mcfpp.lang.type.MCFPPType
7+
import top.mcfpp.lang.value.MCFPPValue
68
import top.mcfpp.model.*
79
import top.mcfpp.model.function.Function
810
import top.mcfpp.model.field.GlobalField
@@ -41,33 +43,12 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
4143
@Override
4244
override fun visitVarWithSelector(ctx: mcfppParser.VarWithSelectorContext): Var<*> {
4345
Project.ctx = ctx
44-
val namespaceID : Pair<String?, String>
4546
if(ctx.primary() != null){
4647
currSelector = visit(ctx.primary())
4748
}
4849
if(currSelector is UnknownVar){
49-
if(ctx.primary() != null || ctx.type().className() != null){
50-
namespaceID = if(ctx.primary() != null){
51-
null to ctx.primary().text
52-
} else{
53-
StringHelper.splitNamespaceID(ctx.type().text)
54-
}
55-
val o = GlobalField.getObject(namespaceID.first, namespaceID.second)
56-
if(o != null) {
57-
currSelector = o.getType()
58-
} else{
59-
LogProcessor.error("Undefined type: ${namespaceID.second}")
60-
currSelector = UnknownVar("${ctx.type().className().text}_type_" + UUID.randomUUID())
61-
}
62-
}else{
63-
currSelector = CompoundDataCompanion(
64-
//基本类型
65-
when(ctx.type().text){
66-
"int" -> MCInt.data
67-
else -> TODO()
68-
}
69-
)
70-
}
50+
val type = MCFPPType.parseFromIdentifier(ctx.type().text, Function.currFunction.field)
51+
currSelector = type
7152
}
7253
for (selector in ctx.selector()){
7354
visit(selector)
@@ -77,8 +58,9 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
7758

7859
@Override
7960
override fun visitSelector(ctx: mcfppParser.SelectorContext?): Var<*> {
80-
currSelector = visit(ctx!!.`var`())!!.getTempVar()
81-
return (currSelector as Var<*>)
61+
//进入visitVar,currSelector作为成员选择的上下文
62+
currSelector = visit(ctx!!.`var`())
63+
return currSelector as Var<*>
8264
}
8365

8466
/**
@@ -95,6 +77,31 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
9577
} else if (ctx.value() != null) {
9678
//数字
9779
return visit(ctx.value())
80+
} else if (ctx.range() != null){
81+
//是范围
82+
val left = ctx.range().num1?.let { visit(it) }
83+
val right = ctx.range().num2?.let { visit(it) }
84+
if(left is MCNumber<*>? && right is MCNumber<*>?){
85+
if(left is MCFPPValue<*>? && right is MCFPPValue<*>?){
86+
val leftValue = left?.value.toString().toFloatOrNull()
87+
val rightValue = right?.value.toString().toFloatOrNull()
88+
return RangeVarConcrete(leftValue to rightValue)
89+
}else{
90+
val range = RangeVar()
91+
if(left is MCInt){
92+
range.left = MCFloat(range.identifier + "_left")
93+
}
94+
if(right is MCInt){
95+
range.right = MCFloat(range.identifier + "_right")
96+
}
97+
left?.let { range.left.assign(it) }
98+
right?.let { range.right.assign(it) }
99+
return range
100+
}
101+
}else{
102+
LogProcessor.error("Range sides should be a number: ${left?.type} and ${right?.type}")
103+
return UnknownVar("range_" + UUID.randomUUID())
104+
}
98105
} else {
99106
//this或者super
100107
val s = if(ctx.SUPER() != null){
@@ -120,7 +127,7 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
120127
@InsertCommand
121128
override fun visitVar(ctx: mcfppParser.VarContext): Var<*> {
122129
Project.ctx = ctx
123-
return if (ctx.Identifier() != null && ctx.arguments() == null) {
130+
if (ctx.Identifier() != null && ctx.arguments() == null) {
124131
//变量
125132
//没有数组选取
126133
val qwq: String = ctx.Identifier().text
@@ -142,19 +149,38 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
142149
if (ctx.identifierSuffix() == null || ctx.identifierSuffix().size == 0) {
143150
return re
144151
} else {
145-
if(re is Indexable<*>){
146-
for (value in ctx.identifierSuffix()) {
152+
for (value in ctx.identifierSuffix()) {
153+
if(value.conditionalExpression() != null){
154+
if(re !is Indexable<*>){
155+
LogProcessor.error("Cannot index ${re.type}")
156+
return UnknownVar("${re.identifier}_index_" + UUID.randomUUID())
157+
}
158+
//索引
147159
val index = visit(value.conditionalExpression())!!
148160
re = (re as Indexable<*>).getByIndex(index)
161+
}else{
162+
if(!re.isTemp) re = re.getTempVar()
163+
//初始化
164+
for (initializer in value.objectInitializer()){
165+
val id = initializer.Identifier().text
166+
val v = visit(initializer.expression())
167+
val (m, b) = re.getMemberVar(id, re.getAccess(Function.currFunction))
168+
if(!b){
169+
LogProcessor.error("Cannot access member $id")
170+
}
171+
if(m == null) {
172+
LogProcessor.error("Member $id not found")
173+
continue
174+
}
175+
m.replacedBy(m.assign(v))
176+
}
149177
}
150-
}else{
151-
throw IllegalArgumentException("Cannot index ${re.type}")
152178
}
153179
return re
154180
}
155181
} else if (ctx.expression() != null) {
156182
// '(' expression ')'
157-
visit(ctx.expression())
183+
return McfppLeftExprVisitor().visit(ctx.expression())
158184
} else {
159185
//函数的调用
160186
Function.addComment(ctx.text)
@@ -184,15 +210,28 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
184210
}
185211
//调用函数
186212
return if (func is UnknownFunction) {
187-
//可能是构造函数
188-
var cls: Class? = GlobalField.getClass(p.first, p.second)
189-
if (cls == null) {
190-
LogProcessor.error("Function " + ctx.text + " not defined")
191-
Function.addComment("[Failed to Compile]${ctx.text}")
192-
func.invoke(normalArgs,currSelector)
193-
return func.returnVar
194-
}
195-
if(cls is GenericClass){
213+
var cls: Class? = if(ctx.arguments().readOnlyArgs() != null){
214+
GlobalField.getClass(p.first, p.second ,readOnlyArgs.map { it.type })
215+
}else{
216+
GlobalField.getClass(p.first, p.second)
217+
}
218+
//可能是构造函数
219+
if (cls == null) {
220+
val template: DataTemplate? = GlobalField.getTemplate(p.first, p.second)
221+
if(template == null){
222+
LogProcessor.error("Function ${func.identifier}<${readOnlyArgs.joinToString(",") { it.type.typeName }}>(${normalArgs.map { it.type.typeName }.joinToString(",")}) not defined")
223+
Function.addComment("[Failed to Compile]${ctx.text}")
224+
func.invoke(normalArgs,currSelector)
225+
return func.returnVar
226+
}
227+
//模板默认构造函数
228+
if(readOnlyArgs.isNotEmpty() || normalArgs.isNotEmpty()){
229+
LogProcessor.error("Template constructor ${template.identifier} cannot have arguments")
230+
return UnknownVar("${template.identifier}_type_" + UUID.randomUUID())
231+
}
232+
return DataTemplateObject(template)
233+
}
234+
if(cls is GenericClass){
196235
//实例化泛型函数
197236
cls = cls.compile(readOnlyArgs)
198237
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ open class DataTemplateObject : Var<DataTemplateObject> {
5050
constructor(templateObject: DataTemplateObject) : super(templateObject) {
5151
templateType = templateObject.templateType
5252
instanceField = templateObject.instanceField
53+
5354
}
5455

5556
override fun doAssign(b: Var<*>): DataTemplateObject {
@@ -247,6 +248,7 @@ open class DataTemplateObject : Var<DataTemplateObject> {
247248
}
248249
}
249250

251+
250252
}
251253

252254
class DataTemplateObjectConcrete: DataTemplateObject, MCFPPValue<CompoundTag>{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ open class MCFloat : MCNumber<Float> {
298298
* @return 计算结果
299299
*/
300300
@InsertCommand
301-
override fun isGreaterOrEqual(a: Var<*>): MCBool {
301+
override fun isBiggerOrEqual(a: Var<*>): MCBool {
302302
//re = t >= a
303-
if(!isTemp) return getTempVar().isGreaterOrEqual(a)
303+
if(!isTemp) return getTempVar().isBiggerOrEqual(a)
304304
val qwq: MCFloat = if (a !is MCFloat) a.explicitCast(MCFPPBaseType.Float) as MCFloat else a
305305
val re = MCBool()
306306
if(qwq != tempFloat) qwq.toTempEntity()
@@ -749,7 +749,7 @@ class MCFloatConcrete : MCFloat, MCFPPValue<Float>{
749749
* @return 计算结果
750750
*/
751751
@InsertCommand
752-
override fun isGreaterOrEqual(a: Var<*>): MCBool {
752+
override fun isBiggerOrEqual(a: Var<*>): MCBool {
753753
//re = t >= a
754754
if(!isTemp) return (getTempVar() as MCFloat).isSmallerOrEqual(a)
755755
val qwq: MCFloat = if (a !is MCFloat) a.explicitCast(MCFPPBaseType.Float) as MCFloat else a

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ open class MCInt : MCNumber<Int> {
281281
}
282282

283283
@InsertCommand
284-
override fun isGreaterOrEqual(a: Var<*>): MCBool {
284+
override fun isBiggerOrEqual(a: Var<*>): MCBool {
285285
//re = t <= a
286286
val qwq: MCInt = if (a !is MCInt) a.explicitCast(MCFPPBaseType.Int) as MCInt else a
287287
val re: MCBool
@@ -602,13 +602,13 @@ class MCIntConcrete : MCInt, MCFPPValue<Int>{
602602
return if (qwq is MCIntConcrete) {
603603
MCBoolConcrete(value <= qwq.value)
604604
} else {
605-
qwq.isGreaterOrEqual(this)
605+
qwq.isBiggerOrEqual(this)
606606
}
607607
}
608608

609609
@Override
610610
@InsertCommand
611-
override fun isGreaterOrEqual(a: Var<*>): MCBool {
611+
override fun isBiggerOrEqual(a: Var<*>): MCBool {
612612
//re = t <= a
613613
val qwq: MCInt = if (a !is MCInt) a.explicitCast(MCFPPBaseType.Int) as MCInt else a
614614
return if (qwq is MCIntConcrete) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ abstract class MCNumber<T> : Var<MCNumber<T>>, OnScoreboard {
111111
* @param a 右侧值
112112
* @return 计算结果
113113
*/
114-
abstract override fun isGreaterOrEqual(a: Var<*>): MCBool
114+
abstract override fun isBiggerOrEqual(a: Var<*>): MCBool
115115

116116
/**
117117
* 这个数是否等于a

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import top.mcfpp.util.TextTranslator
1717
import top.mcfpp.util.TextTranslator.translate
1818
import java.io.Serializable
1919
import java.util.*
20-
import javax.xml.crypto.Data
2120

2221
/**
2322
* mcfpp所有类型的基类。在mcfpp中,一个变量可以是固定的,也就是mcfpp编译
@@ -31,9 +30,9 @@ import javax.xml.crypto.Data
3130
* 除此之外,变量还有临时变量的区别,对于匿名的变量,编译器一般会默认它为临时
3231
* 的变量,从而在各种处理上进行优化。当然,匿名变量的声明往往在编译过程中声明。
3332
* mcfpp本身的语法并不支持匿名变量。
34-
*
3533
*/
3634
abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serializable{
35+
3736
/**
3837
* 在Minecraft中的标识符
3938
*/
@@ -72,11 +71,6 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
7271
*/
7372
var isTemp = false
7473

75-
/**
76-
* 这个变量是否已经被初始化
77-
*/
78-
var hasInit = false
79-
8074
open var parent : CanSelectMember? = null
8175

8276
/**
@@ -117,7 +111,7 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
117111
isTemp = `var`.isTemp
118112
stackIndex = `var`.stackIndex
119113
isConst = `var`.isConst
120-
nbtPath = `var`.nbtPath
114+
nbtPath = `var`.nbtPath.clone()
121115
}
122116

123117
/**
@@ -376,7 +370,7 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
376370
* @param a 右侧值
377371
* @return 计算结果
378372
*/
379-
open fun isGreaterOrEqual(a: Var<*>): MCBool {
373+
open fun isBiggerOrEqual(a: Var<*>): MCBool {
380374
LogProcessor.error("type ${type.typeName} not support operation '>='")
381375
return MCBool("")
382376
}
@@ -429,6 +423,10 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
429423
return Member.AccessModifier.PUBLIC
430424
}
431425

426+
fun assignMemberVar(member: String, b: Var<*>) {
427+
getMemberVar(member, Member.AccessModifier.PUBLIC).first?.assign(b)
428+
}
429+
432430
override fun toString(): String {
433431
return if(this is MCFPPValue<*>){
434432
"[$type,value=$value]"

0 commit comments

Comments
 (0)