Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     <see cref="ISyntaxTreeItem.Execute(ISourceReader, IExecutionScope, bool)" />
        /// </summary>
        public IScriptExecution Execute(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (reader == null)
            {
                return(null);
            }
            var word = reader.LastKeyword;

            if (DefaultLanguageNodes.Assignation.IsStartOfNode(word, scope))
            {
                return(DefaultLanguageNodes.Assignation.Execute(reader, scope, skipExec));
            }

            if (DefaultLanguageNodes.Call.IsStartOfNode(word, scope))
            {
                return(DefaultLanguageNodes.Call.Execute(reader, scope, skipExec));
            }

            if (DefaultLanguageNodes.Variable.IsStartOfNode(word, scope))
            {
                return(DefaultLanguageNodes.Variable.Execute(reader, scope, skipExec));
            }

            if (DefaultLanguageNodes.Constant.IsStartOfNode(word, scope))
            {
                return(DefaultLanguageNodes.Constant.Execute(reader, scope, skipExec));
            }

            throw new SyntaxException(reader, "Not recognized as operation");
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
 void TestScope(IExecutionScope scope, string intName, string wrongName, string delegateName)
 {
     TestAccess(scope, intName, wrongName, delegateName);
     // At this point, scope contains intName and delegateName
     TestContains(scope, intName, wrongName, delegateName);
     TestIsOfType(scope, intName, wrongName, delegateName);
 }
Exemplo n.º 6
0
        void TestAccess(IExecutionScope scope, string intName, string wrongName, string delegateName)
        {
            scope[intName] = 1;
            Assert.AreEqual(1, scope[intName]);
            try
            {
                object b = scope[wrongName];
                Assert.Fail("Accessing a wrong name should throw an error");
            }
            catch (ScopeException se)
            {
                se.RemoveUnusedWarning();
            }
            catch
            {
                Assert.Fail("Unknown error thrown when accessing wrong name");
            }
            Func <object> test = () => new object();

            scope[delegateName] = test;
            try
            {
                object o = ((Func <object>)scope[delegateName])();
                if (o == null)
                {
                    Assert.Fail("stored delegate returned null");
                }
            }
            catch
            {
                Assert.Fail("Stored delegate not of right type");
            }
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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();
 }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
 /// <summary>
 ///     <see cref="ISyntaxTreeItem.IsStartOfNode(string, IExecutionScope)" />
 /// </summary>
 public bool IsStartOfNode(string word, IExecutionScope scope)
 {
     return(DefaultLanguageNodes.Call.IsStartOfNode(word, scope) ||
            DefaultLanguageNodes.Assignation.IsStartOfNode(word, scope) ||
            DefaultLanguageNodes.Variable.IsStartOfNode(word, scope) ||
            DefaultLanguageNodes.Constant.IsStartOfNode(word, scope));
 }
Exemplo n.º 11
0
 /// <summary>
 ///     <see cref="ISyntaxTreeItem.IsStartOfNode(string, IExecutionScope)" />
 /// </summary>
 public bool IsStartOfNode(string word, IExecutionScope scope)
 {
     if (scope == null)
     {
         return(false);
     }
     return(scope.Contains(word) && scope.IsOfType <MethodInfo>(word));
 }
Exemplo n.º 12
0
 void TestIsOfType(IExecutionScope scope, string intName, string wrongName, string delegateName)
 {
     Assert.IsTrue(scope.IsOfType <int>(intName));
     Assert.IsFalse(scope.IsOfType <string>(intName));
     Assert.IsFalse(scope.IsOfType <object>(wrongName));
     Assert.IsTrue(scope.IsOfType <Func <object> >(delegateName));
     Assert.IsFalse(scope.IsOfType <string>(delegateName));
 }
Exemplo n.º 13
0
        void TestSubScope(IExecutionScope scope, string intName, string wrongName, string delegateName)
        {
            IExecutionScope subscope = scope.MakeSubScope();

            TestScope(subscope, "sub" + intName, "sub" + wrongName, "sub" + delegateName);
            TestContains(subscope, intName, wrongName, delegateName);
            TestIsOfType(subscope, intName, wrongName, delegateName);
        }
Exemplo n.º 14
0
 /// <summary>
 ///     <see cref="ISyntaxTreeItem.IsStartOfNode(string, IExecutionScope)" />
 /// </summary>
 public bool IsStartOfNode(string word, IExecutionScope scope)
 {
     if (string.IsNullOrEmpty(word))
     {
         return(false);
     }
     return(word.StartsWith(DefaultLanguageKeywords.VariableFirstSymbol));
 }
 protected bool ExtractState(IExecutionScope scope)
 {
     if (!(scope is IExecutionScope <bool> scopeWithBoolState))
     {
         throw new Exception($"{scope} does not have bool state.");
     }
     return(scopeWithBoolState.State);
 }
Exemplo n.º 16
0
 /// <summary>
 ///     <see cref="ISyntaxTreeItem.IsStartOfNode(string, IExecutionScope)" />
 /// </summary>
 public bool IsStartOfNode(string word, IExecutionScope scope)
 {
     if (string.IsNullOrEmpty(word))
     {
         return(false);
     }
     return(word.Equals(DefaultLanguageKeywords.WhileKeyword));
 }
Exemplo n.º 17
0
        /// <summary>
        ///     Constructor for Script.
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="defaultScope"></param>
        public Script(ISourceReader reader, IExecutionScope defaultScope)
        {
            m_Reader = new LoopedSourceReader(reader);
            m_Scope  = defaultScope;
            var exec = DefaultLanguageNodes.ScriptRoot.Execute(m_Reader, m_Scope.MakeSubScope(), false);

            ExpectedArguments = exec.ExecuteNext() as IDictionary <string, Type>;
            m_Reader.Reset();
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
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);
                }
            }
        }
Exemplo n.º 20
0
        private static void ExploreAssembly(Assembly assembly, ref IExecutionScope scope)
        {
            var types = assembly.GetTypes();

            foreach (var type in types)
            {
                if (!type.IsPublic)
                {
                    continue;
                }
                ExploreType(type, ref scope);
            }
        }
Exemplo n.º 21
0
        public void ScopeFactoryTypesTest()
        {
            IScopeFactory   factory = new DefaultImplementations.ScopeFactory();
            IExecutionScope scope   = factory.MakeScope();

            Assert.IsTrue(scope.Contains("FirstType"));
            Assert.IsTrue(scope.IsOfType <Type>("FirstType"));
            Type t = scope["FirstType"] as Type;

            Assert.IsNotNull(t);
            Assert.AreEqual(typeof(FirstType), t);

            Assert.IsFalse(scope.Contains("SecondType"));
            Assert.IsTrue(scope.Contains("Another"));
            Assert.IsTrue(scope.IsOfType <Type>("Another"));
            t = scope["Another"] as Type;
            Assert.IsNotNull(t);
            Assert.AreEqual(typeof(SecondType), t);

            Assert.IsFalse(scope.Contains("ThirdType"));
            Assert.IsTrue(scope.Contains("Int32"));
            Assert.IsTrue(scope.IsOfType <Type>("Int32"));
            t = scope["Int32"] as Type;
            Assert.IsNotNull(t);
            Assert.AreEqual(typeof(int), t);

            Assert.IsFalse(scope.Contains("FouthType"));
            Assert.IsTrue(scope.Contains("BoolType"));
            Assert.IsTrue(scope.IsOfType <Type>("BoolType"));
            t = scope["BoolType"] as Type;
            Assert.IsNotNull(t);
            Assert.AreEqual(typeof(bool), t);

            Assert.IsFalse(scope.Contains("StringLink"));
            Assert.IsTrue(scope.Contains("String"));
            Assert.IsTrue(scope.IsOfType <Type>("String"));
            t = scope["String"] as Type;
            Assert.IsNotNull(t);
            Assert.AreEqual(typeof(string), t);
            Assert.IsTrue(scope.Contains("cr:String"));
            IConstantReader cr = scope["cr:String"] as IConstantReader;

            Assert.IsNotNull(cr);
            object res;

            Assert.IsTrue(cr.TryRead("\"test\"", out res));
            Assert.AreEqual("test", res);
        }
Exemplo n.º 22
0
        private static void TryExposedType(Type type, ref IExecutionScope scope)
        {
            var candidate = Attribute.GetCustomAttribute(type, typeof(ExposedTypeAttribute));
            var exposed   = candidate as ExposedTypeAttribute;

            if (exposed == null)
            {
                return;
            }
            if (exposed.LinkToType != null)
            {
                AddType(exposed.LinkToType, exposed, ref scope);
                return;
            }
            AddType(type, exposed, ref scope);
        }
Exemplo n.º 23
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));
            }
        }
Exemplo n.º 24
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);
        }
Exemplo n.º 25
0
        private IEnumerator <object> Exec(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            IDictionary <string, Type> pmap = new Dictionary <string, Type>();

            if (DefaultLanguageNodes.InputSection.IsStartOfNode(reader.LastKeyword, scope))
            {
                var    inputexec = DefaultLanguageNodes.InputSection.Execute(reader, scope, skipExec);
                object lastValue = null;
                foreach (var o in inputexec)
                {
                    lastValue = o;
                }
                if (lastValue is IDictionary <string, Type> == false)
                {
                    throw new OperationException(reader, "Input section does not yield parameters map.");
                }
                pmap = lastValue as IDictionary <string, Type>;
            }
            yield return(pmap);

            foreach (var kvp in pmap)
            {
                if (!scope.Contains(kvp.Key))
                {
                    throw new OperationException(reader,
                                                 string.Format("Execution scope does not contain input parameter {0}", kvp.Key));
                }
                if (!kvp.Value.IsAssignableFrom(scope[kvp.Key].GetType()))
                {
                    throw new OperationException(reader,
                                                 string.Format("Provided input parameter {0} wrong type. Expected {1}got {2}", kvp.Key,
                                                               kvp.Value, scope[kvp.Key].GetType()));
                }
            }
            var scriptScope = scope.MakeSubScope();

            while (!reader.ReadingComplete)
            {
                var exec = DefaultLanguageNodes.Statement.Execute(reader, scriptScope, skipExec);
                foreach (var o in exec)
                {
                    yield return(o);
                }
            }
        }
Exemplo n.º 26
0
        private static void ExploreType(Type type, ref IExecutionScope scope)
        {
            TryExposedType(type, ref scope);
            var methods = type.GetMethods();

            foreach (var method in methods)
            {
                if (!method.IsPublic)
                {
                    continue;
                }
                if (!method.IsStatic)
                {
                    continue;
                }
                TryExposedCall(method, ref scope);
            }
        }
Exemplo n.º 27
0
        /// <summary>
        ///     <see cref="ISyntaxTreeItem.Execute(ISourceReader, IExecutionScope, bool)" />
        /// </summary>
        public IScriptExecution Execute(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (reader == null)
            {
                return(null);
            }
            if (DefaultLanguageNodes.If.IsStartOfNode(reader.LastKeyword, scope))
            {
                return(DefaultLanguageNodes.If.Execute(reader, scope, skipExec));
            }

            if (DefaultLanguageNodes.Loop.IsStartOfNode(reader.LastKeyword, scope))
            {
                return(DefaultLanguageNodes.Loop.Execute(reader, scope, skipExec));
            }

            throw new SyntaxException(reader, "Not recognized as section");
        }
Exemplo n.º 28
0
        public void ScopeFactoryCallsTest()
        {
            IScopeFactory   factory = new DefaultImplementations.ScopeFactory();
            IExecutionScope scope   = factory.MakeScope();

            Assert.IsTrue(scope.Contains("firstMethod"));
            Assert.IsTrue(scope.IsOfType <MethodInfo>("firstMethod"));
            var method = scope["firstMethod"] as MethodInfo;

            Assert.IsNotNull(method);
            Assert.AreEqual("OK", method.Invoke(null, new object[0]));
            Assert.IsFalse(scope.Contains("secondMethod"));
            Assert.IsTrue(scope.Contains("second"));
            Assert.IsTrue(scope.IsOfType <MethodInfo>("second"));
            method = scope["second"] as MethodInfo;
            Assert.IsNotNull(method);
            Assert.AreEqual("OKI", method.Invoke(null, new object[0]));
        }
Exemplo n.º 29
0
        /// <summary>
        ///     <see cref="ISyntaxTreeItem.Execute(ISourceReader, IExecutionScope,bool)" />
        /// </summary>
        public IScriptExecution Execute(ISourceReader reader, IExecutionScope scope, bool skipExec)
        {
            if (reader == null)
            {
                return(null);
            }
            if (DefaultLanguageNodes.Declaration.IsStartOfNode(reader.LastKeyword, scope))
            {
                return(DefaultLanguageNodes.Declaration.Execute(reader, scope, skipExec));
            }

            if (DefaultLanguageNodes.ListOfDeclarations.IsStartOfNode(reader.LastKeyword, scope))
            {
                return(DefaultLanguageNodes.ListOfDeclarations.Execute(reader, scope, skipExec));
            }

            throw new SyntaxException(reader, "Not recognized as input statement");
        }
Exemplo n.º 30
0
        public void TestAssignation()
        {
            IExecutionScope assignScope = m_Scope.MakeSubScope();

            assignScope["$x"] = 1;

            AssignSubTest("set $x 2", assignScope, "$x", 2, "$y");
            AssignSubTest("set $y 1", assignScope, "$y", 1, "$z");
            try
            {
                AssignSubTest("set 2 $y", assignScope, "$x", 2, "$z");
                Assert.Fail("set 2 $y should throw syntax exception");
            }
            catch (SyntaxException se)
            {
                se.RemoveUnusedWarning();
            }
        }