public void CreateIndexedExpression()
        {
            IndexedExpression expr = new IndexedExpression(new ConstantExpression("foo"), new ConstantExpression(0));

            Assert.AreEqual("foo", expr.TargetExpression.Evaluate(null));
            Assert.AreEqual(0, expr.IndexExpression.Evaluate(null));
        }
        public void GetIndexedStringOutOfBound()
        {
            Machine machine = new Machine();

            IndexedExpression expression = new IndexedExpression(new ConstantExpression("foo"), new ConstantExpression(10));

            var result = expression.Evaluate(machine.RootContext);

            Assert.IsNull(result);
        }
        public void GetIndexedStringNegativeIndexAsNil()
        {
            Machine machine = new Machine();

            IndexedExpression expression = new IndexedExpression(new ConstantExpression("bar"), new ConstantExpression(-10));

            var result = expression.Evaluate(machine.RootContext);

            Assert.IsNull(result);
        }
        public void EvaluateIndexedExpressionListInteger()
        {
            var list = new List <int>()
            {
                1, 2, 3
            };
            IndexedExpression expr = new IndexedExpression(new ConstantExpression(list), new ConstantExpression(1));

            Assert.AreEqual(2, expr.Evaluate(null));
        }
Esempio n. 5
0
        public void ParseSimpleIndexedExpression()
        {
            Parser parser   = new Parser("a[1]");
            var    expected = new IndexedExpression(new NameExpression("a"), new ConstantExpression(1));
            var    result   = parser.ParseExpression();

            Assert.IsNotNull(result);
            Assert.AreEqual(expected, result);

            Assert.IsNull(parser.ParseCommand());
        }
        public void GetIndexedString()
        {
            Machine machine = new Machine();

            IndexedExpression expression = new IndexedExpression(new ConstantExpression("foo"), new ConstantExpression(0));

            var result = expression.Evaluate(machine.RootContext);

            Assert.IsNotNull(result);
            Assert.AreEqual("f", result);
        }
Esempio n. 7
0
        /// <summary>
        /// Sets the <see cref="ICSharpOutputNode.WriteDown"/> flag.
        /// </summary>
        public override void SetWriteDown()
        {
            if (WriteDown)
            {
                return;
            }

            WriteDown = true;

            IndexedExpression.SetWriteDown();
            FeatureCall.SetWriteDown();
        }
        public void GetIndexedOutOfBandValue()
        {
            Machine machine = new Machine();

            machine.ExecuteText("a = [1,2,3]");

            IndexedExpression expression = new IndexedExpression(new NameExpression("a"), new ConstantExpression(10));

            var result = expression.Evaluate(machine.RootContext);

            Assert.IsNull(result);
        }
        public void GetIndexedNegativeValue()
        {
            Machine machine = new Machine();

            machine.ExecuteText("a = [1,2,3]");

            IndexedExpression expression = new IndexedExpression(new NameExpression("a"), new ConstantExpression(-1));

            var result = expression.Evaluate(machine.RootContext);

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result);
        }
Esempio n. 10
0
        /// <summary>
        /// Gets the source code corresponding to the expression.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        /// <param name="expressionContext">The context.</param>
        /// <param name="skippedIndex">Index of a destination to skip.</param>
        public override void WriteCSharp(ICSharpWriter writer, ICSharpExpressionContext expressionContext, int skippedIndex)
        {
            Debug.Assert(WriteDown);

            ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();

            IndexedExpression.WriteCSharp(writer, SourceExpressionContext, -1);

            string IndexedText = SourceExpressionContext.ReturnValue;

            CSharpArgument.CSharpArgumentList(writer, expressionContext, FeatureCall, -1, false, out string ArgumentListText, out IList <string> OutgoingResultList);

            expressionContext.SetSingleReturnValue($"{IndexedText}[{ArgumentListText}]");
        }
        public void GetIndexedDictionaryEntryAsNil()
        {
            Machine machine = new Machine();
            var     hash    = new DynamicHash();

            hash[new Symbol("one")] = 1;
            hash[new Symbol("two")] = 2;

            IndexedExpression expression = new IndexedExpression(new ConstantExpression(hash), new ConstantExpression(new Symbol("three")));

            var result = expression.Evaluate(machine.RootContext);

            Assert.IsNull(result);
        }
        public override void GenerateCode(ILCodeGenerator cg)
        {
            //generamos la expresion a la que se le hace []
            IndexedExpression.GenerateCode(cg);

            //generamos el indice
            Index.GenerateCode(cg);

            //cargamos el elemento del array
            if (LoadVariableInTheStack)
            {
                cg.ILGenerator.Emit(OpCodes.Ldelem, IndexedExpression.NodeInfo.ElementsType.ILType);
            }
        }
        public void Equals()
        {
            IndexedExpression expr1 = new IndexedExpression(new NameExpression("a"), new ConstantExpression(1));
            IndexedExpression expr2 = new IndexedExpression(new NameExpression("a"), new ConstantExpression(2));
            IndexedExpression expr3 = new IndexedExpression(new NameExpression("b"), new ConstantExpression(1));
            IndexedExpression expr4 = new IndexedExpression(new NameExpression("a"), new ConstantExpression(1));

            Assert.IsTrue(expr1.Equals(expr4));
            Assert.IsTrue(expr4.Equals(expr1));
            Assert.AreEqual(expr1.GetHashCode(), expr4.GetHashCode());

            Assert.IsFalse(expr1.Equals(expr2));
            Assert.IsFalse(expr2.Equals(expr1));
            Assert.IsFalse(expr1.Equals(expr3));
            Assert.IsFalse(expr3.Equals(expr1));
            Assert.IsFalse(expr1.Equals(null));
            Assert.IsFalse(expr1.Equals("foo"));
        }
Esempio n. 14
0
        private IExpression ParseTerm()
        {
            IExpression expression = null;

            if (this.TryParseToken(TokenType.Operator, "-"))
            {
                expression = new NegativeExpression(this.ParseTerm());
            }
            else if (this.TryParseToken(TokenType.Operator, "+"))
            {
                expression = this.ParseTerm();
            }
            else if (this.TryParseToken(TokenType.Operator, "!"))
            {
                expression = new NegationExpression(this.ParseTerm());
            }
            else
            {
                expression = this.ParseSimpleTerm();
            }

            if (expression == null)
            {
                return(null);
            }

            while (true)
            {
                if (this.TryParseToken(TokenType.Separator, "."))
                {
                    string name = this.ParseName();

                    if (this.TryParseToken(TokenType.Separator, "{"))
                    {
                        expression = new DotExpression(expression, name, new IExpression[] { this.ParseBlockExpression(true) });
                    }
                    else if (this.NextTokenStartsExpressionList())
                    {
                        expression = new DotExpression(expression, name, this.ParseExpressionList());
                    }
                    else
                    {
                        expression = new DotExpression(expression, name, new IExpression[0]);
                    }

                    continue;
                }

                if (this.TryParseToken(TokenType.Separator, "::"))
                {
                    string name = this.ParseName();

                    expression = new DoubleColonExpression(expression, name);

                    continue;
                }

                if (this.TryParseToken(TokenType.Separator, "["))
                {
                    IExpression indexexpr = this.ParseExpression();
                    this.ParseToken(TokenType.Separator, "]");
                    expression = new IndexedExpression(expression, indexexpr);

                    continue;
                }

                break;
            }

            return(expression);
        }
        public void EvaluateIndexedExpressionStringInteger()
        {
            IndexedExpression expr = new IndexedExpression(new ConstantExpression("foo"), new ConstantExpression(0));

            Assert.AreEqual("f", expr.Evaluate(null));
        }
        public override void CheckSemantic(SymbolTable symbolTable, List <CompileError> errors)
        {
            //la politica que se siguio fue al encontrar un error en el array access no seguir buscando errores
            //al modo de C#

            //chequeamos la IndexedExpression
            IndexedExpression.CheckSemantic(symbolTable, errors);

            //chequeamos el Index
            Index.CheckSemantic(symbolTable, errors);

            //si alguno evalua de error entonces este tambien
            if (IndexedExpression.NodeInfo.Equals(SemanticInfo.SemanticError) || Index.NodeInfo.Equals(SemanticInfo.SemanticError))
            {
                ///el nodo evalúa de error
                NodeInfo = SemanticInfo.SemanticError;
            }

            //si IndexedExpression no evaluo de error
            if (!Object.Equals(IndexedExpression.NodeInfo, SemanticInfo.SemanticError))
            {
                //IndexedExpression tiene que ser compatible con array
                if (!IndexedExpression.NodeInfo.Type.BuiltInType.IsCompatibleWith(BuiltInType.Array))
                {
                    errors.Add(new CompileError
                    {
                        Line         = IndexedExpression.Line,
                        Column       = IndexedExpression.CharPositionInLine,
                        ErrorMessage = string.Format("Cannot apply indexing with [] to an expression of type '{0}'", IndexedExpression.NodeInfo.Type.Name),
                        Kind         = ErrorKind.Semantic
                    });

                    ///el nodo evalúa de error
                    NodeInfo = SemanticInfo.SemanticError;
                }
            }

            //si Index no evaluo de error
            if (!Object.Equals(Index.NodeInfo, SemanticInfo.SemanticError))
            {
                //Index tiene que ser compatible con int
                if (!Index.NodeInfo.BuiltInType.IsCompatibleWith(BuiltInType.Int))
                {
                    errors.Add(new CompileError
                    {
                        Line         = Index.Line,
                        Column       = Index.CharPositionInLine,
                        ErrorMessage = string.Format("Cannot implicitly convert type '{0}' to 'int'", Index.NodeInfo.Type.Name),
                        Kind         = ErrorKind.Semantic
                    });

                    ///el nodo evalúa de error
                    NodeInfo = SemanticInfo.SemanticError;
                }
            }

            //seteamos los campos necesarios
            if (!Object.Equals(NodeInfo, SemanticInfo.SemanticError))
            {
                NodeInfo.Name        = SemanticInfo.NoName;
                NodeInfo.ElementKind = SymbolKind.NoSymbol;

                NodeInfo.BuiltInType = IndexedExpression.NodeInfo.ElementsType.BuiltInType;

                NodeInfo.ElementsType = IndexedExpression.NodeInfo.ElementsType.ElementsType;
                NodeInfo.Fields       = IndexedExpression.NodeInfo.ElementsType.Fields;

                NodeInfo.Type = IndexedExpression.NodeInfo.ElementsType;

                //esta linea me parece que no es necesaria
                NodeInfo.ILType = IndexedExpression.NodeInfo.ElementsType.ILType;
            }
        }