qse/ase/awk/run.c

161 lines
2.6 KiB
C
Raw Normal View History

2006-01-26 15:35:20 +00:00
/*
2006-03-03 11:45:45 +00:00
* $Id: run.c,v 1.5 2006-03-03 11:45:45 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);
static int __run_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde);
static xp_awk_val_t* __eval_expr (xp_awk_t* awk, xp_awk_nde_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
#if 0
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-03 11:45:45 +00:00
#endif
2006-02-23 15:37:34 +00:00
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_ASS:
if (__run_assignment (
awk, (xp_awk_nde_ass_t*)nde) == -1) return -1;
2006-02-23 15:37:34 +00:00
break;
2006-03-03 11:45:45 +00:00
#if 0
case XP_AWK_NDE_NUM:
2006-02-23 15:37:34 +00:00
break;
2006-03-03 11:45:45 +00:00
#endif
default:
/* this should never be reached */
// TODO: set errnum ....
return -1;
2006-01-26 15:35:20 +00:00
}
return 0;
}
2006-02-23 15:37:34 +00:00
2006-03-03 11:45:45 +00:00
static int __run_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde)
2006-02-23 15:37:34 +00:00
{
2006-03-03 11:45:45 +00:00
if (nde->type == XP_AWK_NDE_NAMED)
{
xp_awk_nde_var_t* left = (xp_awk_nde_var_t*)nde->left;
xp_awk_val_t* right = __eval_expr (awk, nde->right);
2006-02-23 15:37:34 +00:00
2006-03-03 11:45:45 +00:00
xp_assert (left != XP_NULL);
if (right == XP_NULL) return -1;
if (xp_awk_map_put (
&awk->run.named, left->id.name, right) == XP_NULL)
{
2006-02-23 15:37:34 +00:00
awk->errnum = XP_AWK_ENOMEM;
return -1;
}
}
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
{
/* this should never be reached */
// TODO: set errnum ....
2006-02-23 15:37:34 +00:00
return -1;
}
return 0;
}
2006-03-03 11:45:45 +00:00
static xp_awk_val_t* __eval_expr (xp_awk_t* awk, xp_awk_nde_t* nde)
2006-02-23 15:37:34 +00:00
{
2006-03-03 11:45:45 +00:00
return XP_NULL;
2006-02-23 15:37:34 +00:00
}