diff --git a/ase/awk/awk.h b/ase/awk/awk.h index 1c858919..0366dc3e 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.13 2006-01-18 15:16:01 bacon Exp $ + * $Id: awk.h,v 1.14 2006-01-19 10:56:34 bacon Exp $ */ #ifndef _XP_AWK_AWK_H_ @@ -32,7 +32,9 @@ enum XP_AWK_ERPAREN, /* right parenthesis expected */ XP_AWK_ECOMMA, /* comma expected */ XP_AWK_ESEMICOLON, /* semicolon expected */ - XP_AWK_EEXPR /* expression expected */ + XP_AWK_EEXPR, /* expression expected */ + + XP_AWK_EWHILE /* keyword 'while' is expected */ }; /* diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 824b630d..268072c0 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.23 2006-01-18 16:12:58 bacon Exp $ + * $Id: parse.c,v 1.24 2006-01-19 10:56:35 bacon Exp $ */ #include @@ -88,7 +88,7 @@ static xp_awk_node_t* __parse_funcall (xp_awk_t* awk, xp_char_t* name); static xp_awk_node_t* __parse_if (xp_awk_t* awk); static xp_awk_node_t* __parse_while (xp_awk_t* awk); static xp_awk_node_t* __parse_for (xp_awk_t* awk); -static xp_awk_node_t* __parse_do (xp_awk_t* awk); +static xp_awk_node_t* __parse_dowhile (xp_awk_t* awk); static xp_awk_node_t* __parse_break (xp_awk_t* awk); static xp_awk_node_t* __parse_continue (xp_awk_t* awk); static xp_awk_node_t* __parse_return (xp_awk_t* awk); @@ -341,15 +341,15 @@ static xp_awk_node_t* __parse_statement_nb (xp_awk_t* awk) if (__get_token(awk) == -1) return XP_NULL; return __parse_for (awk); } - else if (MATCH(awk,TOKEN_DO)) { - if (__get_token(awk) == -1) return XP_NULL; - return __parse_do (awk); - } /* * keywords that require a terminating semicolon */ - if (MATCH(awk,TOKEN_BREAK)) { + if (MATCH(awk,TOKEN_DO)) { + if (__get_token(awk) == -1) return XP_NULL; + node = __parse_dowhile (awk); + } + else if (MATCH(awk,TOKEN_BREAK)) { if (__get_token(awk) == -1) return XP_NULL; node = __parse_break(awk); } @@ -868,9 +868,65 @@ static xp_awk_node_t* __parse_for (xp_awk_t* awk) return XP_NULL; } -static xp_awk_node_t* __parse_do (xp_awk_t* awk) +static xp_awk_node_t* __parse_dowhile (xp_awk_t* awk) { - return XP_NULL; + xp_awk_node_t* test, * body; + xp_awk_node_while_t* node; + + body = __parse_statement (awk); + if (body == XP_NULL) return XP_NULL; + + if (!MATCH(awk,TOKEN_WHILE)) { + xp_awk_clrpt (body); + PANIC (awk, XP_AWK_EWHILE); + } + + if (__get_token(awk) == -1) { + xp_awk_clrpt (body); + return XP_NULL; + } + + if (!MATCH(awk,TOKEN_LPAREN)) { + xp_awk_clrpt (body); + PANIC (awk, XP_AWK_ELPAREN); + } + + if (__get_token(awk) == -1) { + xp_awk_clrpt (body); + return XP_NULL; + } + + test = __parse_expression (awk); + if (test == XP_NULL) { + xp_awk_clrpt (body); + return XP_NULL; + } + + if (!MATCH(awk,TOKEN_RPAREN)) { + xp_awk_clrpt (body); + xp_awk_clrpt (test); + PANIC (awk, XP_AWK_ERPAREN); + } + + if (__get_token(awk) == -1) { + xp_awk_clrpt (body); + 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_DOWHILE; + node->next = XP_NULL; + node->test = test; + node->body = body; + + return (xp_awk_node_t*)node; } static xp_awk_node_t* __parse_break (xp_awk_t* awk) diff --git a/ase/awk/tree.c b/ase/awk/tree.c index deeec3bd..74b219f8 100644 --- a/ase/awk/tree.c +++ b/ase/awk/tree.c @@ -1,5 +1,5 @@ /* - * $Id: tree.c,v 1.5 2006-01-18 16:12:58 bacon Exp $ + * $Id: tree.c,v 1.6 2006-01-19 10:56:35 bacon Exp $ */ #include @@ -118,14 +118,19 @@ static void __print_statements (xp_awk_node_t* tree, int depth) __print_expr_node (((xp_awk_node_if_t*)p)->test); xp_printf (XP_TEXT(")\n")); -// TODO: identation of depth + 1 if then_part or else_part is not a block 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)->then_part->type == XP_AWK_NODE_BLOCK) + __print_statements (((xp_awk_node_if_t*)p)->then_part, depth); + else + __print_statements (((xp_awk_node_if_t*)p)->then_part, depth + 1); if (((xp_awk_node_if_t*)p)->else_part != XP_NULL) { __print_tabs (depth); xp_printf (XP_TEXT("else\n")); - __print_statements (((xp_awk_node_if_t*)p)->else_part, depth); + if (((xp_awk_node_if_t*)p)->else_part->type == XP_AWK_NODE_BLOCK) + __print_statements (((xp_awk_node_if_t*)p)->else_part, depth); + else + __print_statements (((xp_awk_node_if_t*)p)->else_part, depth + 1); } break; case XP_AWK_NODE_WHILE: @@ -133,10 +138,28 @@ static void __print_statements (xp_awk_node_t* tree, int 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->type == XP_AWK_NODE_BLOCK) { + __print_statements (((xp_awk_node_while_t*)p)->body, depth); + } + else { + __print_statements (((xp_awk_node_while_t*)p)->body, depth + 1); + } + break; - 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); + case XP_AWK_NODE_DOWHILE: + __print_tabs (depth); + xp_printf (XP_TEXT("do\n")); + if (((xp_awk_node_while_t*)p)->body->type == XP_AWK_NODE_BLOCK) { + __print_statements (((xp_awk_node_while_t*)p)->body, depth); + } + else { + __print_statements (((xp_awk_node_while_t*)p)->body, depth + 1); + } + + __print_tabs (depth); + xp_printf (XP_TEXT("while (")); + __print_expr_node (((xp_awk_node_while_t*)p)->test); + xp_printf (XP_TEXT(");\n")); break; case XP_AWK_NODE_BREAK: @@ -222,6 +245,7 @@ void xp_awk_clrpt (xp_awk_node_t* tree) break; case XP_AWK_NODE_WHILE: + case XP_AWK_NODE_DOWHILE: 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); diff --git a/ase/awk/tree.h b/ase/awk/tree.h index 3344b91e..dbd33a42 100644 --- a/ase/awk/tree.h +++ b/ase/awk/tree.h @@ -1,5 +1,5 @@ /* - * $Id: tree.h,v 1.10 2006-01-18 16:12:58 bacon Exp $ + * $Id: tree.h,v 1.11 2006-01-19 10:56:35 bacon Exp $ */ #ifndef _XP_AWK_TREE_H_ @@ -21,7 +21,8 @@ enum XP_AWK_NODE_VAR, XP_AWK_NODE_CALL, XP_AWK_NODE_IF, - XP_AWK_NODE_WHILE + XP_AWK_NODE_WHILE, + XP_AWK_NODE_DOWHILE }; typedef struct xp_awk_node_t xp_awk_node_t; @@ -32,7 +33,6 @@ typedef struct xp_awk_node_term_t xp_awk_node_term_t; typedef struct xp_awk_node_call_t xp_awk_node_call_t; typedef struct xp_awk_node_if_t xp_awk_node_if_t; typedef struct xp_awk_node_while_t xp_awk_node_while_t; -typedef struct xp_awk_node_do_t xp_awk_node_do_t; #define XP_AWK_NODE_HDR \ int type; \ @@ -92,13 +92,6 @@ struct xp_awk_node_while_t xp_awk_node_t* body; }; -struct xp_awk_node_do_t -{ - XP_AWK_NODE_HDR; - xp_awk_node_t* body; - xp_awk_node_t* test; -}; - #ifdef __cplusplus extern "C" { #endif