C语言解释器-12 控制结构之break、continue和return
2013-03-28 17:21
363 查看
这三种控制结构都必须结合上下文环境共同作用。看起来它们更像是一种标记。因此上,它们的实现也相当简单:
return多了一个返回值的表达式:
一切需要回溯到Block,在Block运行时,会检测这三种控制结构:
再看ExecuteControlFlow():
通过返回false来立即中止上层Block的运行循环。
public class Break : ControlFlow { public override void Print(int tabs = 0) { Debug.WriteLine(new string('\t', tabs) + "break;"); } }
public class Continue : ControlFlow { public override void Print(int tabs = 0) { Debug.WriteLine(new string('\t', tabs) + "continue;"); } }
return多了一个返回值的表达式:
public class Return : ControlFlow { public Expression.ExpressionNode Expression; public override void Print(int tabs = 0) { if (Expression == null) Debug.WriteLine(new string('\t', tabs) + "return ;"); else { Debug.WriteLine(new string('\t', tabs) + "return " + Expression.ToString()); } } }
一切需要回溯到Block,在Block运行时,会检测这三种控制结构:
public override void Run(Context ctx) { if (IsFirstRunning) { Block parentBlock = this.ParentBlock; if (parentBlock != null) { this.OnReturn += parentBlock.OnReturn; this.OnBreak += parentBlock.OnBreak; this.OnContinue += parentBlock.OnContinue; } IsFirstRunning = false; } foreach (Context stx in Children) { if (stx is ControlFlow.ControlFlow) { if (!ExecuteControlFlow(stx)) break;
再看ExecuteControlFlow():
private bool ExecuteControlFlow(Context stx) { if (stx is ControlFlow.Return) { if (OnReturn != null) OnReturn(this, stx as ControlFlow.Return); return false; } else { if (stx is ControlFlow.Break) { if (OnBreak != null) OnBreak(this); return false; } else if (stx is ControlFlow.Continue) { if (OnContinue != null) OnContinue(this); return false; } else stx.Run(this); } return true; }
通过返回false来立即中止上层Block的运行循环。
相关文章推荐
- 控制循环结构: break、continue和return
- java学习12--其他流程控制结构:break和continue
- c语言学习--循环结构中break、continue、goto、return和exit的区别(加精)
- 【Java】Java_16 控制循环结构Break、Continue、Return
- Java基础循环结构,以及循环控制语句break,continue,return
- C语言之循环结构中break、continue、goto、return和exit的区别
- C语言中流程控制语句(if,switch,for,while,do-while,continue,break,return)
- java的选择结构switch和循环结构for while do while以及流程控制语句break continue return
- 控制循环结构(break、continue)、return结束方法
- java循环控制中break、continue、return的比较
- 循环结构中break、continue、return和exit的区别
- C语言中的return,exit,break,continue
- break、continue和return的用法(附:2IO口控制4按键程序)
- c语言有4个跳转语句return、goto、break、continue
- 70个shell常用操作、 shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- 循环结构中break、continue、return和exit的区别
- 循环结构中break、continue、return和exi()t的区别
- Java中的跳转控制语句之break、continue、return
- java循环结构中break、continue、return的区别及使用情景
- 【转载】循环结构中break、continue、return和exit的区别