Inheritance: Value, System.ComponentModel.INotifyPropertyChanged
Ejemplo n.º 1
0
        private LLVMValueRef GenerateExpression(Expression expression, LLVMBuilderRef builder)
        {
            switch (expression.ExpressionType)
            {
                case ExpressionType.IntegerLiteral:
                    {
                        IntegerLiteral i = (IntegerLiteral)expression;
                        return LLVM.ConstInt(LLVM.Int32Type(), (ulong)i.Value, true);
                    }
                case ExpressionType.FloatLiteral:
                    {
                        FloatLiteral f = (FloatLiteral)expression;
                        return LLVM.ConstReal(LLVM.DoubleType(), f.Value);
                    }
                case ExpressionType.StringLiteral:
                    {
                        StringLiteral str = (StringLiteral)expression;
                        LLVMValueRef[] data = str.Value.Select(x => LLVM.ConstInt(LLVM.Int8Type(), x, true)).ToArray();
                        return LLVM.ConstArray(LLVM.Int8Type(), data);
                    }
                case ExpressionType.UnaryOperator:
                    {
                        throw new Exception("not implemented");
                    }
                case ExpressionType.BinaryOperator:
                    {
                        BinaryExpression be = (BinaryExpression)expression;
                        return GenerateBinaryExpression(be, builder);
                    }
                case ExpressionType.Identifier:
                    {
                        Identifier var = (Identifier)expression;
                        LLVMValueRef inst = _table.GetVar(var.Value);

                        return LLVM.BuildLoad(builder, inst, "");
                    }
                case ExpressionType.Declaration:
                    {
                        Declaration decl = (Declaration)expression;

                        LLVMTypeRef type = StringToType(decl.TypeName);

                        LLVMValueRef inst = LLVM.BuildAlloca(builder, type, "");
                        _table.AddVar(decl.Name, inst);
                        return inst;
                    }
                case ExpressionType.Return:
                    {
                        ReturnStatement ret = (ReturnStatement)expression;
                        LLVMValueRef exp = GenerateExpression(ret.Expression, builder);

                        return LLVM.BuildRet(builder, exp);
                    }
                case ExpressionType.Cast:
                    {
                        Cast cast = (Cast)expression;

                        LLVMValueRef exp = GenerateExpression(cast.Expression, builder);
                        LLVMTypeRef type = StringToType(cast.CastType);

                        // TODO: a more generic casting mechanism
                        if (type.TypeKind == LLVM.DoubleType().TypeKind)
                        {
                            return LLVM.BuildSIToFP(builder, exp, LLVM.DoubleType(), "");
                        }
                        else if (type.TypeKind == LLVM.Int32Type().TypeKind)
                        {
                            return LLVM.BuildFPToSI(builder, exp, LLVM.Int32Type(), "");
                        }
                        else
                        {
                            throw new Exception("unsupported cast");
                        }
                    }
                case ExpressionType.FunctionCall:
                    {
                        FunctionCall fcn = (FunctionCall)expression;
                        return GenerateFunctionCall(fcn, builder);
                    }
                case ExpressionType.Empty:
                    return GenerateNoOp(builder);
                default:
                    throw new Exception("Unknown expression type in codegen");
            }
        }
 public BinaryExpression(Expression lhs, Expression rhs, string op)
 {
     _lhs = lhs;
     _rhs = rhs;
     _op = op;
 }
 public ReturnStatement(Expression expression)
 {
     _expression = expression;
 }
Ejemplo n.º 4
0
        private Expression ParseStatementHelper(Expression curr, int minPrecedence)
        {
            //TODO: figure out unary operators (increment, decrement, function call, etc.)
            if (_tokens.Current.Type != TokenType.Operator)
            {
                throw new Exception("Expected operator in expression.");
            }

            while (_tokens.Current.Type == TokenType.Operator && OperatorPrecedence(_tokens.Current) >= minPrecedence)
            {
                Token op = _tokens.Current;
                _tokens.Advance();
                Expression next = MakeNode();

                while (_tokens.Current.Type == TokenType.Operator &&
                       OperatorPrecedence(_tokens.Current) > OperatorPrecedence(op))
                {
                    next = ParseStatementHelper(next, OperatorPrecedence(_tokens.Current));
                }

                curr = new BinaryExpression(curr, next, op.Text);
            }

            return curr;
        }
Ejemplo n.º 5
0
 public Cast(string castType, Expression expression)
 {
     _castType = castType;
     _expression = expression;
 }