Exemplo n.º 1
0
        public void OperatorAttribute_DefaultValuesAreSet()
        {
            var attribute = new OperatorAttribute();

            Assert.Equal(0, attribute.Precedence);
            Assert.Equal(Associativity.Left, attribute.Associativity);
        }
Exemplo n.º 2
0
        private void LookForOperatorsIn(Assembly asm)
        {
            foreach (Type t in asm.GetTypes())
            {
                if (t.IsDefined(typeof(OperatorAttribute)))
                {
                    try
                    {
                        OperatorAttribute attr = t.GetCustomAttribute <OperatorAttribute>();

                        var instance = Activator.CreateInstance(t);

                        var infoMethods = t.GetMethods().
                                          Where(y => y.GetCustomAttributes().OfType <ActionAttribute>().Any() && y.ReturnType == typeof(ActionResponse)).
                                          ToDictionary(x => x.GetCustomAttribute <ActionAttribute>().Name.ToLower(), x => x);

                        executorDict.Add(attr.Name.ToLower(), new Executor(instance, infoMethods));
                    }
                    catch (Exception e)
                    {
                        log.Error($"Exceptio while initializing the Interpreter Instance{e.Message}");
                    }
                }
            }
        }
Exemplo n.º 3
0
 private void Visit(BinaryExpression expr)
 {
     result.Append("(");
     Visit(expr.Left);
     result.Append(" " + OperatorAttribute.GetOperator(expr) + " ");
     Visit(expr.Right);
     result.Append(")");
 }
Exemplo n.º 4
0
        public void OnImportsSatisfied()
        {
            // This method is called when all Imports have been resolved.
            // Right now, we've a collection of resolved instances; using these instances,
            // we'll populate our dictionary which contains the types that correspond with each symbol.

            _expressions.Clear();

            foreach (var e in _expressionImports)
            {
                var symbol = OperatorAttribute.GetOperator(e);

                if (symbol != String.Empty)
                {
                    if (_expressions.ContainsKey(symbol) == false)
                    {
                        _expressions.Add(symbol, e.GetType());
                    }
                }
            }
        }
        private static void LoadOperators()
        {
            System.Type nodeType = typeof(Node);
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(nodeType);
            System.Type[] allTypes = assembly.GetTypes();

            for (int i = 0; i < allTypes.Length; i++)
            {
                System.Type type = allTypes[i];
                if (type.IsSubclassOf(nodeType) && !type.IsAbstract)
                {
                    OperatorAttribute[] attributes = (OperatorAttribute[])type.GetCustomAttributes(typeof(OperatorAttribute), true);
                    if (attributes != null)
                    {
                        for (int attrIndex = 0; attrIndex < attributes.Length; attrIndex++)
                        {
                            OperatorAttribute operatorAttribute = attributes[attrIndex];
                            Interpreter.operators.Add(operatorAttribute.Symbol, new OperatorDescriptor(operatorAttribute.Priority, type));
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
 private void Visit(UnaryExpression expr)
 {
     Visit(expr.Operand);
     result.Append(OperatorAttribute.GetOperator(expr));
 }