Exemplo n.º 1
0
        public void Literal()
        {
            ASTLiteral node = new ASTLiteral();

            node.LiteralType = LiteralType.Double;
            node.Value       = 10.5d;


            string result = node.Serialize();

            Console.WriteLine(result);
        }
Exemplo n.º 2
0
        public CodeObject Visit(ASTLiteral literal)
        {
            // TODO: Temp hack for ints
            if (literal.LiteralType == LiteralType.Int32)
            {
                IConvertible convertible = literal.Value as IConvertible;

                return(new CodePrimitiveExpression(convertible.ToDouble(null)));
            }
            else
            {
                return(new CodePrimitiveExpression(literal.Value));
            }
        }
Exemplo n.º 3
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);
        }