C 언어 해석기 - 10 제어 구조의 순환
5113 단어 C#C 언어해석기형식 언어 및 로봇
기본 클래스는 다음과 같습니다.
public class Loop : ControlFlow
{
public Expression.ExpressionNode Condition;
public virtual Block Body
{
get { return Children.Count > 0 ? Children.Last() as Block : null; }
}
public bool NeedBreak = false;
public bool NeedContinue = false;
while, do..while。 , while 。 while :
public override void Run(Context ctx)
{
Debug.WriteLine("Begin While.");
base.Run(ctx);
while (true)
{
if (Condition != null)
{
Expression.Operand.Value condVal = Condition.Evaluate(this).GetValue(this);
Debug.WriteLine(string.Format("Condition: [{0}] = {1}", Condition.ToString(), condVal.ToString()));
if (condVal.AsInt == 0)
{
break;
}
}
Body.Run(this);
if (NeedBreak)
break;
} // while
Debug.WriteLine("End While.");
}
do...while와while의 다른 부분은 순환체를 먼저 운행하고 판단을 운행하는 데 있다.
public override void Run(Context ctx)
{
Debug.WriteLine("Begin Do...While.");
base.Run(ctx);
while(true)
{
Body.Run(this);
if (NeedBreak)
break;
if (Condition != null)
{
Expression.Operand.Value val = Condition.Evaluate(this).GetValue(this);
Debug.WriteLine(string.Format("Condition : {0} = {1}", Condition.ToString(), val.ToString()));
if (val.AsInt == 0)
{
break;
}
}
} // while
Debug.WriteLine("End Do...While.");
}
복잡한 것은 for 순환이다. 다른 순환 구조보다 초기화 부분과 교체 부분이 많다.
public class ForLoop : Loop
{
public Context Initializer
{
get { return Children.Count > 0 ? Children.First() : null; }
}
public Expression.ExpressionNode Iterator;
초기화 부분에서 순환 변수를 정의할 수 있기 때문에, 예를 들어 for (int i = 0;...) 이다.따라서 다음 두 가지 방법을 다시 불러와야 한다.
public override bool HasDefined(string str)
{
if (FindByName(str) == null)
return base.HasDefined(str);
else
return true;
}
public override Context FindByName(string str)
{
Context res = null;
if (Initializer != null)
{
Initializer.Parent = null;
res = Initializer.FindByName(str);
Initializer.Parent = this;
}
if (res == null)
return this.Parent.FindByName(str);
else
return res;
}
그리고 for 순환의 실행 코드를 확인할 수 있습니다.
public override void Run(Context ctx)
{
Debug.WriteLine("Begin For.");
if (Initializer != null)
Initializer.Run(this);
base.Run(ctx);
while (true)
{
if (Condition != null)
{
Expression.Operand.Value val = Condition.Evaluate(this).GetValue(this);
Debug.WriteLine(string.Format("Condition: [{0}] = {1}", Condition.ToString(), val.ToString()));
if (val.AsInt == 0)
{
break;
}
}
Body.Run(this);
if (NeedBreak)
break;
if (Iterator != null)
Iterator.Evaluate(this);
} // while
if (Initializer != null)
{
Initializer.FreeLocalVariables();
}
Debug.WriteLine("End For.");
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.