From af63818095b60c4d225959138317bfb6433280cd Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 7 May 2006 17:45:08 +0000 Subject: [PATCH] *** empty log message *** --- ase/awk/parse.c | 63 +++++++++++++++++++++++++++++++++++++----------- ase/awk/run.c | 52 +++++++++++++++++++++++++++++++++++++--- ase/awk/tree.c | 64 ++++++++++++++++++++++++------------------------- ase/awk/tree.h | 3 ++- 4 files changed, 131 insertions(+), 51 deletions(-) diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 550d9887..6e5567f5 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.101 2006-05-06 12:52:36 bacon Exp $ + * $Id: parse.c,v 1.102 2006-05-07 17:45:08 bacon Exp $ */ #include @@ -107,7 +107,7 @@ static xp_awk_t* __collect_locals (xp_awk_t* awk, xp_size_t nlocals); static xp_awk_nde_t* __parse_function (xp_awk_t* awk); static xp_awk_nde_t* __parse_begin (xp_awk_t* awk); static xp_awk_nde_t* __parse_end (xp_awk_t* awk); -static xp_awk_nde_t* __parse_patternless (xp_awk_t* awk); +static xp_awk_nde_t* __parse_ptnblock (xp_awk_t* awk, xp_awk_nde_t* ptn); static xp_awk_nde_t* __parse_action (xp_awk_t* awk); static xp_awk_nde_t* __parse_block (xp_awk_t* awk, xp_bool_t is_top); @@ -315,8 +315,17 @@ static void __dump (xp_awk_t* awk) chain = awk->tree.chain; while (chain != XP_NULL) { - if (chain->pattern != XP_NULL) xp_awk_prnpt (chain->pattern); - if (chain->action != XP_NULL) xp_awk_prnpt (chain->action); + if (chain->pattern != XP_NULL) + { + /*xp_awk_prnpt (chain->pattern);*/ + xp_awk_prnptnpt (chain->pattern); + } + + if (chain->action != XP_NULL) + { + xp_awk_prnpt (chain->action); + } + xp_printf (XP_T("\n")); chain = chain->next; } @@ -398,11 +407,11 @@ static xp_awk_t* __parse_progunit (xp_awk_t* awk) } else if (MATCH(awk,TOKEN_LBRACE)) { - if (__parse_patternless(awk) == XP_NULL) return XP_NULL; + /* pattern less block */ + if (__parse_ptnblock(awk,XP_NULL) == XP_NULL) return XP_NULL; } else { - /* TODO: process patterns and expressions */ /* expressions /regular expression/ @@ -412,17 +421,43 @@ static xp_awk_t* __parse_progunit (xp_awk_t* awk) (pattern) pattern, pattern */ + xp_awk_nde_t* ptn; + + ptn = __parse_expression (awk); + if (ptn == XP_NULL) return XP_NULL; + + xp_assert (ptn->next == XP_NULL); + + if (MATCH(awk,TOKEN_COMMA)) + { + if (__get_token(awk) == -1) + { + xp_awk_clrpt (ptn); + return XP_NULL; + } + + ptn->next = __parse_expression (awk); + if (ptn->next == XP_NULL) + { + xp_awk_clrpt (ptn); + return XP_NULL; + } + } - if (__parse_expression (awk) == XP_NULL) return XP_NULL; if (MATCH(awk,TOKEN_LBRACE)) { - if (__parse_patternless(awk) == XP_NULL) return XP_NULL; + if (__parse_ptnblock (awk,ptn) == XP_NULL) + { + xp_awk_clrpt (ptn); + return XP_NULL; + } } else { - /* { print $0; } */ - /* TODO: XXXX */ - xp_printf (XP_T("BLOCKLESS NOT IMPLEMENTED....\n")); + /* pattern without a block */ + /* TODO: ... pattern { print $0; }*/ + xp_awk_clrpt (ptn); + xp_printf (XP_T("BLOCKLESS NOT IMPLEMENTED\n")); PANIC (awk, XP_AWK_EINTERNAL); } } @@ -672,7 +707,7 @@ static xp_awk_nde_t* __parse_end (xp_awk_t* awk) return nde; } -static xp_awk_nde_t* __parse_patternless (xp_awk_t* awk) +static xp_awk_nde_t* __parse_ptnblock (xp_awk_t* awk, xp_awk_nde_t* ptn) { xp_awk_nde_t* nde; xp_awk_chain_t* chain; @@ -687,7 +722,7 @@ static xp_awk_nde_t* __parse_patternless (xp_awk_t* awk) PANIC (awk, XP_AWK_ENOMEM); } - chain->pattern = XP_NULL; + chain->pattern = ptn; chain->action = nde; chain->next = XP_NULL; @@ -2897,7 +2932,7 @@ static int __get_token (xp_awk_t* awk) return -1; } -xp_printf (XP_T("token -> [%s]\n"), XP_STR_BUF(&awk->token.name)); +/*xp_printf (XP_T("token -> [%s]\n"), XP_STR_BUF(&awk->token.name));*/ return 0; } diff --git a/ase/awk/run.c b/ase/awk/run.c index cbe54298..4e71c2b0 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.86 2006-05-06 12:52:36 bacon Exp $ + * $Id: run.c,v 1.87 2006-05-07 17:45:08 bacon Exp $ */ #include @@ -603,6 +603,11 @@ static int __run_if_statement (xp_awk_run_t* run, xp_awk_nde_if_t* nde) xp_awk_val_t* test; int n = 0; + /* the test expression for the if statement cannot have + * chained expressions. this should not be allowed by the + * parser first of all */ + xp_assert (nde->test->next == XP_NULL); + test = __eval_expression (run, nde->test); if (test == XP_NULL) return -1; @@ -626,6 +631,10 @@ static int __run_while_statement (xp_awk_run_t* run, xp_awk_nde_while_t* nde) if (nde->type == XP_AWK_NDE_WHILE) { + /* no chained expressions are allowed for the test + * expression of the while statement */ + xp_assert (nde->test->next == XP_NULL); + /* TODO: handle run-time abortion... */ while (1) { @@ -664,6 +673,10 @@ static int __run_while_statement (xp_awk_run_t* run, xp_awk_nde_while_t* nde) } else if (nde->type == XP_AWK_NDE_DOWHILE) { + /* no chained expressions are allowed for the test + * expression of the while statement */ + xp_assert (nde->test->next == XP_NULL); + /* TODO: handle run-time abortion... */ do { @@ -705,6 +718,7 @@ static int __run_for_statement (xp_awk_run_t* run, xp_awk_nde_for_t* nde) if (nde->init != XP_NULL) { + xp_assert (nde->init->next == XP_NULL); val = __eval_expression(run,nde->init); if (val == XP_NULL) return -1; @@ -718,6 +732,10 @@ static int __run_for_statement (xp_awk_run_t* run, xp_awk_nde_for_t* nde) { xp_awk_val_t* test; + /* no chained expressions for the test expression of + * the for statement are allowed */ + xp_assert (nde->test->next == XP_NULL); + test = __eval_expression (run, nde->test); if (test == XP_NULL) return -1; @@ -759,6 +777,7 @@ static int __run_for_statement (xp_awk_run_t* run, xp_awk_nde_for_t* nde) if (nde->incr != XP_NULL) { + xp_assert (nde->incr->next == XP_NULL); val = __eval_expression(run,nde->incr); if (val == XP_NULL) return -1; @@ -814,6 +833,10 @@ static int __run_foreach_statement (xp_awk_run_t* run, xp_awk_nde_foreach_t* nde xp_assert (test->type == XP_AWK_NDE_EXP_BIN && test->opcode == XP_AWK_BINOP_IN); + /* chained expressions should not be allowed + * by the parser first of all */ + xp_assert (test->right->next == XP_NULL); + rv = __eval_expression (run, test->right); if (rv == XP_NULL) return -1; @@ -852,6 +875,11 @@ static int __run_return_statement (xp_awk_run_t* run, xp_awk_nde_return_t* nde) if (nde->val != XP_NULL) { xp_awk_val_t* val; + + /* chained expressions should not be allowed + * by the parser first of all */ + xp_assert (nde->val->next == XP_NULL); + /*xp_printf (XP_T("returning....\n"));*/ val = __eval_expression(run, nde->val); if (val == XP_NULL) return -1; @@ -873,6 +901,10 @@ static int __run_exit_statement (xp_awk_run_t* run, xp_awk_nde_exit_t* nde) { xp_awk_val_t* val; + /* chained expressions should not be allowed + * by the parser first of all */ + xp_assert (nde->val->next == XP_NULL); + val = __eval_expression(run, nde->val); if (val == XP_NULL) return -1; @@ -951,6 +983,7 @@ static xp_awk_val_t* __eval_assignment (xp_awk_run_t* run, xp_awk_nde_t* nde) xp_assert (ass->left != XP_NULL && ass->right != XP_NULL); + xp_assert (ass->right->next == XP_NULL); val = __eval_expression(run, ass->right); if (val == XP_NULL) return XP_NULL; @@ -960,6 +993,7 @@ static xp_awk_val_t* __eval_assignment (xp_awk_run_t* run, xp_awk_nde_t* nde) { xp_awk_val_t* val2, * tmp; + xp_assert (ass->left->next == XP_NULL); val2 = __eval_expression (run, ass->left); if (val2 == XP_NULL) { @@ -1234,11 +1268,13 @@ static xp_awk_val_t* __eval_binary (xp_awk_run_t* run, xp_awk_nde_t* nde) } else { + xp_assert (exp->left->next == XP_NULL); left = __eval_expression (run, exp->left); if (left == XP_NULL) return XP_NULL; xp_awk_refupval (left); + xp_assert (exp->right->next == XP_NULL); right = __eval_expression (run, exp->right); if (right == XP_NULL) { @@ -1277,6 +1313,7 @@ static xp_awk_val_t* __eval_binop_lor ( /* short-circuit evaluation required special treatment */ xp_awk_val_t* lv, * rv, * res; + xp_assert (left->next == XP_NULL); lv = __eval_expression (run, left); if (lv == XP_NULL) return XP_NULL; @@ -1287,6 +1324,7 @@ static xp_awk_val_t* __eval_binop_lor ( } else { + xp_assert (right->next == XP_NULL); rv = __eval_expression (run, right); if (rv == XP_NULL) { @@ -1319,6 +1357,7 @@ static xp_awk_val_t* __eval_binop_land ( /* short-circuit evaluation required special treatment */ xp_awk_val_t* lv, * rv, * res; + xp_assert (left->next == XP_NULL); lv = __eval_expression (run, left); if (lv == XP_NULL) return XP_NULL; @@ -1329,6 +1368,7 @@ static xp_awk_val_t* __eval_binop_land ( } else { + xp_assert (right->next == XP_NULL); rv = __eval_expression (run, right); if (rv == XP_NULL) { @@ -1368,6 +1408,7 @@ static xp_awk_val_t* __eval_binop_in ( if (str == XP_NULL) return XP_NULL; /* evaluate the right-hand side of the operator */ + xp_assert (right->next == XP_NULL); rv = __eval_expression (run, right); if (rv == XP_NULL) { @@ -2033,7 +2074,7 @@ static xp_awk_val_t* __eval_binop_nm ( { xp_awk_val_t* res; -/* TODO: ... */ +/* TODO: implement nm operator... */ if (left->type == XP_AWK_VAL_REX && right->type == XP_AWK_VAL_STR) { @@ -2061,6 +2102,7 @@ static xp_awk_val_t* __eval_unary (xp_awk_run_t* run, xp_awk_nde_t* nde) xp_assert (exp->type == XP_AWK_NDE_EXP_UNR); xp_assert (exp->left != XP_NULL && exp->right == XP_NULL); + xp_assert (exp->left->next == XP_NULL); left = __eval_expression (run, exp->left); if (left == XP_NULL) return XP_NULL; @@ -2167,6 +2209,7 @@ static xp_awk_val_t* __eval_incpre (xp_awk_run_t* run, xp_awk_nde_t* nde) PANIC (run, XP_AWK_EOPERAND); } + xp_assert (exp->left->next == XP_NULL); left = __eval_expression (run, exp->left); if (left == XP_NULL) return XP_NULL; @@ -2252,6 +2295,7 @@ static xp_awk_val_t* __eval_incpst (xp_awk_run_t* run, xp_awk_nde_t* nde) PANIC (run, XP_AWK_EOPERAND); } + xp_assert (exp->left->next == XP_NULL); left = __eval_expression (run, exp->left); if (left == XP_NULL) return XP_NULL; @@ -2347,11 +2391,14 @@ static xp_awk_val_t* __eval_cnd (xp_awk_run_t* run, xp_awk_nde_t* nde) xp_awk_val_t* tv, * v; xp_awk_nde_cnd_t* cnd = (xp_awk_nde_cnd_t*)nde; + xp_assert (cnd->test->next == XP_NULL); tv = __eval_expression (run, cnd->test); if (tv == XP_NULL) return XP_NULL; xp_awk_refupval (tv); + xp_assert (cnd->left->next == XP_NULL && + cnd->right->next == XP_NULL); v = (xp_awk_boolval(tv))? __eval_expression (run, cnd->left): __eval_expression (run, cnd->right); @@ -2936,7 +2983,6 @@ static xp_char_t* __idxnde_to_str (xp_awk_run_t* run, xp_awk_nde_t* nde) if (nde->next == XP_NULL) { /* single node index */ - idx = __eval_expression (run, nde); if (idx == XP_NULL) return XP_NULL; diff --git a/ase/awk/tree.c b/ase/awk/tree.c index 2fe93d80..66194617 100644 --- a/ase/awk/tree.c +++ b/ase/awk/tree.c @@ -1,5 +1,5 @@ /* - * $Id: tree.c,v 1.47 2006-05-06 12:52:36 bacon Exp $ + * $Id: tree.c,v 1.48 2006-05-07 17:45:08 bacon Exp $ */ #include @@ -99,46 +99,35 @@ static int __print_expression (xp_awk_nde_t* nde) case XP_AWK_NDE_ASS: if (__print_expression (((xp_awk_nde_ass_t*)nde)->left) == -1) return -1; - xp_printf (XP_T(" %s "), - __assop_str[((xp_awk_nde_exp_t*)nde)->opcode]); + xp_printf (XP_T(" %s "), __assop_str[((xp_awk_nde_exp_t*)nde)->opcode]); if (__print_expression (((xp_awk_nde_ass_t*)nde)->right) == -1) return -1; xp_assert ((((xp_awk_nde_ass_t*)nde)->right)->next == XP_NULL); break; case XP_AWK_NDE_EXP_BIN: xp_printf (XP_T("(")); - if (__print_expression(((xp_awk_nde_exp_t*)nde)->left) == -1) - return -1; + if (__print_expression(((xp_awk_nde_exp_t*)nde)->left) == -1) return -1; xp_assert ((((xp_awk_nde_exp_t*)nde)->left)->next == XP_NULL); - xp_printf (XP_T(" %s "), - __binop_str[((xp_awk_nde_exp_t*)nde)->opcode]); - if (((xp_awk_nde_exp_t*)nde)->right->type == XP_AWK_NDE_ASS) - xp_printf (XP_T("(")); - if (__print_expression (((xp_awk_nde_exp_t*)nde)->right) == -1) - return -1; - if (((xp_awk_nde_exp_t*)nde)->right->type == XP_AWK_NDE_ASS) - xp_printf (XP_T(")")); - xp_assert ((((xp_awk_nde_exp_t*)nde)->right)->next == XP_NULL); - xp_printf (XP_T(")")); + xp_printf (XP_T(" %s "), __binop_str[((xp_awk_nde_exp_t*)nde)->opcode]); + if (((xp_awk_nde_exp_t*)nde)->right->type == XP_AWK_NDE_ASS) xp_printf (XP_T("(")); + if (__print_expression (((xp_awk_nde_exp_t*)nde)->right) == -1) return -1; + if (((xp_awk_nde_exp_t*)nde)->right->type == XP_AWK_NDE_ASS) xp_printf (XP_T(")")); + xp_assert ((((xp_awk_nde_exp_t*)nde)->right)->next == XP_NULL); xp_printf (XP_T(")")); break; case XP_AWK_NDE_EXP_UNR: xp_assert (((xp_awk_nde_exp_t*)nde)->right == XP_NULL); - xp_printf (XP_T("%s("), - __unrop_str[((xp_awk_nde_exp_t*)nde)->opcode]); - if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1) - return -1; + xp_printf (XP_T("%s("), __unrop_str[((xp_awk_nde_exp_t*)nde)->opcode]); + if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1) return -1; xp_printf (XP_T(")")); break; case XP_AWK_NDE_EXP_INCPRE: xp_assert (((xp_awk_nde_exp_t*)nde)->right == XP_NULL); - xp_printf (XP_T("%s("), - __incop_str[((xp_awk_nde_exp_t*)nde)->opcode]); - if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1) - return -1; + xp_printf (XP_T("%s("), __incop_str[((xp_awk_nde_exp_t*)nde)->opcode]); + if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1) return -1; xp_printf (XP_T(")")); break; @@ -146,23 +135,18 @@ static int __print_expression (xp_awk_nde_t* nde) xp_assert (((xp_awk_nde_exp_t*)nde)->right == XP_NULL); xp_printf (XP_T("(")); - if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1) - return -1; - xp_printf (XP_T(")%s"), - __incop_str[((xp_awk_nde_exp_t*)nde)->opcode]); + if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1) return -1; + xp_printf (XP_T(")%s"), __incop_str[((xp_awk_nde_exp_t*)nde)->opcode]); break; case XP_AWK_NDE_CND: xp_printf (XP_T("(")); - if (__print_expression(((xp_awk_nde_cnd_t*)nde)->test) == -1) - return -1; + if (__print_expression(((xp_awk_nde_cnd_t*)nde)->test) == -1) return -1; xp_printf (XP_T(")?")); - if (__print_expression(((xp_awk_nde_cnd_t*)nde)->left) == -1) - return -1; + if (__print_expression(((xp_awk_nde_cnd_t*)nde)->left) == -1) return -1; xp_printf (XP_T(":")); - if (__print_expression(((xp_awk_nde_cnd_t*)nde)->right) == -1) - return -1; + if (__print_expression(((xp_awk_nde_cnd_t*)nde)->right) == -1) return -1; break; case XP_AWK_NDE_INT: @@ -530,6 +514,20 @@ void xp_awk_prnpt (xp_awk_nde_t* tree) __print_statements (tree, 0); } +void xp_awk_prnptnpt (xp_awk_nde_t* tree) +{ + xp_awk_nde_t* nde = tree; + + while (nde != XP_NULL) + { + __print_expression (tree); + if (nde->next == XP_NULL) break; + + xp_printf (XP_T(",")); + nde = nde->next; + } +} + void xp_awk_clrpt (xp_awk_nde_t* tree) { xp_awk_nde_t* p = tree; diff --git a/ase/awk/tree.h b/ase/awk/tree.h index ff868247..800640f9 100644 --- a/ase/awk/tree.h +++ b/ase/awk/tree.h @@ -1,5 +1,5 @@ /* - * $Id: tree.h,v 1.40 2006-04-29 12:09:29 bacon Exp $ + * $Id: tree.h,v 1.41 2006-05-07 17:45:08 bacon Exp $ */ #ifndef _XP_AWK_TREE_H_ @@ -278,6 +278,7 @@ extern "C" { #endif void xp_awk_prnpt (xp_awk_nde_t* tree); +void xp_awk_prnptnpt (xp_awk_nde_t* tree); void xp_awk_clrpt (xp_awk_nde_t* tree); #ifdef __cplusplus