Exemplo n.º 1
0
        public void ParseRecordExpressionTest()
        {
            //Create parser and parse tokens
            ExpressionParser expressionParser = new ExpressionParser(Init("{token1:\"token1\", token2:10, token3:'symbol}"));
            RecordExpression expression       = expressionParser.ParseRecordExpression();

            //Test Record contents
            Assert.AreEqual(3, expression.GetRecords().Count);
            List <ISyntaxNode> .Enumerator recordEnumerator = expression.GetRecords().GetEnumerator();

            recordEnumerator.MoveNext();
            KeyValuePair current = (KeyValuePair)recordEnumerator.Current;

            Assert.AreEqual("token1", current.GetKey());
            Assert.AreEqual(typeof(TextExpression), current.GetValue().GetType());
            Assert.AreEqual("\"token1\"", current.GetValue().ToString());

            recordEnumerator.MoveNext();
            current = (KeyValuePair)recordEnumerator.Current;

            Assert.AreEqual("token2", current.GetKey());
            Assert.AreEqual(typeof(NumExpression), current.GetValue().GetType());
            Assert.AreEqual(10, ((NumExpression)current.GetValue()).GetNum());

            recordEnumerator.MoveNext();
            current = (KeyValuePair)recordEnumerator.Current;

            Assert.AreEqual("token3", current.GetKey());
            Assert.AreEqual(typeof(SymExpression), current.GetValue().GetType());
            Assert.AreEqual("'symbol", current.GetValue().ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Retrieve concrete expression of an fieldexpression
        /// </summary>
        /// <param name="expression">FieldExpression</param>
        /// <returns>Concrete expression</returns>
        private Expression GetExpression(FieldExpression expression)
        {
            Expression expr = expression.GetExpression();

            //Get real expression, not a variable
            while (expr is VarExpression)
            {
                expr = SymbolTable.GetVariableDefinition(((VarExpression)expr).GetVariableIdentifier());
            }

            //Get specific record from recordExpression
            if (expr is RecordExpression)
            {
                RecordExpression record      = (RecordExpression)expr;
                ISyntaxNode[]    recordArray = record.GetRecords().ToArray();
                foreach (KeyValuePair pair in recordArray)
                {
                    if (pair.GetKey() == expression.GetIdentifier())
                    {
                        return(pair.GetValue());
                    }
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Interpret RecordExpression
        /// </summary>
        /// <param name="expression">RecordExpression to interpret</param>
        public override void Visit(RecordExpression expression)
        {
            //convert record to textvalue
            String tempRecord = "{";

            for (int i = 0; i < expression.GetRecords().Count; i++)
            {
                KeyValuePair pair = (KeyValuePair)expression.GetRecords().Get(i);

                //Convert KeyValuePair to textual representation
                tempRecord += pair.GetKey() + ":";
                pair.GetValue().AcceptVisitor(this);
                tempRecord += TextValue;

                //Add seperator
                if (i != (expression.GetRecords().Count - 1))
                {
                    tempRecord += ",";
                }
            }

            tempRecord += "}";
            TextValue   = tempRecord;
        }