Skip to content

Commit 1f75313

Browse files
committed
函数参数如果已知,将会对函数参数进行优化
1 parent 720e851 commit 1f75313

29 files changed

+242
-203
lines changed

.mclib

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,29 @@
1919
"id":"test",
2020
"functions":[
2121
{
22-
"id":"test",
22+
"id":"main",
2323
"normalParams":[
24-
{
25-
"id":"t",
26-
"type":"template(test:Test)",
27-
"isStatic":false
28-
}
24+
2925
],
30-
"returnType":"template(test:Test)",
26+
"returnType":"void",
3127
"isAbstract":false,
3228
"tags":[
3329

3430
]
3531
},
3632
{
37-
"id":"main",
33+
"id":"test",
3834
"normalParams":[
39-
35+
{
36+
"id":"a",
37+
"type":"int",
38+
"isStatic":false
39+
},
40+
{
41+
"id":"b",
42+
"type":"int",
43+
"isStatic":false
44+
}
4045
],
4146
"returnType":"void",
4247
"isAbstract":false,
@@ -49,35 +54,7 @@
4954

5055
],
5156
"template":[
52-
{
53-
"id":"Test",
54-
"parents":[
55-
"mcfpp.lang:DataObject"
56-
],
57-
"field":{
58-
"vars":[
59-
{
60-
"id":"a",
61-
"type":"int"
62-
},
63-
{
64-
"id":"b",
65-
"type":"int"
66-
},
67-
{
68-
"id":"c",
69-
"type":"int"
70-
},
71-
{
72-
"id":"d",
73-
"type":"int"
74-
}
75-
],
76-
"functions":[
77-
78-
]
79-
}
80-
}
57+
8158
],
8259
"enum":[
8360

src/main/antlr/mcfppParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ parameterList
267267

268268
//参数
269269
parameter
270-
: STATIC? type Identifier ('=' expression)?
270+
: STATIC? type Identifier ('=' value)?
271271
;
272272

273273
//表达式

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
107107
val f = Function(
108108
ctx.Identifier().text,
109109
Interface.currInterface!!,
110-
MCFPPType.parseFromIdentifier(if(ctx.functionReturnType() == null) "void" else ctx.functionReturnType().text, typeScope)
110+
MCFPPType.parseFromIdentifier(if(ctx.functionReturnType() == null) "void" else ctx.functionReturnType().text, typeScope),
111+
null
111112
)
112113
//解析参数
113114
f.addParamsFromContext(ctx.functionParams())
@@ -296,7 +297,8 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
296297
ctx.Identifier().text,
297298
Class.currClass!!,
298299
Class.currClass!! is ObjectClass,
299-
type
300+
type,
301+
ctx.functionBody()
300302
)
301303
}
302304
if(!isStatic){
@@ -330,7 +332,8 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
330332
ctx.Identifier().text,
331333
Class.currClass!!,
332334
false,
333-
type
335+
type,
336+
null
334337
)
335338
f.isAbstract = true
336339
if(f.isStatic){
@@ -466,7 +469,7 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
466469
val f = if(ctx.functionParams()?.readOnlyParams() != null && ctx.functionParams().readOnlyParams().parameterList().parameter().size != 0){
467470
GenericFunction(identifier, Project.currNamespace, type, ctx.functionBody())
468471
}else {
469-
Function(identifier, Project.currNamespace,type)
472+
Function(identifier, Project.currNamespace,type, ctx.functionBody())
470473
}
471474
//解析参数
472475
ctx.functionParams()?.let { f.addParamsFromContext(it) }
@@ -499,7 +502,7 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
499502
val f: Function
500503
//是否是内联函数
501504
val identifier : String = ctx.Identifier().text
502-
f = InlineFunction(identifier, Project.currNamespace, ctx)
505+
f = InlineFunction(identifier, Project.currNamespace, ctx.functionBody())
503506
//解析参数
504507
f.addParamsFromContext(ctx.functionParams())
505508
//TODO 解析函数的注解
@@ -592,7 +595,7 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
592595
val f = if(ctx.functionParams().readOnlyParams() != null && ctx.functionParams().readOnlyParams().parameterList().parameter().size != 0){
593596
GenericExtensionFunction(ctx.Identifier().text, data, Project.currNamespace, MCFPPType.parseFromIdentifier(ctx.functionReturnType().text, typeScope), ctx.functionBody())
594597
}else{
595-
ExtensionFunction(ctx.Identifier().text, data, Project.currNamespace, MCFPPType.parseFromIdentifier(ctx.functionReturnType().text, typeScope))
598+
ExtensionFunction(ctx.Identifier().text, data, Project.currNamespace, MCFPPType.parseFromIdentifier(ctx.functionReturnType().text, typeScope), ctx.functionBody())
596599
}
597600
//解析参数
598601
f.accessModifier = AccessModifier.PUBLIC
@@ -769,7 +772,8 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
769772
ctx.Identifier().text,
770773
DataTemplate.currTemplate!!,
771774
DataTemplate.currTemplate is ObjectDataTemplate,
772-
type
775+
type,
776+
ctx.functionBody()
773777
)
774778
}
775779
if(!isStatic){

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
125125
if(ctx.VAR() != null){
126126
//自动判断类型
127127
val init: Var<*> = McfppExprVisitor().visit(ctx.expression())!!
128-
val `var` = Var.build(ctx.Identifier().text, init.type, Function.currFunction)
128+
val `var` = if(fieldModifier == "import"){
129+
val qwq = Var.buildUnConcrete(ctx.Identifier().text, init.type, Function.currFunction)
130+
qwq.hasAssigned = true
131+
qwq
132+
}else{
133+
Var.build(ctx.Identifier().text, init.type, Function.currFunction)
134+
}
129135
//变量注册
130136
//一定是函数变量
131137
if (!Function.field.putVar(ctx.Identifier().text, `var`, true)) {
@@ -149,22 +155,24 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
149155
`var`.toDynamic(true)
150156
}
151157
}
152-
"import" -> {
153-
`var`.isImport = true
154-
}
155158
}
156159
}else{
157160
//获取类型
158161
val type = MCFPPType.parseFromContext(ctx.type(), Function.currFunction.field)
159162
for (c in ctx.fieldDeclarationExpression()){
160163
//函数变量,生成
161-
var `var` = Var.build(c.Identifier().text, type, Function.currFunction)
164+
var `var` = if(fieldModifier == "import"){
165+
val qwq = Var.buildUnConcrete(c.Identifier().text, type, Function.currFunction)
166+
qwq.hasAssigned = true
167+
qwq
168+
}else{
169+
Var.build(c.Identifier().text, type, Function.currFunction)
170+
}
162171
//变量注册
163172
//一定是函数变量
164173
if (Function.field.containVar(c.Identifier().text)) {
165174
LogProcessor.error("Duplicate defined variable name:" + c.Identifier().text)
166175
}
167-
Function.field.putVar(`var`.identifier, `var`, true)
168176
Function.addComment("field: " + ctx.type().text + " " + c.Identifier().text + if (c.expression() != null) " = " + c.expression().text else "")
169177
//变量初始化
170178
if (c.expression() != null) {
@@ -184,14 +192,12 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
184192
`var`.isConst = true
185193
}
186194
"dynamic" -> {
187-
if(`var` is MCFPPValue<*>){
195+
if(`var` is MCFPPValue<*> && `var`.hasAssigned){
188196
`var`.toDynamic(true)
189197
}
190198
}
191-
"import" -> {
192-
`var`.isImport = true
193-
}
194199
}
200+
Function.field.putVar(`var`.identifier, `var`, true)
195201
}
196202
}
197203
return null

src/main/kotlin/top/mcfpp/commandline/LineCompiler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class LineCompiler {
1616
val OUTPUT_COLOR = "\u001B[38;5;15m"
1717
val RESET_COLOR = "\u001B[0m"
1818

19-
val default = Function("default","mcfpp")
19+
val default = Function("default","mcfpp", context = null)
2020
val defaultFile = MCFPPFile("default")
2121

2222
var unmatchedBraces : String = ""

src/main/kotlin/top/mcfpp/compiletime/CompileTimeFunction.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import top.mcfpp.model.field.IField
99
import kotlin.collections.ArrayList
1010

1111
class CompileTimeFunction : Function {
12-
var context: mcfppParser.FunctionBodyContext
13-
constructor(name:String, namespace:String, returnType: MCFPPType, context:mcfppParser.FunctionBodyContext):super(name,namespace,returnType){
14-
this.context=context
15-
}
12+
constructor(name:String, namespace:String, returnType: MCFPPType, context:mcfppParser.FunctionBodyContext):super(name,namespace,returnType, context)
1613

1714
fun setField(parent: IField){
1815
this.field = CompileTimeFunctionField(parent,this)

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ object DatapackCreator {
106106
Paths.get("$currPath/functions/${f.nameWithNamespace}.mcfunction"),
107107
f.cmdStr.toByteArray()
108108
)
109+
if(f.compiledFunctions.isNotEmpty()){
110+
for (cf in f.compiledFunctions.values){
111+
LogProcessor.debug("Writing File: $currPath\\functions\\${cf.nameWithNamespace}.mcfunction")
112+
Files.createDirectories(Paths.get("$currPath/functions"))
113+
Files.write(
114+
Paths.get("$currPath/functions/${cf.nameWithNamespace}.mcfunction"),
115+
cf.cmdStr.toByteArray()
116+
)
117+
}
118+
119+
}
109120
}
110121
}
111122
namespace.value.field.forEachClass { cls ->
@@ -126,6 +137,14 @@ object DatapackCreator {
126137
Paths.get("$currPath/functions/" + f.nameWithNamespace + ".mcfunction"),
127138
f.cmdStr.toByteArray()
128139
)
140+
if(f.compiledFunctions.isNotEmpty()){
141+
for (cf in f.compiledFunctions.values) {
142+
LogProcessor.debug("Writing File: $currPath\\functions\\" + cf.nameWithNamespace + ".mcfunction")
143+
//TODO 可能无法正确创建文件夹
144+
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(cls.identifier)))
145+
Files.write(Paths.get("$currPath/functions/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
146+
}
147+
}
129148
}
130149
}
131150
//构造函数
@@ -138,6 +157,14 @@ object DatapackCreator {
138157
Paths.get("$currPath/functions/" + c.nameWithNamespace + ".mcfunction"),
139158
c.cmdStr.toByteArray()
140159
)
160+
if(c.compiledFunctions.isNotEmpty()){
161+
for (cf in c.compiledFunctions.values) {
162+
LogProcessor.debug("Writing File: $currPath\\functions\\" + cf.nameWithNamespace + ".mcfunction")
163+
//TODO 可能无法正确创建文件夹
164+
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(cls.identifier)))
165+
Files.write(Paths.get("$currPath/functions/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
166+
}
167+
}
141168
}
142169
}
143170
}
@@ -161,6 +188,14 @@ object DatapackCreator {
161188
Paths.get("$currPath/functions/" + f.nameWithNamespace + ".mcfunction"),
162189
f.cmdStr.toByteArray()
163190
)
191+
if(f.compiledFunctions.isNotEmpty()){
192+
for (cf in f.compiledFunctions.values) {
193+
LogProcessor.debug("Writing File: $currPath\\functions\\" + cf.nameWithNamespace + ".mcfunction")
194+
//TODO 可能无法正确创建文件夹
195+
Files.createDirectories(Paths.get("$currPath/functions/" + StringHelper.toLowerCase(t.identifier)))
196+
Files.write(Paths.get("$currPath/functions/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
197+
}
198+
}
164199
}
165200
}
166201
}
@@ -183,6 +218,14 @@ object DatapackCreator {
183218
Paths.get("$currPath/functions/" + f.nameWithNamespace + ".mcfunction"),
184219
f.cmdStr.toByteArray()
185220
)
221+
if(f.compiledFunctions.isNotEmpty()){
222+
for (cf in f.compiledFunctions.values) {
223+
LogProcessor.debug("Writing File: $currPath\\functions\\" + cf.nameWithNamespace + ".mcfunction")
224+
//TODO 可能无法正确创建文件夹
225+
Files.createDirectories(Paths.get("$currPath\\functions/" + StringHelper.toLowerCase(obj.identifier)))
226+
Files.write(Paths.get("$currPath/functions/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
227+
}
228+
}
186229
}
187230
}
188231
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ object FunctionReader: ILibJsonReader<Function> {
140140
val ctx = Utils.fromByteArrayString<mcfppParser.FunctionBodyContext>(jsonObject["context"].toString())
141141
GenericFunction(identifier, namespace, returnType, ctx)
142142
}else{
143-
Function(identifier, namespace, returnType)
143+
Function(identifier, namespace, returnType, null)
144144
}
145145
currFunction = function
146146
//参数获取

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class MCFPPFile(path : String) : File(path) {
2929
val namespace: String
3030

3131
//TODO 同名文件的顶级函数之间的命名冲突
32-
val topFunction = Function(StringHelper.toLegalIdentifier(this.name))
32+
val topFunction = Function(StringHelper.toLegalIdentifier(this.name), context = null)
3333

3434
init {
3535
val n = Project.config.sourcePath.toAbsolutePath().relativize(this.toPath().toAbsolutePath().parent).toString()
@@ -96,7 +96,8 @@ class MCFPPFile(path : String) : File(path) {
9696
//创建默认函数
9797
val func = Function(
9898
StringHelper.toLowerCase(nameWithoutExtension + "_default"), Project.currNamespace,
99-
MCFPPBaseType.Void
99+
MCFPPBaseType.Void,
100+
context = null
100101
)
101102
Function.currFunction = func
102103
McfppImVisitor().visit(tree())

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class ReturnedMCBool(val parentFunction: Function) : MCBool() {
1919
this.isStatic = false
2020
this.isTemp = false
2121
this.isConst = false
22-
this.isImport = false
2322
}
2423

2524
override fun doAssign(b: Var<*>): MCBool {

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ abstract class Var<Self: Var<Self>> : Member, Cloneable, CanSelectMember, Serial
6161
var isConst = false
6262
var hasAssigned = false
6363

64-
/**
65-
* 这个变量是否是引入的。对应import关键字
66-
*/
67-
var isImport = false
68-
6964
/**
7065
* 是否是临时变量
7166
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ open class Class : CompoundData {
8585
constructor(identifier: String, namespace: String = Project.currNamespace) {
8686
this.identifier = identifier
8787
this.namespace = namespace
88-
classPreInit = Function("_class_preinit_$identifier", this, false)
88+
classPreInit = Function("_class_preinit_$identifier", this, false, context = null)
8989
field.addFunction(classPreInit,true)
9090
}
9191

0 commit comments

Comments
 (0)