touched up code

This commit is contained in:
hyung-hwan 2011-05-26 08:42:26 +00:00
parent 2cbe0d4681
commit 38b00c0756
3 changed files with 332 additions and 276 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: std.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ * $Id: std.c 481 2011-05-25 14:42:26Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -279,7 +279,7 @@ qse_awk_t* qse_awk_openstdwithmmgr (qse_mmgr_t* mmgr, qse_size_t xtnsize)
QSE_MEMSET (xtn, 0, QSE_SIZEOF(xtn_t)); QSE_MEMSET (xtn, 0, QSE_SIZEOF(xtn_t));
/* add intrinsic functions */ /* add intrinsic functions */
if (add_functions (awk) == -1) if (add_functions (awk) <= -1)
{ {
qse_awk_close (awk); qse_awk_close (awk);
return QSE_NULL; return QSE_NULL;
@ -348,9 +348,11 @@ static qse_ssize_t sf_in_open (
xtn->s.in.handle = QSE_NULL; xtn->s.in.handle = QSE_NULL;
return 1; return 1;
default:
return -1;
} }
/* this should never happen */
qse_awk_seterrnum (awk, QSE_AWK_EINTERN, QSE_NULL);
return -1;
} }
else else
{ {
@ -470,10 +472,10 @@ static qse_ssize_t sf_in_read (
} }
return n; return n;
} }
default:
return -1;
} }
qse_awk_seterrnum (awk, QSE_AWK_EINTERN, QSE_NULL);
return -1;
} }
else else
{ {
@ -509,9 +511,10 @@ static qse_ssize_t sf_in (
case QSE_AWK_SIO_READ: case QSE_AWK_SIO_READ:
return sf_in_read (awk, arg, data, size, xtn); return sf_in_read (awk, arg, data, size, xtn);
default:
return -1;
} }
qse_awk_seterrnum (awk, QSE_AWK_EINTERN, QSE_NULL);
return -1;
} }
static qse_ssize_t sf_out ( static qse_ssize_t sf_out (
@ -520,7 +523,9 @@ static qse_ssize_t sf_out (
{ {
xtn_t* xtn = QSE_XTN (awk); xtn_t* xtn = QSE_XTN (awk);
if (cmd == QSE_AWK_SIO_OPEN) switch (cmd)
{
case QSE_AWK_SIO_OPEN:
{ {
switch (xtn->s.out.type) switch (xtn->s.out.type)
{ {
@ -563,8 +568,12 @@ static qse_ssize_t sf_out (
xtn->s.out.handle = QSE_NULL; xtn->s.out.handle = QSE_NULL;
return 1; return 1;
} }
break;
} }
else if (cmd == QSE_AWK_SIO_CLOSE)
case QSE_AWK_SIO_CLOSE:
{ {
switch (xtn->s.out.type) switch (xtn->s.out.type)
{ {
@ -590,8 +599,11 @@ static qse_ssize_t sf_out (
xtn->s.out.u.cpl.osp->ptr; xtn->s.out.u.cpl.osp->ptr;
return 0; return 0;
} }
break;
} }
else if (cmd == QSE_AWK_SIO_WRITE)
case QSE_AWK_SIO_WRITE:
{ {
switch (xtn->s.out.type) switch (xtn->s.out.type)
{ {
@ -632,8 +644,12 @@ static qse_ssize_t sf_out (
return n; return n;
} }
} }
break;
}
} }
qse_awk_seterrnum (awk, QSE_AWK_EINTERN, QSE_NULL);
return -1; return -1;
} }
@ -675,6 +691,7 @@ int qse_awk_parsestd (
qse_awk_seterrnum (awk, QSE_AWK_EINVAL, QSE_NULL); qse_awk_seterrnum (awk, QSE_AWK_EINVAL, QSE_NULL);
return -1; return -1;
} }
xtn->s.in.type = in->type; xtn->s.in.type = in->type;
xtn->s.in.handle = QSE_NULL; xtn->s.in.handle = QSE_NULL;
sio.in = sf_in; sio.in = sf_in;
@ -1139,12 +1156,12 @@ static qse_ssize_t awk_rio_console (
qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod,
qse_char_t* data, qse_size_t size) qse_char_t* data, qse_size_t size)
{ {
if (cmd == QSE_AWK_RIO_OPEN) switch (cmd)
{ {
case QSE_AWK_RIO_OPEN:
return open_rio_console (rtx, riod); return open_rio_console (rtx, riod);
}
else if (cmd == QSE_AWK_RIO_CLOSE) case QSE_AWK_RIO_CLOSE:
{
if (riod->handle != QSE_NULL && if (riod->handle != QSE_NULL &&
riod->handle != qse_sio_in && riod->handle != qse_sio_in &&
riod->handle != qse_sio_out && riod->handle != qse_sio_out &&
@ -1154,8 +1171,8 @@ static qse_ssize_t awk_rio_console (
} }
return 0; return 0;
}
else if (cmd == QSE_AWK_RIO_READ) case QSE_AWK_RIO_READ:
{ {
qse_ssize_t nn; qse_ssize_t nn;
@ -1165,7 +1182,7 @@ static qse_ssize_t awk_rio_console (
qse_sio_t* sio = (qse_sio_t*)riod->handle; qse_sio_t* sio = (qse_sio_t*)riod->handle;
n = open_rio_console (rtx, riod); n = open_rio_console (rtx, riod);
if (n == -1) return -1; if (n <= -1) return -1;
if (n == 0) if (n == 0)
{ {
@ -1184,25 +1201,24 @@ static qse_ssize_t awk_rio_console (
return nn; return nn;
} }
else if (cmd == QSE_AWK_RIO_WRITE)
{ case QSE_AWK_RIO_WRITE:
return qse_sio_putsn ( return qse_sio_putsn (
(qse_sio_t*)riod->handle, (qse_sio_t*)riod->handle,
data, data,
size size
); );
}
else if (cmd == QSE_AWK_RIO_FLUSH) case QSE_AWK_RIO_FLUSH:
{
return qse_sio_flush ((qse_sio_t*)riod->handle); return qse_sio_flush ((qse_sio_t*)riod->handle);
}
else if (cmd == QSE_AWK_RIO_NEXT) case QSE_AWK_RIO_NEXT:
{ {
int n; int n;
qse_sio_t* sio = (qse_sio_t*)riod->handle; qse_sio_t* sio = (qse_sio_t*)riod->handle;
n = open_rio_console (rtx, riod); n = open_rio_console (rtx, riod);
if (n == -1) return -1; if (n <= -1) return -1;
if (n == 0) if (n == 0)
{ {
@ -1221,6 +1237,9 @@ static qse_ssize_t awk_rio_console (
return n; return n;
} }
}
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINTERN, QSE_NULL);
return -1; return -1;
} }
@ -1296,7 +1315,7 @@ qse_awk_rtx_t* qse_awk_rtx_openstd (
rxtn = (rxtn_t*) QSE_XTN (rtx); rxtn = (rxtn_t*) QSE_XTN (rtx);
QSE_MEMSET (rxtn, 0, QSE_SIZEOF(rxtn_t)); QSE_MEMSET (rxtn, 0, QSE_SIZEOF(rxtn_t));
if (qse_gettime (&now) == -1) rxtn->seed = 0; if (qse_gettime (&now) <= -1) rxtn->seed = 0;
else rxtn->seed = (unsigned int) now; else rxtn->seed = (unsigned int) now;
srand (rxtn->seed); srand (rxtn->seed);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: val.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ * $Id: val.c 481 2011-05-25 14:42:26Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -599,25 +599,33 @@ void qse_awk_rtx_freeval (
qse_dprintf (QSE_T("\n")); qse_dprintf (QSE_T("\n"));
#endif #endif
if (val->type == QSE_AWK_VAL_NIL) switch (val->type)
{
case QSE_AWK_VAL_NIL:
{ {
QSE_AWK_FREE (rtx->awk, val); QSE_AWK_FREE (rtx->awk, val);
break;
} }
else if (val->type == QSE_AWK_VAL_INT)
case QSE_AWK_VAL_INT:
{ {
((qse_awk_val_int_t*)val)->nde = ((qse_awk_val_int_t*)val)->nde =
(qse_awk_nde_int_t*)rtx->vmgr.ifree; (qse_awk_nde_int_t*)rtx->vmgr.ifree;
rtx->vmgr.ifree = (qse_awk_val_int_t*)val; rtx->vmgr.ifree = (qse_awk_val_int_t*)val;
break;
} }
else if (val->type == QSE_AWK_VAL_REAL)
case QSE_AWK_VAL_REAL:
{ {
((qse_awk_val_real_t*)val)->nde = ((qse_awk_val_real_t*)val)->nde =
(qse_awk_nde_real_t*)rtx->vmgr.rfree; (qse_awk_nde_real_t*)rtx->vmgr.rfree;
rtx->vmgr.rfree = (qse_awk_val_real_t*)val; rtx->vmgr.rfree = (qse_awk_val_real_t*)val;
break;
} }
else if (val->type == QSE_AWK_VAL_STR)
case QSE_AWK_VAL_STR:
{ {
#ifdef ENABLE_FEATURE_SCACHE #ifdef ENABLE_FEATURE_SCACHE
if (cache) if (cache)
{ {
qse_awk_val_str_t* v = (qse_awk_val_str_t*)val; qse_awk_val_str_t* v = (qse_awk_val_str_t*)val;
@ -633,10 +641,13 @@ void qse_awk_rtx_freeval (
else QSE_AWK_FREE (rtx->awk, val); else QSE_AWK_FREE (rtx->awk, val);
} }
else else
#endif #endif
QSE_AWK_FREE (rtx->awk, val); QSE_AWK_FREE (rtx->awk, val);
break;
} }
else if (val->type == QSE_AWK_VAL_REX)
case QSE_AWK_VAL_REX:
{ {
/* don't free ptr as it is inlined to val /* don't free ptr as it is inlined to val
QSE_AWK_FREE (rtx->awk, ((qse_awk_val_rex_t*)val)->ptr); QSE_AWK_FREE (rtx->awk, ((qse_awk_val_rex_t*)val)->ptr);
@ -648,26 +659,33 @@ void qse_awk_rtx_freeval (
*/ */
QSE_AWK_FREE (rtx->awk, val); QSE_AWK_FREE (rtx->awk, val);
break;
} }
else if (val->type == QSE_AWK_VAL_MAP)
case QSE_AWK_VAL_MAP:
{ {
qse_htb_fini (((qse_awk_val_map_t*)val)->map); qse_htb_fini (((qse_awk_val_map_t*)val)->map);
QSE_AWK_FREE (rtx->awk, val); QSE_AWK_FREE (rtx->awk, val);
break;
} }
else if (val->type == QSE_AWK_VAL_REF)
case QSE_AWK_VAL_REF:
{ {
if (cache && rtx->fcache_count < QSE_COUNTOF(rtx->fcache)) if (cache && rtx->fcache_count < QSE_COUNTOF(rtx->fcache))
{ {
rtx->fcache[rtx->fcache_count++] = rtx->fcache[rtx->fcache_count++] = (qse_awk_val_ref_t*)val;
(qse_awk_val_ref_t*)val;
} }
else QSE_AWK_FREE (rtx->awk, val); else QSE_AWK_FREE (rtx->awk, val);
break;
} }
else
default:
{ {
QSE_ASSERTX ( QSE_ASSERTX (
!"should never happen - invalid value type", !"should never happen - invalid value type",
"the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h"); "the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h");
break;
}
} }
} }
@ -745,12 +763,13 @@ qse_bool_t qse_awk_rtx_valtobool (qse_awk_rtx_t* run, const qse_awk_val_t* val)
return QSE_FALSE; /* TODO: is this correct? */ return QSE_FALSE; /* TODO: is this correct? */
case QSE_AWK_VAL_REF: case QSE_AWK_VAL_REF:
return QSE_FALSE; /* TODO: is this correct? */ return QSE_FALSE; /* TODO: is this correct? */
}
default:
QSE_ASSERTX ( QSE_ASSERTX (
!"should never happen - invalid value type", !"should never happen - invalid value type",
"the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h"); "the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h");
return QSE_FALSE; return QSE_FALSE;
}
} }
static int str_to_str ( static int str_to_str (
@ -809,7 +828,6 @@ static int str_to_str (
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return -1; return -1;
} }
return 0; return 0;
} }
@ -823,13 +841,15 @@ static int str_to_str (
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return -1; return -1;
} }
return 0; return 0;
} }
}
default:
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
return -1; return -1;
}
}
} }
static int val_int_to_str ( static int val_int_to_str (
@ -924,6 +944,12 @@ static int val_int_to_str (
} }
break; break;
} }
default:
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
return -1;
}
} }
t = v->val; t = v->val;
@ -1090,16 +1116,19 @@ int qse_awk_rtx_valtostr (
qse_awk_val_str_t* vs = (qse_awk_val_str_t*)v; qse_awk_val_str_t* vs = (qse_awk_val_str_t*)v;
return str_to_str (rtx, vs->val.ptr, vs->val.len, out); return str_to_str (rtx, vs->val.ptr, vs->val.len, out);
} }
}
#ifdef DEBUG_VAL default:
{
#ifdef DEBUG_VAL
qse_dprintf ( qse_dprintf (
QSE_T("ERROR: WRONG VALUE TYPE [%d] in qse_awk_rtx_valtostr\n"), QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_valtostr\n"),
v->type); v->type
#endif );
#endif
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL); qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL);
return -1; return -1;
}
}
} }
qse_char_t* qse_awk_rtx_valtocpldup ( qse_char_t* qse_awk_rtx_valtocpldup (
@ -1117,25 +1146,27 @@ qse_char_t* qse_awk_rtx_valtocpldup (
int qse_awk_rtx_valtonum ( int qse_awk_rtx_valtonum (
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_long_t* l, qse_real_t* r) qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_long_t* l, qse_real_t* r)
{ {
if (v->type == QSE_AWK_VAL_NIL) switch (v->type)
{
case QSE_AWK_VAL_NIL:
{ {
*l = 0; *l = 0;
return 0; return 0;
} }
if (v->type == QSE_AWK_VAL_INT) case QSE_AWK_VAL_INT:
{ {
*l = ((qse_awk_val_int_t*)v)->val; *l = ((qse_awk_val_int_t*)v)->val;
return 0; /* long */ return 0; /* long */
} }
if (v->type == QSE_AWK_VAL_REAL) case QSE_AWK_VAL_REAL:
{ {
*r = ((qse_awk_val_real_t*)v)->val; *r = ((qse_awk_val_real_t*)v)->val;
return 1; /* real */ return 1; /* real */
} }
if (v->type == QSE_AWK_VAL_STR) case QSE_AWK_VAL_STR:
{ {
return qse_awk_rtx_strtonum ( return qse_awk_rtx_strtonum (
rtx, 0, rtx, 0,
@ -1145,14 +1176,20 @@ int qse_awk_rtx_valtonum (
); );
} }
#ifdef DEBUG_VAL default:
{
#ifdef DEBUG_VAL
qse_dprintf ( qse_dprintf (
QSE_T("ERROR: WRONG VALUE TYPE [%d] in qse_awk_rtx_valtonum\n"), QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_valtonum\n"),
v->type); v->type
#endif );
#endif
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL); qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL);
return -1; /* error */ return -1; /* error */
}
}
} }
int qse_awk_rtx_strtonum ( int qse_awk_rtx_strtonum (

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk03.c 457 2011-05-12 16:16:57Z hyunghwan.chung $ * $Id: awk03.c 481 2011-05-25 14:42:26Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -25,7 +25,7 @@
static const qse_char_t* src = QSE_T( static const qse_char_t* src = QSE_T(
"function init() { a = 20; return a; }" "function init() { a = 20; return a; }"
"function main() { return ++a; }" "function main() { return ++a; }"
"function fini() { print ++a; return a; }" "function fini() { print \"a in fini() =>\", ++a; return a; }"
); );
static const qse_char_t* fnc[] = static const qse_char_t* fnc[] =