@@ -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 */
@@ -4447,6 +4505,33 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
44474505 prefix_op = OP_add ;
44484506 else if (lex_accept (T_decrement ))
44494507 prefix_op = OP_sub ;
4508+
4509+ /* Check for parenthesized expression as statement */
4510+ if (lex_peek (T_open_bracket , NULL )) {
4511+ /* This could be a cast or parenthesized expression */
4512+ read_expr (parent , & bb );
4513+ read_ternary_operation (parent , & bb );
4514+
4515+ /* Handle postfix operators if present */
4516+ if (lex_peek (T_increment , NULL ) || lex_peek (T_decrement , NULL )) {
4517+ /* For now, just consume the token to avoid crash */
4518+ /* TODO: Implement proper postfix handling for expressions */
4519+ if (lex_accept (T_increment )) {
4520+ /* Increment - not implemented yet */
4521+ } else if (lex_accept (T_decrement )) {
4522+ /* Decrement - not implemented yet */
4523+ }
4524+ }
4525+
4526+ lex_expect (T_semicolon );
4527+ return bb ;
4528+ }
4529+
4530+ /* Check for const qualifier */
4531+ bool has_const = false;
4532+ if (lex_accept (T_const ))
4533+ has_const = true;
4534+
44504535 /* must be an identifier or asterisk (for pointer dereference) */
44514536 bool has_asterisk = lex_peek (T_asterisk , NULL );
44524537 if (!lex_peek (T_identifier , token ) && !has_asterisk )
@@ -4522,14 +4607,15 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
45224607 if (type ) {
45234608 var = require_typed_var (parent , type );
45244609 read_full_var_decl (var , false, false);
4610+ if (has_const )
4611+ var -> is_const_qualified = true;
45254612 add_insn (parent , bb , OP_allocat , var , NULL , NULL , 0 , NULL );
45264613 add_symbol (bb , var );
45274614 if (lex_accept (T_assign )) {
45284615 if (lex_peek (T_open_curly , NULL ) &&
45294616 (var -> array_size > 0 || var -> ptr_level > 0 )) {
4530- parse_array_init (
4531- var , parent , & bb ,
4532- 1 ); /* FIXED: Emit code for locals in functions */
4617+ /* Emit code for locals in functions */
4618+ parse_array_init (var , parent , & bb , 1 );
45334619 } else if (lex_peek (T_open_curly , NULL ) &&
45344620 (var -> type -> base_type == TYPE_struct ||
45354621 var -> type -> base_type == TYPE_typedef )) {
@@ -5245,6 +5331,11 @@ void read_global_statement(void)
52455331
52465332 lex_expect (T_semicolon );
52475333 } else {
5334+ /* Skip const qualifier if present */
5335+ bool typedef_is_const = false;
5336+ if (lex_accept (T_const ))
5337+ typedef_is_const = true;
5338+
52485339 char base_type [MAX_TYPE_LEN ];
52495340 type_t * base ;
52505341 type_t * type = add_type ();
@@ -5278,6 +5369,7 @@ void parse_internal(void)
52785369 GLOBAL_FUNC = add_func ("" , true);
52795370 GLOBAL_FUNC -> stack_size = 4 ;
52805371 GLOBAL_FUNC -> bbs = arena_calloc (BB_ARENA , 1 , sizeof (basic_block_t ));
5372+ GLOBAL_FUNC -> bbs -> belong_to = GLOBAL_FUNC ; /* Set the belong_to pointer */
52815373
52825374 /* built-in types */
52835375 TY_void = add_named_type ("void" );
0 commit comments