added more character conversion functions

This commit is contained in:
hyunghwan.chung 2017-01-12 10:06:43 +00:00
parent d130ec02ec
commit ec3285da57
10 changed files with 96 additions and 51 deletions

View File

@ -13,7 +13,6 @@ class MyObject(Object)
'not implemented' 'not implemented'
'subsystem error' 'subsystem error'
'internal error that should never have happened' 'internal error that should never have happened'
'insufficient system memory' 'insufficient system memory'
'insufficient object memory' 'insufficient object memory'
@ -39,6 +38,7 @@ class MyObject(Object)
'divide by zero' 'divide by zero'
'I/O error' 'I/O error'
'encoding conversion error' 'encoding conversion error'
'buffer full'
). ).
synerrmsgs := #( synerrmsgs := #(

View File

@ -58,12 +58,13 @@ static moo_ooch_t errstr_24[] = {'s','e','m','a','p','h','o','r','e',' ','l','i'
static moo_ooch_t errstr_25[] = {'d','i','v','i','d','e',' ','b','y',' ','z','e','r','o','\0'}; static moo_ooch_t errstr_25[] = {'d','i','v','i','d','e',' ','b','y',' ','z','e','r','o','\0'};
static moo_ooch_t errstr_26[] = {'I','/','O',' ','e','r','r','o','r','\0'}; static moo_ooch_t errstr_26[] = {'I','/','O',' ','e','r','r','o','r','\0'};
static moo_ooch_t errstr_27[] = {'e','n','c','o','d','i','n','g',' ','c','o','n','v','e','r','s','i','o','n',' ','e','r','r','o','r','\0'}; static moo_ooch_t errstr_27[] = {'e','n','c','o','d','i','n','g',' ','c','o','n','v','e','r','s','i','o','n',' ','e','r','r','o','r','\0'};
static moo_ooch_t errstr_28[] = {'b','u','f','f','e','r',' ','f','u','l','l','\0'};
static moo_ooch_t* errstr[] = static moo_ooch_t* errstr[] =
{ {
errstr_0, errstr_1, errstr_2, errstr_3, errstr_4, errstr_5, errstr_6, errstr_7, errstr_0, errstr_1, errstr_2, errstr_3, errstr_4, errstr_5, errstr_6, errstr_7,
errstr_8, errstr_9, errstr_10, errstr_11, errstr_12, errstr_13, errstr_14, errstr_15, errstr_8, errstr_9, errstr_10, errstr_11, errstr_12, errstr_13, errstr_14, errstr_15,
errstr_16, errstr_17, errstr_18, errstr_19, errstr_20, errstr_21, errstr_22, errstr_23, errstr_16, errstr_17, errstr_18, errstr_19, errstr_20, errstr_21, errstr_22, errstr_23,
errstr_24, errstr_25, errstr_26, errstr_27 errstr_24, errstr_25, errstr_26, errstr_27, errstr_28
}; };
#if defined(MOO_INCLUDE_COMPILER) #if defined(MOO_INCLUDE_COMPILER)

View File

@ -434,12 +434,7 @@ reswitch:
/* get the length */ /* get the length */
for (bslen = 0; bsp[bslen]; bslen++); for (bslen = 0; bsp[bslen]; bslen++);
if (moo_convbtooochars (moo, bsp, &bslen, MOO_NULL, &slen) <= -1) if (moo_convbtooochars (moo, bsp, &bslen, MOO_NULL, &slen) <= -1) goto oops;
{
/* conversion error */
moo->errnum = MOO_EECERR;
goto oops;
}
/* slen holds the length after conversion */ /* slen holds the length after conversion */
n = slen; n = slen;
@ -508,12 +503,7 @@ reswitch:
/* get the length */ /* get the length */
for (uslen = 0; usp[uslen]; uslen++); for (uslen = 0; usp[uslen]; uslen++);
if (moo_convutooochars (moo, usp, &uslen, MOO_NULL, &slen) <= -1) if (moo_convutooochars (moo, usp, &uslen, MOO_NULL, &slen) <= -1) goto oops;
{
/* conversion error */
moo->errnum = MOO_EECERR;
goto oops;
}
/* slen holds the length after conversion */ /* slen holds the length after conversion */
n = slen; n = slen;

View File

@ -165,13 +165,8 @@ static MOO_INLINE moo_ooi_t open_input (moo_t* moo, moo_ioarg_t* arg)
moo_oow_t bcslen = MOO_COUNTOF(bcs); moo_oow_t bcslen = MOO_COUNTOF(bcs);
moo_oow_t ucslen; moo_oow_t ucslen;
if (moo_convootobcstr (moo, arg->name, &ucslen, bcs, &bcslen) <= -1) if (moo_convootobcstr (moo, arg->name, &ucslen, bcs, &bcslen) <= -1) return -1;
{
moo_seterrnum (moo, MOO_EECERR);
return -1;
}
/* TODO: make bcs relative to the includer */ /* TODO: make bcs relative to the includer */
#if defined(__DOS__) || defined(_WIN32) || defined(__OS2__) #if defined(__DOS__) || defined(_WIN32) || defined(__OS2__)
fp = fopen (bcs, "rb"); fp = fopen (bcs, "rb");
@ -230,7 +225,6 @@ static MOO_INLINE moo_ooi_t read_input (moo_t* moo, moo_ioarg_t* arg)
moo_oow_t bcslen, ucslen, remlen; moo_oow_t bcslen, ucslen, remlen;
int x; int x;
bb = (bb_t*)arg->handle; bb = (bb_t*)arg->handle;
MOO_ASSERT (moo, bb != MOO_NULL && bb->fp != MOO_NULL); MOO_ASSERT (moo, bb != MOO_NULL && bb->fp != MOO_NULL);
do do
@ -253,12 +247,9 @@ static MOO_INLINE moo_ooi_t read_input (moo_t* moo, moo_ioarg_t* arg)
bcslen = bb->len; bcslen = bb->len;
ucslen = MOO_COUNTOF(arg->buf); ucslen = MOO_COUNTOF(arg->buf);
x = moo_convbtooochars (moo, bb->buf, &bcslen, arg->buf, &ucslen); x = moo_convbtooochars (moo, bb->buf, &bcslen, arg->buf, &ucslen);
x = moo_convbtooochars (moo, bb->buf, &bcslen, arg->buf, &ucslen); if (x <= -1 && ucslen <= 0) return -1;
if (x <= -1 && ucslen <= 0) /* if ucslen is greater than 0, i see that some characters have been
{ * converted properly */
moo_seterrnum (moo, MOO_EECERR);
return -1;
}
remlen = bb->len - bcslen; remlen = bb->len - bcslen;
if (remlen > 0) memmove (bb->buf, &bb->buf[bcslen], remlen); if (remlen > 0) memmove (bb->buf, &bb->buf[bcslen], remlen);
@ -681,7 +672,7 @@ int main (int argc, char* argv[])
if (moo_ignite(moo) <= -1) if (moo_ignite(moo) <= -1)
{ {
printf ("cannot ignite moo - %d\n", moo_geterrnum(moo)); moo_logbfmt (moo, MOO_LOG_ERROR, "cannot ignite moo - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
moo_close (moo); moo_close (moo);
return -1; return -1;
} }
@ -724,7 +715,6 @@ int main (int argc, char* argv[])
printf ("%s ", xtn->source_path); printf ("%s ", xtn->source_path);
} }
printf ("syntax error at line %lu column %lu - ", printf ("syntax error at line %lu column %lu - ",
(unsigned long int)synerr.loc.line, (unsigned long int)synerr.loc.colm); (unsigned long int)synerr.loc.line, (unsigned long int)synerr.loc.colm);
@ -749,7 +739,7 @@ int main (int argc, char* argv[])
} }
else else
{ {
printf ("ERROR: cannot compile code - %d\n", moo_geterrnum(moo)); moo_logbfmt (moo, MOO_LOG_ERROR, "ERROR: cannot compile code - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
} }
moo_close (moo); moo_close (moo);
#if defined(USE_LTDL) #if defined(USE_LTDL)
@ -770,7 +760,7 @@ int main (int argc, char* argv[])
mthname.len = 4; mthname.len = 4;
if (moo_invoke (moo, &objname, &mthname) <= -1) if (moo_invoke (moo, &objname, &mthname) <= -1)
{ {
printf ("ERROR: cannot execute code - %d\n", moo_geterrnum(moo)); moo_logbfmt (moo, MOO_LOG_ERROR, "ERROR: cannot execute code - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
xret = -1; xret = -1;
} }

View File

@ -364,7 +364,7 @@ struct moo_compiler_t
/* unget buffer */ /* unget buffer */
moo_iolxc_t ungot[10]; moo_iolxc_t ungot[10];
int nungots; int nungots;
/* static input data buffer */ /* static input data buffer */
moo_ioarg_t arg; moo_ioarg_t arg;

View File

@ -75,6 +75,7 @@ enum moo_errnum_t
MOO_EDIVBY0, /**< divide by zero */ MOO_EDIVBY0, /**< divide by zero */
MOO_EIOERR, /**< I/O error */ MOO_EIOERR, /**< I/O error */
MOO_EECERR, /**< encoding conversion error */ MOO_EECERR, /**< encoding conversion error */
MOO_EBUFFULL, /**< buffer full */
#if defined(MOO_INCLUDE_COMPILER) #if defined(MOO_INCLUDE_COMPILER)
MOO_ESYNTAX /** < syntax error */ MOO_ESYNTAX /** < syntax error */
@ -1214,7 +1215,7 @@ MOO_EXPORT const moo_ooch_t* moo_geterrstr (
MOO_EXPORT int moo_getoption ( MOO_EXPORT int moo_getoption (
moo_t* moo, moo_t* moo,
moo_option_t id, moo_option_t id,
void* value void* value
); );
/** /**
@ -1351,13 +1352,13 @@ MOO_EXPORT void* moo_callocmem (
); );
MOO_EXPORT void* moo_reallocmem ( MOO_EXPORT void* moo_reallocmem (
moo_t* moo, moo_t* moo,
void* ptr, void* ptr,
moo_oow_t size moo_oow_t size
); );
MOO_EXPORT void moo_freemem ( MOO_EXPORT void moo_freemem (
moo_t* moo, moo_t* moo,
void* ptr void* ptr
); );
@ -1371,7 +1372,7 @@ MOO_EXPORT int moo_genpfmethod (
moo_oop_t _class, moo_oop_t _class,
moo_method_type_t type, moo_method_type_t type,
const moo_ooch_t* mthname, const moo_ooch_t* mthname,
int variadic, int variadic,
const moo_ooch_t* name const moo_ooch_t* name
); );
@ -1482,8 +1483,13 @@ MOO_EXPORT const moo_ooch_t* moo_errnumtoerrstr (
moo_errnum_t errnum moo_errnum_t errnum
); );
#if defined(MOO_INCLUDE_COMPILER) #if defined(MOO_INCLUDE_COMPILER)
/* =========================================================================
* COMPILER FUNCTIONS
* ========================================================================= */
MOO_EXPORT int moo_compile ( MOO_EXPORT int moo_compile (
moo_t* moo, moo_t* moo,
moo_ioimpl_t io moo_ioimpl_t io

View File

@ -625,23 +625,87 @@ int moo_convutoutf8cstr (const moo_uch_t* ucs, moo_oow_t* ucslen, moo_bch_t* bcs
int moo_convbtouchars (moo_t* moo, const moo_bch_t* bcs, moo_oow_t* bcslen, moo_uch_t* ucs, moo_oow_t* ucslen) int moo_convbtouchars (moo_t* moo, const moo_bch_t* bcs, moo_oow_t* bcslen, moo_uch_t* ucs, moo_oow_t* ucslen)
{ {
/* length bound */ /* length bound */
return bcsn_to_ucsn_with_cmgr (bcs, bcslen, ucs, ucslen, moo->cmgr, 0); int n;
n = bcsn_to_ucsn_with_cmgr (bcs, bcslen, ucs, ucslen, moo->cmgr, 0);
/* -1: illegal character, -2, buffer too small, -3: incomplete sequence */
moo->errnum = (n == -2)? MOO_EBUFFULL: MOO_EECERR;
return n;
} }
int moo_convutobchars (moo_t* moo, const moo_uch_t* ucs, moo_oow_t* ucslen, moo_bch_t* bcs, moo_oow_t* bcslen) int moo_convutobchars (moo_t* moo, const moo_uch_t* ucs, moo_oow_t* ucslen, moo_bch_t* bcs, moo_oow_t* bcslen)
{ {
/* length bound */ /* length bound */
return ucsn_to_bcsn_with_cmgr (ucs, ucslen, bcs, bcslen, moo->cmgr); int n;
n = ucsn_to_bcsn_with_cmgr (ucs, ucslen, bcs, bcslen, moo->cmgr);
moo->errnum = (n == -2)? MOO_EBUFFULL: MOO_EECERR;
return n;
} }
int moo_convbtoucstr (moo_t* moo, const moo_bch_t* bcs, moo_oow_t* bcslen, moo_uch_t* ucs, moo_oow_t* ucslen) int moo_convbtoucstr (moo_t* moo, const moo_bch_t* bcs, moo_oow_t* bcslen, moo_uch_t* ucs, moo_oow_t* ucslen)
{ {
/* null-terminated. */ /* null-terminated. */
return bcs_to_ucs_with_cmgr (bcs, bcslen, ucs, ucslen, moo->cmgr, 0); int n;
n = bcs_to_ucs_with_cmgr (bcs, bcslen, ucs, ucslen, moo->cmgr, 0);
moo->errnum = (n == -2)? MOO_EBUFFULL: MOO_EECERR;
return n;
} }
int moo_convutobcstr (moo_t* moo, const moo_uch_t* ucs, moo_oow_t* ucslen, moo_bch_t* bcs, moo_oow_t* bcslen) int moo_convutobcstr (moo_t* moo, const moo_uch_t* ucs, moo_oow_t* ucslen, moo_bch_t* bcs, moo_oow_t* bcslen)
{ {
/* null-terminated */ /* null-terminated */
return ucs_to_bcs_with_cmgr (ucs, ucslen, bcs, bcslen, moo->cmgr); int n;
n = ucs_to_bcs_with_cmgr (ucs, ucslen, bcs, bcslen, moo->cmgr);
moo->errnum = (n == -2)? MOO_EBUFFULL: MOO_EECERR;
return n;
}
/* ----------------------------------------------------------------------- */
moo_uch_t* moo_dupbtouchars (moo_t* moo, const moo_bch_t* bcs, moo_oow_t bcslen, moo_oow_t* ucslen)
{
moo_oow_t inlen, reqlen;
moo_uch_t* ptr;
inlen = bcslen;
if (moo_convbtouchars (moo, bcs, &inlen, MOO_NULL, &reqlen) <= -1)
{
/* note it's also an error if no full conversion is possible in this function */
return MOO_NULL;
}
ptr = moo_allocmem (moo, reqlen * MOO_SIZEOF(moo_uch_t));
if (!ptr) return MOO_NULL;
inlen = bcslen;
moo_convbtouchars (moo, bcs, &inlen, ptr, ucslen);
return ptr;
}
moo_bch_t* moo_duputobchars (moo_t* moo, const moo_uch_t* ucs, moo_oow_t ucslen, moo_oow_t* bcslen)
{
moo_oow_t inlen, reqlen;
moo_bch_t* ptr;
inlen = ucslen;
if (moo_convutobchars (moo, ucs, &inlen, MOO_NULL, &reqlen) <= -1)
{
/* note it's also an error if no full conversion is possible in this function */
return MOO_NULL;
}
ptr = moo_allocmem (moo, reqlen * MOO_SIZEOF(moo_bch_t));
if (!ptr) return MOO_NULL;
inlen = ucslen;
moo_convutobchars (moo, ucs, &inlen, ptr, bcslen);
return ptr;
} }

View File

@ -181,11 +181,7 @@ static moo_pfrc_t pf_write (moo_t* moo, moo_ooi_t nargs)
bcslen = MOO_COUNTOF(bcs); bcslen = MOO_COUNTOF(bcs);
if ((n = moo_convootobchars (moo, &oomsg->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1) if ((n = moo_convootobchars (moo, &oomsg->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1)
{ {
if (n != -2 || ucslen <= 0) if (n != -2 || ucslen <= 0) return MOO_PF_HARD_FAILURE;
{
moo_seterrnum (moo, MOO_EECERR);
return MOO_PF_HARD_FAILURE;
}
} }
write (con->fd, bcs, bcslen); /* TODO: error handling */ write (con->fd, bcs, bcslen); /* TODO: error handling */

View File

@ -122,6 +122,7 @@ reterr:
MOO_STACK_SETRETTOERROR (moo, nargs); MOO_STACK_SETRETTOERROR (moo, nargs);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
} }
/* /*
struct bcstr_node_t struct bcstr_node_t
{ {
@ -141,6 +142,7 @@ static void free_bcstr_node (moo_t* moo, bcstr_node_t* bcstr)
moo_freemem (moo, bcstr); moo_freemem (moo, bcstr);
} }
*/ */
static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs) static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
{ {
#if defined(USE_DYNCALL) #if defined(USE_DYNCALL)

View File

@ -159,11 +159,7 @@ static moo_pfrc_t __pf_puts (moo_t* moo, moo_ooi_t nargs, moo_oow_t limit)
/* TODO: implement character conversion into stdio and use it instead of vm's conversion facility. */ /* TODO: implement character conversion into stdio and use it instead of vm's conversion facility. */
if ((n = moo_convootobchars (moo, &x->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1) if ((n = moo_convootobchars (moo, &x->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1)
{ {
if (n != -2 || ucslen <= 0) if (n != -2 || ucslen <= 0) goto reterr;
{
moo_seterrnum (moo, MOO_EECERR);
goto reterr;
}
} }
if (fwrite (bcs, 1, bcslen, rcv->fp) < bcslen) if (fwrite (bcs, 1, bcslen, rcv->fp) < bcslen)