Beispiel #1
0
        /// <summary>
        /// Builds the statements that generate the value of this expression
        /// </summary>
        /// <param name="block">Block to which to add the statements</param>
        /// <param name="expr">Expression to build</param>
        /// <param name="operand1">First operand</param>
        /// <param name="operand2">second operand</param>
        /// <returns></returns>
        public static Declaration BuildStatements(Block block, Expression expr, Token operand1, Token operand2)
        {
            Token opToken = expr.Tokens[0];

            var            decl          = block.CreateTempDeclaration(TypeHelper.GetType(block, operand1));
            ValueStatement initialAssign = AssignmentHelper.ParseSingle(block, decl, operand1);

            block.Statements.Add(initialAssign);

            var asm = System.Reflection.Assembly.GetExecutingAssembly();

            foreach (var type in asm.GetTypes())
            {
                if (type.BaseType == typeof(MathStatement))
                {
                    try
                    {
                        IMathOperator mathOp = (IMathOperator)asm.CreateInstance(type.FullName);
                        if (opToken.Equals(mathOp.GetHandledOperator()))
                        {
                            MathStatement mathStatement;
                            Datum         datum = Datum.Parse(block, operand2);
                            if (datum == null)
                            {
                                MessageSystem.Instance.ThrowNewError(MessageSystem.ErrorCode.UndeclaredVar);
                                return(null);
                            }

                            if (decl.Type.IndirectionLevels > 0)
                            {
                                Type derefType = decl.Type.Clone() as Type;
                                derefType.Dereference();

                                var offsetDecl = block.CreateTempDeclaration(new BuiltInType("int"));
                                block.Statements.Add(AssignmentHelper.ParseSingle(block, offsetDecl, operand2));
                                var  imm  = new Immediate(new Token(derefType.Size.ToString()));
                                Mult mult = new Mult(offsetDecl, imm);
                                block.Statements.Add(mult);
                                datum = offsetDecl;
                            }

                            mathStatement = Activator.CreateInstance(type,
                                                                     new object[] { decl, datum }) as MathStatement;
                            block.Statements.Add(mathStatement);
                            break;
                        }
                    }
                    catch (MissingMethodException) { }
                }
            }

            return(decl);
        }
        public void Visitor_Should_FindAllValueStatements()
        {
            // Arrange
            var firstValue = "1";
            var fifthValue = "5";

            // Create first statement
            var valueStatement1a = new ValueStatement(firstValue);
            var valueStatement1b = new ValueStatement("2");
            var binaryStatement1 = new BinaryStatement(valueStatement1a, valueStatement1b);

            // Create second statement (nested)
            var valueStatement2a  = new ValueStatement("3");
            var valueStatement22a = new ValueStatement("4");
            var valueStatement22b = new ValueStatement(fifthValue);
            var binaryStatement22 = new BinaryStatement(valueStatement22a, valueStatement22b);
            var binaryStatement2  = new BinaryStatement(valueStatement2a, binaryStatement22);

            var syntaxTree = new SyntaxTree(new[] { binaryStatement1, binaryStatement2 });

            var result = new List <string>();
            var syntaxTreeValueVisitor = new SyntaxTreeValueVisitor();

            syntaxTreeValueVisitor.VisitResultEvent += (sender, e) =>
            {
                Trace.WriteLine(string.Format("Level {0}, Value Expression {1}", e.Depth, e.Value));
                result.Add(e.Value);
            };

            // Act
            // Run through the syntax tree with the simple value visitor
            Trace.WriteLine("Start SyntaxTreeValueVisitor");
            syntaxTree.Accept(syntaxTreeValueVisitor);

            Trace.WriteLine("End SyntaxTreeValueVisitor");
            Trace.WriteLine(string.Empty);

            // Run through the syntax tree with the indent visitor
            Trace.WriteLine("Start SyntaxTreeIndendedVisitor");
            syntaxTree.Accept(new SyntaxTreeIndentVisitor());
            Trace.WriteLine("End SyntaxTreeIndendedVisitor");

            // Assert
            Assert.AreEqual(5, result.Count);
            Assert.AreEqual(firstValue, result[0]);
            Assert.AreEqual(fifthValue, result[4]);
        }
Beispiel #3
0
        private static Vertex ApplyValueStatement(Vertex currentVertex, ValueStatement valueStatement)
        {
            var vertexState = currentVertex.State.Values.ToDictionary(x => x.Key, x => x.Value);

            foreach (var fluent in valueStatement.Condition.ExtractFluentsValues())
            {
                vertexState[fluent.Key] = fluent.Value;
            }
            var vertexEvaluatingValueStatement = graph.Vertexes.Where(v => v.State.Values.All(x => vertexState[x.Key] == x.Value)).ToList();

            if (vertexEvaluatingValueStatement.Count > 1)
            {
                throw new InvalidOperationException("vertexEvaluatingValueStatement length is grater than 1.");
            }

            return(vertexEvaluatingValueStatement.FirstOrDefault());
        }
Beispiel #4
0
 public override object Visit(ValueStatement that, object value)
 {
     _writer.Write("value ");
     that.Name.Visit(this);
     _writer.Write(" is ");
     that.Type.Visit(this);
     _writer.Write(" := ");
     that.Initializer.Visit(this);
     _writer.WriteLine();
     return null;
 }
        public override object Visit(ValueStatement that, object value = null)
        {
            _symbols.EnterBlock(that.Name.Symbol, that);
            _symbols.LeaveBlock(that.Name.Symbol);

            return null;
        }
        public override object Visit(ValueStatement that, object value = null)
        {
            PrintPrologue(that);
            PrintDefinition(that);
            PrintNodeId("Type", that.Type);
            PrintNodeId("Initializer", that.Initializer);
            PrintEpilogue(that);

            that.Name.Visit(this);
            that.Type.Visit(this);
            that.Initializer.Visit(this);

            return null;
        }
Beispiel #7
0
 public override object Visit(ValueStatement that, object value)
 {
     that.Type.Visit(this);
     #if false
     _writer.Write(" _");
     that.Name.Visit(this);
     _writer.WriteLine(";");
     #endif
     return null;
 }
 public override object Visit(ValueStatement that, object value = null)
 {
     that.Name.Visit(this);
     that.Type.Visit(this);
     #if false
     /** \todo Infer initializer types and enable the statement below. */
     that.Initializer.Visit(this);
     #endif
     return null;
 }
Beispiel #9
0
 public virtual object Visit(ValueStatement that, object value)
 {
     throw new System.NotImplementedException();
 }