Skip to content

Commit 2fb9463

Browse files
committed
引入单例,同时将静态成员更改为伴随对象
修复函数不被运行的问题 修复枚举类不会被库读取的问题
1 parent ef5122b commit 2fb9463

37 files changed

+933
-365
lines changed

.mclib

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
{
44
"id":"default",
55
"functions":[
6+
{
7+
"id":"test",
8+
"readonlyParam":[
9+
{
10+
"id":"i",
11+
"type":"int",
12+
"isStatic":false
13+
}
14+
],
15+
"context":"ACED00057372002E746F702E6D636670702E7574696C2E53657269616C697A61626C6546756E6374696F6E426F6479436F6E746578745F70EEA0543D2A420200007870",
16+
"normalParams":[
17+
{
18+
"id":"p",
19+
"type":"int",
20+
"isStatic":false
21+
}
22+
],
23+
"returnType":"void",
24+
"isAbstract":false,
25+
"tags":[
26+
27+
]
28+
},
629
{
730
"id":"main",
831
"normalParams":[
@@ -12,6 +35,21 @@
1235
"isAbstract":false,
1336
"tags":[
1437

38+
]
39+
},
40+
{
41+
"id":"test_0",
42+
"normalParams":[
43+
{
44+
"id":"p",
45+
"type":"int",
46+
"isStatic":false
47+
}
48+
],
49+
"returnType":"void",
50+
"isAbstract":false,
51+
"tags":[
52+
1553
]
1654
}
1755
],
@@ -20,6 +58,9 @@
2058
],
2159
"template":[
2260

61+
],
62+
"enum":[
63+
2364
]
2465
}
2566
]

src/main/antlr/mcfppParser.g4

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ typeDeclaration
6363
//类或函数声明
6464
declarations
6565
: classDeclaration
66+
| objectClassDeclaration
6667
| functionDeclaration
6768
| inlineFunctionDeclaration
6869
| nativeFuncDeclaration
6970
| compileTimeFuncDeclaration
7071
| compileTimeClassDeclaration
7172
| nativeClassDeclaration
7273
| templateDeclaration
74+
| objectTemplateDeclaration
7375
| extensionFunctionDeclaration
7476
| interfaceDeclaration
7577
| globalDeclaration
@@ -83,7 +85,11 @@ globalDeclaration
8385

8486
//类声明
8587
classDeclaration
86-
: classAnnotation? STATIC? FINAL? ABSTRACT? OBJECT? CLASS classWithoutNamespace readOnlyParams? (COLON className (',' className)*)? classBody
88+
: classAnnotation? STATIC? FINAL? ABSTRACT? CLASS classWithoutNamespace readOnlyParams? (COLON className (',' className)*)? classBody
89+
;
90+
91+
objectClassDeclaration
92+
: classAnnotation? OBJECT CLASS classWithoutNamespace readOnlyParams? (COLON className (',' className)*)? classBody
8793
;
8894

8995
compileTimeClassDeclaration
@@ -94,16 +100,12 @@ nativeClassDeclaration
94100
: CLASS classWithoutNamespace '=' javaRefer ';'
95101
;
96102

97-
staticClassMemberDeclaration
98-
: accessModifier? STATIC classMember
99-
;
100-
101103
classMemberDeclaration
102104
: accessModifier? classMember
103105
;
104106

105107
classBody
106-
: '{' (doc_comment? (classMemberDeclaration|staticClassMemberDeclaration))* '}'
108+
: '{' (doc_comment? classMemberDeclaration)* '}'
107109
;
108110

109111
//类成员
@@ -136,18 +138,19 @@ templateDeclaration
136138
: FINAL? OBJECT? DATA classWithoutNamespace (COLON className)? templateBody
137139
;
138140

141+
//数据模板
142+
objectTemplateDeclaration
143+
: FINAL? OBJECT DATA classWithoutNamespace (COLON className)? templateBody
144+
;
145+
139146
templateBody
140-
: '{' (doc_comment? (templateMemberDeclaration|staticTemplateMemberDeclaration))* '}'
147+
: '{' (doc_comment? templateMemberDeclaration)* '}'
141148
;
142149

143150
templateMemberDeclaration
144151
: accessModifier? templateMember
145152
;
146153

147-
staticTemplateMemberDeclaration
148-
: accessModifier? STATIC templateMember
149-
;
150-
151154
templateMember
152155
: templateFunctionDeclaration
153156
| templateFieldDeclaration
@@ -259,7 +262,7 @@ parameterList
259262

260263
//参数
261264
parameter
262-
: STATIC? type Identifier '=' expression
265+
: STATIC? type Identifier ('=' expression)?
263266
;
264267

265268
//表达式

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,6 @@ object Project {
207207
c.field.putVar(c.identifier, v.resolve(c), true)
208208
}
209209
}
210-
for (v in c.staticField.allVars){
211-
if(v is UnresolvedVar){
212-
c.staticField.putVar(c.identifier, v.resolve(c), true)
213-
}
214-
}
215210
}
216211
}
217212
}
@@ -224,11 +219,6 @@ object Project {
224219
c.field.putVar(c.identifier, v.resolve(c), true)
225220
}
226221
}
227-
for (v in c.staticField.allVars){
228-
if(v is UnresolvedVar){
229-
c.staticField.putVar(c.identifier, v.resolve(c), true)
230-
}
231-
}
232222
}
233223
}
234224
}
@@ -311,9 +301,11 @@ object Project {
311301
Function.addCommand("execute unless score math mcfpp_init matches 1 run function math:_init")
312302
//向load中添加类初始化命令
313303
for (n in GlobalField.localNamespaces.values){
314-
n.field.forEachClass { c->
304+
n.field.forEachObject { c->
315305
run {
316-
c.classPreStaticInit.invoke(ArrayList(), callerClassP = null)
306+
if(c is ObjectClass){
307+
c.classPreInit.invoke(ArrayList(), callerClassP = null)
308+
}
317309
}
318310
}
319311
}

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -422,22 +422,12 @@ class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
422422
} else{
423423
StringHelper.splitNamespaceID(ctx.type().text)
424424
}
425-
val clazz = GlobalField.getClass(namespaceID.first, namespaceID.second)
426-
if (clazz != null) {
427-
currSelector = clazz.getType()
428-
}else{
429-
val template = GlobalField.getTemplate(namespaceID.first, namespaceID.second)
430-
if(template != null){
431-
currSelector = template.getType()
432-
}else{
433-
val enum = GlobalField.getEnum(namespaceID.first, namespaceID.second)
434-
if(enum != null){
435-
currSelector = enum.getType()
436-
}else{
437-
LogProcessor.error("Undefined type: ${namespaceID.second}")
438-
currSelector = UnknownVar("${ctx.type().className().text}_type_" + UUID.randomUUID())
439-
}
440-
}
425+
val o = GlobalField.getObject(namespaceID.first, namespaceID.second)
426+
if(o != null) {
427+
currSelector = o.getType()
428+
} else{
429+
LogProcessor.error("Undefined type: ${namespaceID.second}")
430+
currSelector = UnknownVar("${ctx.type().className().text}_type_" + UUID.randomUUID())
441431
}
442432
}else{
443433
currSelector = CompoundDataCompanion(

0 commit comments

Comments
 (0)