Skip to content

Commit 24f0d17

Browse files
committed
修复for的编译问题
1 parent f7631bb commit 24f0d17

File tree

2 files changed

+56
-43
lines changed

2 files changed

+56
-43
lines changed

.mclib

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"id":"default",
55
"functions":[
66
{
7-
"id":"generateSequenceDoWhile",
7+
"id":"generateSequenceFor",
88
"normalParams":[
99

1010
],
@@ -15,7 +15,7 @@
1515
]
1616
},
1717
{
18-
"id":"_dowhile_78ddf9e4-56ed-4439-93bc-bceb4e47f9ec",
18+
"id":"_for_7f20e3fa-440c-4610-93e5-68d9961970f9",
1919
"normalParams":[
2020

2121
],
@@ -26,7 +26,7 @@
2626
]
2727
},
2828
{
29-
"id":"_dowhile_de4d3e61-9c4a-4c3f-81ae-3e7decf37e3b",
29+
"id":"_for_loop_8ae8a0fe-449b-49ae-97cb-ec1a2751e85d",
3030
"normalParams":[
3131

3232
],
@@ -37,7 +37,18 @@
3737
]
3838
},
3939
{
40-
"id":"expression_52569370-85ad-4996-bbe1-8b209ead925a",
40+
"id":"expression_8ab728bf-b02a-4a8b-8443-391cd9b62182",
41+
"normalParams":[
42+
43+
],
44+
"returnType":"void",
45+
"isAbstract":false,
46+
"tags":[
47+
48+
]
49+
},
50+
{
51+
"id":"_forblock_671a526c-cac2-4fdd-8eb1-a8db3ab42369",
4152
"normalParams":[
4253

4354
],

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

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
722722
fun enterForStatement(ctx: mcfppParser.ForStatementContext) {
723723
Project.ctx = ctx
724724
Function.addCommand("#for start")
725+
//for语句对应的函数
725726
val forFunc: Function = InternalFunction("_for_", Function.currFunction)
726727
forFunc.parent.add(Function.currFunction)
727728
if(!GlobalField.localNamespaces.containsKey(forFunc.namespace))
@@ -768,43 +769,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
768769
GlobalField.localNamespaces[forLoopFunc.namespace]!!.field.addFunction(forLoopFunc,false)
769770
Function.addCommand("data modify storage mcfpp:system " + Project.config.defaultNamespace + ".stack_frame prepend value {}")
770771
Function.addCommand(Commands.function(forLoopFunc))
771-
772-
}
773-
774-
775-
/**
776-
* 进入for update语句块。
777-
* 由于在编译过程中,编译器会首先编译for语句的for control部分,也就是for后面的括号,这就意味着forUpdate语句将会先forBlock
778-
* 被写入到命令函数中。因此我们需要将forUpdate语句中的命令临时放在一个列表内部,然后在forBlock调用完毕后加上它的命令
779-
*
780-
* @param ctx the parse tree
781-
*/
782-
783-
override fun visitForUpdate(ctx: mcfppParser.ForUpdateContext): Any? {
784-
enterForUpdate(ctx)
785-
super.visitForUpdate(ctx)
786-
exitForUpdate(ctx)
787-
return null
788-
}
789-
790-
fun enterForUpdate(ctx: mcfppParser.ForUpdateContext) {
791-
Project.ctx = ctx
792-
forInitCommands = Function.currFunction.commands
793-
Function.currFunction.commands = forUpdateCommands
794-
}
795-
796-
//暂存
797-
private var forInitCommands = CommandList()
798-
private var forUpdateCommands = CommandList()
799-
800-
/**
801-
* 离开for update。暂存for update缓存,恢复主缓存,准备forblock编译
802-
* @param ctx the parse tree
803-
*/
804-
805-
fun exitForUpdate(ctx: mcfppParser.ForUpdateContext) {
806-
Project.ctx = ctx
807-
Function.currFunction.commands = forInitCommands
772+
Function.currFunction = forLoopFunc
808773
}
809774

810775
override fun visitForBlock(ctx: mcfppParser.ForBlockContext): Any? {
@@ -818,10 +783,10 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
818783
* 进入for block语句。此时当前函数为父函数
819784
* @param ctx the parse tree
820785
*/
821-
822786
@InsertCommand
823787
fun enterForBlock(ctx: mcfppParser.ForBlockContext) {
824788
Project.ctx = ctx
789+
//currFunction 是 forLoop
825790
val parent: mcfppParser.ForStatementContext = ctx.parent as mcfppParser.ForStatementContext
826791
val exp: MCBool = McfppExprVisitor().visit(parent.forControl().expression()) as MCBool
827792
//匿名函数的定义。这里才是正式的for函数哦喵
@@ -866,7 +831,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
866831
* 离开for block语句。此时当前函数仍然是for的函数
867832
* @param ctx the parse tree
868833
*/
869-
834+
870835
@InsertCommand
871836
fun exitForBlock(ctx: mcfppParser.ForBlockContext) {
872837
Project.ctx = ctx
@@ -877,8 +842,45 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
877842
Function.addCommand("data remove storage mcfpp:system " + Project.config.defaultNamespace + ".stack_frame[0]")
878843
//继续销毁for-loop函数的栈
879844
Function.addCommand("data remove storage mcfpp:system " + Project.config.defaultNamespace + ".stack_frame[0]")
845+
Function.addCommand("return 1")
880846
Function.currFunction = Function.currFunction.parent[0]
881847
}
848+
849+
/**
850+
* 进入for update语句块。
851+
* 由于在编译过程中,编译器会首先编译for语句的for control部分,也就是for后面的括号,这就意味着forUpdate语句将会先forBlock
852+
* 被写入到命令函数中。因此我们需要将forUpdate语句中的命令临时放在一个列表内部,然后在forBlock调用完毕后加上它的命令
853+
*
854+
* @param ctx the parse tree
855+
*/
856+
857+
override fun visitForUpdate(ctx: mcfppParser.ForUpdateContext): Any? {
858+
enterForUpdate(ctx)
859+
super.visitForUpdate(ctx)
860+
exitForUpdate(ctx)
861+
return null
862+
}
863+
864+
fun enterForUpdate(ctx: mcfppParser.ForUpdateContext) {
865+
Project.ctx = ctx
866+
forInitCommands = Function.currFunction.commands
867+
Function.currFunction.commands = forUpdateCommands
868+
}
869+
870+
//暂存
871+
private var forInitCommands = CommandList()
872+
private var forUpdateCommands = CommandList()
873+
874+
/**
875+
* 离开for update。暂存for update缓存,恢复主缓存,准备forblock编译
876+
* @param ctx the parse tree
877+
*/
878+
879+
fun exitForUpdate(ctx: mcfppParser.ForUpdateContext) {
880+
Project.ctx = ctx
881+
Function.currFunction.commands = forInitCommands
882+
}
883+
882884
//endregion
883885

884886

0 commit comments

Comments
 (0)