예제 #1
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (!IsStartOfNode(reader.LastKeyword, scope))
            {
                throw new SyntaxException(reader, "While declaration should start with while keyword.");
            }
            reader.ReadNext();
            var           conditionReader = new LoopedSourceReader(reader);
            LoopCondition cond            = () => Condition(conditionReader, scope, skipExec);

            yield return(cond);
        }
예제 #2
0
        private IEnumerable <object> Condition(LoopedSourceReader reader, IExecutionScope scope, bool skipExec)
        {
            var    exec      = DefaultLanguageNodes.Operation.Execute(reader, scope, skipExec);
            object lastValue = null;

            foreach (var o in exec)
            {
                lastValue = o;
                yield return(o);
            }
            reader.Reset();
            if (lastValue is bool == false)
            {
                throw new OperationException(reader, "While condition is not bool");
            }
            yield return((bool)lastValue);
        }
예제 #3
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (!IsStartOfNode(reader.LastKeyword, scope))
            {
                throw new SyntaxException(reader, "Loop section should start with loop keyword");
            }
            reader.ReadNext();
            var    loopScope       = scope.MakeSubScope();
            var    declarationexec = DefaultLanguageNodes.LoopDeclaration.Execute(reader, loopScope, skipExec);
            object lastValue       = null;

            foreach (var o in declarationexec)
            {
                lastValue = o;
                if (o is LoopCondition == false)
                {
                    yield return(o);
                }
            }
            if (lastValue is LoopCondition == false)
            {
                throw new OperationException(reader, "No condition generated for loop");
            }
            var condition    = lastValue as LoopCondition;
            var loopedReader = new LoopedSourceReader(reader);

            loopedReader.ForgetFirst();
            var first = true;

            while (true)
            {
                var condexec = condition();
                lastValue = null;
                foreach (var o in condexec)
                {
                    lastValue = o;
                    if (!skipExec)
                    {
                        yield return(o);
                    }
                }
                if (first)
                {
                    loopedReader.AddSnapshot();
                    first = false;
                }
                if (lastValue is bool == false)
                {
                    throw new OperationException(loopedReader, "Invalid returned value by condition");
                }
                var doLoop = (bool)lastValue;
                if (!doLoop)
                {
                    break;
                }
                var exec = DefaultLanguageNodes.Statement.Execute(loopedReader, loopScope, skipExec);
                foreach (var o in exec)
                {
                    if (!skipExec)
                    {
                        yield return(o);
                    }
                }
                loopedReader.Reset();
            }
        }