2006-01-26 15:35:20 +00:00
|
|
|
/*
|
2006-03-07 16:09:18 +00:00
|
|
|
* $Id: run.c,v 1.10 2006-03-07 16:09:18 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>
|
2006-03-07 16:09:18 +00:00
|
|
|
#include <xp/bas/string.h>
|
|
|
|
#include <xp/bas/memory.h>
|
2006-01-26 15:35:20 +00:00
|
|
|
#endif
|
|
|
|
|
2006-03-06 04:04:47 +00:00
|
|
|
static int __activate_block (xp_awk_t* awk, xp_awk_nde_blk_t* nde);
|
2006-03-05 17:07:33 +00:00
|
|
|
static int __run_block (xp_awk_t* awk, xp_awk_nde_blk_t* nde);
|
2006-03-03 11:45:45 +00:00
|
|
|
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-03-07 15:55:14 +00:00
|
|
|
static xp_awk_val_t* __eval_binary (xp_awk_t* awk, xp_awk_nde_exp_t* nde);
|
2006-01-26 15:35:20 +00:00
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
int __printval (xp_awk_pair_t* pair)
|
|
|
|
{
|
|
|
|
xp_printf (XP_TEXT("%s = "), (const xp_char_t*)pair->key);
|
|
|
|
xp_awk_printval ((xp_awk_val_t*)pair->val);
|
|
|
|
xp_printf (XP_TEXT("\n"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-03-05 17:07:33 +00:00
|
|
|
xp_assert (awk->tree.begin->type == XP_AWK_NDE_BLK);
|
2006-03-07 15:55:14 +00:00
|
|
|
if (__run_block (awk,
|
|
|
|
(xp_awk_nde_blk_t*)awk->tree.begin) == -1) return -1;
|
2006-01-26 15:35:20 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 11:45:45 +00:00
|
|
|
if (awk->tree.end != XP_NULL)
|
|
|
|
{
|
2006-03-05 17:07:33 +00:00
|
|
|
xp_assert (awk->tree.end->type == XP_AWK_NDE_BLK);
|
2006-03-07 15:55:14 +00:00
|
|
|
if (__run_block (awk,
|
|
|
|
(xp_awk_nde_blk_t*)awk->tree.end) == -1) return -1;
|
2006-01-26 15:35:20 +00:00
|
|
|
}
|
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
xp_awk_map_walk (&awk->run.named, __printval);
|
2006-01-26 15:35:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-06 04:04:47 +00:00
|
|
|
static int __activate_block (xp_awk_t* awk, xp_awk_nde_blk_t* nde)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
if (nde->nlocals == 0 && awk->run.top_frame != XP_NULL) {
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
static int __run_block (xp_awk_t* awk, xp_awk_nde_blk_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-05 17:07:33 +00:00
|
|
|
xp_assert (nde->type == XP_AWK_NDE_BLK);
|
2006-02-23 15:37:34 +00:00
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
p = nde->body;
|
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-05 17:07:33 +00:00
|
|
|
case XP_AWK_NDE_BLK:
|
|
|
|
if (__run_block(awk, (xp_awk_nde_blk_t*)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;
|
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
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:
|
2006-03-07 15:55:14 +00:00
|
|
|
val = __eval_binary(awk,(xp_awk_nde_exp_t*)nde);
|
|
|
|
break;
|
2006-03-04 15:54:37 +00:00
|
|
|
|
|
|
|
case XP_AWK_NDE_EXP_UNR:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_STR:
|
2006-03-05 17:07:33 +00:00
|
|
|
val = xp_awk_makestrval(
|
|
|
|
((xp_awk_nde_str_t*)nde)->buf,
|
|
|
|
((xp_awk_nde_str_t*)nde)->len);
|
2006-02-23 15:37:34 +00:00
|
|
|
break;
|
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
case XP_AWK_NDE_INT:
|
|
|
|
val = xp_awk_makeintval(((xp_awk_nde_int_t*)nde)->val);
|
2006-03-04 15:54:37 +00:00
|
|
|
break;
|
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
/* TODO:
|
|
|
|
case XP_AWK_NDE_REAL:
|
|
|
|
val = xp_awk_makerealval(((xp_awk_nde_real_t*)nde)->val);
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
|
2006-03-04 15:54:37 +00:00
|
|
|
case XP_AWK_NDE_ARG:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_ARGIDX:
|
|
|
|
|
|
|
|
case XP_AWK_NDE_NAMED:
|
2006-03-07 15:55:14 +00:00
|
|
|
{
|
|
|
|
xp_awk_nde_var_t* tgt = (xp_awk_nde_var_t*)nde;
|
|
|
|
xp_awk_pair_t* pair;
|
|
|
|
|
|
|
|
pair = xp_awk_map_get(&awk->run.named,tgt->id.name);
|
|
|
|
if (pair == XP_NULL) val = xp_awk_val_nil;
|
|
|
|
else val = xp_awk_cloneval (pair->val);
|
|
|
|
}
|
|
|
|
break;
|
2006-03-04 15:54:37 +00:00
|
|
|
|
|
|
|
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-05 17:07:33 +00:00
|
|
|
xp_awk_nde_var_t* tgt;
|
|
|
|
|
|
|
|
tgt = (xp_awk_nde_var_t*)nde->left;
|
2006-03-04 15:54:37 +00:00
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
if (tgt->type == XP_AWK_NDE_NAMED)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
2006-03-06 04:04:47 +00:00
|
|
|
/* xp_awk_val_t* old, * new; */
|
|
|
|
xp_awk_pair_t* pair;
|
|
|
|
xp_awk_val_t* new;
|
2006-03-05 17:07:33 +00:00
|
|
|
xp_char_t* name;
|
2006-02-23 15:37:34 +00:00
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
new = __eval_expression(awk, nde->right);
|
2006-03-04 15:54:37 +00:00
|
|
|
|
|
|
|
xp_assert (tgt != XP_NULL);
|
|
|
|
if (new == XP_NULL) return XP_NULL;
|
|
|
|
|
2006-03-06 04:04:47 +00:00
|
|
|
pair = xp_awk_map_get(&awk->run.named, tgt->id.name);
|
|
|
|
if (pair == XP_NULL)
|
|
|
|
{
|
2006-03-05 17:07:33 +00:00
|
|
|
name = xp_strdup (tgt->id.name);
|
2006-03-06 04:04:47 +00:00
|
|
|
if (name == XP_NULL)
|
|
|
|
{
|
2006-03-05 17:07:33 +00:00
|
|
|
xp_awk_freeval(new);
|
|
|
|
awk->errnum = XP_AWK_ENOMEM;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
}
|
2006-03-06 04:04:47 +00:00
|
|
|
else name = pair->key;
|
2006-03-03 11:45:45 +00:00
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
if (xp_awk_map_put(&awk->run.named, name, new) == XP_NULL)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
2006-03-05 17:07:33 +00:00
|
|
|
xp_free (name);
|
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
|
|
|
|
|
|
|
v = new;
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-05 17:07:33 +00:00
|
|
|
else if (tgt->type == XP_AWK_NDE_GLOBAL)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-05 17:07:33 +00:00
|
|
|
else if (tgt->type == XP_AWK_NDE_LOCAL)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-05 17:07:33 +00:00
|
|
|
else if (tgt->type == XP_AWK_NDE_ARG)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
2006-03-05 17:07:33 +00:00
|
|
|
else if (tgt->type == XP_AWK_NDE_NAMEDIDX)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-05 17:07:33 +00:00
|
|
|
else if (tgt->type == XP_AWK_NDE_GLOBALIDX)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-05 17:07:33 +00:00
|
|
|
else if (tgt->type == XP_AWK_NDE_LOCALIDX)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
2006-02-23 15:37:34 +00:00
|
|
|
}
|
2006-03-05 17:07:33 +00:00
|
|
|
else if (tgt->type == XP_AWK_NDE_ARGIDX)
|
2006-03-03 11:45:45 +00:00
|
|
|
{
|
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
|
|
|
}
|
2006-03-07 15:55:14 +00:00
|
|
|
|
|
|
|
static xp_awk_val_t* __eval_binary (xp_awk_t* awk, xp_awk_nde_exp_t* nde)
|
|
|
|
{
|
|
|
|
xp_awk_val_t* left, * right;
|
|
|
|
|
|
|
|
xp_assert (nde->type == XP_AWK_NDE_EXP_BIN);
|
|
|
|
|
|
|
|
left = __eval_expression (awk, nde->left);
|
|
|
|
if (left == XP_NULL) return XP_NULL;
|
|
|
|
|
|
|
|
right = __eval_expression (awk, nde->right);
|
|
|
|
if (right == XP_NULL)
|
|
|
|
{
|
|
|
|
xp_awk_freeval(left);
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: a lot of things to do....
|
|
|
|
if (nde->opcode == XP_AWK_BINOP_PLUS)
|
|
|
|
{
|
|
|
|
if (left->type == XP_AWK_VAL_INT &&
|
|
|
|
right->type == XP_AWK_VAL_INT)
|
|
|
|
{
|
|
|
|
((xp_awk_val_int_t*)left)->val +=
|
|
|
|
((xp_awk_val_int_t*)right)->val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_awk_freeval(right);
|
|
|
|
return left;
|
|
|
|
}
|