X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=parser.c;h=69dba39ee72707fdad82a5e3650229f8f4f0b892;hb=22420f9404b76d3ab2536c7d3903cbcf05a792a8;hp=520da3a1e5bda2fdffc45748b4317076a9a90ab0;hpb=3bd432f7fe5fa4d3f84ddeaf5806398c141e4f28;p=cparser diff --git a/parser.c b/parser.c index 520da3a..69dba39 100644 --- a/parser.c +++ b/parser.c @@ -154,14 +154,14 @@ static inline const token_t *look_ahead(int num) #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0) -void error(void) +static void error(void) { #ifdef ABORT_ON_ERROR abort(); #endif } -void parser_print_prefix_pos(const source_position_t source_position) +static void parser_print_prefix_pos(const source_position_t source_position) { fputs(source_position.input_name, stderr); fputc(':', stderr); @@ -169,14 +169,15 @@ void parser_print_prefix_pos(const source_position_t source_position) fputs(": ", stderr); } -void parser_print_error_prefix_pos(const source_position_t source_position) +static void parser_print_error_prefix_pos( + const source_position_t source_position) { parser_print_prefix_pos(source_position); fputs("error: ", stderr); error(); } -void parser_print_error_prefix(void) +static void parser_print_error_prefix(void) { parser_print_prefix_pos(token.source_position); error(); @@ -2396,8 +2397,13 @@ static type_t *get_unexpr_arithmetic_type(const expression_t *expression) static type_t *get_unexpr_dereference_type(const expression_t *expression) { - (void) expression; - /* TODO... */ + type_t *expression_type = expression->datatype; + + if(expression_type->type == TYPE_POINTER) { + pointer_type_t *pointer_type = (pointer_type_t*) expression_type; + return pointer_type->points_to; + } + panic("deref TODO..."); return NULL; } @@ -2459,55 +2465,103 @@ CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT, CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT, get_unexpr_arithmetic_type) -#define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \ -static \ -expression_t *parse_##binexpression_type(unsigned precedence, \ - expression_t *left) \ -{ \ - eat(token_type); \ - \ - expression_t *right = parse_sub_expression(precedence); \ - \ - binary_expression_t *binexpr \ - = allocate_ast_zero(sizeof(binexpr[0])); \ - binexpr->expression.type = EXPR_BINARY; \ - binexpr->type = binexpression_type; \ - binexpr->left = left; \ - binexpr->right = right; \ - \ - return (expression_t*) binexpr; \ -} - -CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA) -CREATE_BINEXPR_PARSER('*', BINEXPR_MUL) -CREATE_BINEXPR_PARSER('/', BINEXPR_DIV) -CREATE_BINEXPR_PARSER('%', BINEXPR_MOD) -CREATE_BINEXPR_PARSER('+', BINEXPR_ADD) -CREATE_BINEXPR_PARSER('-', BINEXPR_SUB) -CREATE_BINEXPR_PARSER('<', BINEXPR_LESS) -CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER) -CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN) -CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL) -CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL) -CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL) -CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL) -CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND) -CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR) -CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR) -CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND) -CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR) -CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT) -CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT) -CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN) -CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN) -CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN) -CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN) -CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN) -CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN) -CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN) -CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN) -CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN) -CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN) +static type_t *get_binexpr_int_type(const expression_t *left, + const expression_t *right) +{ + (void) left; + (void) right; + return type_int; +} + +static type_t *get_binexpr_arithmetic_type(const expression_t *left, + const expression_t *right) +{ + (void) right; + return left->datatype; +} + +static type_t *get_binexpr_arithmetic_assign_type(const expression_t *left, + const expression_t *right) +{ + (void) right; + /* TODO */ + return left->datatype; +} + +static type_t *get_binexpr_right_type(const expression_t *left, + const expression_t *right) +{ + (void) left; + return right->datatype; +} + +#define CREATE_BINEXPR_PARSER(token_type, binexpression_type, tfunc) \ +static expression_t *parse_##binexpression_type(unsigned precedence, \ + expression_t *left) \ +{ \ + eat(token_type); \ + \ + expression_t *right = parse_sub_expression(precedence); \ + \ + binary_expression_t *binexpr \ + = allocate_ast_zero(sizeof(binexpr[0])); \ + binexpr->expression.type = EXPR_BINARY; \ + binexpr->type = binexpression_type; \ + binexpr->left = left; \ + binexpr->right = right; \ + binexpr->expression.datatype = tfunc(left, right); \ + \ + return (expression_t*) binexpr; \ +} + +CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, get_binexpr_right_type) +CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, get_binexpr_int_type) +CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, get_binexpr_int_type) +CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, get_binexpr_right_type) +CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, + get_binexpr_int_type) +CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL, + get_binexpr_int_type) +CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, + get_binexpr_int_type) +CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL, + get_binexpr_int_type) +CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, + get_binexpr_int_type) +CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, + get_binexpr_int_type) +CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT, + get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT, + get_binexpr_arithmetic_type) +CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN, + get_binexpr_arithmetic_assign_type) +CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN, + get_binexpr_arithmetic_assign_type) +CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN, + get_binexpr_arithmetic_assign_type) +CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN, + get_binexpr_arithmetic_assign_type) +CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN, + get_binexpr_arithmetic_assign_type) +CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN, + get_binexpr_arithmetic_assign_type) +CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN, + get_binexpr_arithmetic_assign_type) +CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN, + get_binexpr_arithmetic_assign_type) +CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN, + get_binexpr_arithmetic_assign_type) +CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN, + get_binexpr_arithmetic_assign_type) static expression_t *parse_sub_expression(unsigned precedence) { @@ -2556,8 +2610,8 @@ static expression_t *parse_expression(void) -void register_expression_parser(parse_expression_function parser, - int token_type, unsigned precedence) +static void register_expression_parser(parse_expression_function parser, + int token_type, unsigned precedence) { expression_parser_function_t *entry = &expression_parsers[token_type]; @@ -2571,8 +2625,9 @@ void register_expression_parser(parse_expression_function parser, entry->precedence = precedence; } -void register_expression_infix_parser(parse_expression_infix_function parser, - int token_type, unsigned precedence) +static void register_expression_infix_parser( + parse_expression_infix_function parser, int token_type, + unsigned precedence) { expression_parser_function_t *entry = &expression_parsers[token_type];