Skip to content

Commit fb100c0

Browse files
authored
Merge pull request #249 from sysprog21/struct-decl
Fix struct variable declaration crash in parser
2 parents 13e8800 + cc085a5 commit fb100c0

File tree

2 files changed

+160
-1
lines changed

2 files changed

+160
-1
lines changed

src/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3180,7 +3180,7 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
31803180
type = find_type(token, find_type_flag);
31813181
if (type) {
31823182
var = require_typed_var(parent, type);
3183-
read_full_var_decl(var, 0, 0);
3183+
read_partial_var_decl(var, NULL);
31843184
add_insn(parent, bb, OP_allocat, var, NULL, NULL, 0, NULL);
31853185
add_symbol(bb, var);
31863186
if (lex_accept(T_assign)) {

tests/driver.sh

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3756,4 +3756,163 @@ int main() {
37563756
}
37573757
EOF
37583758

3759+
# Struct Variable Declaration Tests (Bug Fix Validation)
3760+
echo "Testing struct variable declaration functionality..."
3761+
3762+
# Test 1: Basic struct variable declaration (the original bug case)
3763+
try_ 0 << EOF
3764+
struct point {
3765+
int x;
3766+
int y;
3767+
};
3768+
3769+
int main() {
3770+
struct point p;
3771+
return 0;
3772+
}
3773+
EOF
3774+
3775+
# Test 2: Struct variable declaration with initialization
3776+
try_ 42 << EOF
3777+
struct point {
3778+
int x;
3779+
int y;
3780+
};
3781+
3782+
int main() {
3783+
struct point p;
3784+
p.x = 20;
3785+
p.y = 22;
3786+
return p.x + p.y;
3787+
}
3788+
EOF
3789+
3790+
# Test 3: Multiple struct variable declarations
3791+
try_ 20 << EOF
3792+
struct point {
3793+
int x;
3794+
int y;
3795+
};
3796+
3797+
int main() {
3798+
struct point p1;
3799+
struct point p2;
3800+
p1.x = 10;
3801+
p1.y = 15;
3802+
p2.x = 3;
3803+
p2.y = 2;
3804+
return p1.x + p1.y - p2.x - p2.y;
3805+
}
3806+
EOF
3807+
3808+
# Test 4: Struct variable declaration in nested scope
3809+
try_ 100 << EOF
3810+
struct data {
3811+
int value;
3812+
};
3813+
3814+
int main() {
3815+
{
3816+
struct data d;
3817+
d.value = 100;
3818+
return d.value;
3819+
}
3820+
}
3821+
EOF
3822+
3823+
# Test 5: Struct with char fields
3824+
try_ 142 << EOF
3825+
struct character {
3826+
char first;
3827+
char second;
3828+
};
3829+
3830+
int main() {
3831+
struct character ch;
3832+
ch.first = 'A';
3833+
ch.second = 'M';
3834+
return ch.first + ch.second; /* 65 + 77 = 142 */
3835+
}
3836+
EOF
3837+
3838+
# Test 6: Struct with typedef (working pattern)
3839+
try_ 55 << EOF
3840+
typedef struct {
3841+
int data;
3842+
void *next;
3843+
} node_t;
3844+
3845+
int main() {
3846+
node_t n;
3847+
n.data = 55;
3848+
n.next = 0;
3849+
return n.data;
3850+
}
3851+
EOF
3852+
3853+
# Test 7: Struct with pointer arithmetic
3854+
try_ 25 << EOF
3855+
typedef struct {
3856+
int width;
3857+
int height;
3858+
} rect_t;
3859+
3860+
int main() {
3861+
rect_t rectangle;
3862+
rectangle.width = 5;
3863+
rectangle.height = 5;
3864+
return rectangle.width * rectangle.height;
3865+
}
3866+
EOF
3867+
3868+
# Test 8: Struct variable with multiple fields
3869+
try_ 15 << EOF
3870+
typedef struct {
3871+
int first;
3872+
int second;
3873+
} container_t;
3874+
3875+
int main() {
3876+
container_t c;
3877+
c.first = 10;
3878+
c.second = 5;
3879+
return c.first + c.second; /* 10 + 5 = 15 */
3880+
}
3881+
EOF
3882+
3883+
# Test 9: Simple struct array access
3884+
try_ 10 << EOF
3885+
typedef struct {
3886+
int x;
3887+
int y;
3888+
} point_t;
3889+
3890+
int main() {
3891+
point_t p;
3892+
p.x = 3;
3893+
p.y = 7;
3894+
return p.x + p.y; /* 3+7 = 10 */
3895+
}
3896+
EOF
3897+
3898+
# Test 10: Struct variable declaration mixed with other declarations
3899+
try_ 88 << EOF
3900+
typedef struct {
3901+
int x;
3902+
int y;
3903+
} coord_t;
3904+
3905+
int main() {
3906+
int a = 10;
3907+
coord_t pos;
3908+
int b = 20;
3909+
coord_t vel;
3910+
pos.x = 15;
3911+
pos.y = 18;
3912+
vel.x = 25;
3913+
vel.y = 20;
3914+
return a + b + pos.x + pos.y + vel.x; /* 10+20+15+18+25 = 88 */
3915+
}
3916+
EOF
3917+
37593918
echo OK

0 commit comments

Comments
 (0)