commit fdd210a1d7ee0a95479b7394e768ea88d678eae7
parent f0491c9fd00c072db17890f4dc4f0516771f96fc
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Tue, 29 Nov 2022 19:43:46 -0500
comp/c: allow constant expressions in array typedefs
Diffstat:
3 files changed, 9 insertions(+), 10 deletions(-)
diff --git a/fs/ar/puff.c b/fs/ar/puff.c
@@ -31,8 +31,6 @@
*/
/* maximum bits in a code */
#define MAXBITS 15
-/* TODO: allow constant expressions array typedefs, for example MAXBITS+1 */
-#define MAXBITSPLUSONE 16
/* maximum number of literal/length codes */
#define MAXLCODES 286
/* maximum number of distance codes */
@@ -250,7 +248,7 @@ static int construct(huffman *h, short *length, int n)
int symbol; /* current symbol when stepping through length[] */
int len; /* current length when stepping through h->count[] */
int left; /* number of possible codes left of current length */
- short offs[MAXBITSPLUSONE]; /* offsets in symbol table for each length */
+ short offs[MAXBITS+1]; /* offsets in symbol table for each length */
/* count number of codes of each length */
for (len = 0; len <= MAXBITS; len++)
@@ -440,8 +438,8 @@ static int codes(state *s, huffman *lencode, huffman *distcode)
*/
static int virgin = 1;
-static short lencnt[MAXBITSPLUSONE], lensym[FIXLCODES];
-static short distcnt[MAXBITSPLUSONE], distsym[MAXDCODES];
+static short lencnt[MAXBITS+1], lensym[FIXLCODES];
+static short distcnt[MAXBITS+1], distsym[MAXDCODES];
static huffman lencode, distcode;
static int fixed(state *s)
@@ -578,8 +576,8 @@ static int dynamic(state *s)
int index; /* index of lengths[] */
int err; /* construct() return value */
short lengths[MAXCODES]; /* descriptor code lengths */
- short lencnt[MAXBITSPLUSONE], lensym[MAXLCODES]; /* lencode memory */
- short distcnt[MAXBITSPLUSONE], distsym[MAXDCODES]; /* distcode memory */
+ short lencnt[MAXBITS+1], lensym[MAXLCODES]; /* lencode memory */
+ short distcnt[MAXBITS+1], distsym[MAXDCODES]; /* distcode memory */
huffman lencode, distcode; /* length and distance codes */
int symbol; /* decoded value */
int len; /* last length to repeat */
diff --git a/fs/comp/c/pgen.fs b/fs/comp/c/pgen.fs
@@ -90,6 +90,7 @@ BOPSCNT wordtbl bopgentbl ( -- )
alias _err parseType ( tok -- type? f ) \ forward declaration
alias _err parseDeclarator ( type -- ctype ) \ forward declaration
+alias noop parseExpression ( tok -- ) \ forward declaration
\ Parsing strategy: we dig down recursively through nextt until we get to our
\ identifier. Before that identifier, we can hit chars like ( and *.
@@ -113,7 +114,8 @@ alias _err parseDeclarator ( type -- ctype ) \ forward declaration
: _post ( ctype -- ctype )
begin ( ctype ) nextt case
'[' of isChar?^
- nextt parse _assert ( ctype nbelem )
+ nextt parseExpression
+ vmop^ :noop# vmop :isconst# vmop arg ( ctype nbelem )
nextt ']' expectChar ( ctype nbelem )
over to CType nbelem endof
'(' of isChar?^
@@ -176,7 +178,6 @@ current to parseDeclarator
then ;
current to parseType
-alias noop parseExpression ( tok -- ) \ forward declaration
alias noop parseFactor ( tok -- ) \ forward declaration
\ we have a func call and its target in in vmop
diff --git a/fs/tests/comp/c/test.c b/fs/tests/comp/c/test.c
@@ -134,7 +134,7 @@ int array() {
}
#define GLOB2SZ 3
-static int global1 = 1234, global2[GLOB2SZ] = {4, 5, 6};
+static int global1 = 1234, global2[GLOB2SZ+1-1] = {4, 5, 6};
int global() {
return global1;