Ejemplo n.º 1
0
        /// <summary>
        /// Provides the value associated to this Expression
        /// </summary>
        /// <param name="context">The context on which the value must be found</param>
        /// <returns></returns>
        public override Values.IValue GetValue(InterpretationContext context)
        {
            Values.ListValue retVal = null;

            Values.ListValue value = ListExpression.GetValue(context) as Values.ListValue;
            if (value != null)
            {
                int token = PrepareIteration(context);
                retVal = new Values.ListValue((Types.Collection)GetExpressionType(), new List <Values.IValue>());
                foreach (Values.IValue v in value.Val)
                {
                    if (v != EFSSystem.EmptyValue)
                    {
                        IteratorVariable.Value = v;

                        if (conditionSatisfied(context))
                        {
                            retVal.Val.Add(IteratorExpression.GetValue(context));
                        }
                    }
                    NextIteration();
                }
                EndIteration(context, token);
            }

            return(retVal);
        }
Ejemplo n.º 2
0
        public IExpression Parse(Parser parser, Token token)
        {
            var iterator = parser.ParseExpression(1);

            IteratorExpression iteratorExpr = null;
            string             listName     = null;

            if (iterator is IteratorExpression)
            {
                iteratorExpr = (IteratorExpression)iterator;
            }

            else if (iterator is IdentifierExpression)
            {
                listName = ((IdentifierExpression)iterator).Value;
            }

            else
            {
                ErrorManager.CreateError("You can only iterate over a list, str literal or iterator expression!", ErrorType.Error, token.Line, token.Column);
                return(null);
            }

            var block = parser.ParseExpression(1);

            if (!(block is BlockExpression))
            {
                ErrorManager.CreateError("For expression needs a block!", ErrorType.Error, token.Line, token.Column);
                return(null);
            }

            return(new ForExpression(token.Line, token.Column, iteratorExpr, listName, (BlockExpression)block));
        }
        private System.Linq.Expressions.Expression GetEnumerableIterator(
            System.Linq.Expressions.Expression contextParameter, IteratorExpression iex)
        {
            var fb = new FunctionBuilder(CompilationContext.Configuration);

            return(System.Linq.Expressions.Expression.Block(
                       System.Linq.Expressions.Expression.Assign(contextParameter,
                                                                 System.Linq.Expressions.Expression.New(
                                                                     typeof(IteratorBindingContext).GetConstructor(new[] { typeof(BindingContext) }),
                                                                     new System.Linq.Expressions.Expression[] { CompilationContext.BindingContext })),
                       System.Linq.Expressions.Expression.Call(
#if netstandard
                           new Action <IteratorBindingContext, IEnumerable, Action <TextWriter, object>, Action <TextWriter, object> >(Iterate).GetMethodInfo(),
#else
                           new Action <IteratorBindingContext, IEnumerable, Action <TextWriter, object>,
                                       Action <TextWriter, object> >(Iterate).Method,
#endif
                           new[]
            {
                System.Linq.Expressions.Expression.Convert(contextParameter, typeof(IteratorBindingContext)),
                System.Linq.Expressions.Expression.Convert(iex.Sequence, typeof(IEnumerable)),
                fb.Compile(new[] { iex.Template }, contextParameter),
                fb.Compile(new[] { iex.IfEmpty }, CompilationContext.BindingContext)
            })));
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Provides the value associated to this Expression
        /// </summary>
        /// <param name="context">The context on which the value must be found</param>
        /// <param name="explain">The explanation to fill, if any</param>
        /// <returns></returns>
        protected internal override IValue GetValue(InterpretationContext context, ExplanationPart explain)
        {
            ListValue retVal = null;

            ListValue value = ListExpression.GetValue(context, explain) as ListValue;

            if (value != null)
            {
                int token = PrepareIteration(context);
                retVal = new ListValue((Collection)GetExpressionType(), new List <IValue>());
                List <IValue> range = new List <IValue>(value.Val);
                foreach (IValue v in range)
                {
                    if (v != EfsSystem.Instance.EmptyValue)
                    {
                        // All elements should always be != from EmptyValue
                        ElementFound           = true;
                        IteratorVariable.Value = v;

                        if (ConditionSatisfied(context, explain))
                        {
                            MatchingElementFound = true;
                            retVal.Val.Add(IteratorExpression.GetValue(context, explain));
                        }
                    }
                    NextIteration();
                }
                EndIteration(context, explain, token);
            }

            return(retVal);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Provides the value associated to this Expression
        /// </summary>
        /// <param name="context">The context on which the value must be found</param>
        /// <returns></returns>
        public override Values.IValue GetValue(InterpretationContext context)
        {
            Values.IValue retVal = null;

            Values.ListValue value = ListExpression.GetValue(context) as Values.ListValue;
            if (value != null)
            {
                int token = PrepareIteration(context);
                context.LocalScope.setVariable(AccumulatorVariable);
                AccumulatorVariable.Value = InitialValue.GetValue(context);

                foreach (Values.IValue v in value.Val)
                {
                    if (v != EFSSystem.EmptyValue)
                    {
                        IteratorVariable.Value = v;
                        if (conditionSatisfied(context))
                        {
                            AccumulatorVariable.Value = IteratorExpression.GetValue(context);
                        }
                    }
                    NextIteration();
                }
                EndIteration(context, token);
                retVal = AccumulatorVariable.Value;
            }
            else
            {
                AddError("Cannot evaluate list value " + ListExpression.ToString());
            }

            return(retVal);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Provides the type of this expression
        /// </summary>
        /// <returns></returns>
        public override Type GetExpressionType()
        {
            Type retVal = null;

            Type iteratorType = IteratorExpression.GetExpressionType();

            if (iteratorType != null)
            {
                Collection collection = (Collection)acceptor.getFactory().createCollection();
                collection.Enclosing = EfsSystem.Instance;
                collection.Type      = iteratorType;
                Collection originalListType = ListExpression.GetExpressionType() as Collection;
                if (originalListType != null)
                {
                    collection.setMaxSize(originalListType.getMaxSize());
                }

                retVal = collection;
            }
            else
            {
                AddError("Cannot evaluate iterator type for " + ToString(), RuleChecksEnum.SemanticAnalysisError);
            }

            return(retVal);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Checks the expression and appends errors to the root tree node when inconsistencies are found
        /// </summary>
        public override void CheckExpression()
        {
            base.CheckExpression();

            Type initialValueType = InitialValue.GetExpressionType();

            if (initialValueType != null)
            {
                Collection listExpressionType = ListExpression.GetExpressionType() as Collection;
                if (listExpressionType != null)
                {
                    IteratorExpression.CheckExpression();
                }
            }
            else
            {
                AddError("Cannot determine initial value expression type for " + ToString(), RuleChecksEnum.SemanticAnalysisError);
            }


            bool refToResultFound = false;

            foreach (Usage usage in IteratorExpression.StaticUsage.AllUsages)
            {
                if (usage.Referenced == AccumulatorVariable && usage.Mode == Usage.ModeEnum.Read)
                {
                    refToResultFound = true;
                    break;
                }
            }
            if (!refToResultFound)
            {
                AddError("REDUCE expressions should reference RESULT variable", RuleChecksEnum.SyntaxError);
            }
        }
Ejemplo n.º 8
0
        protected override Expression VisitIteratorExpression(IteratorExpression iex)
        {
            var iteratorBindingContext = Expression.Variable(typeof(BindingContext), "context");

            return(Expression.Block(
                       new[]
            {
                iteratorBindingContext
            },
                       Expression.IfThenElse(
                           Expression.TypeIs(iex.Sequence, typeof(IEnumerable)),
                           Expression.IfThenElse(
#if netstandard
                               Expression.Call(new Func <object, bool>(IsNonListDynamic).GetMethodInfo(), new[] { iex.Sequence }),
#else
                               Expression.Call(new Func <object, bool>(IsNonListDynamic).Method, new[] { iex.Sequence }),
#endif
                               GetDynamicIterator(iteratorBindingContext, iex),
                               Expression.IfThenElse(
#if netstandard
                                   Expression.Call(new Func <object, bool>(IsGenericDictionary).GetMethodInfo(), new[] { iex.Sequence }),
#else
                                   Expression.Call(new Func <object, bool>(IsGenericDictionary).Method, new[] { iex.Sequence }),
#endif
                                   GetDictionaryIterator(iteratorBindingContext, iex),
                                   GetEnumerableIterator(iteratorBindingContext, iex))),
                           GetObjectIterator(iteratorBindingContext, iex))
                       ));
        }
Ejemplo n.º 9
0
        public override void Switch(IAstTransformer transformer, out Node resultingNode)
        {
            IteratorExpression thisNode           = (IteratorExpression)this;
            Expression         resultingTypedNode = thisNode;

            transformer.OnIteratorExpression(thisNode, ref resultingTypedNode);
            resultingNode = resultingTypedNode;
        }
        /// <summary>
        /// Checks the expression and appends errors to the root tree node when inconsistencies are found
        /// </summary>
        public override void checkExpression()
        {
            base.checkExpression();

            Types.Collection listExpressionType = ListExpression.GetExpressionType() as Types.Collection;
            if (listExpressionType != null)
            {
                IteratorExpression.checkExpression();
            }
        }
Ejemplo n.º 11
0
 public override void WriteTo(TextWriter w)
 {
     w.Write("for(");
     IteratorVariable.Declaration.WriteTo(w);
     Control.WriteTo(w);
     w.Write(";");
     IteratorExpression.WriteTo(w);
     w.Write(")");
     Body.WriteTo(w);
 }
Ejemplo n.º 12
0
 public override void OnIteratorExpression(IteratorExpression node)
 {
     Write("(");
     Switch(node.Expression);
     Write(" for ");
     WriteCommaSeparatedList(node.Declarations);
     Write(" in ");
     Switch(node.Iterator);
     Switch(node.Filter);
     Write(")");
 }
Ejemplo n.º 13
0
        /// <summary>
        ///     Provides the type of this expression
        /// </summary>
        /// <returns></returns>
        public override Type GetExpressionType()
        {
            Type retVal = null;

            if (IteratorExpression != null)
            {
                retVal = IteratorExpression.GetExpressionType();
            }

            return(retVal);
        }
Ejemplo n.º 14
0
        public void ShouldCreateSimpleIterator()
        {
            IteratorExpression enumerable = X.Enumerable(
                X.Yield("hello".AsExpression()),
                X.Yield("world".AsExpression())
                );

            enumerable
            .Compile <string>()
            .SequenceEqual(new[] { "hello", "world" })
            .ShouldBeTrue();
        }
        /// <summary>
        /// Provides the expression text
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string retVal = OPERATOR + " " + ListExpression.ToString();

            if (Condition != null)
            {
                retVal += " | " + Condition.ToString();
            }

            retVal = retVal + " USING " + IteratorExpression.ToString();

            return(retVal);
        }
        /// <summary>
        /// Performs the semantic analysis of the expression
        /// </summary>
        /// <param name="instance">the reference instance on which this element should analysed</param>
        /// <paraparam name="expectation">Indicates the kind of element we are looking for</paraparam>
        /// <returns>True if semantic analysis should be continued</returns>
        public override bool SemanticAnalysis(Utils.INamable instance, Filter.AcceptableChoice expectation)
        {
            bool retVal = base.SemanticAnalysis(instance, expectation);

            if (retVal)
            {
                AccumulatorVariable.Type = IteratorExpression.GetExpressionType();

                Accumulator.SemanticAnalysis(instance, Filter.AllMatches);
            }

            return(retVal);
        }
Ejemplo n.º 17
0
        /// <summary>
        ///     Provides the surface of this function if it has been statically defined
        /// </summary>
        /// <param name="context">the context used to create the surface</param>
        /// <param name="xParam">The X axis of this surface</param>
        /// <param name="yParam">The Y axis of this surface</param>
        /// <param name="explain"></param>
        /// <returns>The surface which corresponds to this expression</returns>
        public override Surface CreateSurface(InterpretationContext context, Parameter xParam, Parameter yParam,
                                              ExplanationPart explain)
        {
            Surface retVal = base.CreateSurface(context, xParam, yParam, explain);

            Surface surface = InitialValue.CreateSurface(context, xParam, yParam, explain);

            if (surface != null)
            {
                ListValue value = ListExpression.GetValue(context, explain) as ListValue;
                if (value != null)
                {
                    int token = PrepareIteration(context);
                    AccumulatorVariable.Value = surface.Function;

                    foreach (IValue v in value.Val)
                    {
                        if (v != EfsSystem.Instance.EmptyValue)
                        {
                            // All elements should always be != from EmptyValue
                            ElementFound           = true;
                            IteratorVariable.Value = v;
                            if (ConditionSatisfied(context, explain))
                            {
                                MatchingElementFound      = true;
                                AccumulatorVariable.Value = IteratorExpression.GetValue(context, explain);
                            }
                        }
                        NextIteration();
                    }
                    Function function = AccumulatorVariable.Value as Function;
                    if (function != null)
                    {
                        retVal = function.Surface;
                    }
                    else
                    {
                        throw new Exception("Expression does not reduces to a function");
                    }
                    EndIteration(context, explain, token);
                }
            }
            else
            {
                throw new Exception("Cannot create surface for initial value " + InitialValue);
            }
            retVal.XParameter = xParam;
            retVal.YParameter = yParam;

            return(retVal);
        }
Ejemplo n.º 18
0
        public void ShouldCreateComplexIterator()
        {
            ParameterExpression i = Expression.Parameter(typeof(int), "i");

            IteratorExpression enumerable = X.Enumerable(
                X.While(Expression.LessThan(i, 20.AsExpression()),
                        X.Yield(Expression.PostIncrementAssign(i))
                        )
                );

            enumerable
            .Compile <int>()
            .SequenceEqual(Enumerable.Range(0, 20))
            .ShouldBeTrue();
        }
Ejemplo n.º 19
0
        /// <summary>
        ///     Creates the graph associated to this expression, when the given parameter ranges over the X axis
        /// </summary>
        /// <param name="context">The interpretation context</param>
        /// <param name="parameter">The parameters of *the enclosing function* for which the graph should be created</param>
        /// <param name="explain"></param>
        /// <returns></returns>
        public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain)
        {
            Graph retVal = base.CreateGraph(context, parameter, explain);

            Graph graph = InitialValue.CreateGraph(context, parameter, explain);

            if (graph != null)
            {
                ListValue value = ListExpression.GetValue(context, explain) as ListValue;
                if (value != null)
                {
                    int token = PrepareIteration(context);
                    AccumulatorVariable.Value = graph.Function;

                    foreach (IValue v in value.Val)
                    {
                        if (v != EfsSystem.Instance.EmptyValue)
                        {
                            // All elements should always be != from EmptyValue
                            ElementFound           = true;
                            IteratorVariable.Value = v;
                            if (ConditionSatisfied(context, explain))
                            {
                                MatchingElementFound      = true;
                                AccumulatorVariable.Value = IteratorExpression.GetValue(context, explain);
                            }
                        }
                        NextIteration();
                    }
                    Function function = AccumulatorVariable.Value as Function;
                    if (function != null)
                    {
                        retVal = function.Graph;
                    }
                    else
                    {
                        retVal = Function.CreateGraphForValue(AccumulatorVariable.Value);
                    }
                    EndIteration(context, explain, token);
                }
            }
            else
            {
                throw new Exception("Cannot create graph for initial value " + InitialValue);
            }

            return(retVal);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Provides the surface of this function if it has been statically defined
        /// </summary>
        /// <param name="context">the context used to create the surface</param>
        /// <param name="xParam">The X axis of this surface</param>
        /// <param name="yParam">The Y axis of this surface</param>
        /// <returns>The surface which corresponds to this expression</returns>
        public override Functions.Surface createSurface(Interpreter.InterpretationContext context, Parameter xParam, Parameter yParam)
        {
            Functions.Surface retVal = base.createSurface(context, xParam, yParam);

            Functions.Surface surface = InitialValue.createSurface(context, xParam, yParam);
            if (surface != null)
            {
                Values.ListValue value = ListExpression.GetValue(context) as Values.ListValue;
                if (value != null)
                {
                    int token = PrepareIteration(context);
                    AccumulatorVariable.Value = surface.Function;

                    foreach (Values.IValue v in value.Val)
                    {
                        if (v != EFSSystem.EmptyValue)
                        {
                            IteratorVariable.Value = v;
                            if (conditionSatisfied(context))
                            {
                                AccumulatorVariable.Value = IteratorExpression.GetValue(context);
                            }
                        }
                        NextIteration();
                    }
                    Functions.Function function = AccumulatorVariable.Value as Functions.Function;
                    if (function != null)
                    {
                        retVal = function.Surface;
                    }
                    else
                    {
                        throw new Exception("Expression does not reduces to a function");
                    }
                    EndIteration(context, token);
                }
            }
            else
            {
                throw new Exception("Cannot create surface for initial value " + InitialValue.ToString());
            }
            retVal.XParameter = xParam;
            retVal.YParameter = yParam;

            return(retVal);
        }
Ejemplo n.º 21
0
        /// <summary>
        ///     Checks the expression and appends errors to the root tree node when inconsistencies are found
        /// </summary>
        public override void CheckExpression()
        {
            base.CheckExpression();

            Collection listExpressionType = ListExpression.GetExpressionType() as Collection;

            if (listExpressionType != null)
            {
                IteratorExpression.CheckExpression();
            }

            Accumulator.CheckExpression();
            if (!(DefinedAccumulator.GetExpressionType() is Range))
            {
                AddError("Accumulator expression should be a range", RuleChecksEnum.SyntaxError);
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Checks the expression and appends errors to the root tree node when inconsistencies are found
        /// </summary>
        public override void checkExpression()
        {
            base.checkExpression();

            Types.Type initialValueType = InitialValue.GetExpressionType();
            if (initialValueType != null)
            {
                Types.Collection listExpressionType = ListExpression.GetExpressionType() as Types.Collection;
                if (listExpressionType != null)
                {
                    IteratorExpression.checkExpression();
                }
            }
            else
            {
                AddError("Cannot determine initial value expression type for " + ToString());
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Creates the graph associated to this expression, when the given parameter ranges over the X axis
        /// </summary>
        /// <param name="context">The interpretation context</param>
        /// <param name="parameter">The parameters of *the enclosing function* for which the graph should be created</param>
        /// <returns></returns>
        public override Functions.Graph createGraph(InterpretationContext context, Parameter parameter)
        {
            Functions.Graph retVal = base.createGraph(context, parameter);

            Functions.Graph graph = InitialValue.createGraph(context, parameter);
            if (graph != null)
            {
                Values.ListValue value = ListExpression.GetValue(context) as Values.ListValue;
                if (value != null)
                {
                    int token = PrepareIteration(context);
                    AccumulatorVariable.Value = graph.Function;

                    foreach (Values.IValue v in value.Val)
                    {
                        if (v != EFSSystem.EmptyValue)
                        {
                            IteratorVariable.Value = v;
                            if (conditionSatisfied(context))
                            {
                                AccumulatorVariable.Value = IteratorExpression.GetValue(context);
                            }
                        }
                        NextIteration();
                    }
                    Functions.Function function = AccumulatorVariable.Value as Functions.Function;
                    if (function != null)
                    {
                        retVal = function.Graph;
                    }
                    else
                    {
                        retVal = Functions.Function.createGraphForValue(AccumulatorVariable.Value);
                    }
                    EndIteration(context, token);
                }
            }
            else
            {
                throw new Exception("Cannot create graph for initial value " + InitialValue.ToString());
            }

            return(retVal);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Provides the type of this expression
        /// </summary>
        /// <param name="context">The interpretation context</param>
        /// <returns></returns>
        public override Types.Type GetExpressionType()
        {
            Types.Type retVal = null;

            Types.Type iteratorType = IteratorExpression.GetExpressionType();
            if (iteratorType != null)
            {
                Types.Collection collection = (Types.Collection)Generated.acceptor.getFactory().createCollection();
                collection.Enclosing = EFSSystem;
                collection.Type      = iteratorType;

                retVal = collection;
            }
            else
            {
                AddError("Cannot evaluate iterator type for " + ToString());
            }

            return(retVal);
        }
Ejemplo n.º 25
0
        private Expression GetDynamicIterator(Expression contextParameter, IteratorExpression iex)
        {
            var fb = new FunctionBuilder(CompilationContext.Engine);

            return(Expression.Block(
                       Expression.Assign(contextParameter,
                                         Expression.New(
                                             typeof(ObjectEnumeratorBindingContext).GetConstructor(new[] { typeof(BindingContext) }), CompilationContext.BindingContext)),
                       Expression.Call(
#if netstandard
                           new Action <ObjectEnumeratorBindingContext, IDynamicMetaObjectProvider, Action <TextWriter, object>, Action <TextWriter, object> >(Iterate).GetMethodInfo(),
#else
                           new Action <ObjectEnumeratorBindingContext, IDynamicMetaObjectProvider, Action <TextWriter, object>, Action <TextWriter, object> >(Iterate).Method,
#endif
                           new[]
            {
                Expression.Convert(contextParameter, typeof(ObjectEnumeratorBindingContext)),
                Expression.Convert(iex.Sequence, typeof(IDynamicMetaObjectProvider)),
                fb.Compile(new [] { iex.Template }, contextParameter),
                fb.Compile(new [] { iex.IfEmpty }, CompilationContext.BindingContext)
            })));
        }
Ejemplo n.º 26
0
        /// <summary>
        ///     Provides the value associated to this Expression
        /// </summary>
        /// <param name="context">The context on which the value must be found</param>
        /// <param name="explain">The explanation to fill, if any</param>
        /// <returns></returns>
        protected internal override IValue GetValue(InterpretationContext context, ExplanationPart explain)
        {
            IValue retVal = null;

            ListValue value = ListExpression.GetValue(context, explain) as ListValue;

            if (value != null)
            {
                int token = PrepareIteration(context);
                context.LocalScope.SetVariable(AccumulatorVariable);
                AccumulatorVariable.Value = InitialValue.GetValue(context, explain);
                foreach (IValue v in value.Val)
                {
                    if (v != EfsSystem.Instance.EmptyValue)
                    {
                        // All elements should always be != from EmptyValue
                        ElementFound           = true;
                        IteratorVariable.Value = v;
                        if (ConditionSatisfied(context, explain))
                        {
                            MatchingElementFound      = true;
                            AccumulatorVariable.Value = IteratorExpression.GetValue(context, explain);
                        }
                    }
                    NextIteration();
                }
                EndIteration(context, explain, token);
                retVal = AccumulatorVariable.Value;
            }
            else
            {
                AddError("Cannot evaluate list value " + ListExpression, RuleChecksEnum.ExecutionFailed);
            }

            return(retVal);
        }
Ejemplo n.º 27
0
 protected virtual System.Linq.Expressions.Expression VisitIteratorExpression(IteratorExpression iex)
 {
     return(iex);
 }
Ejemplo n.º 28
0
        /// <summary>
        /// Checks the expression and appends errors to the root tree node when inconsistencies are found
        /// </summary>
        /// <param name="context">The interpretation context</param>
        public override void checkExpression()
        {
            base.checkExpression();

            IteratorExpression.checkExpression();
        }
Ejemplo n.º 29
0
 protected virtual Expression VisitIteratorExpression(IteratorExpression iex)
 {
     return(iex);
 }
 /// <summary>
 /// Provides the type of this expression
 /// </summary>
 /// <param name="context">The interpretation context</param>
 /// <returns></returns>
 public override Types.Type GetExpressionType()
 {
     return(IteratorExpression.GetExpressionType());
 }