Exemple #1
0
int postfix_expression(void) {
	if ( primary_expression() ) {
	} else if( postfix_expression() ) {
		if( lookaheadT.type == LBRACKET ) {
			match(LBRACKET);
			expression();
			match(RBRACKET);
		} else if( lookaheadT.type == LPAREN ) {
			match(LPAREN);
			if( argument_expression_list() ) {
			}
			match(RPAREN);
		} else if( lookaheadT.type == PERIOD ) {
			match(PERIOD);
			match(ID);
		} else if( lookaheadT.type == PTR_OP ) {	
			match(PTR_OP);
			match(ID);
		} else if( lookaheadT.type == INC_OP ) {
			match(INC_OP);
		} else if( lookaheadT.type == DEC_OP ) {	
			match(DEC_OP);
		} else {
			abort();
		}
	} else {
		abort();
	}
}
Exemple #2
0
int argument_expression_list(void) {
	if( assignment_expression() ) {
	} else if( argument_expression_list() ) {
		match(COMMA);
		assignment_expression();
	} else {
		abort();
	}
}
Exemple #3
0
/**
后缀表达式
<postfix_expression>::=<primary_expression>
                      {<TK_OPENPA><expression><TK_CLOSEPA>
                      |<TK_OPENPA><TK_CLOSEPA>
                      |<TK_OPENPA><arguement_expression_list><TK_CLOSEPA>
                      |<TK_DOT><IDENTIFIER>
                      |<TK_POINTSTO><IDENTIFIER>}
*/
void postfix_expression(){
    primary_expression();
    while(1){
        if(TK_DOT == token || TK_POINTSTO == token){
            get_token();
            token |= SC_MEMBER;
            get_token();
        }else if(TK_OPENBR == token){
            get_token();
            expression();
            skip(TK_CLOSEBR);
			get_token();
        }else if(TK_OPENPA == token){
            argument_expression_list();
        }else
            break;
    }
}
Exemple #4
0
struct Symbol* postfix_expression(struct PostfixExpression* node, struct Symbol** orig_symbol)
{
    struct Symbol* symbol, *ref, *newSymbol = 0, *symbol1;
    switch (node->type) {
        case 0:
            return primary_expression(node->primaryExpression, orig_symbol);
        case 1:
            symbol = load_symbol(postfix_expression(node->postfixExpression, orig_symbol));
            test_referenceable(symbol);
            ref = load_symbol(expression_func(node->expression));
            *orig_symbol = new_symbol("", symbol->storage, 0, symbol->specifier, symbol->stars - 1, 0, 0);
            if (symbol->stars == 1)
                (*orig_symbol)->specifier = 32;
            ADDSTRING("  ");
            code_gen_symbol('%', *orig_symbol);
            ADDSTRING(" = getelementptr inbounds ");
            code_gen_type_specifier(symbol->specifier, 0, symbol->length, symbol->stars);
            ADDSTRING(" ");
            if (symbol->depth == 0)
                code_gen_symbol('@', symbol);
            else
                code_gen_symbol('%', symbol);
            if (symbol->length)
                ADDSTRING(", i32 0");
            ADDSTRING(", ");
            test_integer(ref);
            code_gen_type_specifier(ref->specifier, 0, 0, 0);
            ADDSTRING(" ");
            code_gen_symbol('%', ref);
            ADDSTRING("\n");
            ADDSTRING("  ");
            newSymbol = new_symbol("", 0, 2, symbol->specifier, symbol->stars - 1, 0, 0);
            newSymbol->reference = *orig_symbol;
            code_gen_symbol('%', newSymbol);
            ADDSTRING(" = load ");
            code_gen_type_specifier(symbol->specifier, 0, (*orig_symbol)->length, symbol->stars);
            ADDSTRING(" ");
            code_gen_symbol('%', *orig_symbol);
            ADDSTRING(", align ");
            if (newSymbol->stars)
            {
                if (PTR_LENGTH == 8)
                {
                    ADDSTRING("8\n");
                }
                else
                {
                    ADDSTRING("4\n");
                }
            }
            else
            {
                int len = len_gen_type_specifier(newSymbol->specifier);
                sprintf(buf, "%d\n", len);
                ADDSTRING(buf);
            }
            return newSymbol;
        case 2:
        case 3:
            symbol = postfix_expression(node->postfixExpression, orig_symbol);
            test_functionable(symbol);
            if (node->type == 3)
            {
                argument_expression_list(node->argumentExpressionList, symbol->parameterlist);
            }
            if (!(symbol->specifier & 0x02)) {
                newSymbol = new_symbol("", symbol->storage, 2, symbol->specifier, symbol->stars, 0, symbol->length);
                ADDSTRING("  ");
                code_gen_symbol('%', newSymbol);
                ADDSTRING(" = call ");
            } else {
                ADDSTRING("  call ");
                newSymbol = symbol;
            }
            if (strcmp(symbol->name, "free") != 0)
            {
                code_gen_type_specifier(symbol->specifier, 0, symbol->length, symbol->stars);
                ADDSTRING(" ");
                if (strcmp(symbol->name, "printf") == 0 || strcmp(symbol->name, "scanf") == 0)
                {
                    ADDSTRING("(i8*, ...)* ");
                }
                code_gen_symbol('@', symbol);
            }
            else
            {
                ADDSTRING("i32 (");
                code_gen_type_specifier(symbol_arg[0]->specifier, 0, 0, symbol_arg[0]->stars);
                ADDSTRING(", ...)* bitcast (i32 (...)* @free to i32 (");
                code_gen_type_specifier(symbol_arg[0]->specifier, 0, 0, symbol_arg[0]->stars);
                ADDSTRING(", ...)*)");
            }
            ADDSTRING("(");
            if (node->type == 3)
            {
                pop_arg();
            }
            ADDSTRING(")\n");
            *orig_symbol = newSymbol;
            return newSymbol;
        case 4:
        case 5:
            // to do: struct
            return 0;
        case 6:
        case 7:
            symbol = load_symbol(postfix_expression(node->postfixExpression, orig_symbol));
            test_changeable(*orig_symbol);
            symbol1 = new_symbol("", symbol->storage, 2, symbol->specifier, symbol->stars, 0, symbol->length);
            ADDSTRING("  ");
            code_gen_symbol('%', symbol1);
            ADDSTRING(" = ");
            if ((symbol->specifier & (3 << 6)) > 0)
            {
                ADDSTRING("f");
            }
            if (node->type == 6)
            {
                ADDSTRING("add ");
            }
            else
            {
                ADDSTRING("sub ");
            }
            code_gen_type_specifier(symbol->specifier,1,symbol->length, symbol->stars);
            ADDSTRING(" ");
            code_gen_symbol('%', symbol);
            ADDSTRING(", ");
            if ((symbol->specifier & (3 << 6)) > 0)
            {
                ADDSTRING("1.000000e+00\n");
            }
            else
            {
                ADDSTRING("1\n");
            }
            ADDSTRING("  store ");
            code_gen_type_specifier(symbol->specifier,0,symbol->length, symbol->stars);
            ADDSTRING(" ");
            code_gen_symbol('%', symbol1);
            ADDSTRING(", ");
            code_gen_type_specifier(symbol->specifier,0,symbol->length, symbol->stars);
            ADDSTRING("* ");
            code_gen_symbol('%', *orig_symbol);
            ADDSTRING(", align ");
            int l = len_gen_type_specifier(symbol->specifier);
            sprintf(buf, "%d", l);
            ADDSTRING(buf);
            ADDSTRING("\n");
            return symbol;
        default:
            return 0;
    }
}