touched up the parser code for qse_awk_t a bit

This commit is contained in:
hyung-hwan 2011-07-26 01:32:49 +00:00
parent a9635f0ee0
commit 6ac70e5958
2 changed files with 160 additions and 181 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: parse.c 518 2011-07-24 14:24:13Z hyunghwan.chung $ * $Id: parse.c 520 2011-07-25 07:32:49Z 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.
@ -3354,7 +3354,8 @@ union folded_t
typedef union folded_t folded_t; typedef union folded_t folded_t;
static int fold_constants_for_binop ( static int fold_constants_for_binop (
qse_awk_t* awk, qse_awk_nde_t* left, qse_awk_nde_t* right, int opcode, folded_t* folded) qse_awk_t* awk, qse_awk_nde_t* left, qse_awk_nde_t* right,
int opcode, folded_t* folded)
{ {
int fold = -1; int fold = -1;
@ -3529,6 +3530,72 @@ static int fold_constants_for_binop (
return fold; return fold;
} }
static qse_awk_nde_t* new_int_node (
qse_awk_t* awk, qse_long_t lv, const qse_awk_loc_t* loc)
{
qse_awk_nde_int_t* tmp;
tmp = (qse_awk_nde_int_t*) QSE_AWK_ALLOC (
awk, QSE_SIZEOF(qse_awk_nde_int_t));
if (tmp == QSE_NULL)
{
SETERR_LOC (awk, QSE_AWK_ENOMEM, loc);
return QSE_NULL;
}
QSE_MEMSET (tmp, 0, QSE_SIZEOF(*tmp));
tmp->type = QSE_AWK_NDE_INT;
tmp->loc = *loc;
tmp->val = lv;
return (qse_awk_nde_t*)tmp;
}
static qse_awk_nde_t* new_real_node (
qse_awk_t* awk, qse_real_t rv, const qse_awk_loc_t* loc)
{
qse_awk_nde_real_t* tmp;
tmp = (qse_awk_nde_real_t*) QSE_AWK_ALLOC (
awk, QSE_SIZEOF(qse_awk_nde_real_t));
if (tmp == QSE_NULL)
{
SETERR_LOC (awk, QSE_AWK_ENOMEM, loc);
return QSE_NULL;
}
QSE_MEMSET (tmp, 0, QSE_SIZEOF(*tmp));
tmp->type = QSE_AWK_NDE_REAL;
tmp->loc = *loc;
tmp->val = rv;
return (qse_awk_nde_t*)tmp;
}
static QSE_INLINE void update_int_node (
qse_awk_t* awk, qse_awk_nde_int_t* node, qse_long_t lv)
{
node->val = lv;
if (node->str)
{
QSE_AWK_FREE (awk, node->str);
node->str = QSE_NULL;
node->len = 0;
}
}
static QSE_INLINE void update_real_node (
qse_awk_t* awk, qse_awk_nde_real_t* node, qse_real_t rv)
{
node->val = rv;
if (node->str)
{
QSE_AWK_FREE (awk, node->str);
node->str = QSE_NULL;
node->len = 0;
}
}
static qse_awk_nde_t* parse_binary ( static qse_awk_nde_t* parse_binary (
qse_awk_t* awk, const qse_awk_loc_t* xloc, qse_awk_t* awk, const qse_awk_loc_t* xloc,
int skipnl, const binmap_t* binmap, int skipnl, const binmap_t* binmap,
@ -3579,121 +3646,79 @@ static qse_awk_nde_t* parse_binary (
} }
fold = fold_constants_for_binop (awk, left, right, opcode, &folded); fold = fold_constants_for_binop (awk, left, right, opcode, &folded);
if (fold == QSE_AWK_NDE_INT) switch (fold)
{ {
if (fold == left->type) case QSE_AWK_NDE_INT:
{ if (fold == left->type)
qse_awk_clrpt (awk, right);
((qse_awk_nde_int_t*)left)->val = folded.l;
if (((qse_awk_nde_int_t*)left)->str)
{ {
QSE_AWK_FREE (awk, ((qse_awk_nde_int_t*)left)->str); qse_awk_clrpt (awk, right);
((qse_awk_nde_int_t*)left)->str = QSE_NULL; update_int_node (awk, (qse_awk_nde_int_t*)left, folded.l);
((qse_awk_nde_int_t*)left)->len = 0;
} }
} else if (fold == right->type)
else if (fold == right->type)
{
qse_awk_clrpt (awk, left);
((qse_awk_nde_int_t*)right)->val = folded.l;
if (((qse_awk_nde_int_t*)right)->str)
{ {
QSE_AWK_FREE (awk, ((qse_awk_nde_int_t*)right)->str); qse_awk_clrpt (awk, left);
((qse_awk_nde_int_t*)right)->str = QSE_NULL; update_int_node (awk, (qse_awk_nde_int_t*)right, folded.l);
((qse_awk_nde_int_t*)right)->len = 0; left = right;
} }
left = right; else
}
else
{
qse_awk_nde_int_t* tmp;
qse_awk_clrpt (awk, right);
qse_awk_clrpt (awk, left);
tmp = (qse_awk_nde_int_t*) QSE_AWK_ALLOC (
awk, QSE_SIZEOF(qse_awk_nde_int_t));
if (tmp == QSE_NULL)
{ {
qse_awk_clrpt (awk, right);
qse_awk_clrpt (awk, left);
left = new_int_node (awk, folded.l, xloc);
if (left == QSE_NULL) return QSE_NULL;
}
break;
case QSE_AWK_NDE_REAL:
if (fold == left->type)
{
qse_awk_clrpt (awk, right);
update_real_node (awk, (qse_awk_nde_real_t*)left, folded.r);
}
else if (fold == right->type)
{
qse_awk_clrpt (awk, left);
update_real_node (awk, (qse_awk_nde_real_t*)right, folded.r);
left = right;
}
else
{
qse_awk_clrpt (awk, right);
qse_awk_clrpt (awk, left);
left = new_real_node (awk, folded.r, xloc);
if (left == QSE_NULL) return QSE_NULL;
}
break;
default:
{
qse_awk_nde_exp_t* nde;
nde = (qse_awk_nde_exp_t*) QSE_AWK_ALLOC (
awk, QSE_SIZEOF(qse_awk_nde_exp_t));
if (nde == QSE_NULL)
{
qse_awk_clrpt (awk, right);
qse_awk_clrpt (awk, left);
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc); SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc);
return QSE_NULL; return QSE_NULL;
} }
QSE_MEMSET (tmp, 0, QSE_SIZEOF(qse_awk_nde_int_t));
tmp->type = QSE_AWK_NDE_INT; nde->type = QSE_AWK_NDE_EXP_BIN;
tmp->loc = *xloc; nde->loc = *xloc;
tmp->val = folded.l; nde->next = QSE_NULL;
left = (qse_awk_nde_t*)tmp; nde->opcode = opcode;
nde->left = left;
nde->right = right;
left = (qse_awk_nde_t*)nde;
break;
} }
} }
else if (fold == QSE_AWK_NDE_REAL)
{
if (fold == left->type)
{
qse_awk_clrpt (awk, right);
((qse_awk_nde_real_t*)left)->val = folded.r;
if (((qse_awk_nde_real_t*)left)->str)
{
QSE_AWK_FREE (awk, ((qse_awk_nde_real_t*)left)->str);
((qse_awk_nde_real_t*)left)->str = QSE_NULL;
((qse_awk_nde_real_t*)left)->len = 0;
}
}
else if (fold == right->type)
{
qse_awk_clrpt (awk, left);
((qse_awk_nde_real_t*)right)->val = folded.r;
if (((qse_awk_nde_real_t*)right)->str)
{
QSE_AWK_FREE (awk, ((qse_awk_nde_real_t*)right)->str);
((qse_awk_nde_real_t*)right)->str = QSE_NULL;
((qse_awk_nde_real_t*)right)->len = 0;
}
left = right;
}
else
{
qse_awk_nde_real_t* tmp;
qse_awk_clrpt (awk, right);
qse_awk_clrpt (awk, left);
tmp = (qse_awk_nde_real_t*) QSE_AWK_ALLOC (
awk, QSE_SIZEOF(qse_awk_nde_real_t));
if (tmp == QSE_NULL)
{
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc);
return QSE_NULL;
}
QSE_MEMSET (tmp, 0, QSE_SIZEOF(qse_awk_nde_real_t));
tmp->type = QSE_AWK_NDE_REAL;
tmp->loc = *xloc;
tmp->val = folded.r;
left = (qse_awk_nde_t*)tmp;
}
}
else
{
qse_awk_nde_exp_t* nde;
nde = (qse_awk_nde_exp_t*) QSE_AWK_ALLOC (
awk, QSE_SIZEOF(qse_awk_nde_exp_t));
if (nde == QSE_NULL)
{
qse_awk_clrpt (awk, right);
qse_awk_clrpt (awk, left);
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc);
return QSE_NULL;
}
nde->type = QSE_AWK_NDE_EXP_BIN;
nde->loc = *xloc;
nde->next = QSE_NULL;
nde->opcode = opcode;
nde->left = left;
nde->right = right;
left = (qse_awk_nde_t*)nde;
}
} }
while (1); while (1);
@ -4073,69 +4098,27 @@ static qse_awk_nde_t* parse_unary (
case QSE_AWK_NDE_INT: case QSE_AWK_NDE_INT:
if (left->type == fold) if (left->type == fold)
{ {
((qse_awk_nde_int_t*)left)->val = folded.l; update_int_node (awk, (qse_awk_nde_int_t*)left, folded.l);
if (((qse_awk_nde_int_t*)left)->str)
{
QSE_AWK_FREE (awk, ((qse_awk_nde_int_t*)left)->str);
((qse_awk_nde_int_t*)left)->str = QSE_NULL;
((qse_awk_nde_int_t*)left)->len = 0;
}
return left; return left;
} }
else else
{ {
qse_awk_nde_int_t* tmp;
QSE_ASSERT (left->type == QSE_AWK_NDE_REAL); QSE_ASSERT (left->type == QSE_AWK_NDE_REAL);
qse_awk_clrpt (awk, left); qse_awk_clrpt (awk, left);
return new_int_node (awk, folded.l, xloc);
tmp = (qse_awk_nde_int_t*) QSE_AWK_ALLOC (
awk, QSE_SIZEOF(qse_awk_nde_int_t));
if (tmp == QSE_NULL)
{
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc);
return QSE_NULL;
}
QSE_MEMSET (tmp, 0, QSE_SIZEOF(qse_awk_nde_int_t));
tmp->type = QSE_AWK_NDE_INT;
tmp->loc = *xloc;
tmp->val = folded.l;
return (qse_awk_nde_t*)tmp;
} }
case QSE_AWK_NDE_REAL: case QSE_AWK_NDE_REAL:
if (left->type == fold) if (left->type == fold)
{ {
((qse_awk_nde_real_t*)left)->val = folded.r; update_real_node (awk, (qse_awk_nde_real_t*)left, folded.r);
if (((qse_awk_nde_real_t*)left)->str)
{
QSE_AWK_FREE (awk, ((qse_awk_nde_real_t*)left)->str);
((qse_awk_nde_real_t*)left)->str = QSE_NULL;
((qse_awk_nde_real_t*)left)->len = 0;
}
return left; return left;
} }
else else
{ {
qse_awk_nde_real_t* tmp;
QSE_ASSERT (left->type == QSE_AWK_NDE_INT); QSE_ASSERT (left->type == QSE_AWK_NDE_INT);
qse_awk_clrpt (awk, left); qse_awk_clrpt (awk, left);
return new_real_node (awk, folded.r, xloc);
tmp = (qse_awk_nde_real_t*) QSE_AWK_ALLOC (
awk, QSE_SIZEOF(qse_awk_nde_real_t));
if (tmp == QSE_NULL)
{
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc);
return QSE_NULL;
}
QSE_MEMSET (tmp, 0, QSE_SIZEOF(qse_awk_nde_real_t));
tmp->type = QSE_AWK_NDE_REAL;
tmp->loc = *xloc;
tmp->val = folded.r;
return (qse_awk_nde_t*)tmp;
} }
default: default:
@ -4320,20 +4303,17 @@ static qse_awk_nde_t* parse_primary_nogetline (
{ {
qse_awk_nde_int_t* nde; qse_awk_nde_int_t* nde;
nde = (qse_awk_nde_int_t*) QSE_AWK_ALLOC ( nde = (qse_awk_nde_int_t*)new_int_node (
awk, QSE_SIZEOF(qse_awk_nde_int_t)); awk,
if (nde == QSE_NULL) qse_awk_strxtolong (awk,
{ QSE_STR_PTR(awk->tok.name),
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc); QSE_STR_LEN(awk->tok.name),
return QSE_NULL; 0, QSE_NULL
} ),
xloc
);
if (nde == QSE_NULL) return QSE_NULL;
nde->type = QSE_AWK_NDE_INT;
nde->loc = *xloc;
nde->next = QSE_NULL;
nde->val = qse_awk_strxtolong (awk,
QSE_STR_PTR(awk->tok.name),
QSE_STR_LEN(awk->tok.name), 0, QSE_NULL);
nde->str = QSE_AWK_STRXDUP (awk, nde->str = QSE_AWK_STRXDUP (awk,
QSE_STR_PTR(awk->tok.name), QSE_STR_PTR(awk->tok.name),
QSE_STR_LEN(awk->tok.name)); QSE_STR_LEN(awk->tok.name));
@ -4361,20 +4341,17 @@ static qse_awk_nde_t* parse_primary_nogetline (
{ {
qse_awk_nde_real_t* nde; qse_awk_nde_real_t* nde;
nde = (qse_awk_nde_real_t*) QSE_AWK_ALLOC ( nde = (qse_awk_nde_real_t*) new_real_node (
awk, QSE_SIZEOF(qse_awk_nde_real_t)); awk,
if (nde == QSE_NULL) qse_awk_strxtoreal (awk,
{ QSE_STR_PTR(awk->tok.name),
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc); QSE_STR_LEN(awk->tok.name),
return QSE_NULL; QSE_NULL
} ),
xloc
);
if (nde == QSE_NULL) return QSE_NULL;
nde->type = QSE_AWK_NDE_REAL;
nde->loc = *xloc;
nde->next = QSE_NULL;
nde->val = qse_awk_strxtoreal (awk,
QSE_STR_PTR(awk->tok.name),
QSE_STR_LEN(awk->tok.name), QSE_NULL);
nde->str = QSE_AWK_STRXDUP (awk, nde->str = QSE_AWK_STRXDUP (awk,
QSE_STR_PTR(awk->tok.name), QSE_STR_PTR(awk->tok.name),
QSE_STR_LEN(awk->tok.name)); QSE_STR_LEN(awk->tok.name));

View File

@ -1,5 +1,5 @@
/* /*
* $Id: run.c 518 2011-07-24 14:24:13Z hyunghwan.chung $ * $Id: run.c 520 2011-07-25 07:32:49Z 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.
@ -604,7 +604,8 @@ static int set_global (
return 0; return 0;
} }
QSE_INLINE void qse_awk_rtx_setretval (qse_awk_rtx_t* rtx, qse_awk_val_t* val) QSE_INLINE void qse_awk_rtx_setretval (
qse_awk_rtx_t* rtx, qse_awk_val_t* val)
{ {
qse_awk_rtx_refdownval (rtx, STACK_RETVAL(rtx)); qse_awk_rtx_refdownval (rtx, STACK_RETVAL(rtx));
STACK_RETVAL(rtx) = val; STACK_RETVAL(rtx) = val;
@ -612,7 +613,8 @@ QSE_INLINE void qse_awk_rtx_setretval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
qse_awk_rtx_refupval (rtx, val); qse_awk_rtx_refupval (rtx, val);
} }
QSE_INLINE int qse_awk_rtx_setgbl (qse_awk_rtx_t* rtx, int id, qse_awk_val_t* val) QSE_INLINE int qse_awk_rtx_setgbl (
qse_awk_rtx_t* rtx, int id, qse_awk_val_t* val)
{ {
QSE_ASSERT (id >= 0 && id < (int)QSE_LDA_SIZE(rtx->awk->parse.gbls)); QSE_ASSERT (id >= 0 && id < (int)QSE_LDA_SIZE(rtx->awk->parse.gbls));
return set_global (rtx, (qse_size_t)id, QSE_NULL, val); return set_global (rtx, (qse_size_t)id, QSE_NULL, val);