cleaned up code

This commit is contained in:
hyung-hwan 2009-03-02 08:54:28 +00:00
parent 004ba389f2
commit 8e221ca599
4 changed files with 99 additions and 64 deletions

View File

@ -33,12 +33,16 @@ enum qse_sed_errnum_t
QSE_SED_ECMDGB, /* command garbled */
QSE_SED_ELBLTL, /* label too long */
QSE_SED_EREXBL, /* regular expression build error */
QSE_SED_EA2NNC, /* address 2 not necessary */
QSE_SED_EA1PHB, /* address 1 prohibited */
QSE_SED_EA2PHB, /* address 2 prohibited */
QSE_SED_ENEWLN /* a new line is expected */
};
typedef struct qse_sed_t qse_sed_t;
typedef struct qse_sed_c_t qse_sed_c_t; /* command */
typedef enum qse_sed_errnum_t qse_sed_errnum_t;
struct qse_sed_t
{
QSE_DEFINE_COMMON_FIELDS (sed)
@ -48,7 +52,13 @@ struct qse_sed_t
qse_str_t rexbuf; /* temporary regular expression buffer */
/* command array */
qse_lda_t cmds;
/*qse_lda_t cmds;*/
struct
{
qse_sed_c_t* buf;
qse_sed_c_t* cur;
qse_sed_c_t* end;
} cmd;
};

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c 89 2009-02-28 15:27:03Z hyunghwan.chung $
* $Id: run.c 91 2009-03-01 14:54:28Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -3584,15 +3584,7 @@ static qse_awk_val_t* do_assignment_map (
len = QSE_COUNTOF(idxbuf);
str = idxnde_to_str (run, var->idx, idxbuf, &len);
/* TODO: VERIFY
if (str == QSE_NULL)
{
str = idxnde_to_str (run, var->idx, QSE_NULL, &len);
*/
if (str == QSE_NULL) return QSE_NULL;
/*
}
*/
if (str == QSE_NULL) return QSE_NULL;
#ifdef DEBUG_RUN
qse_dprintf (QSE_T("**** index str=>%s, map->ref=%d, map->type=%d\n"),
@ -3896,17 +3888,7 @@ static qse_awk_val_t* eval_binop_in (
str = (left->type == QSE_AWK_NDE_GRP)?
idxnde_to_str (run, ((qse_awk_nde_grp_t*)left)->body, idxbuf, &len):
idxnde_to_str (run, left, idxbuf, &len);
/* TODO: VERIFY
if (str == QSE_NULL)
{
str = (left->type == QSE_AWK_NDE_GRP)?
idxnde_to_str (run, ((qse_awk_nde_grp_t*)left)->body, QSE_NULL, &len):
idxnde_to_str (run, left, QSE_NULL, &len);
*/
if (str == QSE_NULL) return QSE_NULL;
/*
}
*/
if (str == QSE_NULL) return QSE_NULL;
/* evaluate the right-hand side of the operator */
QSE_ASSERT (right->next == QSE_NULL);
@ -6081,15 +6063,7 @@ static qse_awk_val_t** get_reference_indexed (
len = QSE_COUNTOF(idxbuf);
str = idxnde_to_str (run, nde->idx, idxbuf, &len);
/* TODO: VERIFY
if (str == QSE_NULL)
{
str = idxnde_to_str (run, nde->idx, QSE_NULL, &len);
*/
if (str == QSE_NULL) return QSE_NULL;
/*
}
*/
if (str == QSE_NULL) return QSE_NULL;
pair = qse_map_search ((*(qse_awk_val_map_t**)val)->map, str, len);
if (pair == QSE_NULL)
@ -6237,15 +6211,7 @@ static qse_awk_val_t* eval_indexed (
len = QSE_COUNTOF(idxbuf);
str = idxnde_to_str (run, nde->idx, idxbuf, &len);
/* TODO: VERIFY
if (str == QSE_NULL)
{
str = idxnde_to_str (run, nde->idx, QSE_NULL, &len);
*/
if (str == QSE_NULL) return QSE_NULL;
/*
}
*/
if (str == QSE_NULL) return QSE_NULL;
pair = qse_map_search ((*(qse_awk_val_map_t**)val)->map, str, len);
if (str != idxbuf) QSE_AWK_FREE (run->awk, str);

View File

@ -65,6 +65,16 @@ qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
return QSE_NULL;
}
/* TODO: use different data structure... */
sed->cmd.buf = QSE_MMGR_ALLOC (sed->mmgr, QSE_SIZEOF(qse_sed_c_t) * 1000);
if (sed->cmd.buf == QSE_NULL)
{
qse_str_fini (&sed->rexbuf);
return QSE_NULL;
}
sed->cmd.cur = sed->cmd.buf;
sed->cmd.end = sed->cmd.buf + 1000 - 1;
return sed;
}
@ -187,8 +197,9 @@ static const qse_char_t* command (
qse_sed_t* sed, const qse_char_t* ptr, const qse_char_t* end)
{
qse_cint_t c;
qse_sed_c_t* cmd = sed->cmd.cur;
c = CC (ptr, end);
c = NC (ptr, end);
switch (c)
{
@ -196,33 +207,58 @@ static const qse_char_t* command (
sed->errnum = QSE_SED_ECMDNR;
return QSE_NULL;
#if 0
case QSE_T('{'):
/* insert a negaited branch command at the beginning
/* insert a negated branch command at the beginning
* of a group. this way, all the commands in a group
* can be skipped. the branch target is set once a
* corresponding } is met. */
cmd.type = QSE_SED_C_BRANCH;
cmd.negfl = !cmd.negfl;
cmd->type = QSE_SED_C_JMP;
cmd->negfl = !cmd->negfl;
break;
case QSE_T('}'):
break;
case QSE_T('='):
cmd = QSE_SED_C_EQ;
if (ad2.type != QSE_SED_A_NONE)
case QSE_T(':'):
if (cmd->a1.type != QSE_SED_A_NONE)
{
sed->errnum = QSE_SED_EA2NNC;
/* label cannot have an address */
sed->errnum = QSE_SED_EA1PHB;
return QSE_NULL;
}
/* skip white spaces */
/* TODO: ... */
break;
case QSE_T('='):
cmd->type = QSE_SED_C_EQ;
if (cmd->a2.type != QSE_SED_A_NONE)
{
sed->errnum = QSE_SED_EA2PHB;
return QSE_NULL;
}
break;
#endif
case QSE_T(':'):
break;
case QSE_T('a'):
cmd->type = QSE_SED_C_A;
if (cmd->a2.type != QSE_SED_A_NONE)
{
sed->errnum = QSE_SED_EA2PHB;
return QSE_NULL;
}
c = CC (ptr, end);
if (c == QSE_T('\\')) c = NC (ptr, end);
if (c != QSE_T('\n')) /* TODO: handle \r\n or others */
{
/* new line is expected */
sed->errnum = QSE_SED_ENEWLN;
return QSE_NULL;
}
/* TODO: get the next line... */
break;
case QSE_T('c'):
@ -297,8 +333,16 @@ static const qse_char_t* fcomp (
{
qse_cint_t c;
const qse_char_t* end = ptr + len;
qse_sed_a_t a1, a2;
qse_sed_c_t* cmd = sed->cmd.cur;
/*
* # comment
* :label
* zero-address-command
* address[!] one-address-command
* address-range[!] address-range-command
*/
while (1)
{
c = CC (ptr, end);
@ -315,29 +359,36 @@ static const qse_char_t* fcomp (
if (c == QSE_T(';'))
{
/* semicolon without a meaningful address-command pair */
ptr++;
continue;
}
/* initialize the current command */
QSE_MEMSET (cmd, 0, QSE_SIZEOF(*cmd));
/* process address */
ptr = address (sed, ptr, end, &a1);
ptr = address (sed, ptr, end, &cmd->a1);
if (ptr == QSE_NULL) return QSE_NULL;
c = CC (ptr, end);
if (a1.type != QSE_SED_A_NONE)
if (cmd->a1.type != QSE_SED_A_NONE)
{
/* if (a1.type == QSE_SED_A_LAST)
/* if (cmd->a1.type == QSE_SED_A_LAST)
{
// TODO: ????
} */
if (c == QSE_T(',') || c == QSE_T(';'))
{
/* maybe an address range */
ptr++;
ptr = address (sed, ptr, end, &a2);
/* TODO: skip white spaces??? */
ptr = address (sed, ptr, end, &cmd->a2);
if (ptr == QSE_NULL) return QSE_NULL;
c = CC (ptr, end);
}
else a2.type = QSE_SED_A_NONE;
else cmd->a2.type = QSE_SED_A_NONE;
}
/* skip white spaces */
@ -347,14 +398,21 @@ static const qse_char_t* fcomp (
c = CC (ptr, end);
}
if (c == QSE_T('!'))
{
/* negate */
cmd->negfl = 1;
}
ptr = command (sed, ptr, end);
if (ptr == QSE_NULL) return QSE_NULL;
if (sed->cmd.cur >= sed->cmd.end)
{
/* TODO: too many commands */
}
cmd = ++sed->cmd.cur;
}
return ptr;

View File

@ -24,7 +24,6 @@
typedef qse_int_t qse_sed_line_t;
typedef struct qse_sed_a_t qse_sed_a_t; /* address */
typedef struct qse_sed_c_t qse_sed_c_t; /* command */
typedef struct qse_sed_l_t qse_sed_l_t; /* label */
struct qse_sed_a_t
@ -61,7 +60,9 @@ struct qse_sed_c_t
enum
{
QSE_SED_C_A,
QSE_SED_C_JMP,
QSE_SED_C_A, /* append text */
QSE_SED_C_B,
QSE_SED_C_C,
QSE_SED_C_CD,
@ -70,7 +71,7 @@ struct qse_sed_c_t
QSE_SED_C_CP,
QSE_SED_C_D,
QSE_SED_C_E,
QSE_SED_C_EQ,
QSE_SED_C_EQ, /* print the current line number */
QSE_SED_C_F,
QSE_SED_C_G,
QSE_SED_C_CG,