Skip to content

Commit d855a76

Browse files
committed
函数参数缺省值
1 parent c981355 commit d855a76

File tree

21 files changed

+308
-202
lines changed

21 files changed

+308
-202
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ build/
44
!**/src/main/**/build/
55
!**/src/test/**/build/
66
/downloader/
7+
/src/test/java/top/mcfpp/test/QAQ.java
78

89
### IntelliJ IDEA ###
910
.idea/modules.xml

.mclib

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
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+
},
621
{
722
"id":"main",
823
"normalParams":[
@@ -16,41 +31,7 @@
1631
}
1732
],
1833
"classes":[
19-
{
20-
"id":"Test",
21-
"parents":[
22-
"mcfpp.lang:DataObject"
23-
],
24-
"field":{
25-
"vars":[
26-
{
27-
"id":"qwq",
28-
"type":"nbt"
29-
}
30-
],
31-
"functions":[
32-
{
33-
"id":"test",
34-
"normalParams":[
35-
36-
],
37-
"returnType":"void",
38-
"isAbstract":false,
39-
"tags":[
40-
41-
]
42-
}
43-
]
44-
},
45-
"staticField":{
46-
"vars":[
47-
48-
],
49-
"functions":[
50-
51-
]
52-
}
53-
}
34+
5435
],
5536
"template":[
5637

src/main/antlr/mcfppLexer.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ IMPORT: 'import';
9999
INLINE:'inline';
100100

101101
CLASS:'class';
102+
OBJECT:'object';
102103
INTERFACE:'interface';
103104
DATA:'data';
104105
FUNCTION:'func';

src/main/antlr/mcfppParser.g4

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ globalDeclaration
8282

8383
//类声明
8484
classDeclaration
85-
: classAnnotation? STATIC? FINAL? ABSTRACT? CLASS classWithoutNamespace readOnlyParams? (COLON className (',' className)*)? classBody
85+
: classAnnotation? STATIC? FINAL? ABSTRACT? OBJECT? CLASS classWithoutNamespace readOnlyParams? (COLON className (',' className)*)? classBody
8686
;
8787

8888
compileTimeClassDeclaration
@@ -132,7 +132,7 @@ classFieldDeclaration
132132

133133
//数据模板
134134
templateDeclaration
135-
: FINAL? DATA classWithoutNamespace (COLON className)? templateBody
135+
: FINAL? OBJECT? DATA classWithoutNamespace (COLON className)? templateBody
136136
;
137137

138138
templateBody
@@ -244,7 +244,7 @@ parameterList
244244

245245
//参数
246246
parameter
247-
: STATIC? type Identifier
247+
: STATIC? type Identifier '=' expression
248248
;
249249

250250
//表达式
@@ -350,7 +350,7 @@ selector
350350
;
351351

352352
arguments
353-
: readOnlyArgs? normalArgs?
353+
: readOnlyArgs? normalArgs
354354
;
355355

356356
readOnlyArgs
@@ -533,6 +533,7 @@ classWithoutNamespace
533533
funcAnnoation
534534
: '@' id=Identifier arguments?
535535
;
536+
536537
classAnnotation
537538
: '@' id=Identifier arguments?
538539
;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ open class Command {
141141
* @param command 固定的命令字符串
142142
* @return
143143
*/
144-
fun build(command: String, blank: Boolean = true) : Command{
145-
if(blank) commandStringList.add(" ")
144+
fun build(command: String, withBlank: Boolean = true) : Command{
145+
if(withBlank) commandStringList.add(" ")
146146
commandStringList.add(command)
147147
return this
148148
}
149149

150-
fun build(command: Command, blank: Boolean = true): Command{
151-
if(blank) commandStringList.add(" ")
150+
fun build(command: Command, withBlank: Boolean = true): Command{
151+
if(withBlank) commandStringList.add(" ")
152152
for (kv in command.replacePoint){
153153
replacePoint[kv.key] = kv.value + commandStringList.size
154154
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import top.mcfpp.lang.*
77
import top.mcfpp.lang.type.MCFPPClassType
88
import top.mcfpp.lib.NBTPath
99
import top.mcfpp.model.function.Function
10+
import top.mcfpp.model.function.NoStackFunction
1011
import java.util.*
1112

1213
/**
@@ -148,4 +149,14 @@ object Commands {
148149
Project.macroFunction[f] = command.toMacro()
149150
return Command.build("function mcfpp:dynamic/$f")
150151
}
152+
153+
fun fakeFunction(parent: Function , operation: () -> Unit) : Array<Command>{
154+
val l = Function.currFunction
155+
val f = NoStackFunction("", parent)
156+
Function.currFunction = f
157+
operation()
158+
Function.currFunction = l
159+
return f.commands.toTypedArray()
160+
161+
}
151162
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,11 @@ class MCIntConcrete : MCInt, MCFPPValue<Int>{
490490
}
491491
Function.addCommand(cmd.last().build("scoreboard players set @s $`object` $value"))
492492
} else {
493-
val cmd: String = if (!isTemp)
494-
"execute store result $nbtPath int 1 run "
493+
val cmd = if (!isTemp)
494+
Command("execute store result").build(nbtPath.toCommandPart()).build("int 1 run ")
495495
else
496-
""
497-
Function.addCommand(cmd + "scoreboard players set $name $`object` $value")
496+
Command("")
497+
Function.addCommand(cmd.build("scoreboard players set $name $`object` $value", false))
498498
}
499499
val re = MCInt(this)
500500
if(replace){

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,24 @@ class Namespace(val identifier: String) {
7676
if(np.type is UnresolvedType){
7777
f.normalParams[index].type = (np.type as UnresolvedType).resolve(f.field)
7878
}
79+
f.field.putVar(np.identifier, np.buildVar())
7980
}
8081
if(f is GenericFunction){
81-
for ((index, np) in f.readOnlyParams.withIndex()){
82-
if(np.type is UnresolvedType){
83-
f.readOnlyParams[index].type = (np.type as UnresolvedType).resolve(f.field)
82+
for ((index, rp) in f.readOnlyParams.withIndex()){
83+
if(rp.type is UnresolvedType){
84+
f.readOnlyParams[index].type = (rp.type as UnresolvedType).resolve(f.field)
8485
}
86+
f.field.putVar(rp.identifier, rp.buildVar())
8587
}
8688
}
8789
if(f is NativeFunction){
88-
for ((index, np) in f.readOnlyParams.withIndex()){
89-
if(np.type is UnresolvedType){
90-
f.readOnlyParams[index].type = (np.type as UnresolvedType).resolve(f.field)
90+
for ((index, rp) in f.readOnlyParams.withIndex()){
91+
if(rp.type is UnresolvedType){
92+
f.readOnlyParams[index].type = (rp.type as UnresolvedType).resolve(f.field)
9193
}
94+
f.field.putVar(rp.identifier, rp.buildVar())
9295
}
9396
}
94-
f.parseParams()
9597
if(f.returnType is UnresolvedType){
9698
f.returnType = (f.returnType as UnresolvedType).resolve(f.field)
9799
}

src/main/kotlin/top/mcfpp/model/field/CompoundDataField.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ class CompoundDataField : IFieldWithFunction, IFieldWithVar, IFieldWithType {
178178
return f
179179
}
180180
}
181+
for (f in functions) {
182+
if(f is Generic<*> && f.isSelfWithDefaultValue(key, readOnlyParams, normalParams)){
183+
return f
184+
}
185+
if(f.isSelfWithDefaultValue(key, normalParams)){
186+
return f
187+
}
188+
}
181189
return UnknownFunction(key)
182190
}
183191

src/main/kotlin/top/mcfpp/model/field/NamespaceField.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ open class NamespaceField: IFieldWithClass, IFieldWithFunction, IFieldWithTempla
126126
return f
127127
}
128128
}
129+
for (f in functions) {
130+
if(f is Generic<*> && f.isSelfWithDefaultValue(key, readOnlyParams, normalParams)){
131+
return f
132+
}
133+
if(f.isSelfWithDefaultValue(key, normalParams)){
134+
return f
135+
}
136+
}
129137
return null
130138
}
131139

0 commit comments

Comments
 (0)