From 2e70ee9475a966f08001a742ed2bbd554ce236f0 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 27 Jun 2006 10:53:04 +0000 Subject: [PATCH] *** empty log message *** --- ase/awk/awk.c | 4 +- ase/awk/awk_i.h | 3 +- ase/awk/parse.c | 104 ++++++++++++++++++++++++++++++++++--------- ase/awk/run.c | 9 ++-- ase/test/awk/awk.c | 9 ++-- ase/test/awk/t11.awk | 10 +++++ 6 files changed, 107 insertions(+), 32 deletions(-) create mode 100644 ase/test/awk/t11.awk diff --git a/ase/awk/awk.c b/ase/awk/awk.c index b3ce94fe..d238b952 100644 --- a/ase/awk/awk.c +++ b/ase/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.56 2006-06-22 04:25:44 bacon Exp $ + * $Id: awk.c,v 1.57 2006-06-27 10:53:04 bacon Exp $ */ #include @@ -75,6 +75,8 @@ xp_awk_t* xp_awk_open (void) awk->tree.chain = XP_NULL; awk->tree.chain_tail = XP_NULL; + awk->token.prev = 0; + awk->token.type = 0; awk->token.line = 1; awk->token.column = 1; diff --git a/ase/awk/awk_i.h b/ase/awk/awk_i.h index 7d69b8cb..1eb83225 100644 --- a/ase/awk/awk_i.h +++ b/ase/awk/awk_i.h @@ -1,5 +1,5 @@ /* - * $Id: awk_i.h,v 1.21 2006-06-21 13:52:15 bacon Exp $ + * $Id: awk_i.h,v 1.22 2006-06-27 10:53:04 bacon Exp $ */ #ifndef _XP_AWK_AWKI_H_ @@ -118,6 +118,7 @@ struct xp_awk_t /* token */ struct { + int prev; int type; xp_str_t name; xp_size_t line; diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 4f81ec5d..b7c3e728 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.122 2006-06-25 15:26:57 bacon Exp $ + * $Id: parse.c,v 1.123 2006-06-27 10:53:04 bacon Exp $ */ #include @@ -248,7 +248,11 @@ static struct __kwent __bvtab[] = c = (awk)->lex.curc; \ } while(0) -#define SET_TOKEN_TYPE(awk,code) ((awk)->token.type = code) +#define SET_TOKEN_TYPE(awk,code) \ + do { \ + (awk)->token.prev = (awk)->token.type; \ + (awk)->token.type = (code); \ + } while (0); #define ADD_TOKEN_CHAR(awk,c) \ do { \ @@ -2782,13 +2786,12 @@ static xp_awk_nde_t* __parse_delete (xp_awk_t* awk) static xp_awk_nde_t* __parse_print (xp_awk_t* awk) { xp_awk_nde_print_t* nde; - xp_awk_nde_t* args = XP_NULL; + xp_awk_nde_t* args = XP_NULL; + xp_awk_nde_t* args_tail = XP_NULL; + xp_awk_nde_t* tail_prev = XP_NULL; xp_awk_nde_t* out = XP_NULL; int out_type; - /* TODO: handle expression list, not just a single expression */ - /* TODO: handle ambiguiouty print "1111" > "2222". is > redirection? */ - if (!MATCH(awk,TOKEN_SEMICOLON) && !MATCH(awk,TOKEN_GT) && !MATCH(awk,TOKEN_RSHIFT) && @@ -2797,27 +2800,86 @@ static xp_awk_nde_t* __parse_print (xp_awk_t* awk) { args = __parse_expression (awk); if (args == XP_NULL) return XP_NULL; - } - out_type = MATCH(awk,TOKEN_GT)? XP_AWK_PRINT_FILE: - MATCH(awk,TOKEN_RSHIFT)? XP_AWK_PRINT_FILE_APPEND: - MATCH(awk,TOKEN_BOR)? XP_AWK_PRINT_PIPE: - MATCH(awk,TOKEN_BORAND)? XP_AWK_PRINT_COPROC: - XP_AWK_PRINT_CONSOLE; + args_tail = args; + tail_prev = XP_NULL; - if (out_type != XP_AWK_PRINT_CONSOLE) - { - if (__get_token(awk) == -1) + while (MATCH(awk,TOKEN_COMMA)) { - if (args != XP_NULL) xp_awk_clrpt (args); - return XP_NULL; + if (__get_token(awk) == -1) + { + xp_awk_clrpt (args); + return XP_NULL; + } + + args_tail->next = __parse_expression (awk); + if (args_tail->next == XP_NULL) + { + xp_awk_clrpt (args); + return XP_NULL; + } + + tail_prev = args_tail; + args_tail = args_tail->next; } - out = __parse_expression(awk); - if (out == XP_NULL) + /* print 1 > 2 would print 1 to the file named 2. + * print (1 > 2) would print (1 > 2) in the console */ + if (awk->token.prev != TOKEN_RPAREN && + args_tail->type == XP_AWK_NDE_EXP_BIN) { - if (args != XP_NULL) xp_awk_clrpt (args); - return XP_NULL; + xp_awk_nde_exp_t* ep = (xp_awk_nde_exp_t*)args_tail; + if (ep->opcode == XP_AWK_BINOP_GT) + { + xp_awk_nde_t* tmp = args_tail; + + if (tail_prev != XP_NULL) + tail_prev->next = ep->left; + else args = ep->left; + + out = ep->right; + out_type = XP_AWK_PRINT_FILE; + + xp_free (tmp); + } + else if (ep->opcode == XP_AWK_BINOP_RSHIFT) + { + xp_awk_nde_t* tmp = args_tail; + + if (tail_prev != XP_NULL) + tail_prev->next = ep->left; + else args = ep->left; + + out = ep->right; + out_type = XP_AWK_PRINT_FILE_APPEND; + + xp_free (tmp); + } + } + } + + if (out == XP_NULL) + { + out_type = MATCH(awk,TOKEN_GT)? XP_AWK_PRINT_FILE: + MATCH(awk,TOKEN_RSHIFT)? XP_AWK_PRINT_FILE_APPEND: + MATCH(awk,TOKEN_BOR)? XP_AWK_PRINT_PIPE: + MATCH(awk,TOKEN_BORAND)? XP_AWK_PRINT_COPROC: + XP_AWK_PRINT_CONSOLE; + + if (out_type != XP_AWK_PRINT_CONSOLE) + { + if (__get_token(awk) == -1) + { + if (args != XP_NULL) xp_awk_clrpt (args); + return XP_NULL; + } + + out = __parse_expression(awk); + if (out == XP_NULL) + { + if (args != XP_NULL) xp_awk_clrpt (args); + return XP_NULL; + } } } diff --git a/ase/awk/run.c b/ase/awk/run.c index c41d3acc..52c09ff2 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.108 2006-06-26 15:09:28 bacon Exp $ + * $Id: run.c,v 1.109 2006-06-27 10:53:04 bacon Exp $ */ #include @@ -1110,7 +1110,7 @@ static int __run_print_statement (xp_awk_run_t* run, xp_awk_nde_print_t* nde) if (p->out != XP_NULL) { v = __eval_expression (run, p->out); - if (out == XP_NULL) return -1; + if (v == XP_NULL) return -1; xp_awk_refupval (v); out = xp_awk_valtostr (v, &errnum, XP_NULL); @@ -1127,7 +1127,8 @@ static int __run_print_statement (xp_awk_run_t* run, xp_awk_nde_print_t* nde) if (p->args == XP_NULL) { /* TODO: get $0 ans use it for v */ - v = xp_awk_makestrval0 (XP_T("\n")); + v = xp_awk_makestrval0 ( + XP_T("\n")); if (v == XP_NULL) { if (out != XP_NULL) xp_free (out); @@ -1168,6 +1169,8 @@ static int __run_print_statement (xp_awk_run_t* run, xp_awk_nde_print_t* nde) xp_awk_refdownval (run, v); /* TODO: how to handle n == -1 && errnum == XP_AWK_ENOERR. that is the user handler returned an error... */ + + /* TODO: print proper field separator */ } /* TODO: predefine the new line string diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index 93f79e7c..1fa2a5fc 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.43 2006-06-26 15:09:28 bacon Exp $ + * $Id: awk.c,v 1.44 2006-06-27 10:53:04 bacon Exp $ */ #include @@ -230,11 +230,8 @@ xp_printf (XP_TEXT("closing %s of type %d\n"), epa->name, epa->type); case XP_AWK_IO_WRITE: { - /* - if (_fputts (data, size, (FILE*)epa->handle) == XP_NULL) - return 0; - return size; - */ + /* TODO: how to return error or 0 */ + _fputts (data, /*size,*/ (FILE*)epa->handle); return -1; } diff --git a/ase/test/awk/t11.awk b/ase/test/awk/t11.awk new file mode 100644 index 00000000..f66d8e7a --- /dev/null +++ b/ase/test/awk/t11.awk @@ -0,0 +1,10 @@ +BEGIN +{ + print "this is only a test"; + print; + print 1, 2, (3 >> 10); + print 1, 2, 3 >> 10; + print 3, 4, 5 >> 10; + close (10); + print "-------------" >> 10; +}