Пример #1
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            var varexec    = DefaultLanguageNodes.Variable.Execute(reader, scope, true);
            var identifier = varexec.ExecuteNext() as string;

            reader.ReadNext();
            if (!reader.LastKeyword.Equals(DefaultLanguageKeywords.EachKeyword))
            {
                throw new SyntaxException(reader,
                                          "In loop each declaration, the variable name should be folowed by the each keyword");
            }
            reader.ReadNext();
            var    sourceexec = DefaultLanguageNodes.Operation.Execute(reader, scope, skipExec);
            object lastValue  = null;

            foreach (var o in sourceexec)
            {
                lastValue = o;
                yield return(o);
            }
            if (lastValue is ICollection == false)
            {
                throw new OperationException(reader, "Each loop source is not a collection");
            }
            var           source = (lastValue as ICollection).GetEnumerator();
            LoopCondition cond   = () => Condition(identifier, source, scope);

            yield return(cond);
        }
Пример #2
0
 private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
 {
     if (reader.ReadingComplete)
     {
         throw new SyntaxException(reader, "Unexpected end of file");
     }
     if (!IsStartOfNode(reader.LastKeyword, scope))
     {
         throw new SyntaxException(reader, "Lists should start with the list begin symbol.");
     }
     reader.ReadNext();
     if (reader.ReadingComplete)
     {
         throw new SyntaxException(reader, "Unexpected end of file");
     }
     while (!reader.LastKeyword.Equals(DefaultLanguageKeywords.ListEndSymbol))
     {
         if (skipExec)
         {
             reader.ReadNext();
             continue;
         }
         var exec = DefaultLanguageNodes.Statement.Execute(reader, scope, skipExec);
         foreach (var o in exec)
         {
             yield return(o);
         }
         if (reader.ReadingComplete)
         {
             throw new SyntaxException(reader, "Unexpected end of file");
         }
     }
     reader.ReadNext();
 }
Пример #3
0
        private static Type ReadType(ISourceReader reader, IExecutionScope scope)
        {
            reader.ReadNext();
            if (reader.ReadingComplete)
            {
                throw new SyntaxException(reader, "Unexpected end of file");
            }
            if (!scope.Contains(reader.LastKeyword) || !scope.IsOfType <Type>(reader.LastKeyword))
            {
                throw new SyntaxException(reader, string.Format("Unknown type {0}", reader.LastKeyword));
            }
            var t = scope[reader.LastKeyword] as Type;

            if (t == null)
            {
                throw new SyntaxException(reader, "Type resolving failed.");
            }
            if (scope.IsGeneric(reader.LastKeyword))
            {
                reader.ReadNext();
                if (!reader.LastKeyword.Equals(DefaultLanguageKeywords.GenericTypeArgumentKeyword))
                {
                    throw new SyntaxException(reader,
                                              "In a Generic declaration, the Generic argument keyword should follow the Type name.");
                }
                Type innerType = ReadType(reader, scope);
                t = t.GetGenericTypeDefinition().MakeGenericType(innerType);
            }

            return(t);
        }
Пример #4
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (!IsStartOfNode(reader.LastKeyword, scope))
            {
                throw new SyntaxException(reader, "If section should start with if keyword");
            }
            reader.ReadNext();
            var    ifScope    = scope.MakeSubScope();
            var    condexec   = DefaultLanguageNodes.Operation.Execute(reader, ifScope, skipExec);
            object lastResult = null;

            foreach (var o in condexec)
            {
                lastResult = o;
                if (!skipExec)
                {
                    yield return(o);
                }
            }
            if (lastResult is bool == false)
            {
                throw new OperationException(reader, "if condition is not boolean value");
            }
            var cond     = (bool)lastResult;
            var thenexec = DefaultLanguageNodes.Statement.Execute(reader, ifScope, !cond);

            foreach (var o in thenexec)
            {
                if (cond)
                {
                    yield return(o);
                }
            }
            if (reader.ReadingComplete)
            {
                yield break;
            }
            if (!reader.LastKeyword.Equals(DefaultLanguageKeywords.ElseKeyword))
            {
                yield break;
            }
            reader.ReadNext();
            var elseexec = DefaultLanguageNodes.Statement.Execute(reader, ifScope, cond);

            foreach (var o in elseexec)
            {
                if (!cond)
                {
                    yield return(o);
                }
            }
        }
Пример #5
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (reader.ReadingComplete)
            {
                throw new SyntaxException(reader, "Unexpected end of file");
            }
            if (!reader.LastKeyword.Equals(DefaultLanguageKeywords.InputKeyword))
            {
                throw new SyntaxException(reader, "Input sections should start with the input keyword");
            }
            reader.ReadNext();
            if (reader.ReadingComplete)
            {
                throw new SyntaxException(reader, "Unexpected end of file");
            }
            IDictionary <string, Type> parametersMap = new Dictionary <string, Type>();
            var exec = DefaultLanguageNodes.InputStatement.Execute(reader, scope, skipExec);
            var pmap = exec.ExecuteNext() as IDictionary <string, Type>;

            foreach (var kvp in pmap)
            {
                parametersMap.Add(kvp);
            }
            yield return(parametersMap);
        }
Пример #6
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (reader.ReadingComplete)
            {
                throw new SyntaxException(reader, "Unexpected end of file");
            }
            var word = reader.LastKeyword;

            if (!IsStartOfNode(word, scope))
            {
                throw new SyntaxException(reader, "Variable name does not start with $");
            }
            var identifier = word;

            yield return(identifier);

            object value = null;

            if (!skipExec)
            {
                try
                {
                    value = scope[identifier];
                }
                catch (ScopeException se)
                {
                    throw new ScopeException(reader, se.Description);
                }
            }
            reader.ReadNext();
            yield return(value);
        }
Пример #7
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            object res  = null;
            var    word = reader.LastKeyword;

            foreach (var id in scope.KnownIdentifiers)
            {
                if (!id.StartsWith("cr:"))
                {
                    continue;
                }
                var cr = scope[id] as IConstantReader;
                if (cr.TryRead(word, out res))
                {
                    break;
                }
                res = null;
            }
            if (res == null)
            {
                throw new SyntaxException(reader, string.Format("Unrecognized word : {0}", word));
            }
            reader.ReadNext();
            yield return(res);
        }
Пример #8
0
 void TestReader(ISourceReader reader, string[] expected)
 {
     foreach (string str in expected)
     {
         Assert.IsFalse(reader.ReadingComplete);
         Assert.IsNotNull(reader.LastKeyword);
         Assert.AreEqual(str, reader.LastKeyword);
         reader.ReadNext();
     }
 }
Пример #9
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);
        }
Пример #10
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (reader.ReadingComplete)
            {
                throw new SyntaxException(reader, "Unexpected end of file");
            }
            if (!IsStartOfNode(reader.LastKeyword, scope))
            {
                throw new SyntaxException(reader, "Lists should start with the list begin symbol.");
            }
            reader.ReadNext();
            if (reader.ReadingComplete)
            {
                throw new SyntaxException(reader, "Unexpected end of file");
            }

            IDictionary <string, Type> parametersMap = new Dictionary <string, Type>();

            while (!reader.LastKeyword.Equals(DefaultLanguageKeywords.ListEndSymbol))
            {
                var declaration = new InputDeclaration();
                var exec        = DefaultLanguageNodes.Declaration.Execute(reader, scope, skipExec);
                var o           = exec.ExecuteNext();
                var pmap        = o as IDictionary <string, Type>;
                if (reader.ReadingComplete)
                {
                    throw new SyntaxException(reader, "Unexpected end of file");
                }
                if (pmap == null)
                {
                    throw new SyntaxException(reader, "No parameters");
                }
                foreach (var kvp in pmap)
                {
                    parametersMap.Add(kvp);
                }
            }
            reader.ReadNext();
            yield return(parametersMap);
        }
Пример #11
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope)
        {
            var exec = DefaultLanguageNodes.Variable.Execute(reader, scope, true);
            IDictionary <string, Type> parametersMap = new Dictionary <string, Type>();
            var id = exec.ExecuteNext() as string;

            reader.ReadNext();
            if (reader.ReadingComplete)
            {
                throw new SyntaxException(reader, "Unexpected end of file");
            }
            if (!reader.LastKeyword.Equals(DefaultLanguageKeywords.TypingKeyword))
            {
                throw new SyntaxException(reader,
                                          "In a declaration, the Typing keyword should follow the variable name.");
            }
            Type t = ReadType(reader, scope);

            parametersMap.Add(id, t);
            reader.ReadNext();
            yield return(parametersMap);
        }
Пример #12
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (!IsStartOfNode(reader.LastKeyword, scope))
            {
                throw new SyntaxException(reader, "Assignations should start with the assignation keyword");
            }
            reader.ReadNext();
            if (reader.ReadingComplete)
            {
                throw new SyntaxException(reader, "Unexpected end of file");
            }
            var varexec    = DefaultLanguageNodes.Variable.Execute(reader, scope, skipExec);
            var identifier = varexec.ExecuteNext() as string;

            if (string.IsNullOrEmpty(identifier))
            {
                throw new OperationException(reader, "Identifier of assignation is null");
            }
            reader.ReadNext();
            var    opexec    = DefaultLanguageNodes.Operation.Execute(reader, scope, skipExec);
            object lastValue = null;

            foreach (var o in opexec)
            {
                lastValue = o;
                if (!skipExec)
                {
                    yield return(o);
                }
            }
            if (!skipExec)
            {
                scope[identifier] = lastValue;
                yield return(null);
            }
        }
Пример #13
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (reader.ReadingComplete)
            {
                throw new SyntaxException(reader, "Unexpected end of file");
            }
            var word   = reader.LastKeyword;
            var method = scope[word] as MethodInfo;

            if (method == null)
            {
                throw new SyntaxException(reader, string.Format("{0} is not a call", word));
            }
            reader.ReadNext();
            var parameters = method.GetParameters();
            var args       = new object[parameters.Length];
            var i          = 0;

            foreach (var param in parameters)
            {
                if (reader.ReadingComplete)
                {
                    throw new SyntaxException(reader, "Unexpected end of file");
                }
                var    exec      = DefaultLanguageNodes.Operation.Execute(reader, scope, skipExec);
                object lastValue = null;
                foreach (var o in exec)
                {
                    lastValue = o;
                    if (!skipExec)
                    {
                        yield return(o);
                    }
                }
                if (!skipExec && !param.ParameterType.IsAssignableFrom(lastValue.GetType()))
                {
                    throw new OperationException(reader,
                                                 string.Format("Wrong parameter for argument {0} of call {1}", param.Name, method.Name));
                }
                args[i++] = lastValue;
            }
            if (!skipExec)
            {
                yield return(method.Invoke(null, args));
            }
        }
Пример #14
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();
            }
        }