예제 #1
0
        private LLVMValueRef GenerateFunctionCall(FunctionCall call, LLVMBuilderRef builder)
        {
            LLVMValueRef func = _table.GetFunc(call.Name);

            List<LLVMValueRef> args = new List<LLVMValueRef>();
            for (int i = 0; i < call.ArgCount; ++i)
            {
                Expression argNode = call.Args[i];
                LLVMValueRef arg = GenerateExpression(argNode, builder);

                args.Add(arg);
            }

            return LLVM.BuildCall(builder, func, args.ToArray(), "");
        }
예제 #2
0
        private Expression MakeNode()
        {
            Expression node;
            double dVal;
            int iVal;

            switch (_tokens.Current.Type)
            {
                case TokenType.IntLiteral:
                    {
                        string cur = _tokens.Current.Text;
                        if(cur.StartsWith("0x", StringComparison.Ordinal))
                        {
                            iVal = Convert.ToInt32(cur, 16);
                        }
                        else
                        {
                            iVal = Convert.ToInt32(cur);
                        }

                        node = new IntegerLiteral(iVal);
                        _tokens.Advance();
                    }
                    break;
                case TokenType.OpenParens:
                    {
                        _tokens.Advance();

                        if (_tokens.Current.Type == TokenType.Identifier
                            && _tokens.LookAhead().Type == TokenType.CloseParens)
                        {
                            string type = _tokens.Current.Text;
                            _tokens.AdvanceBy(2);
                            Expression exp = MakeNode();
                            node = new Cast(type, exp);
                        }
                        else
                        {
                            node = ParseStatementHelper(MakeNode(), 0);
                            if (_tokens.Current.Type != TokenType.CloseParens)
                            {
                                throw new Exception("Expected closing parens.");
                            }

                            _tokens.Advance();
                        }
                    }
                    break;
                case TokenType.StringLiteral:
                    {
                        node = new StringLiteral(_tokens.Current.Text);
                        _tokens.Advance();
                    }
                    break;
                case TokenType.FloatLiteral:
                    {
                        dVal = Convert.ToDouble(_tokens.Current.Text);
                        node = new FloatLiteral(dVal);
                        _tokens.Advance();
                    }
                    break;
                case TokenType.Identifier:
                    {
                        if (_tokens.LookAhead().Type == TokenType.Identifier)
                        {
                            node = new Declaration(_tokens.Current.Text, _tokens.LookAhead().Text);
                            _tokens.AdvanceBy(2);
                        }
                        else if (_tokens.LookAhead().Type == TokenType.OpenParens)
                        {
                            string name = _tokens.Current.Text;
                            _tokens.Advance();
                            ImmutableList<Expression> args = ParseFunctionArgs();
                            node = new FunctionCall(name, args);
                        }
                        else
                        {
                            node = new Identifier(_tokens.Current.Text);
                            _tokens.Advance();
                        }
                    }
                    break;
                default:
                    {
                        throw new Exception("Unknown token type in makeNode");
                    }
            }

            return node;
        }