diff --git a/ase/awk/awk.c b/ase/awk/awk.c index 61995ae6..cb31cab1 100644 --- a/ase/awk/awk.c +++ b/ase/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.32 2006-03-26 16:36:30 bacon Exp $ + * $Id: awk.c,v 1.33 2006-03-27 11:43:17 bacon Exp $ */ #include @@ -90,6 +90,7 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk) awk->run.stack_base = 0; awk->run.stack_limit = 0; awk->run.exit_level = 0; + awk->run.icache_count = 0; awk->lex.curc = XP_CHAR_EOF; awk->lex.ungotc_count = 0; diff --git a/ase/awk/awk.h b/ase/awk/awk.h index cfec625c..361e3476 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.36 2006-03-26 16:36:30 bacon Exp $ + * $Id: awk.h,v 1.37 2006-03-27 11:43:17 bacon Exp $ */ #ifndef _XP_AWK_AWK_H_ @@ -13,6 +13,12 @@ #include #endif +/* + * TYPE: xp_awk_t + */ +typedef struct xp_awk_t xp_awk_t; +typedef struct xp_awk_chain_t xp_awk_chain_t; + #include #include #include @@ -20,12 +26,6 @@ #include #include -/* - * TYPE: xp_awk_t - */ -typedef struct xp_awk_t xp_awk_t; -typedef struct xp_awk_chain_t xp_awk_chain_t; - /* * TYPE: xp_awk_io_t */ @@ -98,6 +98,9 @@ struct xp_awk_t xp_size_t stack_base; xp_size_t stack_limit; int exit_level; + + xp_awk_val_int_t* icache[100]; // TODO: ... + xp_size_t icache_count; } run; /* source buffer management */ diff --git a/ase/awk/parse.c b/ase/awk/parse.c index d68b74e8..463d2eb8 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.60 2006-03-25 17:04:36 bacon Exp $ + * $Id: parse.c,v 1.61 2006-03-27 11:43:17 bacon Exp $ */ #include @@ -8,6 +8,7 @@ #include #include #include +#include #include #endif diff --git a/ase/awk/run.c b/ase/awk/run.c index 5b0c49b6..4a7b3872 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.20 2006-03-27 10:19:33 bacon Exp $ + * $Id: run.c,v 1.21 2006-03-27 11:43:17 bacon Exp $ */ #include @@ -111,7 +111,7 @@ static int __run_block (xp_awk_t* awk, xp_awk_nde_blk_t* nde) while (nlocals > 0) { --nlocals; - xp_awk_refdownval (STACK_LOCAL(awk,nlocals)); + xp_awk_refdownval (awk, STACK_LOCAL(awk,nlocals)); __raw_pop (awk); } @@ -183,7 +183,7 @@ static int __run_statement (xp_awk_t* awk, xp_awk_nde_t* nde) static int __run_if_statement (xp_awk_t* awk, xp_awk_nde_if_t* nde) { xp_awk_val_t* test; - int n; + int n = 0; test = __eval_expression (awk, nde->test); if (test == XP_NULL) return -1; @@ -198,7 +198,7 @@ static int __run_if_statement (xp_awk_t* awk, xp_awk_nde_if_t* nde) n = __run_statement (awk, nde->else_part); } - xp_awk_refdownval (test); // TODO: is this correct? + xp_awk_refdownval (awk, test); // TODO: is this correct? return n; } @@ -220,44 +220,61 @@ static int __run_while_statement (xp_awk_t* awk, xp_awk_nde_while_t* nde) // TODO: break.... continue...., global exit, return... run-time abortion... if (__run_statement(awk,nde->body) == -1) { - xp_awk_refdownval (test); + xp_awk_refdownval (awk, test); return -1; } } else { - xp_awk_refdownval (test); + xp_awk_refdownval (awk, test); break; } - xp_awk_refdownval (test); + xp_awk_refdownval (awk, test); + + if (awk->run.exit_level == EXIT_BREAK) + { + awk->run.exit_level = EXIT_NONE; + break; + } + else if (awk->run.exit_level == EXIT_CONTINUE) + { + awk->run.exit_level = EXIT_NONE; + } + else if (awk->run.exit_level != EXIT_NONE) break; } } else if (nde->type == XP_AWK_NDE_DOWHILE) { do { + if (__run_statement(awk,nde->body) == -1) + { + // TODO: error handling... + return -1; + } + + if (awk->run.exit_level == EXIT_BREAK) + { + awk->run.exit_level = EXIT_NONE; + break; + } + else if (awk->run.exit_level == EXIT_CONTINUE) + { + awk->run.exit_level = EXIT_NONE; + } + else if (awk->run.exit_level != EXIT_NONE) break; + test = __eval_expression (awk, nde->test); if (test == XP_NULL) return -1; xp_awk_refupval (test); - - if (xp_awk_isvaltrue(test)) + if (!xp_awk_isvaltrue(test)) { - // TODO: break.... continue...., global exit, return... run-time abortion... - if (__run_statement(awk,nde->body) == -1) - { - xp_awk_refdownval (test); - return -1; - } - } - else - { - xp_awk_refdownval (test); + xp_awk_refdownval (awk, test); break; } - - xp_awk_refdownval (test); + xp_awk_refdownval (awk, test); } while (1); } @@ -274,17 +291,6 @@ static int __run_for_statement (xp_awk_t* awk, xp_awk_nde_for_t* nde) while (1) { - if (awk->run.exit_level == EXIT_BREAK) - { - awk->run.exit_level = EXIT_NONE; - break; - } - else if (awk->run.exit_level == EXIT_CONTINUE) - { - awk->run.exit_level = EXIT_NONE; - } - else if (awk->run.exit_level != EXIT_NONE) break; - if (nde->test != XP_NULL) { xp_awk_val_t* test; @@ -297,17 +303,17 @@ static int __run_for_statement (xp_awk_t* awk, xp_awk_nde_for_t* nde) { if (__run_statement(awk,nde->body) == -1) { - xp_awk_refdownval (test); + xp_awk_refdownval (awk, test); return -1; } } else { - xp_awk_refdownval (test); + xp_awk_refdownval (awk, test); break; } - xp_awk_refdownval (test); + xp_awk_refdownval (awk, test); } else { @@ -317,6 +323,17 @@ static int __run_for_statement (xp_awk_t* awk, xp_awk_nde_for_t* nde) } } + if (awk->run.exit_level == EXIT_BREAK) + { + awk->run.exit_level = EXIT_NONE; + break; + } + else if (awk->run.exit_level == EXIT_CONTINUE) + { + awk->run.exit_level = EXIT_NONE; + } + else if (awk->run.exit_level != EXIT_NONE) break; + if (nde->incr != XP_NULL) { if (__eval_expression(awk,nde->incr) == XP_NULL) return -1; @@ -408,7 +425,7 @@ static xp_awk_val_t* __eval_expression (xp_awk_t* awk, xp_awk_nde_t* nde) break; case XP_AWK_NDE_INT: - val = xp_awk_makeintval(((xp_awk_nde_int_t*)nde)->val); + val = xp_awk_makeintval(awk,((xp_awk_nde_int_t*)nde)->val); break; /* TODO: @@ -493,7 +510,7 @@ static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde) name = xp_strdup (tgt->id.name); if (name == XP_NULL) { - xp_awk_freeval(val); + xp_awk_freeval (awk, val); awk->errnum = XP_AWK_ENOMEM; return XP_NULL; } @@ -506,7 +523,7 @@ static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde) if (xp_awk_map_put(&awk->run.named, name, val) == XP_NULL) { xp_free (name); - xp_awk_freeval (val); + xp_awk_freeval (awk, val); awk->errnum = XP_AWK_ENOMEM; return XP_NULL; } @@ -523,13 +540,13 @@ static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde) } else if (tgt->type == XP_AWK_NDE_LOCAL) { - xp_awk_refdownval(STACK_LOCAL(awk,tgt->id.idxa)); + xp_awk_refdownval (awk, STACK_LOCAL(awk,tgt->id.idxa)); STACK_LOCAL(awk,tgt->id.idxa) = val; xp_awk_refupval (val); } else if (tgt->type == XP_AWK_NDE_ARG) { - xp_awk_refdownval(STACK_ARG(awk,tgt->id.idxa)); + xp_awk_refdownval (awk, STACK_ARG(awk,tgt->id.idxa)); STACK_ARG(awk,tgt->id.idxa) = val; xp_awk_refupval (val); } @@ -569,7 +586,7 @@ static xp_awk_val_t* __eval_binary (xp_awk_t* awk, xp_awk_nde_exp_t* nde) right = __eval_expression (awk, nde->right); if (right == XP_NULL) { - xp_awk_refdownval (left); + xp_awk_refdownval (awk, left); return XP_NULL; } @@ -602,7 +619,7 @@ static xp_awk_val_t* __eval_binary (xp_awk_t* awk, xp_awk_nde_exp_t* nde) else { */ - res = xp_awk_makeintval (r); + res = xp_awk_makeintval (awk, r); //} } } @@ -629,7 +646,7 @@ static xp_awk_val_t* __eval_binary (xp_awk_t* awk, xp_awk_nde_exp_t* nde) else { */ - res = xp_awk_makeintval (r); + res = xp_awk_makeintval (awk, r); //} } } @@ -648,8 +665,8 @@ static xp_awk_val_t* __eval_binary (xp_awk_t* awk, xp_awk_nde_exp_t* nde) else { */ - xp_awk_refdownval (left); - xp_awk_refdownval (right); + xp_awk_refdownval (awk, left); + xp_awk_refdownval (awk, right); //} return res; @@ -766,7 +783,7 @@ static xp_awk_val_t* __eval_funccall (xp_awk_t* awk, xp_awk_nde_call_t* nde) //xp_printf (XP_TEXT("block run complete nargs = %d\n"), nargs); for (i = 0; i < nargs; i++) { - xp_awk_refdownval (STACK_ARG(awk,i)); + xp_awk_refdownval (awk, STACK_ARG(awk,i)); } //xp_printf (XP_TEXT("got return value\n")); @@ -774,7 +791,7 @@ static xp_awk_val_t* __eval_funccall (xp_awk_t* awk, xp_awk_nde_call_t* nde) * the value must not be freeed event if the reference count * is decremented to zero. */ v = STACK_RETVAL(awk); - xp_awk_refdownval_nofree (v); + xp_awk_refdownval_nofree (awk, v); awk->run.stack_top = (xp_size_t)awk->run.stack[awk->run.stack_base+1]; awk->run.stack_base = (xp_size_t)awk->run.stack[awk->run.stack_base+0]; diff --git a/ase/awk/sa.h b/ase/awk/sa.h index 0b776f9b..e4541d7e 100644 --- a/ase/awk/sa.h +++ b/ase/awk/sa.h @@ -1,5 +1,5 @@ /* - * $Id: sa.h,v 1.13 2006-03-05 17:07:33 bacon Exp $ + * $Id: sa.h,v 1.14 2006-03-27 11:43:17 bacon Exp $ */ #ifndef _XP_AWK_SA_H_ @@ -28,8 +28,8 @@ #define xp_isalpha iswalpha #define xp_isalnum iswalnum #define xp_isspace iswspace -#define xp_toupper toupper -#define xp_tolower tolower +#define xp_toupper towupper +#define xp_tolower towlower #define xp_strcpy wcscpy #define xp_strcmp wcscmp diff --git a/ase/awk/val.c b/ase/awk/val.c index 53922679..640a90c0 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c,v 1.10 2006-03-26 16:36:30 bacon Exp $ + * $Id: val.c,v 1.11 2006-03-27 11:43:17 bacon Exp $ */ #include @@ -7,6 +7,7 @@ #ifndef __STAND_ALONE #include #include +#include #endif static xp_awk_val_nil_t __awk_nil = { XP_AWK_VAL_NIL, 0 }; @@ -27,7 +28,7 @@ static xp_awk_val_int_t __awk_int[] = { XP_AWK_VAL_INT, 0, 9 }, }; -xp_awk_val_t* xp_awk_makeintval (xp_long_t v) +xp_awk_val_t* xp_awk_makeintval (xp_awk_t* awk, xp_long_t v) { xp_awk_val_int_t* val; @@ -37,8 +38,15 @@ xp_awk_val_t* xp_awk_makeintval (xp_long_t v) return (xp_awk_val_t*)&__awk_int[v-__awk_int[0].val]; } - val = (xp_awk_val_int_t*)xp_malloc(xp_sizeof(xp_awk_val_int_t)); - if (val == XP_NULL) return XP_NULL; + if (awk->run.icache_count > 0) + { + val = awk->run.icache[--awk->run.icache_count]; + } + else + { + val = (xp_awk_val_int_t*)xp_malloc(xp_sizeof(xp_awk_val_int_t)); + if (val == XP_NULL) return XP_NULL; + } val->type = XP_AWK_VAL_INT; val->ref = 0; @@ -87,17 +95,39 @@ xp_bool_t xp_awk_isbuiltinval (xp_awk_val_t* val) val <= (xp_awk_val_t*)&__awk_int[xp_countof(__awk_int)-1]); } -void xp_awk_freeval (xp_awk_val_t* val) +void xp_awk_freeval (xp_awk_t* awk, xp_awk_val_t* val) { if (xp_awk_isbuiltinval(val)) return; switch (val->type) { + case XP_AWK_VAL_NIL: + xp_free (val); + break; + + case XP_AWK_VAL_INT: + if (awk->run.icache_count < xp_countof(awk->run.icache)) + { + awk->run.icache[awk->run.icache_count++] = + (xp_awk_val_int_t*)val; + } + else + { + xp_free (val); + } + break; + + case XP_AWK_VAL_REAL: + xp_free (val); + break; + case XP_AWK_VAL_STR: xp_free (((xp_awk_val_str_t*)val)->buf); - default: xp_free (val); + break; } + + /* should never reach here */ } void xp_awk_refupval (xp_awk_val_t* val) @@ -111,7 +141,7 @@ xp_printf (XP_TEXT("\n")); val->ref++; } -void xp_awk_refdownval (xp_awk_val_t* val) +void xp_awk_refdownval (xp_awk_t* awk, xp_awk_val_t* val) { if (xp_awk_isbuiltinval(val)) return; @@ -130,11 +160,11 @@ xp_printf (XP_TEXT("**FREEING ")); xp_awk_printval (val); xp_printf (XP_TEXT("\n")); */ - xp_awk_freeval(val); + xp_awk_freeval(awk, val); } } -void xp_awk_refdownval_nofree (xp_awk_val_t* val) +void xp_awk_refdownval_nofree (xp_awk_t* awk, xp_awk_val_t* val) { if (xp_awk_isbuiltinval(val)) return; @@ -142,7 +172,7 @@ void xp_awk_refdownval_nofree (xp_awk_val_t* val) val->ref--; } -xp_awk_val_t* xp_awk_cloneval (xp_awk_val_t* val) +xp_awk_val_t* xp_awk_cloneval (xp_awk_t* awk, xp_awk_val_t* val) { if (val == XP_NULL) return xp_awk_val_nil; @@ -151,7 +181,7 @@ xp_awk_val_t* xp_awk_cloneval (xp_awk_val_t* val) case XP_AWK_VAL_NIL: return xp_awk_val_nil; case XP_AWK_VAL_INT: - return xp_awk_makeintval (((xp_awk_val_int_t*)val)->val); + return xp_awk_makeintval (awk, ((xp_awk_val_int_t*)val)->val); case XP_AWK_VAL_REAL: return xp_awk_makerealval (((xp_awk_val_real_t*)val)->val); case XP_AWK_VAL_STR: diff --git a/ase/awk/val.h b/ase/awk/val.h index 15b433ad..17f5fb85 100644 --- a/ase/awk/val.h +++ b/ase/awk/val.h @@ -1,5 +1,5 @@ /* - * $Id: val.h,v 1.8 2006-03-26 16:36:30 bacon Exp $ + * $Id: val.h,v 1.9 2006-03-27 11:43:17 bacon Exp $ */ #ifndef _XP_AWK_VAL_H_ @@ -11,10 +11,10 @@ enum { - XP_AWK_VAL_NIL, - XP_AWK_VAL_INT, - XP_AWK_VAL_REAL, - XP_AWK_VAL_STR + XP_AWK_VAL_NIL = 0, + XP_AWK_VAL_INT = 1, + XP_AWK_VAL_REAL = 2, + XP_AWK_VAL_STR = 3 }; typedef struct xp_awk_val_t xp_awk_val_t; @@ -66,16 +66,16 @@ extern "C" { extern xp_awk_val_t* xp_awk_val_nil; -xp_awk_val_t* xp_awk_makeintval (xp_long_t v); +xp_awk_val_t* xp_awk_makeintval (xp_awk_t* awk, xp_long_t v); xp_awk_val_t* xp_awk_makestrval (const xp_char_t* str, xp_size_t len); xp_bool_t xp_awk_isbuiltinval (xp_awk_val_t* val); -void xp_awk_freeval (xp_awk_val_t* val); +void xp_awk_freeval (xp_awk_t* awk, xp_awk_val_t* val); void xp_awk_refupval (xp_awk_val_t* val); -void xp_awk_refdownval (xp_awk_val_t* val); -void xp_awk_refdownval_nofree (xp_awk_val_t* val); +void xp_awk_refdownval (xp_awk_t* awk, xp_awk_val_t* val); +void xp_awk_refdownval_nofree (xp_awk_t* awk, xp_awk_val_t* val); -xp_awk_val_t* xp_awk_cloneval (xp_awk_val_t* val); +xp_awk_val_t* xp_awk_cloneval (xp_awk_t* awk, xp_awk_val_t* val); xp_bool_t xp_awk_isvaltrue (xp_awk_val_t* val); void xp_awk_printval (xp_awk_val_t* val);