diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 034dd019..824b630d 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.22 2006-01-18 15:16:01 bacon Exp $ + * $Id: parse.c,v 1.23 2006-01-18 16:12:58 bacon Exp $ */ #include @@ -261,14 +261,16 @@ static xp_awk_node_t* __parse_block (xp_awk_t* awk) break; } +/* if you want to remove top-level null statement... get it here... */ +/* if (MATCH(awk,TOKEN_SEMICOLON)) { - /* null statement */ if (__get_token(awk) == -1) { if (head != XP_NULL) xp_awk_clrpt (head); return XP_NULL; } continue; } +*/ node = __parse_statement (awk); if (node == XP_NULL) { @@ -298,7 +300,20 @@ static xp_awk_node_t* __parse_statement (xp_awk_t* awk) { xp_awk_node_t* node; - if (MATCH(awk,TOKEN_LBRACE)) { + if (MATCH(awk,TOKEN_SEMICOLON)) { + /* null statement */ + node = (xp_awk_node_t*) xp_malloc (xp_sizeof(xp_awk_node_t)); + if (node == XP_NULL) PANIC (awk, XP_AWK_ENOMEM); + + node->type = XP_AWK_NODE_NULL; + node->next = XP_NULL; + + if (__get_token(awk) == -1) { + xp_free (node); + return XP_NULL; + } + } + else if (MATCH(awk,TOKEN_LBRACE)) { if (__get_token(awk) == -1) return XP_NULL; node = __parse_block (awk); } @@ -316,19 +331,19 @@ static xp_awk_node_t* __parse_statement_nb (xp_awk_t* awk) */ if (MATCH(awk,TOKEN_IF)) { if (__get_token(awk) == -1) return XP_NULL; - return __parse_if(awk); + return __parse_if (awk); } else if (MATCH(awk,TOKEN_WHILE)) { if (__get_token(awk) == -1) return XP_NULL; - return __parse_while(awk); + return __parse_while (awk); } else if (MATCH(awk,TOKEN_FOR)) { if (__get_token(awk) == -1) return XP_NULL; - return __parse_for(awk); + return __parse_for (awk); } else if (MATCH(awk,TOKEN_DO)) { if (__get_token(awk) == -1) return XP_NULL; - return __parse_do(awk); + return __parse_do (awk); } /* @@ -693,8 +708,11 @@ static xp_awk_node_t* __parse_funcall (xp_awk_t* awk, xp_char_t* name) head = curr = XP_NULL; - if (!MATCH(awk,TOKEN_RPAREN)) { - + if (MATCH(awk,TOKEN_RPAREN)) { + /* no parameters to the function call */ + if (__get_token(awk) == -1) return XP_NULL; + } + else { while (1) { node = __parse_expression (awk); if (node == XP_NULL) { @@ -805,7 +823,44 @@ static xp_awk_node_t* __parse_if (xp_awk_t* awk) static xp_awk_node_t* __parse_while (xp_awk_t* awk) { - return XP_NULL; + xp_awk_node_t* test, * body; + xp_awk_node_while_t* node; + + if (!MATCH(awk,TOKEN_LPAREN)) PANIC (awk, XP_AWK_ELPAREN); + if (__get_token(awk) == -1) return XP_NULL; + + test = __parse_expression (awk); + if (test == XP_NULL) return XP_NULL; + + if (!MATCH(awk,TOKEN_RPAREN)) { + xp_awk_clrpt (test); + PANIC (awk, XP_AWK_ERPAREN); + } + + if (__get_token(awk) == -1) { + xp_awk_clrpt (test); + return XP_NULL; + } + + body = __parse_statement (awk); + if (body == XP_NULL) { + xp_awk_clrpt (test); + return XP_NULL; + } + + node = (xp_awk_node_while_t*) xp_malloc (xp_sizeof(xp_awk_node_while_t)); + if (node == XP_NULL) { + xp_awk_clrpt (body); + xp_awk_clrpt (test); + PANIC (awk, XP_AWK_ENOMEM); + } + + node->type = XP_AWK_NODE_WHILE; + node->next = XP_NULL; + node->test = test; + node->body = body; + + return (xp_awk_node_t*)node; } static xp_awk_node_t* __parse_for (xp_awk_t* awk) diff --git a/ase/awk/tree.c b/ase/awk/tree.c index 503f288c..deeec3bd 100644 --- a/ase/awk/tree.c +++ b/ase/awk/tree.c @@ -1,5 +1,5 @@ /* - * $Id: tree.c,v 1.4 2006-01-18 15:16:01 bacon Exp $ + * $Id: tree.c,v 1.5 2006-01-18 16:12:58 bacon Exp $ */ #include @@ -99,6 +99,11 @@ static void __print_statements (xp_awk_node_t* tree, int depth) while (p != XP_NULL) { switch (p->type) { + case XP_AWK_NODE_NULL: + __print_tabs (depth); + xp_printf (XP_TEXT(";\n")); + break; + case XP_AWK_NODE_BLOCK: __print_tabs (depth); xp_printf (XP_TEXT("{\n")); @@ -114,9 +119,8 @@ static void __print_statements (xp_awk_node_t* tree, int depth) xp_printf (XP_TEXT(")\n")); // TODO: identation of depth + 1 if then_part or else_part is not a block - if (((xp_awk_node_if_t*)p)->then_part == XP_NULL) - xp_printf (XP_TEXT(";\n")); - else __print_statements (((xp_awk_node_if_t*)p)->then_part, depth); + xp_assert (((xp_awk_node_if_t*)p)->then_part != XP_NULL); + __print_statements (((xp_awk_node_if_t*)p)->then_part, depth); if (((xp_awk_node_if_t*)p)->else_part != XP_NULL) { __print_tabs (depth); @@ -124,6 +128,16 @@ static void __print_statements (xp_awk_node_t* tree, int depth) __print_statements (((xp_awk_node_if_t*)p)->else_part, depth); } break; + case XP_AWK_NODE_WHILE: + __print_tabs (depth); + xp_printf (XP_TEXT("while (")); + __print_expr_node (((xp_awk_node_while_t*)p)->test); + xp_printf (XP_TEXT(")\n")); + + if (((xp_awk_node_while_t*)p)->body == XP_NULL) + xp_printf (XP_TEXT(";\n")); + else __print_statements (((xp_awk_node_while_t*)p)->body, depth); + break; case XP_AWK_NODE_BREAK: __print_tabs (depth); @@ -189,6 +203,10 @@ void xp_awk_clrpt (xp_awk_node_t* tree) next = p->next; switch (p->type) { + case XP_AWK_NODE_NULL: + xp_free (p); + break; + case XP_AWK_NODE_BLOCK: xp_awk_clrpt (((xp_awk_node_block_t*)p)->body); xp_free (p); @@ -203,6 +221,13 @@ void xp_awk_clrpt (xp_awk_node_t* tree) xp_free (p); break; + case XP_AWK_NODE_WHILE: + xp_awk_clrpt (((xp_awk_node_while_t*)p)->test); + if (((xp_awk_node_while_t*)p)->body != XP_NULL) + xp_awk_clrpt (((xp_awk_node_while_t*)p)->body); + xp_free (p); + break; + case XP_AWK_NODE_BREAK: case XP_AWK_NODE_CONTINUE: xp_free (p); diff --git a/ase/awk/tree.h b/ase/awk/tree.h index 3d4b087e..3344b91e 100644 --- a/ase/awk/tree.h +++ b/ase/awk/tree.h @@ -1,5 +1,5 @@ /* - * $Id: tree.h,v 1.9 2006-01-14 16:09:58 bacon Exp $ + * $Id: tree.h,v 1.10 2006-01-18 16:12:58 bacon Exp $ */ #ifndef _XP_AWK_TREE_H_ @@ -7,6 +7,7 @@ enum { + XP_AWK_NODE_NULL, XP_AWK_NODE_BLOCK, XP_AWK_NODE_BREAK, XP_AWK_NODE_CONTINUE, @@ -19,7 +20,8 @@ enum XP_AWK_NODE_NUM, XP_AWK_NODE_VAR, XP_AWK_NODE_CALL, - XP_AWK_NODE_IF + XP_AWK_NODE_IF, + XP_AWK_NODE_WHILE }; typedef struct xp_awk_node_t xp_awk_node_t; diff --git a/ase/test/awk/Makefile.cl b/ase/test/awk/Makefile.cl index 37d3f767..10fd11aa 100644 --- a/ase/test/awk/Makefile.cl +++ b/ase/test/awk/Makefile.cl @@ -1,7 +1,9 @@ CC = cl -CFLAGS = /nologo /MT /GX /W3 /GR- /D_WIN32_WINNT=0x0400 -I..\..\.. +#CFLAGS = /nologo /MT /W3 /GR- /D_WIN32_WINNT=0x0400 -I..\..\.. +CFLAGS = /nologo /MT /W3 /GR- /D_WIN32_WINNT=0x0400 -I..\..\.. -D__STAND_ALONE LDFLAGS = /libpath:..\..\bas /libpath:..\..\awk -LIBS = xpbas.lib xpawk.lib +#LIBS = xpbas.lib xpawk.lib +LIBS = xpawk.lib all: awk diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index e5ee5ba8..793425be 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -1,4 +1,29 @@ #include + +#ifdef __STAND_ALONE + +static xp_ssize_t process_source (int cmd, void* arg, xp_char_t* data, xp_size_t size) +{ + wchar_t c; + + switch (cmd) { + case XP_AWK_IO_OPEN: + case XP_AWK_IO_CLOSE: + return 0; + + case XP_AWK_IO_DATA: + if (size < 0) return -1; + c = fgetwc (stdin); + if (c == XP_CHAR_EOF) return 0; + *data = c; + return 1; + } + + return -1; +} + +#else + #include #include @@ -21,6 +46,7 @@ static xp_ssize_t process_source (int cmd, void* arg, xp_char_t* data, xp_size_t return -1; } +#endif int xp_main (int argc, xp_char_t* argv[]) { @@ -28,25 +54,25 @@ int xp_main (int argc, xp_char_t* argv[]) #if 0 if (argc != 2) { - xp_fprintf (xp_stderr, XP_TEXT("Usage: %s file\n"), argv[0]); + xp_printf (XP_TEXT("Usage: %s file\n"), argv[0]); return -1; } #endif if (xp_awk_open(&awk) == XP_NULL) { - xp_fprintf (xp_stderr, XP_TEXT("Error: cannot open awk\n")); + xp_printf (XP_TEXT("Error: cannot open awk\n")); return -1; } if (xp_awk_attsrc(&awk, process_source, XP_NULL) == -1) { xp_awk_close (&awk); - xp_fprintf (xp_stderr, XP_TEXT("error: cannot attach source\n")); + xp_printf (XP_TEXT("error: cannot attach source\n")); return -1; } if (xp_awk_parse(&awk) == -1) { xp_awk_close (&awk); - xp_fprintf (xp_stderr, XP_TEXT("error: cannot parse program\n")); + xp_printf (XP_TEXT("error: cannot parse program\n")); return -1; }