From 5dc6889265cb5f0c385778f075279ed8012023a0 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sun, 15 Aug 2021 16:05:11 +0000 Subject: added NULL and EOF (-1 as emum initialized in c4) --- miniany/c4.c | 10 ++++++++-- miniany/cc.c | 16 ++++++++++++++-- miniany/libc-freestanding.c | 7 +------ miniany/test1.c | 6 ++++++ 4 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 miniany/test1.c diff --git a/miniany/c4.c b/miniany/c4.c index a6d24d3..a36fb4a 100644 --- a/miniany/c4.c +++ b/miniany/c4.c @@ -357,7 +357,7 @@ int main(int argc, char **argv) { int fd, bt, ty, poolsz, *idmain; int *pc, *sp, *bp, a, cycle; // vm registers - int i, *t; // temps + int i, *t, neg; // temps --argc; ++argv; if (argc > 0 && **argv == '-' && (*argv)[1] == 's') { src = 1; --argc; ++argv; } @@ -377,13 +377,14 @@ int main(int argc, char **argv) memset(data, 0, poolsz); p = "char else enum if int return sizeof do while " - "EOF EXIT_SUCCESS EXIT_FAILURE " + "EOF EXIT_SUCCESS EXIT_FAILURE NULL " "open read close printf malloc free memset memcmp getchar putstring putnl putchar putint isspace isdigit isalnum isalpha strcmp exit void main"; i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table // add library constants next(); id[Class] = Num; id[Type] = INT; id[Val] = -1; next(); id[Class] = Num; id[Type] = INT; id[Val] = 0; next(); id[Class] = Num; id[Type] = INT; id[Val] = 1; + next(); id[Class] = Num; id[Type] = INT; id[Val] = (int)NULL; i = OPEN; while (i <= EXIT) { next(); id[Class] = Sys; id[Type] = INT; id[Val] = i++; } // add library to symbol table next(); id[Tk] = Char; // handle void type next(); idmain = id; // keep track of main @@ -411,8 +412,13 @@ int main(int argc, char **argv) next(); if (tk == Assign) { next(); + neg = 0; + if (tk == Sub) { next(); neg =1; } if (tk != Num) { printf("%d: bad enum initializer\n", line); return -1; } i = ival; + if (neg) { + i = -ival; + } next(); } id[Class] = Num; id[Type] = INT; id[Val] = i++; diff --git a/miniany/cc.c b/miniany/cc.c index ebdebdb..e5b30c7 100644 --- a/miniany/cc.c +++ b/miniany/cc.c @@ -8,8 +8,7 @@ int token; int DEBUG_SCANNER; enum { - MAX_IDENT_LEN = 20, - TEST = -1 + MAX_IDENT_LEN = 20 }; void pushBack( int c ) @@ -254,11 +253,23 @@ void parseExpression( ) } } +//~ struct Symbol { + //~ char *name; +//~ }; + +//~ struct Symbol *symbol; + +void newSymbol( char *s ) +{ + +} + void parseDeclaration( ) { expect( S_INT, "int" ); expect( S_IDENT, "identifier" ); putstring( "Adding glob: " ); putstring( ident ); putnl( ); + newSymbol( ident ); expect( S_SEMICOLON, ";" ); } @@ -287,6 +298,7 @@ int main( int argc, char **argv ) row = 1; pushback = 0; DEBUG_SCANNER = 1; + //~ symbol = NULL; ident = "12345678901234567890"; token = getToken( ); diff --git a/miniany/libc-freestanding.c b/miniany/libc-freestanding.c index ffed093..fdec615 100644 --- a/miniany/libc-freestanding.c +++ b/miniany/libc-freestanding.c @@ -65,13 +65,8 @@ enum { SYSCALL_WRITE = 4 }; -/* EOF = -1 is a const expression for c4 */ -/* EOF = 0xFFFFFFFF clang doesn't like this */ -/* EOF = 0xFFFFFFFF gcc overflow in conversion from 'enum ' to 'int' changes value from '4294967295' to '-1' */ -/* EOF = 256, this is not standard, but doesn't really matter in freestanding mode, - * code should really not relly on the fact what the value of EOF is (-1 usually) */ enum { - EOF = 256 + EOF = -1 }; int errno; diff --git a/miniany/test1.c b/miniany/test1.c new file mode 100644 index 0000000..ba8dd4f --- /dev/null +++ b/miniany/test1.c @@ -0,0 +1,6 @@ +/* test1 */ + +int i; +int i; + +i = 12+25/5-2*3; // another comment -- cgit v1.2.3-54-g00ecf