Skip to content

Commit e56eced

Browse files
committed
修复模板相关的问题
1 parent 67b4685 commit e56eced

File tree

69 files changed

+1838
-534
lines changed

Some content is hidden

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

69 files changed

+1838
-534
lines changed

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ResourceIDGenerator.jar

56 Bytes
Binary file not shown.

build.gradle.kts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import org.gradle.kotlin.dsl.cpp
2-
import org.jetbrains.kotlin.fir.declarations.builder.buildScript
1+
32
import java.nio.file.Files
4-
import java.nio.file.Path
5-
import java.nio.file.Paths
63
import java.util.Optional
74

85
plugins {
@@ -46,7 +43,7 @@ dependencies {
4643
implementation("org.commonmark:commonmark-ext-task-list-items:0.21.0")
4744
implementation("fr.brouillard.oss:commonmark-ext-notifications:1.1.0")
4845
implementation("info.debatty:java-string-similarity:2.0.0")
49-
antlr("org.antlr:antlr4:4.12.0")
46+
antlr("org.antlr:antlr4:4.13.1")
5047
implementation(kotlin("reflect"))
5148
testImplementation(kotlin("script-runtime"))
5249
implementation("com.google.guava:guava:33.2.0-jre")

mcfpp.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
{
2-
"files": [
3-
"src/main/mcfpp/**"
4-
],
52
"sourcePath": "src/main/mcfpp",
6-
"description": "",
3+
"description": "MCFPP Standard Library",
74
"namespace": "mcfpp",
85
"targetPath": "src/main/resources/lib",
9-
"noDatapack": false,
10-
"ignoreStdLib": true,
11-
"isLib": true
6+
"compileArgs": [
7+
"-ignoreStdLib",
8+
"-isLib"
9+
]
1210
}

src/main/antlr/mcfppLexer.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ STRING: 'string';
130130
JTEXT: 'text';
131131
NBT: 'nbt';
132132
ANY: 'any';
133-
TYPE: 'type';
134133
VOID: 'void';
135134
LIST: 'list';
136135
MAP: 'map';
137136
DICT: 'dict';
137+
TYPE: 'Type';
138138
BYTEARRAY: 'ByteArray';
139139
INTARRAY: 'IntArray';
140140
LONGARRAY: 'LongArray';

src/main/antlr/mcfppParser.g4

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespaceDeclaration
5252
;
5353

5454
importDeclaration
55-
: IMPORT Identifier ('.' Identifier)* ('.' cls = (Identifier|'*'))? (AS Identifier)? (FROM Identifier)? ';'
55+
: IMPORT Identifier (DOT Identifier)* ':' cls = (Identifier|'*') (AS Identifier)? (FROM Identifier)? ';'
5656
;
5757

5858
//类或函数声明
@@ -113,7 +113,7 @@ classBody
113113
//类成员
114114
classMember
115115
: classFunctionDeclaration
116-
| classFieldDeclaration ';'
116+
| classFieldDeclaration
117117
| classConstructorDeclaration
118118
| nativeClassFunctionDeclaration
119119
| abstractClassFunctionDeclaration
@@ -133,7 +133,7 @@ nativeClassFunctionDeclaration
133133
;
134134

135135
classFieldDeclaration
136-
: accessModifier? type fieldDeclarationExpression accessor?
136+
: accessModifier? type fieldDeclarationExpression accessor? ';'
137137
;
138138

139139
accessor
@@ -188,7 +188,7 @@ templateFunctionDeclaration
188188
;
189189

190190
templateFieldDeclaration
191-
: CONST? (singleTemplateFieldType | unionTemplateFieldType) Identifier ('=' expression)? ';' accessor?
191+
: CONST? (singleTemplateFieldType | unionTemplateFieldType) Identifier ('=' expression)? accessor? ';'
192192
;
193193

194194
singleTemplateFieldType
@@ -553,6 +553,8 @@ typeWithoutExcl
553553
: normalType
554554
| VecType
555555
| LIST '<' type '>'
556+
| MAP '<' type '>'
557+
| DICT '<' type '>'
556558
| className readOnlyArgs?
557559
| Identifier
558560
| unionTemplateType
@@ -577,8 +579,6 @@ normalType
577579
| NBT
578580
| TYPE
579581
| ANY
580-
| MAP
581-
| DICT
582582
| BYTEARRAY
583583
| INTARRAY
584584
| LONGARRAY
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package top.mcfpp.annotations;
22

3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target({ElementType.METHOD})
9+
@Retention(RetentionPolicy.RUNTIME)
310
public @interface MNIAccessor {
411
String name();
512
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
package top.mcfpp.annotations;
22

3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target({ElementType.METHOD})
9+
@Retention(RetentionPolicy.RUNTIME)
310
public @interface MNIMember {
411
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package top.mcfpp.annotations;
22

3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target({ElementType.METHOD})
9+
@Retention(RetentionPolicy.RUNTIME)
310
public @interface MNIMutator {
411
String name();
512
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package top.mcfpp.mni.minecraft;
2+
3+
public class AreaData {
4+
}

src/main/java/top/mcfpp/mni/minecraft/BossBarData.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static void remove(DataTemplateObject bossbar, ValueWrapper<CommandReturn
6060
}
6161

6262
@MNIFunction(caller = "BossBar", isObject = true, returnType = "CommandReturn")
63-
public static void list(ValueWrapper<CommandReturn> returnValue){
63+
public static void list(DataTemplateObject bossbar, ValueWrapper<CommandReturn> returnValue){
6464
var command = new Command("bossbar list");
6565
returnValue.setValue(new CommandReturn(command,"bossbar_list"));
6666
Function.Companion.addCommand(command);
@@ -236,7 +236,7 @@ public static void setVisible(DataTemplateObject bossbar, ScoreBool value){
236236
}
237237
}
238238

239-
@MNIFunction(normalParams = "BossBarColor", caller = "BossBar", returnType = "CommandReturn")
239+
@MNIFunction(normalParams = "BossBarColor color", caller = "BossBar", returnType = "CommandReturn")
240240
public static void setColor(EnumVar color, DataTemplateObject caller, ValueWrapper<CommandReturn> re){
241241
var id = Objects.requireNonNull(caller.getMemberVarWithT("id", MCString.class));
242242
Command command = new Command("bossbar set");;
@@ -262,7 +262,7 @@ public static void setColor(EnumVar color, DataTemplateObject caller, ValueWrapp
262262
}
263263
}
264264

265-
@MNIFunction(normalParams = "JsonText", caller = "BossBar", returnType = "CommandReturn")
265+
@MNIFunction(normalParams = "text name", caller = "BossBar", returnType = "CommandReturn")
266266
public static void setName(JsonText name, DataTemplateObject caller, ValueWrapper<CommandReturn> re){
267267
var id = Objects.requireNonNull(caller.getMemberVarWithT("id", MCString.class));
268268
Command command = new Command("bossbar set");
@@ -283,7 +283,7 @@ public static void setName(JsonText name, DataTemplateObject caller, ValueWrappe
283283
}
284284
}
285285

286-
@MNIFunction(normalParams = "Player", caller = "BossBar", returnType = "CommandReturn")
286+
@MNIFunction(normalParams = "Player players", caller = "BossBar", returnType = "CommandReturn")
287287
public static void setVisiblePlayers(PlayerVar players, DataTemplateObject bossbar, ValueWrapper<CommandReturn> returnValue) {
288288
var id = Objects.requireNonNull(bossbar.getMemberVarWithT("id", MCString.class));
289289
Command command = new Command("bossbar set");
@@ -309,7 +309,7 @@ public static void setVisiblePlayers(PlayerVar players, DataTemplateObject bossb
309309
}
310310
}
311311

312-
@MNIFunction(normalParams = "BossBarStyle", caller = "BossBar", returnType = "CommandReturn")
312+
@MNIFunction(normalParams = "BossBarStyle style", caller = "BossBar", returnType = "CommandReturn")
313313
public static void setStyle(EnumVar style, DataTemplateObject caller, ValueWrapper<CommandReturn> re){
314314
var id = Objects.requireNonNull(caller.getMemberVarWithT("id", MCString.class));
315315
Command command = new Command("bossbar set");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ object CompileSettings {
2020
* 最大循环内联次数
2121
*/
2222
var maxWhileInline = 32
23+
24+
var printAll = false
2325
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ fun main(args: Array<String>) {
3333
LogProcessor.error("Cannot find file: $path")
3434
}
3535
compile(Project.readConfig(path)) //读取配置文件
36-
GlobalField.printAll()
36+
37+
if(CompileSettings.printAll) GlobalField.printAll()
3738
}
3839
}
3940

@@ -44,7 +45,7 @@ fun compile(config: ProjectConfig){
4445
Project.compileStage = 0
4546
Project.stageProcessor[0].forEach { it() }
4647
Project.init() //初始化
47-
Project.checkConfig() //检查配置文件
48+
if(!Project.checkConfig()) return //检查配置文件
4849
Project.readProject() //读取引用的库的索引
4950
Project.indexType() //编制类型索引
5051
Project.resolveField() //编制函数索引
@@ -85,6 +86,10 @@ fun parseArgs(args: List<String>){
8586

8687
if (arg.startsWith("-maxWhileInline=")) arg else "$$arg"
8788
-> CompileSettings.maxWhileInline = arg.split("=")[1].toInt()
89+
90+
else -> {
91+
LogProcessor.warn("Invalid argument: $arg")
92+
}
8893
}
8994
}
9095

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

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,38 +153,72 @@ object Project {
153153
val jsonObject: JSONObject = JSONObject.parse(json) as JSONObject
154154

155155
//源代码根目录
156-
config.sourcePath = jsonObject.getString("sourcePath")?.let { Path(it) }
156+
if(jsonObject.containsKey("sourcePath")){
157+
config.sourcePath = Path(jsonObject.getString("sourcePath"))
158+
jsonObject.remove("sourcePath")
159+
}
157160

158161
//版本
159-
config.version = jsonObject.getString("version")?:"1.21"
162+
if(jsonObject.containsKey("version")){
163+
config.version = jsonObject.getString("version")
164+
jsonObject.remove("version")
165+
}
160166

161167
//描述
162-
config.description = jsonObject.getString("description")?:"A datapack compiled by MCFPP"
168+
if(jsonObject.containsKey("description")){
169+
config.description = jsonObject.getString("description")
170+
jsonObject.remove("description")
171+
}
163172

164173
//默认命名空间
165-
config.rootNamespace = jsonObject.getString("namespace")?: "default"
174+
if(jsonObject.containsKey("namespace")){
175+
config.rootNamespace = jsonObject.getString("namespace")
176+
jsonObject.remove("namespace")
177+
}
166178

167179
//调用库
168-
val includesJson: JSONArray = jsonObject.getJSONArray("includes")?: JSONArray()
169-
for (i in 0..<includesJson.size) {
170-
config.includes.add(includesJson.getString(i))
180+
if(jsonObject.containsKey("jars")){
181+
val jarsJson: JSONArray = jsonObject.getJSONArray("jars")
182+
for (i in 0..<jarsJson.size) {
183+
config.jars.add(jarsJson.getString(i))
184+
}
185+
jsonObject.remove("jars")
171186
}
172187

173188
//输出目录
174-
config.targetPath = jsonObject.getString("targetPath")?.let { Path(it) }
189+
if(jsonObject.containsKey("targetPath")){
190+
config.targetPath = Path(jsonObject.getString("targetPath"))
191+
jsonObject.remove("targetPath")
192+
}
175193

176194
//是否生成数据包
177-
config.noDatapack = jsonObject.getBooleanValue("noDatapack")
195+
if(jsonObject.containsKey("noDatapack")){
196+
config.noDatapack = jsonObject.getBoolean("noDatapack")
197+
jsonObject.remove("noDatapack")
198+
}
178199

179200
//注释等级
180-
config.commentLevel = jsonObject.getString("commentLevel")?.let {
181-
try {
182-
CommentLevel.valueOf(it.uppercase())
201+
if(jsonObject.containsKey("commentLevel")){
202+
val str = jsonObject.getString("commentLevel")
203+
config.commentLevel = try {
204+
CommentLevel.valueOf(str.uppercase())
183205
}catch (e: Exception){
184-
LogProcessor.error("Unsupported comment level: $it, using default value \"DEBUG\"")
206+
LogProcessor.error("Unsupported comment level: $str, using default value \"DEBUG\"")
185207
CommentLevel.DEBUG
186208
}
187-
}?: config.commentLevel
209+
jsonObject.remove("commentLevel")
210+
}
211+
212+
//编译参数
213+
if(jsonObject.containsKey("compileArgs")){
214+
val compileArgsJson = jsonObject.getJSONArray("compileArgs")
215+
parseArgs(compileArgsJson.toList(String::class.java))
216+
jsonObject.remove("compileArgs")
217+
}
218+
219+
for (key in jsonObject.keys) {
220+
LogProcessor.warn("Unsupported config item: $key")
221+
}
188222

189223
} catch (e: Exception) {
190224
LogProcessor.error("Error while reading project from file \"$path\"")
@@ -194,7 +228,7 @@ object Project {
194228
return config
195229
}
196230

197-
fun checkConfig(){
231+
fun checkConfig(): Boolean{
198232
if (!Utils.version.contains(config.version)){
199233
LogProcessor.warn("Unsupported version: ${config.version}")
200234
config.version = Utils.version[0]
@@ -207,6 +241,11 @@ object Project {
207241
LogProcessor.warn("Set source path default to \"${config.root.pathString}\"")
208242
config.sourcePath = Path(config.root.pathString)
209243
}
244+
if(config.sourcePath!!.notExists()){
245+
LogProcessor.error("Invalid source path: ${config.sourcePath}")
246+
return false
247+
}
248+
return true
210249
}
211250

212251
/**
@@ -295,6 +334,9 @@ object Project {
295334
files.add(MCFPPFile(it.toFile()))
296335
}
297336
}
337+
if(files.isEmpty()){
338+
LogProcessor.error("Cannot find any mcfpp file in path: ${config.sourcePath}")
339+
}
298340
stageProcessor[compileStage].forEach { it() }
299341
}
300342

0 commit comments

Comments
 (0)