Commit 8ce5f7c
committed
Support complex pointer arithmetic in dereference
Previously, the parser only handled simple identifier dereference (*var)
and would crash on complex expressions like *(ptr + offset). This change
extends the dereference operator handling to accept general expressions
in parentheses.
This enables compilation of previously failing patterns:
- *(p + 4) - direct offset
- *(p + i + 2) - variable in expression
- *(p + i * 2) - arithmetic in expression
It also handles consecutive asterisks ('**pp', '***ppp') by counting
dereference levels and applying them iteratively.
When expressions like arr[0] + arr[1] + arr[2] were parsed, the compiler
was incorrectly applying pointer arithmetic scaling to the values read
from the array elements, resulting in wrong calculations.
The issue was in read_lvalue() which was handling the '+' operator after
array indexing as if it were pointer arithmetic. After arr[0], we have
an integer value, not a pointer, so the '+' should be handled by the
expression parser, not by read_lvalue.
This fix adds a check to ensure pointer arithmetic handling only occurs
when we have a pointer/array that hasn't been dereferenced (i.e., when
lvalue->is_reference is false).
Test case that was failing:
int arr[3] = {10, 20, 12};
return arr[0] + arr[1] + arr[2]; // Was returning 26 instead of 421 parent f2d99dd commit 8ce5f7c
2 files changed
+456
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
964 | 964 | | |
965 | 965 | | |
966 | 966 | | |
967 | | - | |
968 | | - | |
969 | | - | |
970 | | - | |
971 | | - | |
972 | | - | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
973 | 970 | | |
974 | 971 | | |
| 972 | + | |
975 | 973 | | |
976 | | - | |
977 | | - | |
978 | | - | |
979 | | - | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
980 | 986 | | |
981 | 987 | | |
982 | | - | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
983 | 1045 | | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
984 | 1050 | | |
985 | 1051 | | |
986 | 1052 | | |
| |||
1513 | 1579 | | |
1514 | 1580 | | |
1515 | 1581 | | |
1516 | | - | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
1517 | 1588 | | |
1518 | 1589 | | |
1519 | 1590 | | |
| |||
1916 | 1987 | | |
1917 | 1988 | | |
1918 | 1989 | | |
1919 | | - | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
1920 | 1994 | | |
1921 | 1995 | | |
1922 | 1996 | | |
| |||
0 commit comments