private static string GetBoundMessage(string id, SetExpression set)
        {
            string s = $"{ id } is not defined as an identifier in " +
                       $"{ set.Element.ElementIdentifier }[{ GetIdentifiers(set.Element.IndexIdentifiers) }]";

            return(s);
        }
Ejemplo n.º 2
0
        public Unit VisitSetExpression(SetExpression expression)
        {
            Resolve(expression.Value);
            Resolve(expression.Object);

            return(Unit.Value);
        }
 public TypeNode Dispatch(ExpressionNode node, List <TypeNode> parameterTypes)
 {
     return(node switch
     {
         IBinaryNumberOperator n => _numberHelper.VisitBinaryNumOp(n, parameterTypes),
         IBinaryBooleanOperator n => _booleanHelper.VisitBinaryBoolOp(n, parameterTypes),
         IBinarySetOperator n => _setHelper.VisitBinarySetOp(n, parameterTypes),
         SubsetExpression n => _setHelper.VisitSubset(n, parameterTypes),
         SetExpression n => _setHelper.VisitSet(n, parameterTypes),
         NotExpression n => _booleanHelper.VisitNot(n, parameterTypes),
         FunctionCallExpression n => _declarationHelper.VisitFunctionCall(n, parameterTypes),
         IdentifierExpression n => _declarationHelper.VisitIdentifier(n, parameterTypes),
         IntegerLiteralExpression _ => _declarationHelper.VisitIntegerLiteral(),
         RealLiteralExpression _ => _declarationHelper.VisitRealLiteral(),
         BooleanLiteralExpression _ => _declarationHelper.VisitBooleanLiteral(),
         StringLiteralExpression _ => _declarationHelper.VisitStringLiteral(),
         EmptySetLiteralExpression _ => _declarationHelper.VisitEmptySetLiteral(),
         AdditionExpression n => _commonOperatorHelper.VisitAddition(n, parameterTypes),
         SubtractionExpression n => _commonOperatorHelper.VisitSubtraction(n, parameterTypes),
         AbsoluteValueExpression n => _commonOperatorHelper.VisitAbsoluteValue(n, parameterTypes),
         IRelationOperator n => _commonOperatorHelper.VisitRelationalOperator(n, parameterTypes),
         IEquivalenceOperator n => _commonOperatorHelper.VisitEquivalenceOperator(n, parameterTypes),
         NegativeExpression n => _numberHelper.VisitNegative(n, parameterTypes),
         ElementExpression n => _commonOperatorHelper.VisitElement(n, parameterTypes),
         ISetGraphField n => _commonOperatorHelper.VisitISetGraphField(n, parameterTypes),
         IFunctionGraphField n => _commonOperatorHelper.VisitIFunctionGraphField(n, parameterTypes),
         GraphExpression n => _commonOperatorHelper.VisitGraph(n, parameterTypes),
         AnonymousFunctionExpression n => _declarationHelper.VisitAnonymousFunction(n, parameterTypes),
         _ => throw new UnimplementedTypeCheckerException(node, "Dispatch"),
     });
Ejemplo n.º 4
0
        public object VisitSetExpression(SetExpression expression)
        {
            Resolve(expression.Value);
            Resolve(expression.Obj);

            return(null);
        }
        private void DispatchPredicate(SetExpression node, List <string> parameters)
        {
            var newParameters = parameters.ToList();

            newParameters.Add(node.Element.ElementIdentifier);
            newParameters.AddRange(node.Element.IndexIdentifiers);
            _dispatch(node.Predicate, newParameters);
        }
 private void DispatchBounds(SetExpression node, List <string> parameters)
 {
     foreach (var bound in node.Bounds)
     {
         _dispatch(bound.MinValue, parameters);
         _dispatch(bound.MaxValue, parameters);
     }
 }
 private void ThrowExceptionIfBoundsAndIndentifiersDoNotMatch(SetExpression node)
 {
     if (node.Element.IndexIdentifiers.Count != node.Bounds.Count)
     {
         throw new BoundException(node, "The number of bounds is not the same as element identifiers.");
     }
     ThrowExceptionIfBoundsAreNotUnique(node.Bounds);
 }
Ejemplo n.º 8
0
        public void SerializeSetExpression()
        {
            var left  = new VariableExpression("x");
            var right = new LiteralExpression(1);
            var a     = new SetExpression(left, right);
            var b     = Reserialize(a);

            Assert.AreEqual(a, b);
        }
        private SetExpression GetSet()
        {
            ElementNode      element   = GetElement();
            List <BoundNode> bounds    = GetBounds();
            ExpressionNode   predicate = GetPredicate();
            var node = new SetExpression(element, bounds, predicate, 0, 0);

            return(node);
        }
        private List <BoundNode> GetSortedBounds(SetExpression node)
        {
            List <BoundNode> bounds = new List <BoundNode>();

            foreach (var id in node.Element.IndexIdentifiers)
            {
                bounds.Add(GetBound(id, node.Bounds, node));
            }
            return(bounds);
        }
Ejemplo n.º 11
0
        private Set GetManualSet(SetExpression node, List <object> parameters)
        {
            List <Element> elements = new List <Element>();

            foreach (ExpressionNode n in node.Children)
            {
                elements.Add(_interpreter.DispatchElement(n, parameters));
            }
            return(new Set(elements));
        }
Ejemplo n.º 12
0
        public IMember GetValueFromSet(SetExpression expression)
        {
            var contents = new List <IMember>();

            foreach (var item in expression.Items.Take(MaxCollectionSize))
            {
                var value = GetValueFromExpression(item) ?? UnknownType;
                contents.Add(value);
            }
            return(PythonCollectionType.CreateSet(Module, contents, exact: expression.Items.Count <= MaxCollectionSize));
        }
        public IMember GetValueFromSet(SetExpression expression)
        {
            var contents = new List <IMember>();

            foreach (var item in expression.Items)
            {
                var value = GetValueFromExpression(item) ?? UnknownType;
                contents.Add(value);
            }
            return(PythonCollectionType.CreateSet(Interpreter, GetLoc(expression), contents));
        }
 private BoundNode GetBound(string id, List <BoundNode> bounds, SetExpression set)
 {
     foreach (var bound in bounds)
     {
         if (bound.Identifier.Equals(id))
         {
             return(bound);
         }
     }
     throw new InvalidIdentifierException(id, set);
 }
        public async Task <IMember> GetValueFromSetAsync(SetExpression expression, CancellationToken cancellationToken = default)
        {
            var contents = new List <IMember>();

            foreach (var item in expression.Items)
            {
                var value = await GetValueFromExpressionAsync(item, cancellationToken) ?? UnknownType;

                contents.Add(value);
            }
            return(PythonCollectionType.CreateSet(Interpreter, GetLoc(expression), contents));
        }
Ejemplo n.º 16
0
        private TypeNode CheckManualSet(SetExpression node, List <TypeNode> parameterTypes)
        {
            foreach (ExpressionNode n in node.Children)
            {
                TypeEnum type = _getType(n, parameterTypes).Type;
                if (type != TypeEnum.Element)
                {
                    throw new UnmatchableTypesException(n, type, TypeEnum.Element);
                }
            }

            return(new TypeNode(TypeEnum.Set, 0, 0));
        }
        public void UnionSet_SetsWithVaryingNumberOfElementIndices_(Set left, Set right, Set expected)
        {
            IInterpreterSet parent           = Substitute.For <IInterpreterSet>();
            SetHelper       setHelper        = SetUpHelper(parent);
            SetExpression   leftExpr         = new SetExpression(null, null, null, 0, 0);
            SetExpression   rightExpr        = new SetExpression(null, null, null, 0, 0);
            UnionExpression intersectionExpr = new UnionExpression(leftExpr, rightExpr, 0, 0);

            parent.DispatchSet(leftExpr, Arg.Any <List <object> >()).Returns(left);
            parent.DispatchSet(rightExpr, Arg.Any <List <object> >()).Returns(right);

            Set result = setHelper.UnionSet(intersectionExpr, new List <object>());

            result.Should().BeEquivalentTo(expected);
        }
Ejemplo n.º 18
0
 public Set DispatchSet(ExpressionNode node, List <Object> parameters)
 {
     return(node switch
     {
         SetExpression e => _setHelper.SetExpression(e, parameters),
         UnionExpression e => _setHelper.UnionSet(e, parameters),
         IntersectionExpression e => _setHelper.IntersectionSet(e, parameters),
         SubtractionExpression e => _setHelper.SubtractionSet(e, parameters),
         IdentifierExpression e => _genericHelper.Identifier <Set>(e, parameters),
         FunctionCallExpression e => _genericHelper.FunctionCall <Set>(e, parameters),
         VerticesGraphField e => _setHelper.VerticesField(e, parameters),
         EdgesGraphField e => _setHelper.EdgesField(e, parameters),
         EmptySetLiteralExpression e => _setHelper.EmptySetLiteral(e, parameters),
         _ => throw new UnimplementedInterpreterException(node, "DispatctSet")
     });
        public void SubtractionSet_f_f(int[,] left, int[,] right, int[,] expected)
        {
            IInterpreterSet       parent    = Substitute.For <IInterpreterSet>();
            SetHelper             setHelper = SetUpHelper(parent);
            SetExpression         lhsExpr   = new SetExpression(null, null, null, 1, 1);
            SetExpression         rhsExpr   = new SetExpression(null, null, null, 1, 1);
            SubtractionExpression expr      = new SubtractionExpression(lhsExpr, rhsExpr, 0, 0);

            parent.DispatchSet(lhsExpr, Arg.Any <List <object> >()).Returns(getSetFrom2dArray(left));
            parent.DispatchSet(rhsExpr, Arg.Any <List <object> >()).Returns(getSetFrom2dArray(right));

            Set res = setHelper.SubtractionSet(expr, new List <object>());

            res.Should().BeEquivalentTo(getSetFrom2dArray(expected));
        }
        public void IntersectionSet_ValidInput_ReturnsCorrectSet(Set left, Set right, Set exp)
        {
            IInterpreterSet        parent           = Substitute.For <IInterpreterSet>();
            SetHelper              setHelper        = SetUpHelper(parent);
            SetExpression          leftExpr         = new SetExpression(null, null, null, 0, 0);
            SetExpression          rightExpr        = new SetExpression(null, null, null, 0, 0);
            IntersectionExpression intersectionExpr = new IntersectionExpression(leftExpr, rightExpr, 0, 0);

            parent.DispatchSet(leftExpr, Arg.Any <List <object> >()).Returns(left);
            parent.DispatchSet(rightExpr, Arg.Any <List <object> >()).Returns(right);

            Set result = setHelper.IntersectionSet(intersectionExpr, new List <object>());

            result.Should().BeEquivalentTo(exp);
        }
Ejemplo n.º 21
0
        public object VisitSetExpression(SetExpression expression)
        {
            var obj = Evaluate(expression.Obj);

            if (obj is LoxInstance loxInstance)
            {
                var value = Evaluate(expression.Value);

                loxInstance[expression.Name] = value;

                return(value);
            }
            else
            {
                throw new LoxRunTimeException(expression.Name, "Only instances have fields.");
            }
        }
        internal static void GetGraphExpression(int vertexCount, int edgeCount, int indexCount, out GraphExpression graphExpr, out FunctionNode src, out FunctionNode dst)
        {
            SetExpression        vertexSetExpr = GetSet(indexCount, GetBoundList(indexCount, 1, vertexCount));
            SetExpression        edgeSetExpr   = GetSet(indexCount, GetBoundList(indexCount, 1, edgeCount));
            IdentifierExpression srcId         = GetIdExpression(0, false);
            IdentifierExpression dstId         = GetIdExpression(1, false);

            graphExpr = new GraphExpression(vertexSetExpr, edgeSetExpr, srcId, dstId, 0, 0);
            ElementExpression srcElementExpr = GetElementExpression(GetIntList(1, indexCount));
            ConditionNode     srcCondNode    = GetConditionNode(srcElementExpr, indexCount, 0);

            src = GetElementFunctionNode(srcCondNode);
            ElementExpression dstElementExpr = GetElementExpression(GetIntList(1, indexCount), GetIntListWithValue(indexCount, 1));
            ConditionNode     dstCondNode    = GetConditionNode(dstElementExpr, indexCount, 0);

            dst = GetElementFunctionNode(dstCondNode);
        }
        public void SubsetBoolean_GivenSetAndSet_CorrectValueReturned(int[] a, int[] b, bool expected)
        {
            Set              lhsValue   = GetSetWith1DElements(a);
            Set              rhsValue   = GetSetWith1DElements(b);
            SetExpression    lhs        = GetSetInit();
            SetExpression    rhs        = GetSetInit();
            SubsetExpression expression = new SubsetExpression(lhs, rhs, 0, 0);

            IInterpreterBoolean parent = Substitute.For <IInterpreterBoolean>();

            DispatchSetForParent(lhsValue, lhs, parent);
            DispatchSetForParent(rhsValue, rhs, parent);
            BooleanHelper booleanHelper = Utilities.GetBooleanHelper(parent);

            bool res = booleanHelper.SubsetBoolean(expression, new List <object>());

            Assert.AreEqual(expected, res);
        }
        public void SetExpression_f_f(List <int> min, List <int> max, List <List <object> > indexPairs)
        {
            IInterpreterSet  parent           = Substitute.For <IInterpreterSet>();
            SetHelper        setHelper        = SetUpHelper(parent);
            List <BoundNode> bounds           = GetBounds(parent, min, max);
            LessExpression   expr             = new LessExpression(null, null, 0, 0);
            SetExpression    setExpr          = new SetExpression(new ElementNode(null, null, 0, 0), bounds, expr, 0, 0);
            List <Element>   expectedElements = new List <Element>();

            for (int i = 0; i < indexPairs.Count; i++)
            {
                expectedElements.Add(new Element(indexPairs[i].ConvertAll(x => (int)x)));
            }
            Set expected = new Set(expectedElements);

            parent.DispatchBoolean(expr, Arg.Any <List <Object> >()).Returns(x => IsContained(indexPairs, (List <Object>)x[1]));

            Set result = setHelper.SetExpression(setExpr, new List <object>());

            result.Should().BeEquivalentTo(expected);
        }
        public void VisitSet(SetExpression node, List <string> parameters)
        {
            if (node.IsSetBuilder)
            {
                ThrowExceptionIfIdentifiersAreInParameters(node.Element.IndexIdentifiers, parameters);
                ThrowExceptionIfIdentifiersAreNotUnique(node.Element.IndexIdentifiers);
                ThrowExceptionIfBoundsAndIndentifiersDoNotMatch(node);
                ThrowExceptionIfBoundsAreInParameters(node.Bounds, parameters, node);
                ThrowExceptionIfElementAreInParameters(node.Element, parameters);

                node.Bounds = GetSortedBounds(node);
                DispatchBounds(node, parameters);
                DispatchPredicate(node, parameters);
            }
            else
            {
                foreach (ExpressionNode n in node.Children)
                {
                    _dispatch(n, parameters);
                }
            }
        }
Ejemplo n.º 26
0
        public TypeNode VisitSet(SetExpression node, List <TypeNode> parameterTypes)
        {
            if (!node.IsSetBuilder)
            {
                return(CheckManualSet(node, parameterTypes));
            }

            List <TypeNode> indexTypes = new List <TypeNode>();

            foreach (BoundNode n in node.Bounds)
            {
                CheckType(n.MaxValue, parameterTypes, TypeEnum.Integer);
                CheckType(n.MinValue, parameterTypes, TypeEnum.Integer);
                indexTypes.Add(new TypeNode(TypeEnum.Integer, 0, 0));
            }

            parameterTypes.Add(new TypeNode(TypeEnum.Element, 0, 0));
            parameterTypes.AddRange(indexTypes);
            CheckType(node.Predicate, parameterTypes, TypeEnum.Boolean);
            parameterTypes.RemoveRange(parameterTypes.Count - node.Bounds.Count - 1, node.Bounds.Count + 1);
            return(new TypeNode(TypeEnum.Set, 0, 0));
        }
Ejemplo n.º 27
0
        public Set SetExpression(SetExpression node, List <Object> parameters)
        {
            if (!node.IsSetBuilder)
            {
                return(GetManualSet(node, parameters));
            }

            Set        set       = new Set();
            List <int> minValues = new List <int>();
            List <int> maxValues = new List <int>();
            List <int> indices   = new List <int>();

            for (int i = 0; i < node.Bounds.Count; i++)
            {
                minValues.Add(_interpreter.DispatchInt(node.Bounds[i].MinValue, parameters));
                maxValues.Add(_interpreter.DispatchInt(node.Bounds[i].MaxValue, parameters));
                indices.Add(0);
            }

            EvaluateSet(minValues, maxValues, indices, node.Predicate, 0, set, parameters);

            return(set);
        }
        internal static void GetGraphExpression(int vertexCount, int edgeCount, out GraphExpression graphExpr, out FunctionNode src, out FunctionNode dst)
        {
            SetExpression vertexSetExpr = GetSet(1, new List <Tuple <int, int> >()
            {
                new Tuple <int, int>(1, vertexCount)
            });
            SetExpression edgeSetExpr = GetSet(1, new List <Tuple <int, int> >()
            {
                new Tuple <int, int>(1, edgeCount)
            });
            IdentifierExpression srcId = GetIdExpression(0, false);
            IdentifierExpression dstId = GetIdExpression(1, false);

            graphExpr = new GraphExpression(vertexSetExpr, edgeSetExpr, srcId, dstId, 0, 0);
            ElementExpression srcElementExpr = GetElementExpression(1);
            ConditionNode     srcCondNode    = GetConditionNode(srcElementExpr, 1, 0);

            src = GetElementFunctionNode(srcCondNode);
            ElementExpression dstElementExpr = GetElementExpression(1, 1);
            ConditionNode     dstCondNode    = GetConditionNode(dstElementExpr, 1, 0);

            dst = GetElementFunctionNode(dstCondNode);
        }
Ejemplo n.º 29
0
        public void GraphExpression_ValidExpressions_ReturnsCorrectGraph()
        {
            IInterpreterGraph    parent       = Substitute.For <IInterpreterGraph>();
            GraphHelper          graphHelper  = SetUpHelper(parent);
            SetExpression        verticesExpr = new SetExpression(null, null, null, 1, 1);
            SetExpression        edgesExpr    = new SetExpression(null, null, null, 1, 1);
            IdentifierExpression srcExpr      = new IdentifierExpression(null, 1, 1);
            IdentifierExpression dstExpr      = new IdentifierExpression(null, 1, 1);
            GraphExpression      graphExpr    = new GraphExpression(verticesExpr, edgesExpr, srcExpr, dstExpr, 1, 1);
            Set      expectedVerticesSet      = new Set();
            Set      expectedEdgesSet         = new Set();
            Function expectedSrcFunc          = new Function(42);
            Function expectedDstFunc          = new Function(43);
            Graph    expected = new Graph(expectedVerticesSet, expectedEdgesSet, expectedSrcFunc, expectedDstFunc);

            parent.DispatchSet(verticesExpr, Arg.Any <List <Object> >()).Returns(expectedVerticesSet);
            parent.DispatchSet(edgesExpr, Arg.Any <List <Object> >()).Returns(expectedEdgesSet);
            parent.DispatchFunction(srcExpr, Arg.Any <List <Object> >()).Returns(expectedSrcFunc);
            parent.DispatchFunction(dstExpr, Arg.Any <List <Object> >()).Returns(expectedDstFunc);

            Graph result = graphHelper.GraphExpression(graphExpr, new List <Object>());

            result.Should().BeEquivalentTo(expected);
        }
Ejemplo n.º 30
0
 public override void PostWalk(SetExpression node)
 {
 }
Ejemplo n.º 31
0
 public virtual void PostWalk(SetExpression node)
 {
 }
Ejemplo n.º 32
0
 // SetExpression
 public override bool Walk(SetExpression node)
 {
     return false;
 }
Ejemplo n.º 33
0
 public override void PostWalk(SetExpression node)
 {
 }
Ejemplo n.º 34
0
 // SetExpression
 public virtual bool Walk(SetExpression node)
 {
     return true;
 }
Ejemplo n.º 35
0
 // SetExpression
 public override bool Walk(SetExpression node) { return Location >= node.StartIndex && Location <= node.EndIndex; }