Exemplo n.º 1
0
        public CodeObject Visit(ASTBinaryExpression expr)
        {
            CodeBinaryOperatorExpression result = new CodeBinaryOperatorExpression(
                (CodeExpression)expr.Left.Accept(this),
                GenerationUtils.MapBinaryOperator(expr.Operator),
                (CodeExpression)expr.Right.Accept(this)
                );

            return(result);
        }
Exemplo n.º 2
0
        private void ReplaceExistingNamedVar(string namedVarName)
        {
            int blockNo = this.currentBlockNo;

            // Assign namedVar
            this.theConstructor.Statements.Add(GenerationUtils.ConstructAssignNamedVar(namedVarName, blockNo));

            // Replace namedVar in Dictionary
            this.theConstructor.Statements.Add(GenerationUtils.ConstructCallReplaceNameIdMethod(namedVarName));
        }
Exemplo n.º 3
0
        public CodeObject Visit(ASTVerb verb)
        {
            Type verbType = this.metadataRetriever.GetVerbType(verb.VerbId);

            // Construct always true if block to wrap several statements
            CodeConditionStatement result = new CodeConditionStatement();

            result.Condition = new CodePrimitiveExpression(true);

            // Process label if exists
            if (verb.LabelId != null)
            {
                // If namedVar is not exists already
                if (!this.namedVars.ContainsKey(verb.LabelId))
                {
                    this.CreateNewNamedVar(verb.LabelId);
                }

                // verb is Block
                if (typeof(AnyBlock).IsAssignableFrom(verbType))
                {
                    this.ReplaceExistingNamedVar(verb.LabelId);
                }
            }

            // Construct verb declaration
            CodeVariableDeclarationStatement verbDeclaration = new CodeVariableDeclarationStatement();

            verbDeclaration.Type           = new CodeTypeReference(verbType);
            verbDeclaration.Name           = "verb";
            verbDeclaration.InitExpression = this.CreateConstructorCallExpression(verbType, verb.Operands);
            result.TrueStatements.Add(verbDeclaration);

            // Construct Set label Id for Commands
            if (verb.LabelId != null)
            {
                if (typeof(AnyCommand).IsAssignableFrom(verbType))
                {
                    result.TrueStatements.Add(GenerationUtils.ConstructCallCommandSetIdMethod("verb", verb.LabelId));
                }
            }


            // Construct AddVerb method call
            result.TrueStatements.Add(GenerationUtils.ConstructCallAddVerbMethod("verb"));


            // This is needed for Block labels assignment!!!
            if (typeof(AnyBlock).IsAssignableFrom(verbType))
            {
                this.currentBlockNo++;
            }

            return(result);
        }
Exemplo n.º 4
0
        private void CreateNewNamedVar(string namedVarName)
        {
            int namedVarId = this.currentNamedVarNo;

            this.namedVars.Add(namedVarName, this.currentNamedVarNo);
            this.currentNamedVarNo++;

            // Construct NamedVar field
            this.theClass.Members.Add(GenerationUtils.ConstructCreateNamedVar(namedVarName));

            // Assign namedVar
            this.theConstructor.Statements.Add(GenerationUtils.ConstructAssignNamedVar(namedVarName, namedVarId));

            // Put namedVar to Dictionary
            this.theConstructor.Statements.Add(GenerationUtils.ConstructCallAddNameMethod(namedVarName));
        }
Exemplo n.º 5
0
        private CodeObjectCreateExpression CreateConstructorCallExpression(Type verbType, IList <ASTAnyExpression> operands)
        {
            CodeObjectCreateExpression constructorCall = new CodeObjectCreateExpression();

            constructorCall.CreateType = new CodeTypeReference(verbType);

            // TODO: Figure out something with first
            foreach (var ctorParam in verbType.GetConstructors().First().GetParameters())
            {
                if (operands.Count > ctorParam.Position && operands[ctorParam.Position] != null)
                {
                    ASTAnyExpression operand = operands[ctorParam.Position];

                    if (ctorParam.ParameterType == typeof(IDoubleOperand))
                    {
                        if (operand is ASTLiteral)
                        {
                            // Operand is a constant
                            ASTLiteral literalOperand = (operand as ASTLiteral);

                            if (literalOperand.LiteralType == LiteralType.Double)
                            {
                                constructorCall.Parameters.Add(GenerationUtils.ConctructCtorNumberParameter((double)literalOperand.Value));
                            }
                            else
                            {
                                throw new CompilerException("Only double operands supported for the moment: " + operand.GetType().Name);
                            }
                        }
                        else
                        {
                            // Operand is an expression

                            string parameterMethodName = String.Format("Verb{0}_Operand{1}", this.verbNo, ctorParam.Position + 1);

                            // Visit expression
                            CodeExpression operandExpression = (CodeExpression)operand.Accept(this);

                            // Construct method for operand
                            this.CreateOperandMethod(parameterMethodName, operandExpression);

                            // Add method delegate as constructor parameter
                            constructorCall.Parameters.Add(GenerationUtils.ConctructCtorDelegateParameter(parameterMethodName));
                        }
                    }
                    else if (ctorParam.ParameterType == typeof(LiteralOperand))
                    {
                        if (operand is ASTLValue)
                        {
                            string literal = (operand as ASTLValue).Id;
                            constructorCall.Parameters.Add(GenerationUtils.ConstructCtorLiteralParameter(literal));
                        }
                        else
                        {
                            throw new CompilerException("Wrong AST node for Literal operand: " + operand.GetType().Name);
                        }
                    }
                    else
                    {
                        throw new CompilerException("Not supported parameter type: " + ctorParam.ParameterType.Name);
                    }
                }
                else
                {
                    // Set null for null operand and for remaining operands
                    constructorCall.Parameters.Add(new CodePrimitiveExpression(null));
                }
            }

            return(constructorCall);
        }