2006-01-26 15:35:20 +00:00
|
|
|
/*
|
2006-03-04 15:54:37 +00:00
|
|
|
* $Id: run.c,v 1.6 2006-03-04 15:54:37 bacon Exp $
|
2006-01-26 15:35:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/awk/awk.h>
|
2006-03-02 15:36:30 +00:00
|
|
|
|
2006-01-26 15:35:20 +00:00
|
|
|
#ifndef __STAND_ALONE
|
|
|
|
#include <xp/bas/assert.h>
|
|
|
|
#endif
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
static int __run_block (xp_awk_t* awk, xp_awk_nde_t* nde);
|
|
|
|
static int __run_statement (xp_awk_t* awk, xp_awk_nde_t* nde);
|
|
|
|
|
2006-03-04 15:54:37 +00:00
|
|
|
static xp_awk_val_t* __eval_expression (xp_awk_t* awk, xp_awk_nde_t* nde);
|
|
|
|
static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde);
|
2006-01-26 15:35:20 +00:00
|
|
|
|
|
|
|
int xp_awk_run (xp_awk_t* awk)
|
|
|
|
{
|
2006-03-03 11:45:45 +00:00
|
|
|
if (awk->tree.begin != XP_NULL)
|
|
|
|
{
|
2006-01-26 15:35:20 +00:00
|
|
|
if (__run_block(awk, awk->tree.begin) == -1) return -1;
|
|
|
|
}
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
if (awk->tree.end != XP_NULL)
|
|
|
|
{
|
2006-01-26 15:35:20 +00:00
|
|
|
if (__run_block(awk, awk->tree.end) == -1) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
static int __run_block (xp_awk_t* awk, xp_awk_nde_t* nde)
|
2006-01-26 15:35:20 +00:00
|
|
|
{
|
2006-03-03 11:45:45 +00:00
|
|
|
xp_awk_nde_t* p;
|
2006-02-23 15:37:34 +00:00
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
xp_assert (nde->type == XP_AWK_NDE_BLOCK);
|
2006-02-23 15:37:34 +00:00
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
p = nde;
|
2006-02-23 15:37:34 +00:00
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
while (p != XP_NULL)
|
|
|
|
{
|
|
|
|
if (__run_statement(awk, p) == -1) return -1;
|
2006-02-23 15:37:34 +00:00
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2006-01-26 15:35:20 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
static int __run_statement (xp_awk_t* awk, xp_awk_nde_t* nde)
|
2006-01-26 15:35:20 +00:00
|
|
|
{
|
2006-03-03 11:45:45 +00:00
|
|
|
switch (nde->type)
|
|
|
|
{
|
|
|
|
case XP_AWK_NDE_NULL:
|
2006-02-23 15:37:34 +00:00
|
|
|
/* do nothing */
|
|
|
|
break;
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_BLOCK:
|
|
|
|
if (__run_block(awk, nde) == -1) return -1;
|
2006-01-26 15:35:20 +00:00
|
|
|
break;
|
2006-02-23 15:37:34 +00:00
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_IF:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_WHILE:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_DOWHILE:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_FOR:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_BREAK:
|
2006-01-26 15:35:20 +00:00
|
|
|
break;
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_CONTINUE:
|
2006-01-26 15:35:20 +00:00
|
|
|
break;
|
2006-02-23 15:37:34 +00:00
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_RETURN:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_EXIT:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_NEXT:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_NEXTFILE:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
|
|
|
|
2006-03-04 15:54:37 +00:00
|
|
|
default:
|
|
|
|
if (__eval_expression(awk,nde) == XP_NULL) return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static xp_awk_val_t* __eval_expression (xp_awk_t* awk, xp_awk_nde_t* nde)
|
|
|
|
{
|
|
|
|
xp_awk_val_t* val;
|
|
|
|
|
|
|
|
switch (nde->type) {
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_ASS:
|
2006-03-04 15:54:37 +00:00
|
|
|
val = __eval_assignment(awk,(xp_awk_nde_ass_t*)nde);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XP_AWK_NDE_EXP_BIN:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_EXP_UNR:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_STR:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
case XP_AWK_NDE_NUM:
|
2006-03-04 15:54:37 +00:00
|
|
|
// TODO: int, real...
|
|
|
|
val = xp_awk_makeintval();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XP_AWK_NDE_ARG:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_ARGIDX:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_NAMED:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_NAMEDIDX:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_GLOBAL:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_GLOBALIDX:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_LOCAL:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_LOCALIDX:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_POS:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_CALL:
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
2006-03-03 11:45:45 +00:00
|
|
|
|
|
|
|
default:
|
2006-03-04 15:54:37 +00:00
|
|
|
/* somthing wrong */
|
|
|
|
return XP_NULL;
|
2006-01-26 15:35:20 +00:00
|
|
|
}
|
|
|
|
|
2006-03-04 15:54:37 +00:00
|
|
|
return val;
|
2006-01-26 15:35:20 +00:00
|
|
|
}
|
2006-02-23 15:37:34 +00:00
|
|
|
|
2006-03-04 15:54:37 +00:00
|
|
|
static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde)
|
2006-02-23 15:37:34 +00:00
|
|
|
{
|
2006-03-04 15:54:37 +00:00
|
|
|
xp_awk_val_t* v;
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
if (nde->type == XP_AWK_NDE_NAMED)
|
|
|
|
{
|
2006-03-04 15:54:37 +00:00
|
|
|
xp_awk_nde_var_t* tgt;
|
|
|
|
xp_awk_val_t* old, * new;
|
2006-02-23 15:37:34 +00:00
|
|
|
|
2006-03-04 15:54:37 +00:00
|
|
|
tgt = (xp_awk_nde_var_t*)nde->left;
|
|
|
|
new = __eval_expression (awk, nde->right);
|
|
|
|
|
|
|
|
xp_assert (tgt != XP_NULL);
|
|
|
|
if (new == XP_NULL) return XP_NULL;
|
|
|
|
|
|
|
|
old = (xp_awk_val_t*) xp_awk_map_get (&awk->run.named, tgt->id.name);
|
2006-03-03 11:45:45 +00:00
|
|
|
|
|
|
|
if (xp_awk_map_put (
|
2006-03-04 15:54:37 +00:00
|
|
|
&awk->run.named, tgt->id.name, new) == XP_NULL)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
2006-03-04 15:54:37 +00:00
|
|
|
xp_awk_freeval (new);
|
2006-02-23 15:37:34 +00:00
|
|
|
awk->errnum = XP_AWK_ENOMEM;
|
2006-03-04 15:54:37 +00:00
|
|
|
return XP_NULL;
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-04 15:54:37 +00:00
|
|
|
else if (old != XP_NULL)
|
|
|
|
{
|
|
|
|
/* free the old value that has been assigned to the variable */
|
|
|
|
xp_awk_freeval (old);
|
|
|
|
}
|
|
|
|
|
|
|
|
v = new;
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-03 11:45:45 +00:00
|
|
|
else if (nde->type == XP_AWK_NDE_GLOBAL)
|
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-03 11:45:45 +00:00
|
|
|
else if (nde->type == XP_AWK_NDE_LOCAL)
|
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-03 11:45:45 +00:00
|
|
|
else if (nde->type == XP_AWK_NDE_ARG)
|
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
else if (nde->type == XP_AWK_NDE_NAMEDIDX)
|
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-03 11:45:45 +00:00
|
|
|
else if (nde->type == XP_AWK_NDE_GLOBALIDX)
|
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-03 11:45:45 +00:00
|
|
|
else if (nde->type == XP_AWK_NDE_LOCALIDX)
|
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-03 11:45:45 +00:00
|
|
|
else if (nde->type == XP_AWK_NDE_ARGIDX)
|
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-03 11:45:45 +00:00
|
|
|
else
|
|
|
|
{
|
2006-03-04 15:54:37 +00:00
|
|
|
/* this should never be reached. something wrong */
|
2006-03-03 11:45:45 +00:00
|
|
|
// TODO: set errnum ....
|
2006-03-04 15:54:37 +00:00
|
|
|
return XP_NULL;
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
2006-03-04 15:54:37 +00:00
|
|
|
return v;
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|