Skip to content

Commit 4eec9cb

Browse files
committed
类的只读参数重载
修复了泛型类函数的编译问题
1 parent 49e724a commit 4eec9cb

27 files changed

+846
-250
lines changed

.idea/encodings.xml

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

.mclib

Lines changed: 257 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,269 @@
1313
"tags":[
1414

1515
]
16-
},
16+
}
17+
],
18+
"classes":[
1719
{
18-
"id":"test",
19-
"normalParams":[
20+
"id":"Test",
21+
"parents":[
22+
"mcfpp:any"
23+
],
24+
"generic":[
25+
{
26+
"id":"i",
27+
"type":"int"
28+
}
29+
],
30+
"context":"ACED00057372002B746F702E6D636670702E7574696C2E53657269616C697A61626C65436C617373426F6479436F6E74657874054A027FE4B581400200007870",
31+
"field":{
32+
"vars":[
33+
34+
],
35+
"functions":[
36+
{
37+
"id":"_class_preinit_Test",
38+
"normalParams":[
39+
40+
],
41+
"returnType":"void",
42+
"isAbstract":false,
43+
"tags":[
44+
45+
]
46+
}
47+
]
48+
},
49+
"staticField":{
50+
"vars":[
51+
52+
],
53+
"functions":[
54+
{
55+
"id":"_class_prestaticinit_Test",
56+
"normalParams":[
57+
58+
],
59+
"returnType":"void",
60+
"isAbstract":false,
61+
"tags":[
62+
63+
]
64+
}
65+
]
66+
},
67+
"constructors":[
2068

69+
]
70+
},
71+
{
72+
"id":"Test",
73+
"parents":[
74+
"mcfpp:any"
2175
],
22-
"returnType":"void",
23-
"isAbstract":false,
24-
"tags":[
76+
"generic":[
77+
{
78+
"id":"i",
79+
"type":"int"
80+
},
81+
{
82+
"id":"j",
83+
"type":"int"
84+
}
85+
],
86+
"context":"ACED00057372002B746F702E6D636670702E7574696C2E53657269616C697A61626C65436C617373426F6479436F6E74657874054A027FE4B581400200007870",
87+
"field":{
88+
"vars":[
89+
90+
],
91+
"functions":[
92+
{
93+
"id":"_class_preinit_Test",
94+
"normalParams":[
95+
96+
],
97+
"returnType":"void",
98+
"isAbstract":false,
99+
"tags":[
100+
101+
]
102+
}
103+
]
104+
},
105+
"staticField":{
106+
"vars":[
107+
108+
],
109+
"functions":[
110+
{
111+
"id":"_class_prestaticinit_Test",
112+
"normalParams":[
113+
114+
],
115+
"returnType":"void",
116+
"isAbstract":false,
117+
"tags":[
118+
119+
]
120+
}
121+
]
122+
},
123+
"constructors":[
25124

26125
]
126+
},
127+
{
128+
"id":"Test_int_int_0",
129+
"parents":[
130+
"mcfpp:any"
131+
],
132+
"field":{
133+
"vars":[
134+
{
135+
"id":"e702de50-7f50-49f6-82f9-282fde1cbff8",
136+
"type":"int"
137+
},
138+
{
139+
"id":"a09cff90-4fad-425a-8fd6-0b7541219411",
140+
"type":"int"
141+
}
142+
],
143+
"functions":[
144+
{
145+
"id":"_class_preinit_Test_int_int_0",
146+
"normalParams":[
147+
148+
],
149+
"returnType":"void",
150+
"isAbstract":false,
151+
"tags":[
152+
153+
]
154+
},
155+
{
156+
"id":"print",
157+
"normalParams":[
158+
159+
],
160+
"returnType":"void",
161+
"isAbstract":false,
162+
"tags":[
163+
164+
]
165+
},
166+
{
167+
"id":"_init_test_int_int_0_0_lead",
168+
"normalParams":[
169+
170+
],
171+
"returnType":"void",
172+
"isAbstract":false,
173+
"tags":[
174+
175+
]
176+
}
177+
]
178+
},
179+
"staticField":{
180+
"vars":[
181+
182+
],
183+
"functions":[
184+
{
185+
"id":"_class_prestaticinit_Test_int_int_0",
186+
"normalParams":[
187+
188+
],
189+
"returnType":"void",
190+
"isAbstract":false,
191+
"tags":[
192+
193+
]
194+
}
195+
]
196+
},
197+
"constructors":[
198+
{
199+
"normalParams":[
200+
201+
]
202+
}
203+
]
204+
},
205+
{
206+
"id":"Test_int_0",
207+
"parents":[
208+
"mcfpp:any"
209+
],
210+
"field":{
211+
"vars":[
212+
{
213+
"id":"082e4970-deaa-4823-bb56-82e9482be6c1",
214+
"type":"int"
215+
}
216+
],
217+
"functions":[
218+
{
219+
"id":"_class_preinit_Test_int_0",
220+
"normalParams":[
221+
222+
],
223+
"returnType":"void",
224+
"isAbstract":false,
225+
"tags":[
226+
227+
]
228+
},
229+
{
230+
"id":"print",
231+
"normalParams":[
232+
233+
],
234+
"returnType":"void",
235+
"isAbstract":false,
236+
"tags":[
237+
238+
]
239+
},
240+
{
241+
"id":"_init_test_int_0_0_lead",
242+
"normalParams":[
243+
244+
],
245+
"returnType":"void",
246+
"isAbstract":false,
247+
"tags":[
248+
249+
]
250+
}
251+
]
252+
},
253+
"staticField":{
254+
"vars":[
255+
256+
],
257+
"functions":[
258+
{
259+
"id":"_class_prestaticinit_Test_int_0",
260+
"normalParams":[
261+
262+
],
263+
"returnType":"void",
264+
"isAbstract":false,
265+
"tags":[
266+
267+
]
268+
}
269+
]
270+
},
271+
"constructors":[
272+
{
273+
"normalParams":[
274+
275+
]
276+
}
277+
]
27278
}
28-
],
29-
"classes":[
30-
31279
],
32280
"template":[
33281

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies {
4040
antlr("org.antlr:antlr4:4.12.0")
4141
implementation(kotlin("reflect"))
4242
testImplementation(kotlin("script-runtime"))
43+
implementation("com.google.guava:guava:33.2.0-jre")
4344
}
4445

4546
tasks.test {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,12 @@ class McfppExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
553553
}
554554
//调用函数
555555
return if (func is UnknownFunction) {
556+
var cls: Class? = if(ctx.arguments().readOnlyArgs() != null){
557+
GlobalField.getClass(p.first, p.second ,readOnlyArgs.map { it.type })
558+
}else{
559+
GlobalField.getClass(p.first, p.second)
560+
}
556561
//可能是构造函数
557-
var cls: Class? = GlobalField.getClass(p.first, p.second)
558562
if (cls == null) {
559563
LogProcessor.error("Function " + ctx.text + " not defined")
560564
Function.addCommand("[Failed to Compile]${ctx.text}")

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
146146
//注册类
147147
val id = ctx.classWithoutNamespace().text
148148
val namespace = GlobalField.localNamespaces[Project.currNamespace]!!
149+
if(ctx.readOnlyParams() != null){
150+
return null
151+
}
149152
val clazz = if (namespace.field.hasClass(id)) {
150153
namespace.field.getClass(id)
151154
} else {
152155
throw UndefinedException("Class Should have been defined: $id")
153156
}
154-
if(clazz is GenericClass){
155-
return null
156-
}
157157
Class.currClass = clazz
158158
typeScope = Class.currClass!!.field
159159
//解析类中的成员

src/main/kotlin/top/mcfpp/antlr/McfppGenericClassVisitor.kt renamed to src/main/kotlin/top/mcfpp/antlr/McfppGenericClassFieldVisitor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import top.mcfpp.model.function.Constructor
77
import top.mcfpp.model.function.Function
88
import top.mcfpp.util.LogProcessor
99

10-
class McfppGenericClassVisitor(val clazz: Class) : McfppFieldVisitor() {
10+
class McfppGenericClassFieldVisitor(val clazz: Class) : McfppFieldVisitor() {
1111
override fun visitClassDeclaration(ctx: mcfppParser.ClassDeclarationContext): Any? {
1212
Project.ctx = ctx
1313

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package top.mcfpp.antlr
2+
3+
import top.mcfpp.Project
4+
import top.mcfpp.model.Class
5+
import top.mcfpp.model.CompiledGenericClass
6+
import top.mcfpp.model.field.GlobalField
7+
import top.mcfpp.model.function.Function
8+
9+
class McfppGenericClassImVisitor : McfppImVisitor() {
10+
11+
override fun visitClassBody(ctx: mcfppParser.ClassBodyContext): Any? {
12+
enterClassBody(ctx)
13+
visitChildren(ctx)
14+
exitClassBody(ctx)
15+
return null
16+
}
17+
18+
/**
19+
* 进入类体。
20+
* @param ctx the parse tree
21+
*/
22+
private fun enterClassBody(ctx: mcfppParser.ClassBodyContext) {
23+
Project.ctx = ctx
24+
//TODO 注解
25+
}
26+
27+
/**
28+
* 离开类体。将缓存重新指向全局
29+
* @param ctx the parse tree
30+
*/
31+
32+
private fun exitClassBody(ctx: mcfppParser.ClassBodyContext) {
33+
Project.ctx = ctx
34+
Class.currClass = null
35+
Function.currFunction = Function.nullFunction
36+
}
37+
38+
}

0 commit comments

Comments
 (0)