Skip to content

Commit f4d2e42

Browse files
committed
泛型类的实现
1 parent b947e77 commit f4d2e42

File tree

8 files changed

+91
-19
lines changed

8 files changed

+91
-19
lines changed

src/main/antlr/mcfppLexer.g4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ STATIC:'static';
8383
EXTENDS:'extends';
8484
NATIVE:'native';
8585
CONCRETE:'concrete';
86-
FINAL:'final ';
86+
FINAL:'final';
8787

8888
PUBLIC:'public';
8989
PROTECTED:'protected';
9090
PRIVATE:'private';
9191

9292
OVERRIDE: 'override';
9393
ABSTRACT: 'abstract';
94+
IMPL: 'impl';
9495

9596
CONST:'const';
9697
DYNAMIC:'dynamic';

src/main/antlr/mcfppParser.g4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typeDeclaration
6363
//类或函数声明
6464
declarations
6565
: classDeclaration
66+
| genericClassImplement
6667
| objectClassDeclaration
6768
| functionDeclaration
6869
| inlineFunctionDeclaration
@@ -133,6 +134,10 @@ classFieldDeclaration
133134
: accessModifier? type fieldDeclarationExpression
134135
;
135136

137+
genericClassImplement
138+
: IMPL classAnnotation? STATIC? FINAL? ABSTRACT? CLASS classWithoutNamespace readOnlyArgs (COLON className (',' className)*)? classBody
139+
;
140+
136141
//数据模板
137142
templateDeclaration
138143
: FINAL? OBJECT? DATA classWithoutNamespace (COLON className)? templateBody
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package top.mcfpp.mni;
22

33
public class CommandFunctionData {
4+
5+
6+
47
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package top.mcfpp.antlr
22

33
import top.mcfpp.Project
44
import top.mcfpp.lang.MCAny
5+
import top.mcfpp.lang.Var
6+
import top.mcfpp.lang.value.MCFPPValue
57
import top.mcfpp.model.*
68
import top.mcfpp.model.Enum
79
import top.mcfpp.model.field.GlobalField
810
import top.mcfpp.model.generic.ClassParam
911
import top.mcfpp.model.generic.GenericClass
1012
import top.mcfpp.model.generic.GenericObjectClass
13+
import top.mcfpp.model.generic.ImplementedGenericClass
1114
import top.mcfpp.util.LogProcessor
1215
import top.mcfpp.util.StringHelper
1316

@@ -254,6 +257,50 @@ class McfppTypeVisitor: mcfppParserBaseVisitor<Unit>() {
254257
nsp.field.addObject(objectClass.identifier, objectClass)
255258
}
256259

260+
override fun visitGenericClassImplement(ctx: mcfppParser.GenericClassImplementContext) {
261+
Project.ctx = ctx
262+
//注册类
263+
val id = ctx.classWithoutNamespace().text
264+
265+
val readOnlyArgs: ArrayList<Var<*>> = ArrayList()
266+
val exprVisitor = McfppExprVisitor()
267+
for (expr in ctx.readOnlyArgs().expressionList().expression()) {
268+
val arg = exprVisitor.visit(expr)!!
269+
if(arg !is MCFPPValue<*>){
270+
LogProcessor.error("Generic class implement must be a value")
271+
return
272+
}
273+
readOnlyArgs.add(arg)
274+
}
275+
276+
val genericClass = GlobalField.getClass(Project.currNamespace, id)
277+
if(genericClass == null){
278+
LogProcessor.error("Undefined generic class: $id in namespace ${Project.currNamespace}")
279+
return
280+
}
281+
if(genericClass !is GenericClass){
282+
LogProcessor.error("Class $id is not a generic class")
283+
return
284+
}
285+
286+
val cls = ImplementedGenericClass(id, Project.currNamespace, readOnlyArgs, genericClass)
287+
288+
if(ctx.className().size != 0){
289+
for (p in ctx.className()){
290+
//是否存在继承
291+
val qwq = StringHelper.splitNamespaceID(p.text)
292+
val identifier: String = qwq.second
293+
val namespace : String? = qwq.first
294+
cls.extends(Class.Companion.UndefinedClassOrInterface(identifier,namespace))
295+
}
296+
}else{
297+
//继承Any类
298+
cls.extends(MCAny.data)
299+
}
300+
cls.isStaticClass = ctx.STATIC() != null
301+
cls.isAbstract = ctx.ABSTRACT() != null
302+
}
303+
257304
override fun visitTemplateDeclaration(ctx: mcfppParser.TemplateDeclarationContext) {
258305
Project.ctx = ctx
259306
//注册模板

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ open class MCInt : MCNumber<Int> {
4848

4949
override var type: MCFPPType = MCFPPBaseType.Int
5050

51-
@Override
52-
@Throws(VariableConverseException::class)
5351
override fun assign(b: Var<*>) : MCInt {
5452
hasAssigned = true
5553
return when (b) {
@@ -69,7 +67,6 @@ open class MCInt : MCNumber<Int> {
6967
}
7068
}
7169

72-
@Override
7370
override fun cast(type: MCFPPType): Var<*> {
7471
//TODO 类支持
7572
return when (type) {
@@ -127,7 +124,6 @@ open class MCInt : MCNumber<Int> {
127124
) as MCInt
128125
}
129126

130-
@Override
131127
@InsertCommand
132128
override fun plus(a: Var<*>): Var<*> {
133129
//t += a
@@ -146,7 +142,6 @@ open class MCInt : MCNumber<Int> {
146142
}
147143
}
148144

149-
@Override
150145
@InsertCommand
151146
override fun minus(a: Var<*>): Var<*> {
152147
//t -= a
@@ -165,7 +160,6 @@ open class MCInt : MCNumber<Int> {
165160
}
166161
}
167162

168-
@Override
169163
@InsertCommand
170164
override fun multiple(a: Var<*>): Var<*> {
171165
//t *= a
@@ -182,7 +176,6 @@ open class MCInt : MCNumber<Int> {
182176
return this
183177
}
184178

185-
@Override
186179
@InsertCommand
187180
override fun divide(a: Var<*>): Var<*> {
188181
if(!isTemp){
@@ -197,7 +190,6 @@ open class MCInt : MCNumber<Int> {
197190
return this
198191
}
199192

200-
@Override
201193
@InsertCommand
202194
override fun modular(a: Var<*>): Var<*> {
203195
if(!isTemp){
@@ -212,7 +204,6 @@ open class MCInt : MCNumber<Int> {
212204
return this
213205
}
214206

215-
@Override
216207
@InsertCommand
217208
override fun isBigger(a: Var<*>): MCBool {
218209
//re = t > a
@@ -236,7 +227,6 @@ open class MCInt : MCNumber<Int> {
236227
return re
237228
}
238229

239-
@Override
240230
@InsertCommand
241231
override fun isSmaller(a: Var<*>): MCBool {
242232
//re = t < a
@@ -260,7 +250,6 @@ open class MCInt : MCNumber<Int> {
260250
return re
261251
}
262252

263-
@Override
264253
@InsertCommand
265254
override fun isSmallerOrEqual(a: Var<*>): MCBool {
266255
//re = t <= a
@@ -284,7 +273,6 @@ open class MCInt : MCNumber<Int> {
284273
return re
285274
}
286275

287-
@Override
288276
@InsertCommand
289277
override fun isGreaterOrEqual(a: Var<*>): MCBool {
290278
//re = t <= a
@@ -308,7 +296,6 @@ open class MCInt : MCNumber<Int> {
308296
return re
309297
}
310298

311-
@Override
312299
@InsertCommand
313300
override fun isEqual(a: Var<*>): MCBool {
314301
//re = t == a
@@ -332,7 +319,6 @@ open class MCInt : MCNumber<Int> {
332319
return re
333320
}
334321

335-
@Override
336322
@InsertCommand
337323
override fun isNotEqual(a: Var<*>): MCBool {
338324
//re = t != a
@@ -356,7 +342,6 @@ open class MCInt : MCNumber<Int> {
356342
return re
357343
}
358344

359-
@Override
360345
override fun clone(): MCInt {
361346
return MCInt(this)
362347
}
@@ -369,7 +354,6 @@ open class MCInt : MCNumber<Int> {
369354
*
370355
* @return 返回临时变量
371356
*/
372-
@Override
373357
@InsertCommand
374358
override fun getTempVar(): Var<*> {
375359
if (isTemp) return this

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ open class CompoundData : FieldContainer, Serializable {
9898
* 向这个类中添加一个成员
9999
* @param member 要添加的成员
100100
*/
101-
fun addMember(member: Member) {
102-
if (member is Function) {
101+
fun addMember(member: Member): Boolean {
102+
return if (member is Function) {
103103
field.addFunction(member, false)
104104
} else if (member is Var<*>) {
105105
field.putVar(member.identifier, member)
106+
} else {
107+
TODO()
106108
}
107109
}
108110

@@ -224,4 +226,11 @@ open class CompoundData : FieldContainer, Serializable {
224226
}
225227
Project.currNamespace = l
226228
}
229+
230+
231+
fun forMember(operation: (Member) -> Any?){
232+
field.forEachFunction { operation(it) }
233+
field.forEachVar { operation(it) }
234+
}
235+
227236
}

src/main/kotlin/top/mcfpp/model/generic/GenericClass.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ class GenericClass : Class {
104104
}
105105
return false
106106
}
107+
108+
companion object {
109+
class Unknown
110+
}
107111
}
108112

109113
class ClassParam(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package top.mcfpp.model.generic
2+
3+
import top.mcfpp.Project
4+
import top.mcfpp.lang.Var
5+
import top.mcfpp.lang.value.MCFPPValue
6+
import top.mcfpp.model.Class
7+
import top.mcfpp.util.LogProcessor
8+
9+
class ImplementedGenericClass(identifier: String, namespace: String = Project.currNamespace, val readOnlyArgs: List<Var<*>>, val parentGenericClass: GenericClass) : Class(identifier, namespace) {
10+
11+
init {
12+
extends(parentGenericClass.compile(readOnlyArgs))
13+
if(parentGenericClass.compiledClasses.containsKey(readOnlyArgs)){
14+
LogProcessor.error("Duplicate generic class implementation: $identifier ${readOnlyArgs.map { (it as MCFPPValue<*>).value }.joinToString(",")}")
15+
}
16+
parentGenericClass.compiledClasses[readOnlyArgs] = this
17+
}
18+
19+
}

0 commit comments

Comments
 (0)