C 언어 해석기 - 10 제어 구조의 순환

순환은 유사한 구조를 가지고 있다. 조건 판단, 순환체다.
기본 클래스는 다음과 같습니다.
 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.");
        }

좋은 웹페이지 즐겨찾기