@@ -722,6 +722,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
722
722
fun enterForStatement (ctx : mcfppParser.ForStatementContext ) {
723
723
Project .ctx = ctx
724
724
Function .addCommand(" #for start" )
725
+ // for语句对应的函数
725
726
val forFunc: Function = InternalFunction (" _for_" , Function .currFunction)
726
727
forFunc.parent.add(Function .currFunction)
727
728
if (! GlobalField .localNamespaces.containsKey(forFunc.namespace))
@@ -768,43 +769,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
768
769
GlobalField .localNamespaces[forLoopFunc.namespace]!! .field.addFunction(forLoopFunc,false )
769
770
Function .addCommand(" data modify storage mcfpp:system " + Project .config.defaultNamespace + " .stack_frame prepend value {}" )
770
771
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
808
773
}
809
774
810
775
override fun visitForBlock (ctx : mcfppParser.ForBlockContext ): Any? {
@@ -818,10 +783,10 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
818
783
* 进入for block语句。此时当前函数为父函数
819
784
* @param ctx the parse tree
820
785
*/
821
-
822
786
@InsertCommand
823
787
fun enterForBlock (ctx : mcfppParser.ForBlockContext ) {
824
788
Project .ctx = ctx
789
+ // currFunction 是 forLoop
825
790
val parent: mcfppParser.ForStatementContext = ctx.parent as mcfppParser.ForStatementContext
826
791
val exp: MCBool = McfppExprVisitor ().visit(parent.forControl().expression()) as MCBool
827
792
// 匿名函数的定义。这里才是正式的for函数哦喵
@@ -866,7 +831,7 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
866
831
* 离开for block语句。此时当前函数仍然是for的函数
867
832
* @param ctx the parse tree
868
833
*/
869
-
834
+
870
835
@InsertCommand
871
836
fun exitForBlock (ctx : mcfppParser.ForBlockContext ) {
872
837
Project .ctx = ctx
@@ -877,8 +842,45 @@ open class McfppImVisitor: mcfppParserBaseVisitor<Any?>() {
877
842
Function .addCommand(" data remove storage mcfpp:system " + Project .config.defaultNamespace + " .stack_frame[0]" )
878
843
// 继续销毁for-loop函数的栈
879
844
Function .addCommand(" data remove storage mcfpp:system " + Project .config.defaultNamespace + " .stack_frame[0]" )
845
+ Function .addCommand(" return 1" )
880
846
Function .currFunction = Function .currFunction.parent[0 ]
881
847
}
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
+
882
884
// endregion
883
885
884
886
0 commit comments