@@ -1194,8 +1194,11 @@ void read_inner_var_decl(var_t *vd, bool anon, bool is_param)
11941194 vd -> init_val = 0 ;
11951195 /* Preserve typedef pointer level - don't reset if already inherited */
11961196
1197- while (lex_accept (T_asterisk ))
1197+ while (lex_accept (T_asterisk )) {
11981198 vd -> ptr_level ++ ;
1199+ /* Skip const after asterisk for now */
1200+ lex_accept (T_const );
1201+ }
11991202
12001203 /* is it function pointer declaration? */
12011204 if (lex_accept (T_open_bracket )) {
@@ -1281,6 +1284,11 @@ void read_inner_var_decl(var_t *vd, bool anon, bool is_param)
12811284/* starting next_token, need to check the type */
12821285void read_full_var_decl (var_t * vd , bool anon , bool is_param )
12831286{
1287+ /* Skip const qualifier if present */
1288+ bool is_const = false;
1289+ if (lex_accept (T_const ))
1290+ is_const = true;
1291+
12841292 char type_name [MAX_TYPE_LEN ];
12851293 int find_type_flag = lex_accept (T_struct ) ? 2 : 1 ;
12861294 if (find_type_flag == 1 && lex_accept (T_union )) {
@@ -1296,6 +1304,8 @@ void read_full_var_decl(var_t *vd, bool anon, bool is_param)
12961304 }
12971305
12981306 vd -> type = type ;
1307+ if (is_const )
1308+ vd -> is_const_qualified = true;
12991309
13001310 read_inner_var_decl (vd , anon , is_param );
13011311}
@@ -1579,8 +1589,37 @@ void handle_single_dereference(block_t *parent, basic_block_t **bb)
15791589 var_t * var = find_var (token , parent );
15801590 lex_expect (T_identifier );
15811591
1582- /* Push the variable onto the stack */
1583- opstack_push (var );
1592+ /* Check for postfix operators on the pointer before dereferencing
1593+ */
1594+ if (lex_peek (T_increment , NULL ) || lex_peek (T_decrement , NULL )) {
1595+ /* Handle p++ or p-- before dereferencing */
1596+ opcode_t postfix_op = lex_accept (T_increment ) ? OP_add : OP_sub ;
1597+
1598+ /* Push original pointer value for dereference */
1599+ var_t * orig_ptr = require_var (parent );
1600+ gen_name_to (orig_ptr -> var_name );
1601+ add_insn (parent , * bb , OP_assign , orig_ptr , var , NULL , 0 , NULL );
1602+
1603+ /* Create increment value (pointer size) */
1604+ var_t * inc_val = require_var (parent );
1605+ gen_name_to (inc_val -> var_name );
1606+ inc_val -> init_val = var -> type ? var -> type -> size : PTR_SIZE ;
1607+ add_insn (parent , * bb , OP_load_constant , inc_val , NULL , NULL , 0 ,
1608+ NULL );
1609+
1610+ /* Increment the pointer */
1611+ var_t * new_ptr = require_var (parent );
1612+ gen_name_to (new_ptr -> var_name );
1613+ add_insn (parent , * bb , postfix_op , new_ptr , var , inc_val , 0 ,
1614+ NULL );
1615+ add_insn (parent , * bb , OP_assign , var , new_ptr , NULL , 0 , NULL );
1616+
1617+ /* Use original pointer value for dereference */
1618+ opstack_push (orig_ptr );
1619+ } else {
1620+ /* Push the variable onto the stack */
1621+ opstack_push (var );
1622+ }
15841623
15851624 /* Apply dereference */
15861625 rs1 = opstack_pop ();
@@ -1812,9 +1851,28 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
18121851 int cast_ptr_level = 0 ;
18131852
18141853 /* Look ahead to see if we have a typename followed by ) */
1854+ /* Skip const qualifier if present in cast */
1855+ bool has_const_in_cast = false;
1856+ if (lex_peek (T_const , NULL )) {
1857+ has_const_in_cast = true;
1858+ lex_accept (T_const );
1859+ }
1860+
1861+ /* Check for struct/union after const */
1862+ bool is_struct = false;
1863+ bool is_union = false;
1864+ if (lex_peek (T_struct , NULL )) {
1865+ is_struct = true;
1866+ lex_accept (T_struct );
1867+ } else if (lex_peek (T_union , NULL )) {
1868+ is_union = true;
1869+ lex_accept (T_union );
1870+ }
1871+
18151872 if (lex_peek (T_identifier , lookahead_token )) {
18161873 /* Check if it's a basic type or typedef */
1817- type_t * type = find_type (lookahead_token , true);
1874+ int find_flag = (is_struct | is_union ) ? 2 : 1 ;
1875+ type_t * type = find_type (lookahead_token , find_flag );
18181876
18191877 if (type ) {
18201878 /* Save current position to backtrack if needed */
@@ -4334,6 +4392,33 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
43344392 prefix_op = OP_add ;
43354393 else if (lex_accept (T_decrement ))
43364394 prefix_op = OP_sub ;
4395+
4396+ /* Check for parenthesized expression as statement */
4397+ if (lex_peek (T_open_bracket , NULL )) {
4398+ /* This could be a cast or parenthesized expression */
4399+ read_expr (parent , & bb );
4400+ read_ternary_operation (parent , & bb );
4401+
4402+ /* Handle postfix operators if present */
4403+ if (lex_peek (T_increment , NULL ) || lex_peek (T_decrement , NULL )) {
4404+ /* For now, just consume the token to avoid crash */
4405+ /* TODO: Implement proper postfix handling for expressions */
4406+ if (lex_accept (T_increment )) {
4407+ /* Increment - not implemented yet */
4408+ } else if (lex_accept (T_decrement )) {
4409+ /* Decrement - not implemented yet */
4410+ }
4411+ }
4412+
4413+ lex_expect (T_semicolon );
4414+ return bb ;
4415+ }
4416+
4417+ /* Check for const qualifier */
4418+ bool has_const = false;
4419+ if (lex_accept (T_const ))
4420+ has_const = true;
4421+
43374422 /* must be an identifier or asterisk (for pointer dereference) */
43384423 bool has_asterisk = lex_peek (T_asterisk , NULL );
43394424 if (!lex_peek (T_identifier , token ) && !has_asterisk )
@@ -4409,6 +4494,8 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
44094494 if (type ) {
44104495 var = require_typed_var (parent , type );
44114496 read_full_var_decl (var , false, false);
4497+ if (has_const )
4498+ var -> is_const_qualified = true;
44124499 add_insn (parent , bb , OP_allocat , var , NULL , NULL , 0 , NULL );
44134500 add_symbol (bb , var );
44144501 if (lex_accept (T_assign )) {
@@ -5131,6 +5218,11 @@ void read_global_statement(void)
51315218
51325219 lex_expect (T_semicolon );
51335220 } else {
5221+ /* Skip const qualifier if present */
5222+ bool typedef_is_const = false;
5223+ if (lex_accept (T_const ))
5224+ typedef_is_const = true;
5225+
51345226 char base_type [MAX_TYPE_LEN ];
51355227 type_t * base ;
51365228 type_t * type = add_type ();
@@ -5164,6 +5256,7 @@ void parse_internal(void)
51645256 GLOBAL_FUNC = add_func ("" , true);
51655257 GLOBAL_FUNC -> stack_size = 4 ;
51665258 GLOBAL_FUNC -> bbs = arena_calloc (BB_ARENA , 1 , sizeof (basic_block_t ));
5259+ GLOBAL_FUNC -> bbs -> belong_to = GLOBAL_FUNC ; /* Set the belong_to pointer */
51675260
51685261 /* built-in types */
51695262 TY_void = add_named_type ("void" );
0 commit comments