*** empty log message ***

This commit is contained in:
hyung-hwan 2006-03-27 11:43:17 +00:00
parent 4e2d76c9a1
commit 24f1124b8e
7 changed files with 132 additions and 80 deletions

View File

@ -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 <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -90,6 +90,7 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
awk->run.stack_base = 0; awk->run.stack_base = 0;
awk->run.stack_limit = 0; awk->run.stack_limit = 0;
awk->run.exit_level = 0; awk->run.exit_level = 0;
awk->run.icache_count = 0;
awk->lex.curc = XP_CHAR_EOF; awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0; awk->lex.ungotc_count = 0;

View File

@ -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_ #ifndef _XP_AWK_AWK_H_
@ -13,6 +13,12 @@
#include <xp/bas/str.h> #include <xp/bas/str.h>
#endif #endif
/*
* TYPE: xp_awk_t
*/
typedef struct xp_awk_t xp_awk_t;
typedef struct xp_awk_chain_t xp_awk_chain_t;
#include <xp/awk/err.h> #include <xp/awk/err.h>
#include <xp/awk/tree.h> #include <xp/awk/tree.h>
#include <xp/awk/tab.h> #include <xp/awk/tab.h>
@ -20,12 +26,6 @@
#include <xp/awk/val.h> #include <xp/awk/val.h>
#include <xp/awk/run.h> #include <xp/awk/run.h>
/*
* 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 * TYPE: xp_awk_io_t
*/ */
@ -98,6 +98,9 @@ struct xp_awk_t
xp_size_t stack_base; xp_size_t stack_base;
xp_size_t stack_limit; xp_size_t stack_limit;
int exit_level; int exit_level;
xp_awk_val_int_t* icache[100]; // TODO: ...
xp_size_t icache_count;
} run; } run;
/* source buffer management */ /* source buffer management */

View File

@ -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 <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -8,6 +8,7 @@
#include <xp/bas/memory.h> #include <xp/bas/memory.h>
#include <xp/bas/ctype.h> #include <xp/bas/ctype.h>
#include <xp/bas/string.h> #include <xp/bas/string.h>
#include <xp/bas/stdlib.h>
#include <xp/bas/assert.h> #include <xp/bas/assert.h>
#endif #endif

View File

@ -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 <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -111,7 +111,7 @@ static int __run_block (xp_awk_t* awk, xp_awk_nde_blk_t* nde)
while (nlocals > 0) while (nlocals > 0)
{ {
--nlocals; --nlocals;
xp_awk_refdownval (STACK_LOCAL(awk,nlocals)); xp_awk_refdownval (awk, STACK_LOCAL(awk,nlocals));
__raw_pop (awk); __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) static int __run_if_statement (xp_awk_t* awk, xp_awk_nde_if_t* nde)
{ {
xp_awk_val_t* test; xp_awk_val_t* test;
int n; int n = 0;
test = __eval_expression (awk, nde->test); test = __eval_expression (awk, nde->test);
if (test == XP_NULL) return -1; 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); 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; 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... // TODO: break.... continue...., global exit, return... run-time abortion...
if (__run_statement(awk,nde->body) == -1) if (__run_statement(awk,nde->body) == -1)
{ {
xp_awk_refdownval (test); xp_awk_refdownval (awk, test);
return -1; return -1;
} }
} }
else else
{ {
xp_awk_refdownval (test); xp_awk_refdownval (awk, test);
break; 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) else if (nde->type == XP_AWK_NDE_DOWHILE)
{ {
do 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); test = __eval_expression (awk, nde->test);
if (test == XP_NULL) return -1; if (test == XP_NULL) return -1;
xp_awk_refupval (test); xp_awk_refupval (test);
if (!xp_awk_isvaltrue(test))
if (xp_awk_isvaltrue(test))
{ {
// TODO: break.... continue...., global exit, return... run-time abortion... xp_awk_refdownval (awk, test);
if (__run_statement(awk,nde->body) == -1)
{
xp_awk_refdownval (test);
return -1;
}
}
else
{
xp_awk_refdownval (test);
break; break;
} }
xp_awk_refdownval (awk, test);
xp_awk_refdownval (test);
} }
while (1); while (1);
} }
@ -274,17 +291,6 @@ static int __run_for_statement (xp_awk_t* awk, xp_awk_nde_for_t* nde)
while (1) 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) if (nde->test != XP_NULL)
{ {
xp_awk_val_t* test; 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) if (__run_statement(awk,nde->body) == -1)
{ {
xp_awk_refdownval (test); xp_awk_refdownval (awk, test);
return -1; return -1;
} }
} }
else else
{ {
xp_awk_refdownval (test); xp_awk_refdownval (awk, test);
break; break;
} }
xp_awk_refdownval (test); xp_awk_refdownval (awk, test);
} }
else 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 (nde->incr != XP_NULL)
{ {
if (__eval_expression(awk,nde->incr) == XP_NULL) return -1; 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; break;
case XP_AWK_NDE_INT: 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; break;
/* TODO: /* 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); name = xp_strdup (tgt->id.name);
if (name == XP_NULL) if (name == XP_NULL)
{ {
xp_awk_freeval(val); xp_awk_freeval (awk, val);
awk->errnum = XP_AWK_ENOMEM; awk->errnum = XP_AWK_ENOMEM;
return XP_NULL; 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) if (xp_awk_map_put(&awk->run.named, name, val) == XP_NULL)
{ {
xp_free (name); xp_free (name);
xp_awk_freeval (val); xp_awk_freeval (awk, val);
awk->errnum = XP_AWK_ENOMEM; awk->errnum = XP_AWK_ENOMEM;
return XP_NULL; 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) 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; STACK_LOCAL(awk,tgt->id.idxa) = val;
xp_awk_refupval (val); xp_awk_refupval (val);
} }
else if (tgt->type == XP_AWK_NDE_ARG) 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; STACK_ARG(awk,tgt->id.idxa) = val;
xp_awk_refupval (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); right = __eval_expression (awk, nde->right);
if (right == XP_NULL) if (right == XP_NULL)
{ {
xp_awk_refdownval (left); xp_awk_refdownval (awk, left);
return XP_NULL; 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 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 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 else
{ {
*/ */
xp_awk_refdownval (left); xp_awk_refdownval (awk, left);
xp_awk_refdownval (right); xp_awk_refdownval (awk, right);
//} //}
return res; 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); //xp_printf (XP_TEXT("block run complete nargs = %d\n"), nargs);
for (i = 0; i < nargs; i++) 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")); //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 * the value must not be freeed event if the reference count
* is decremented to zero. */ * is decremented to zero. */
v = STACK_RETVAL(awk); 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_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]; awk->run.stack_base = (xp_size_t)awk->run.stack[awk->run.stack_base+0];

View File

@ -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_ #ifndef _XP_AWK_SA_H_
@ -28,8 +28,8 @@
#define xp_isalpha iswalpha #define xp_isalpha iswalpha
#define xp_isalnum iswalnum #define xp_isalnum iswalnum
#define xp_isspace iswspace #define xp_isspace iswspace
#define xp_toupper toupper #define xp_toupper towupper
#define xp_tolower tolower #define xp_tolower towlower
#define xp_strcpy wcscpy #define xp_strcpy wcscpy
#define xp_strcmp wcscmp #define xp_strcmp wcscmp

View File

@ -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 <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -7,6 +7,7 @@
#ifndef __STAND_ALONE #ifndef __STAND_ALONE
#include <xp/bas/string.h> #include <xp/bas/string.h>
#include <xp/bas/memory.h> #include <xp/bas/memory.h>
#include <xp/bas/assert.h>
#endif #endif
static xp_awk_val_nil_t __awk_nil = { XP_AWK_VAL_NIL, 0 }; 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_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; 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]; 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 (awk->run.icache_count > 0)
if (val == XP_NULL) return XP_NULL; {
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->type = XP_AWK_VAL_INT;
val->ref = 0; 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]); 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; if (xp_awk_isbuiltinval(val)) return;
switch (val->type) 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: case XP_AWK_VAL_STR:
xp_free (((xp_awk_val_str_t*)val)->buf); xp_free (((xp_awk_val_str_t*)val)->buf);
default:
xp_free (val); xp_free (val);
break;
} }
/* should never reach here */
} }
void xp_awk_refupval (xp_awk_val_t* val) void xp_awk_refupval (xp_awk_val_t* val)
@ -111,7 +141,7 @@ xp_printf (XP_TEXT("\n"));
val->ref++; 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; if (xp_awk_isbuiltinval(val)) return;
@ -130,11 +160,11 @@ xp_printf (XP_TEXT("**FREEING "));
xp_awk_printval (val); xp_awk_printval (val);
xp_printf (XP_TEXT("\n")); 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; if (xp_awk_isbuiltinval(val)) return;
@ -142,7 +172,7 @@ void xp_awk_refdownval_nofree (xp_awk_val_t* val)
val->ref--; 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; 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: case XP_AWK_VAL_NIL:
return xp_awk_val_nil; return xp_awk_val_nil;
case XP_AWK_VAL_INT: 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: case XP_AWK_VAL_REAL:
return xp_awk_makerealval (((xp_awk_val_real_t*)val)->val); return xp_awk_makerealval (((xp_awk_val_real_t*)val)->val);
case XP_AWK_VAL_STR: case XP_AWK_VAL_STR:

View File

@ -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_ #ifndef _XP_AWK_VAL_H_
@ -11,10 +11,10 @@
enum enum
{ {
XP_AWK_VAL_NIL, XP_AWK_VAL_NIL = 0,
XP_AWK_VAL_INT, XP_AWK_VAL_INT = 1,
XP_AWK_VAL_REAL, XP_AWK_VAL_REAL = 2,
XP_AWK_VAL_STR XP_AWK_VAL_STR = 3
}; };
typedef struct xp_awk_val_t xp_awk_val_t; 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; 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_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); 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_refupval (xp_awk_val_t* val);
void xp_awk_refdownval (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_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); xp_bool_t xp_awk_isvaltrue (xp_awk_val_t* val);
void xp_awk_printval (xp_awk_val_t* val); void xp_awk_printval (xp_awk_val_t* val);