Skip to content

Commit c437633

Browse files
committed
修复单例模板定义问题,修复数据包不能正常生成的问题
1 parent 8a1c94a commit c437633

File tree

14 files changed

+253
-32
lines changed

14 files changed

+253
-32
lines changed

mcfpp.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"description": "",
77
"namespace": "mcfpp",
88
"targetPath": "src/main/resources/lib",
9-
"noDatapack": true,
9+
"noDatapack": false,
1010
"ignoreStdLib": true,
1111
"isLib": true
1212
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fun main(args: Array<String>) {
4343
try{
4444
DatapackCreator.createDatapack(Project.config.targetPath) //生成数据包
4545
}catch (e: Exception){
46-
LogProcessor.error("Cannot create datapack in path: ${Project.config.targetPath}")
46+
LogProcessor.error("Cannot create datapack in path: ${Project.config.targetPath}", e)
4747
}
4848
}
4949
LogProcessor.info("Finished in " + (System.currentTimeMillis() - start) + "ms")

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import top.mcfpp.lang.type.MCFPPType
1111
import top.mcfpp.lang.value.MCFPPValue
1212
import top.mcfpp.model.CanSelectMember
1313
import top.mcfpp.model.Class
14+
import top.mcfpp.model.DataTemplate
1415
import top.mcfpp.model.Namespace
1516
import top.mcfpp.model.field.GlobalField
1617
import top.mcfpp.model.function.Function
@@ -585,10 +586,19 @@ class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
585586
}
586587
//可能是构造函数
587588
if (cls == null) {
588-
LogProcessor.error("Function ${func.identifier}<${readOnlyArgs.joinToString(",") { it.type.typeName }}>(${normalArgs.map { it.type.typeName }.joinToString(",")}) not defined")
589-
Function.addComment("[Failed to Compile]${ctx.text}")
590-
func.invoke(normalArgs,currSelector)
591-
return func.returnVar
589+
val template: DataTemplate? = GlobalField.getTemplate(p.first, p.second)
590+
if(template == null){
591+
LogProcessor.error("Function ${func.identifier}<${readOnlyArgs.joinToString(",") { it.type.typeName }}>(${normalArgs.map { it.type.typeName }.joinToString(",")}) not defined")
592+
Function.addComment("[Failed to Compile]${ctx.text}")
593+
func.invoke(normalArgs,currSelector)
594+
return func.returnVar
595+
}
596+
//模板默认构造函数
597+
if(readOnlyArgs.isNotEmpty() || normalArgs.isNotEmpty()){
598+
LogProcessor.error("Template constructor ${template.identifier} cannot have arguments")
599+
return UnknownVar("${template.identifier}_type_" + UUID.randomUUID())
600+
}
601+
return DataTemplateObject(template)
592602
}
593603
if(cls is GenericClass){
594604
if(defaultGenericClassType != null){

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
10161016
return null
10171017
}
10181018

1019-
fun enterClassFunctionDeclaration(ctx: mcfppParser.ClassFunctionDeclarationContext) {
1019+
private fun enterClassFunctionDeclaration(ctx: mcfppParser.ClassFunctionDeclarationContext) {
10201020
Project.ctx = ctx
10211021
//解析参数
10221022
val types = FunctionParam.parseReadonlyAndNormalParamTypes(ctx.functionParams())
@@ -1031,7 +1031,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
10311031
annoInCompound.clear()
10321032
}
10331033

1034-
fun exitClassFunctionDeclaration(ctx: mcfppParser.ClassFunctionDeclarationContext) {
1034+
private fun exitClassFunctionDeclaration(ctx: mcfppParser.ClassFunctionDeclarationContext) {
10351035
Project.ctx = ctx
10361036
Function.currFunction = Class.currClass!!.classPreInit
10371037
}
@@ -1044,7 +1044,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
10441044
return null
10451045
}
10461046

1047-
fun enterConstructorDeclaration(ctx: mcfppParser.ConstructorDeclarationContext) {
1047+
private fun enterConstructorDeclaration(ctx: mcfppParser.ConstructorDeclarationContext) {
10481048
Project.ctx = ctx
10491049
val types = FunctionParam.parseNormalParamTypes(ctx.normalParams())
10501050
val c = Class.currClass!!.getConstructorByString(types.typeToStringList())!!
@@ -1056,13 +1056,13 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
10561056
annoInCompound.clear()
10571057
}
10581058

1059-
fun exitConstructorDeclaration(ctx: mcfppParser.ConstructorDeclarationContext) {
1059+
private fun exitConstructorDeclaration(ctx: mcfppParser.ConstructorDeclarationContext) {
10601060
Project.ctx = ctx
10611061
Function.currFunction = Class.currClass!!.classPreInit
10621062
}
10631063
//endregion
10641064

1065-
//region struct
1065+
//region template
10661066

10671067
/**
10681068
* 进入类体。
@@ -1076,32 +1076,44 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
10761076
return null
10771077
}
10781078

1079-
fun enterTemplateBody(ctx: mcfppParser.TemplateBodyContext) {
1079+
private fun enterTemplateBody(ctx: mcfppParser.TemplateBodyContext) {
10801080
Project.ctx = ctx
10811081
//获取类的对象
1082-
val parent = ctx.parent as mcfppParser.TemplateDeclarationContext
1083-
val identifier: String = parent.classWithoutNamespace().text
1082+
val parent = ctx.parent
1083+
if(parent is mcfppParser.TemplateDeclarationContext){
1084+
val identifier = parent.classWithoutNamespace().text
1085+
DataTemplate.currTemplate = GlobalField.getTemplate(Project.currNamespace, identifier)
1086+
}else if(parent is mcfppParser.ObjectTemplateDeclarationContext){
1087+
val identifier = parent.classWithoutNamespace().text
1088+
DataTemplate.currTemplate = GlobalField.getObject(Project.currNamespace, identifier) as ObjectDataTemplate
1089+
}else{
1090+
throw Exception("Unknown parent")
1091+
}
10841092
//设置作用域
1085-
DataTemplate.currTemplate = GlobalField.getTemplate(Project.currNamespace, identifier)
10861093
}
10871094

10881095
/**
10891096
* 离开类体。将缓存重新指向全局
10901097
* @param ctx the parse tree
10911098
*/
1092-
fun exitTemplateBody(ctx: mcfppParser.TemplateBodyContext) {
1099+
private fun exitTemplateBody(ctx: mcfppParser.TemplateBodyContext) {
10931100
Project.ctx = ctx
10941101
DataTemplate.currTemplate = null
10951102
}
10961103

1104+
override fun visitObjectTemplateDeclaration(ctx: mcfppParser.ObjectTemplateDeclarationContext?): Any? {
1105+
1106+
return super.visitObjectTemplateDeclaration(ctx)
1107+
}
1108+
10971109
override fun visitTemplateFunctionDeclaration(ctx: mcfppParser.TemplateFunctionDeclarationContext): Any? {
10981110
enterTemplateFunctionDeclaration(ctx)
10991111
super.visitTemplateFunctionDeclaration(ctx)
11001112
exitTemplateFunctionDeclaration(ctx)
11011113
return null
11021114
}
11031115

1104-
fun enterTemplateFunctionDeclaration(ctx: mcfppParser.TemplateFunctionDeclarationContext) {
1116+
private fun enterTemplateFunctionDeclaration(ctx: mcfppParser.TemplateFunctionDeclarationContext) {
11051117
Project.ctx = ctx
11061118
//解析参数
11071119
val types = FunctionParam.parseReadonlyAndNormalParamTypes(ctx.functionParams())

src/main/kotlin/top/mcfpp/io/DatapackCreator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ object DatapackCreator {
175175
}
176176
LogProcessor.debug("Writing File: $currPath\\functions\\" + f.nameWithNamespace + ".mcfunction")
177177
//TODO 可能无法正确创建文件夹
178-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(obj.identifier)))
178+
Files.createDirectories(Paths.get("$currPath\\functions\\" + StringHelper.toLowerCase(obj.identifier) + "\\static"))
179179
if (f is ExtensionFunction){
180-
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(obj.identifier) + "/ex"))
180+
Files.createDirectories(Paths.get("$currPath\\functions\\" + StringHelper.toLowerCase(obj.identifier) + "\\static\\ex"))
181181
}
182182
Files.write(
183183
Paths.get("$currPath/functions/" + f.nameWithNamespace + ".mcfunction"),

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,21 @@ import java.util.*
2525
*/
2626
open class DataTemplateObject : Var<DataTemplateObject> {
2727

28-
val templateType
29-
get() = (type as MCFPPDataTemplateType).template
28+
val templateType: DataTemplate
3029

3130
var instanceField: CompoundDataField
3231

32+
override var type: MCFPPType
33+
get() = templateType.getType()
34+
set(value) {}
35+
3336
/**
3437
* 创建一个模板对象
3538
* @param template 模板的类型
3639
* @param identifier 标识符
3740
*/
3841
constructor(template: DataTemplate, identifier: String = UUID.randomUUID().toString()) {
39-
this.type = template.getType()
42+
this.templateType = template
4043
this.name = identifier
4144
this.identifier = identifier
4245
instanceField = template.field.createInstance(this)
@@ -47,7 +50,7 @@ open class DataTemplateObject : Var<DataTemplateObject> {
4750
* @param templateObject 被复制的模板对象
4851
*/
4952
constructor(templateObject: DataTemplateObject) : super(templateObject) {
50-
type = templateObject.type
53+
templateType = templateObject.templateType
5154
instanceField = templateObject.instanceField
5255
}
5356

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
592592
is MCFPPEnumType -> {
593593
`var` = EnumVar(type.enum, identifier)
594594
}
595+
is MCFPPVectorType -> {
596+
`var` = VectorVar(type.dimension, identifier)
597+
}
595598
//还有模板什么的
596599
else -> {
597600
LogProcessor.error("Unknown type: $type")
@@ -637,6 +640,9 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
637640
is MCFPPEnumType -> {
638641
`var` = EnumVar(type.enum, identifier)
639642
}
643+
is MCFPPVectorType -> {
644+
`var` = VectorVar(type.dimension, identifier)
645+
}
640646
//还有模板什么的
641647
else -> {
642648
LogProcessor.error("Unknown type: $type")

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ open class MCFPPType(
312312
return MCFPPBaseType.Any
313313
}
314314
}
315+
val vecResult = MCFPPVectorType.regex.find(identifier)
316+
if(vecResult != null){
317+
val dimension = identifier.substring(3).toInt()
318+
return MCFPPVectorType(dimension)
319+
}
315320
//普通匹配
316321
val nspID = StringHelper.splitNamespaceID(identifier)
317322
val clazz = GlobalField.getClass(nspID.first, nspID.second)

src/main/kotlin/top/mcfpp/lang/type/MCFPPVectorType.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@ package top.mcfpp.lang.type
22

33
class MCFPPVectorType(val dimension: Int): MCFPPType(parentType = listOf(MCFPPBaseType.Any)) {
44
override val typeName: String
5-
get() = "vec$$dimension"
5+
get() = "vec$dimension"
6+
7+
8+
companion object {
9+
val regex = Regex("^vec\\d+$")
10+
}
11+
612
}

src/main/kotlin/top/mcfpp/model/function/Function.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,19 @@ open class Function : Member, FieldContainer, Serializable {
670670
@Override
671671
override fun equals(other: Any?): Boolean {
672672
if (other is Function) {
673-
return isSelf(other.identifier, other.normalParams.map { it.type })
673+
if (this.identifier == other.identifier && this.normalParams.size == other.normalParams.size) {
674+
if (this.normalParams.size == 0) {
675+
return true
676+
}
677+
//参数比对
678+
for (i in normalParams.indices) {
679+
if (other.normalParams[i].type.typeName != this.normalParams[i].type.typeName) {
680+
return false
681+
}
682+
}
683+
}else{
684+
return false
685+
}
674686
}
675687
return false
676688
}

0 commit comments

Comments
 (0)