Skip to content

Commit 17ec62d

Browse files
committed
修复类的定义问题
现在函数不必要声明参数列表
1 parent 2fb9463 commit 17ec62d

File tree

11 files changed

+194
-130
lines changed

11 files changed

+194
-130
lines changed

.mclib

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,6 @@
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-
},
296
{
307
"id":"main",
318
"normalParams":[
@@ -36,25 +13,81 @@
3613
"tags":[
3714

3815
]
39-
},
16+
}
17+
],
18+
"classes":[
4019
{
41-
"id":"test_0",
42-
"normalParams":[
20+
"id":"Test",
21+
"parents":[
22+
"mcfpp.lang:any"
23+
],
24+
"field":{
25+
"vars":[
26+
{
27+
"id":"i",
28+
"type":"int"
29+
}
30+
],
31+
"functions":[
32+
{
33+
"id":"_class_preinit_Test",
34+
"normalParams":[
35+
36+
],
37+
"returnType":"void",
38+
"isAbstract":false,
39+
"tags":[
40+
41+
]
42+
},
43+
{
44+
"id":"_init_test_0_lead",
45+
"normalParams":[
46+
47+
],
48+
"returnType":"void",
49+
"isAbstract":false,
50+
"tags":[
51+
52+
]
53+
}
54+
]
55+
},
56+
"constructors":[
4357
{
44-
"id":"p",
45-
"type":"int",
46-
"isStatic":false
58+
"normalParams":[
59+
60+
]
4761
}
48-
],
49-
"returnType":"void",
50-
"isAbstract":false,
51-
"tags":[
52-
5362
]
63+
},
64+
{
65+
"id":"Test$",
66+
"parents":[
67+
"mcfpp.lang:any"
68+
],
69+
"field":{
70+
"vars":[
71+
{
72+
"id":"id",
73+
"type":"int"
74+
}
75+
],
76+
"functions":[
77+
{
78+
"id":"_class_preinit_Test",
79+
"normalParams":[
80+
81+
],
82+
"returnType":"void",
83+
"isAbstract":false,
84+
"tags":[
85+
86+
]
87+
}
88+
]
89+
}
5490
}
55-
],
56-
"classes":[
57-
5891
],
5992
"template":[
6093

src/main/antlr/mcfppLexer.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ DATA:'data';
105105
FUNCTION:'func';
106106
ENUM:'enum';
107107

108+
CONSTRUCTOR:'constructor';
109+
108110
GLOBAL:'global';
109111
VAR:'var';
110112

src/main/antlr/mcfppParser.g4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ inlineFunctionDeclaration
187187

188188
//函数声明
189189
functionDeclaration
190-
: funcAnnoation? FUNCTION Identifier functionParams (ARROW functionReturnType)? '{' functionBody '}'
190+
: funcAnnoation? FUNCTION Identifier functionParams? (ARROW functionReturnType)? '{' functionBody '}'
191191
;
192192

193193
extensionFunctionDeclaration
@@ -228,7 +228,7 @@ accessModifier
228228

229229
//构造函数声明
230230
constructorDeclaration
231-
: funcAnnoation? className normalParams '{' functionBody '}'
231+
: funcAnnoation? accessModifier? CONSTRUCTOR normalParams '{' functionBody '}'
232232
;
233233

234234
//变量声明

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
398398
val f = Constructor(Class.currClass!!)
399399
f.addParamsFromContext(ctx.normalParams())
400400
if(!Class.currClass!!.addConstructor(f)){
401-
LogProcessor.error("Already defined constructor: " + ctx.className().text + "(" + ctx.normalParams().text + ")")
401+
LogProcessor.error("Already defined constructor: constructor(" + ctx.normalParams().text + ") in class " + Class.currClass)
402402
}
403403
return f
404404
}
@@ -463,23 +463,26 @@ open class McfppFieldVisitor : mcfppParserBaseVisitor<Any?>() {
463463
//创建函数对象
464464
val identifier : String = ctx.Identifier().text
465465
val type = MCFPPType.parseFromIdentifier(if(ctx.functionReturnType() == null) "void" else ctx.functionReturnType().text, typeScope)
466-
val f = if(ctx.functionParams().readOnlyParams() != null && ctx.functionParams().readOnlyParams().parameterList().parameter().size != 0){
466+
val f = if(ctx.functionParams()?.readOnlyParams() != null && ctx.functionParams().readOnlyParams().parameterList().parameter().size != 0){
467467
GenericFunction(identifier, Project.currNamespace, type, ctx.functionBody())
468468
}else {
469469
Function(identifier, Project.currNamespace,type)
470470
}
471471
//解析参数
472-
f.addParamsFromContext(ctx.functionParams())
472+
ctx.functionParams()?.let { f.addParamsFromContext(it) }
473473
//TODO 解析函数的注解
474474
//不是类的成员
475475
f.ownerType = Function.Companion.OwnerType.NONE
476476
//写入域
477477
val namespace = GlobalField.localNamespaces[f.namespace]!!
478-
if (!namespace.field.hasFunction(f)) {
479-
namespace.field.addFunction(f,false)
480-
} else {
481-
LogProcessor.error("Already defined function:" + f.namespaceID)
478+
if (namespace.field.hasFunction(f)) {
479+
LogProcessor.error("Already defined function: " + f.namespaceID)
480+
Function.currFunction = Function.nullFunction
481+
} else if(namespace.field.hasDeclaredType(f.identifier)) {
482+
LogProcessor.error("Function name conflicted with type name: " + f.identifier)
482483
Function.currFunction = Function.nullFunction
484+
} else{
485+
namespace.field.addFunction(f,false)
483486
}
484487
if (f.isEntrance
485488
&& ctx.functionParams().normalParams().parameterList().parameter().size != 0

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
5050
Project.ctx = ctx
5151
val f: Function
5252
//获取函数对象
53-
val types = FunctionParam.parseReadonlyAndNormalParamTypes(ctx.functionParams())
53+
val types = ctx.functionParams()?.let { FunctionParam.parseReadonlyAndNormalParamTypes(it) }
5454
//获取缓存中的对象
55-
f = GlobalField.getFunction(Project.currNamespace, ctx.Identifier().text, types.first, types.second)
55+
f = GlobalField.getFunction(Project.currNamespace, ctx.Identifier().text, types?.first?:ArrayList(), types?.second?:ArrayList())
5656
Function.currFunction = f
5757
//对函数进行注解处理
5858
for (a in annoInCompound){
@@ -65,7 +65,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
6565
Project.ctx = ctx
6666
//函数是否有返回值
6767
if(Function.currFunction !is Generic<*> && Function.currFunction.returnType != MCFPPBaseType.Void && !Function.currFunction.hasReturnStatement){
68-
LogProcessor.error("A 'return' expression required in function: " + Function.currFunction.namespaceID)
68+
LogProcessor.error("Function should return a value: " + Function.currFunction.namespaceID)
6969
}
7070
Function.currFunction = Function.nullFunction
7171
if (Class.currClass == null) {
@@ -205,7 +205,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
205205
Project.ctx = ctx
206206
Function.addCommand("#expression: " + ctx.text)
207207
if(ctx.basicExpression() != null){
208-
val left: Var<*> = McfppLeftExprVisitor().visit(ctx.basicExpression())!!
208+
val left: Var<*> = McfppLeftExprVisitor().visit(ctx.basicExpression())
209209
if (left.isConst) {
210210
LogProcessor.error("Cannot assign a constant repeatedly: " + left.identifier)
211211
return null
@@ -979,8 +979,13 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
979979
private fun enterClassBody(ctx: mcfppParser.ClassBodyContext) {
980980
Project.ctx = ctx
981981
//获取类的对象
982-
val parent: mcfppParser.ClassDeclarationContext = ctx.parent as mcfppParser.ClassDeclarationContext
983-
val identifier: String = parent.classWithoutNamespace().text
982+
val parent = ctx.parent
983+
val identifier = if(parent is mcfppParser.ClassDeclarationContext){
984+
parent.classWithoutNamespace().text
985+
}else{
986+
parent as mcfppParser.ObjectClassDeclarationContext
987+
parent.classWithoutNamespace().text
988+
}
984989
//设置作用域
985990
Class.currClass = GlobalField.getClass(Project.currNamespace, identifier)
986991
Function.currFunction = Class.currClass!!.classPreInit
@@ -1002,22 +1007,6 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
10021007
Function.currFunction = Function.nullFunction
10031008
}
10041009

1005-
/**
1006-
* 类成员的声明
1007-
* @param ctx the parse tree
1008-
*/
1009-
1010-
override fun visitClassMemberDeclaration(ctx: mcfppParser.ClassMemberDeclarationContext):Any? {
1011-
Project.ctx = ctx
1012-
val memberContext: mcfppParser.ClassMemberContext = ctx.classMember()?:return null
1013-
if (memberContext.classFunctionDeclaration() != null) {
1014-
//函数声明由函数的listener处理
1015-
visit(memberContext.classFunctionDeclaration())
1016-
return null
1017-
}
1018-
return null
1019-
}
1020-
10211010
override fun visitClassFunctionDeclaration(ctx: mcfppParser.ClassFunctionDeclarationContext): Any? {
10221011
//是类的成员函数
10231012
enterClassFunctionDeclaration(ctx)

0 commit comments

Comments
 (0)