qse/ase/awk/run.c

850 lines
18 KiB
C
Raw Normal View History

2006-01-26 15:35:20 +00:00
/*
2006-03-30 16:31:50 +00:00
* $Id: run.c,v 1.27 2006-03-30 16:31:50 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-24 06:33:36 +00:00
#define STACK_INCREMENT 512
2006-03-28 16:33:09 +00:00
#define STACK_AT(awk,n) ((awk)->run.stack[(awk)->run.stack_base+(n)])
2006-03-26 14:03:08 +00:00
#define STACK_NARGS(awk) ((xp_size_t)STACK_AT(awk,3))
#define STACK_ARG(awk,n) STACK_AT(awk,3+1+(n))
#define STACK_LOCAL(awk,n) STACK_AT(awk,3+STACK_NARGS(awk)+1+(n))
2006-03-26 16:36:30 +00:00
#define STACK_RETVAL(awk) STACK_AT(awk,2)
2006-03-28 16:33:09 +00:00
#define STACK_GLOBAL(awk,n) ((awk)->run.stack[n])
2006-03-26 16:36:30 +00:00
#define EXIT_NONE 0
2006-03-27 10:19:33 +00:00
#define EXIT_BREAK 1
#define EXIT_CONTINUE 2
#define EXIT_FUNCTION 3
#define EXIT_GLOBAL 4
2006-03-26 14:03:08 +00:00
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-15 15:34:59 +00:00
static int __run_if_statement (xp_awk_t* awk, xp_awk_nde_if_t* nde);
2006-03-23 13:26:04 +00:00
static int __run_while_statement (xp_awk_t* awk, xp_awk_nde_while_t* nde);
2006-03-23 15:36:20 +00:00
static int __run_for_statement (xp_awk_t* awk, xp_awk_nde_for_t* nde);
static int __run_break_statement (xp_awk_t* awk, xp_awk_nde_break_t* nde);
static int __run_continue_statement (xp_awk_t* awk, xp_awk_nde_continue_t* nde);
static int __run_return_statement (xp_awk_t* awk, xp_awk_nde_return_t* nde);
static int __run_exit_statement (xp_awk_t* awk, xp_awk_nde_exit_t* nde);
2006-03-03 11:45:45 +00:00
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-03-24 06:33:36 +00:00
static xp_awk_val_t* __eval_funccall (xp_awk_t* awk, xp_awk_nde_call_t* nde);
2006-01-26 15:35:20 +00:00
2006-03-26 14:03:08 +00:00
static int __raw_push (xp_awk_t* awk, void* val);
2006-03-26 16:36:30 +00:00
static void __raw_pop (xp_awk_t* awk);
2006-03-25 17:04:36 +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-28 11:32:58 +00:00
xp_size_t nglobals;
2006-03-26 16:36:30 +00:00
// TODO: clear run stack/exit_level
awk->run.exit_level = EXIT_NONE;
2006-03-28 16:33:09 +00:00
xp_assert (awk->run.stack_base == 0 && awk->run.stack_top == 0);
/* secure space for global variables */
2006-03-28 11:32:58 +00:00
nglobals = awk->tree.nglobals;
while (nglobals > 0)
{
--nglobals;
if (__raw_push(awk,xp_awk_val_nil) == -1)
{
// TODO: error handling
return -1;
}
}
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-14 16:40:00 +00:00
xp_printf (XP_TEXT("---------------------------\n"));
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-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-03-26 14:03:08 +00:00
xp_size_t nlocals;
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-03-26 14:03:08 +00:00
nlocals = nde->nlocals;
2006-02-23 15:37:34 +00:00
2006-03-26 14:03:08 +00:00
//xp_printf (XP_TEXT("securing space for local variables nlocals = %d\n"), nlocals);
/* secure space for local variables */
while (nlocals > 0)
{
--nlocals;
if (__raw_push(awk,xp_awk_val_nil) == -1)
{
// TODO: proper error handling...
return -1;
}
/* refupval is not required for xp_awk_val_nil */
}
//xp_printf (XP_TEXT("executing block statements\n"));
2006-03-26 16:36:30 +00:00
while (p != XP_NULL && awk->run.exit_level == EXIT_NONE)
2006-03-03 11:45:45 +00:00
{
2006-03-14 16:40:00 +00:00
if (__run_statement(awk,p) == -1) return -1;
2006-02-23 15:37:34 +00:00
p = p->next;
}
2006-03-26 14:03:08 +00:00
//xp_printf (XP_TEXT("popping off local variables\n"));
/* pop off local variables */
nlocals = nde->nlocals;
while (nlocals > 0)
{
--nlocals;
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, STACK_LOCAL(awk,nlocals));
2006-03-26 14:03:08 +00:00
__raw_pop (awk);
}
2006-02-23 15:37:34 +00:00
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:
2006-03-14 16:40:00 +00:00
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-03-23 15:36:20 +00:00
if (__run_if_statement (
awk, (xp_awk_nde_if_t*)nde) == -1) return -1;
2006-02-23 15:37:34 +00:00
break;
2006-03-15 15:34:59 +00:00
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_WHILE:
case XP_AWK_NDE_DOWHILE:
2006-03-23 15:36:20 +00:00
if (__run_while_statement (
awk, (xp_awk_nde_while_t*)nde) == -1) return -1;
2006-02-23 15:37:34 +00:00
break;
2006-03-23 15:36:20 +00:00
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_FOR:
2006-03-23 15:36:20 +00:00
if (__run_for_statement (
awk, (xp_awk_nde_for_t*)nde) == -1) return -1;
2006-02-23 15:37:34 +00:00
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_BREAK:
2006-03-23 15:36:20 +00:00
if (__run_break_statement(
awk, (xp_awk_nde_break_t*)nde) == -1) return -1;
2006-01-26 15:35:20 +00:00
break;
2006-03-23 15:36:20 +00:00
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_CONTINUE:
2006-03-23 15:36:20 +00:00
if (__run_continue_statement (
awk, (xp_awk_nde_continue_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_RETURN:
2006-03-23 15:36:20 +00:00
if (__run_return_statement (
awk, (xp_awk_nde_return_t*)nde) == -1) return -1;
2006-02-23 15:37:34 +00:00
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_EXIT:
2006-03-23 15:36:20 +00:00
if (__run_exit_statement (
awk, (xp_awk_nde_exit_t*)nde) == -1) return -1;
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;
}
2006-03-15 15:34:59 +00:00
static int __run_if_statement (xp_awk_t* awk, xp_awk_nde_if_t* nde)
{
xp_awk_val_t* test;
2006-03-27 11:43:17 +00:00
int n = 0;
2006-03-15 15:34:59 +00:00
test = __eval_expression (awk, nde->test);
2006-03-23 13:26:04 +00:00
if (test == XP_NULL) return -1;
xp_awk_refupval (test);
2006-03-15 15:34:59 +00:00
if (xp_awk_isvaltrue(test))
{
n = __run_statement (awk, nde->then_part);
}
else if (nde->else_part != XP_NULL)
{
n = __run_statement (awk, nde->else_part);
}
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, test); // TODO: is this correct?
2006-03-23 13:26:04 +00:00
return n;
}
static int __run_while_statement (xp_awk_t* awk, xp_awk_nde_while_t* nde)
{
xp_awk_val_t* test;
if (nde->type == XP_AWK_NDE_WHILE)
{
while (1)
{
test = __eval_expression (awk, nde->test);
if (test == XP_NULL) return -1;
xp_awk_refupval (test);
if (xp_awk_isvaltrue(test))
{
2006-03-23 15:36:20 +00:00
// TODO: break.... continue...., global exit, return... run-time abortion...
if (__run_statement(awk,nde->body) == -1)
2006-03-23 13:26:04 +00:00
{
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, test);
2006-03-23 13:26:04 +00:00
return -1;
}
}
else
{
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, test);
2006-03-23 13:26:04 +00:00
break;
}
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, test);
if (awk->run.exit_level == EXIT_BREAK)
{
awk->run.exit_level = EXIT_NONE;
break;
}
else if (awk->run.exit_level == EXIT_CONTINUE)
{
awk->run.exit_level = EXIT_NONE;
}
else if (awk->run.exit_level != EXIT_NONE) break;
2006-03-23 13:26:04 +00:00
}
}
else if (nde->type == XP_AWK_NDE_DOWHILE)
{
do
{
2006-03-27 11:43:17 +00:00
if (__run_statement(awk,nde->body) == -1)
{
// TODO: error handling...
return -1;
}
if (awk->run.exit_level == EXIT_BREAK)
{
awk->run.exit_level = EXIT_NONE;
break;
}
else if (awk->run.exit_level == EXIT_CONTINUE)
{
awk->run.exit_level = EXIT_NONE;
}
else if (awk->run.exit_level != EXIT_NONE) break;
2006-03-23 13:26:04 +00:00
test = __eval_expression (awk, nde->test);
if (test == XP_NULL) return -1;
xp_awk_refupval (test);
2006-03-27 11:43:17 +00:00
if (!xp_awk_isvaltrue(test))
2006-03-23 13:26:04 +00:00
{
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, test);
2006-03-23 13:26:04 +00:00
break;
}
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, test);
2006-03-23 13:26:04 +00:00
}
while (1);
}
2006-03-23 15:36:20 +00:00
return 0;
}
static int __run_for_statement (xp_awk_t* awk, xp_awk_nde_for_t* nde)
{
if (nde->init != XP_NULL)
{
if (__eval_expression(awk,nde->init) == XP_NULL) return -1;
}
while (1)
{
if (nde->test != XP_NULL)
{
xp_awk_val_t* test;
test = __eval_expression (awk, nde->test);
if (test == XP_NULL) return -1;
xp_awk_refupval (test);
if (xp_awk_isvaltrue(test))
{
if (__run_statement(awk,nde->body) == -1)
{
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, test);
2006-03-23 15:36:20 +00:00
return -1;
}
}
else
{
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, test);
2006-03-23 15:36:20 +00:00
break;
}
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, test);
2006-03-23 15:36:20 +00:00
}
else
{
if (__run_statement(awk,nde->body) == -1)
{
return -1;
}
}
2006-03-27 11:43:17 +00:00
if (awk->run.exit_level == EXIT_BREAK)
{
awk->run.exit_level = EXIT_NONE;
break;
}
else if (awk->run.exit_level == EXIT_CONTINUE)
{
awk->run.exit_level = EXIT_NONE;
}
else if (awk->run.exit_level != EXIT_NONE) break;
2006-03-23 15:36:20 +00:00
if (nde->incr != XP_NULL)
{
if (__eval_expression(awk,nde->incr) == XP_NULL) return -1;
}
}
return 0;
}
static int __run_break_statement (xp_awk_t* awk, xp_awk_nde_break_t* nde)
{
2006-03-27 10:19:33 +00:00
awk->run.exit_level = EXIT_BREAK;
2006-03-23 15:36:20 +00:00
return 0;
}
static int __run_continue_statement (xp_awk_t* awk, xp_awk_nde_continue_t* nde)
{
2006-03-27 10:19:33 +00:00
awk->run.exit_level = EXIT_CONTINUE;
2006-03-23 15:36:20 +00:00
return 0;
}
static int __run_return_statement (xp_awk_t* awk, xp_awk_nde_return_t* nde)
{
2006-03-25 17:04:36 +00:00
if (nde->val != XP_NULL)
{
2006-03-27 10:19:33 +00:00
xp_awk_val_t* val;
2006-03-26 14:03:08 +00:00
//xp_printf (XP_TEXT("returning....\n"));
2006-03-25 17:04:36 +00:00
val = __eval_expression(awk, nde->val);
2006-03-27 10:19:33 +00:00
if (val == XP_NULL)
{
// TODO: error handling
return -1;
}
2006-03-26 16:36:30 +00:00
STACK_RETVAL(awk) = val;
xp_awk_refupval (val); /* see run_funccall for the trick */
2006-03-26 14:03:08 +00:00
//xp_printf (XP_TEXT("set return value....\n"));
2006-03-25 17:04:36 +00:00
}
2006-03-26 16:36:30 +00:00
awk->run.exit_level = EXIT_FUNCTION;
2006-03-23 15:36:20 +00:00
return 0;
}
static int __run_exit_statement (xp_awk_t* awk, xp_awk_nde_exit_t* nde)
{
2006-03-27 10:19:33 +00:00
if (nde->val != XP_NULL)
{
xp_awk_val_t* val;
val = __eval_expression(awk, nde->val);
if (val == XP_NULL)
{
// TODO: error handling
return -1;
}
2006-03-27 14:59:57 +00:00
// TODO: check out the line below..
2006-03-27 10:19:33 +00:00
awk->run.stack[2] = val; /* global return value */
xp_awk_refupval (val);
}
awk->run.exit_level = EXIT_GLOBAL;
2006-03-23 15:36:20 +00:00
return 0;
2006-03-15 15:34:59 +00:00
}
2006-03-04 15:54:37 +00:00
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:
2006-03-22 16:05:50 +00:00
// TODO: .......................
break;
2006-03-04 15:54:37 +00:00
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:
2006-03-27 11:43:17 +00:00
val = xp_awk_makeintval(awk,((xp_awk_nde_int_t*)nde)->val);
2006-03-04 15:54:37 +00:00
break;
2006-03-05 17:07:33 +00:00
case XP_AWK_NDE_REAL:
2006-03-28 16:33:09 +00:00
val = xp_awk_makerealval(awk,((xp_awk_nde_real_t*)nde)->val);
2006-03-05 17:07:33 +00:00
break;
2006-03-04 15:54:37 +00:00
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);
2006-03-23 13:26:04 +00:00
val = (pair == XP_NULL)? xp_awk_val_nil: pair->val;
2006-03-07 15:55:14 +00:00
}
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_NDE_GLOBAL:
2006-03-27 14:59:57 +00:00
{
2006-03-28 16:33:09 +00:00
xp_awk_nde_var_t* tgt = (xp_awk_nde_var_t*)nde;
val = STACK_GLOBAL(awk,tgt->id.idxa);
2006-03-27 14:59:57 +00:00
}
2006-03-26 14:03:08 +00:00
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_NDE_LOCAL:
2006-03-26 14:03:08 +00:00
{
xp_awk_nde_var_t* tgt = (xp_awk_nde_var_t*)nde;
val = STACK_LOCAL(awk,tgt->id.idxa);
}
break;
2006-03-04 15:54:37 +00:00
2006-03-27 14:59:57 +00:00
case XP_AWK_NDE_ARG:
{
xp_awk_nde_var_t* tgt = (xp_awk_nde_var_t*)nde;
val = STACK_ARG(awk,tgt->id.idxa);
}
break;
case XP_AWK_NDE_NAMEDIDX:
break;
case XP_AWK_NDE_GLOBALIDX:
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_NDE_LOCALIDX:
2006-03-26 14:03:08 +00:00
break;
2006-03-27 14:59:57 +00:00
case XP_AWK_NDE_ARGIDX:
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_NDE_POS:
2006-03-26 14:03:08 +00:00
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_NDE_CALL:
2006-03-24 06:33:36 +00:00
val = __eval_funccall(awk, (xp_awk_nde_call_t*)nde);
if (val == XP_NULL) return XP_NULL;
2006-02-23 15:37:34 +00:00
break;
2006-03-03 11:45:45 +00:00
default:
2006-03-27 14:14:00 +00:00
/* somthing wrong. internal error */
/* TODO: set the error code instead of assertion below */
xp_assert (XP_TEXT("should never happen") == XP_NULL);
2006-03-04 15:54:37 +00:00
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-25 17:04:36 +00:00
xp_awk_val_t* val;
2006-03-05 17:07:33 +00:00
xp_awk_nde_var_t* tgt;
2006-03-25 17:04:36 +00:00
xp_assert (nde->left != XP_NULL && nde->right != XP_NULL);
2006-03-05 17:07:33 +00:00
tgt = (xp_awk_nde_var_t*)nde->left;
2006-03-25 17:04:36 +00:00
val = __eval_expression(awk, nde->right);
if (val == XP_NULL) return XP_NULL;
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_pair_t* pair;
2006-03-05 17:07:33 +00:00
xp_char_t* name;
2006-02-23 15:37:34 +00:00
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-27 11:43:17 +00:00
xp_awk_freeval (awk, val);
2006-03-05 17:07:33 +00:00
awk->errnum = XP_AWK_ENOMEM;
return XP_NULL;
}
}
2006-03-23 13:26:04 +00:00
else
{
name = pair->key;
}
2006-03-03 11:45:45 +00:00
2006-03-25 17:04:36 +00:00
if (xp_awk_map_put(&awk->run.named, name, val) == XP_NULL)
2006-03-03 11:45:45 +00:00
{
2006-03-05 17:07:33 +00:00
xp_free (name);
2006-03-27 11:43:17 +00:00
xp_awk_freeval (awk, val);
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
2006-03-25 17:04:36 +00:00
xp_awk_refupval (val);
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-03-28 16:33:09 +00:00
xp_awk_refdownval (awk, STACK_GLOBAL(awk,tgt->id.idxa));
2006-03-26 16:36:30 +00:00
STACK_GLOBAL(awk,tgt->id.idxa) = val;
xp_awk_refupval (val);
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-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, STACK_LOCAL(awk,tgt->id.idxa));
2006-03-26 14:03:08 +00:00
STACK_LOCAL(awk,tgt->id.idxa) = val;
xp_awk_refupval (val);
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-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, STACK_ARG(awk,tgt->id.idxa));
2006-03-26 14:03:08 +00:00
STACK_ARG(awk,tgt->id.idxa) = val;
2006-03-25 17:04:36 +00:00
xp_awk_refupval (val);
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-25 17:04:36 +00:00
return val;
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)
{
2006-03-22 16:05:50 +00:00
xp_awk_val_t* left, * right, * res;
2006-03-07 15:55:14 +00:00
xp_assert (nde->type == XP_AWK_NDE_EXP_BIN);
left = __eval_expression (awk, nde->left);
if (left == XP_NULL) return XP_NULL;
2006-03-23 15:36:20 +00:00
xp_awk_refupval (left);
2006-03-07 15:55:14 +00:00
right = __eval_expression (awk, nde->right);
if (right == XP_NULL)
{
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, left);
2006-03-07 15:55:14 +00:00
return XP_NULL;
}
2006-03-23 15:36:20 +00:00
xp_awk_refupval (right);
2006-03-22 16:05:50 +00:00
res = XP_NULL;
2006-03-07 15:55:14 +00:00
// 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)
{
2006-03-22 16:05:50 +00:00
xp_long_t r =
((xp_awk_val_int_t*)left)->val +
((xp_awk_val_int_t*)right)->val;
2006-03-27 14:15:53 +00:00
res = xp_awk_makeintval (awk, r);
2006-03-22 16:05:50 +00:00
}
}
else if (nde->opcode == XP_AWK_BINOP_MINUS)
{
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
xp_long_t r =
((xp_awk_val_int_t*)left)->val -
((xp_awk_val_int_t*)right)->val;
2006-03-27 14:15:53 +00:00
res = xp_awk_makeintval (awk, r);
2006-03-07 15:55:14 +00:00
}
}
2006-03-27 14:59:57 +00:00
else if (nde->opcode == XP_AWK_BINOP_MUL)
{
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
xp_long_t r =
((xp_awk_val_int_t*)left)->val *
((xp_awk_val_int_t*)right)->val;
res = xp_awk_makeintval (awk, r);
}
}
else if (nde->opcode == XP_AWK_BINOP_DIV)
{
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
xp_long_t r =
((xp_awk_val_int_t*)left)->val /
((xp_awk_val_int_t*)right)->val;
res = xp_awk_makeintval (awk, r);
}
}
else if (nde->opcode == XP_AWK_BINOP_MOD)
{
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
xp_long_t r =
((xp_awk_val_int_t*)left)->val %
((xp_awk_val_int_t*)right)->val;
res = xp_awk_makeintval (awk, r);
}
}
2006-03-30 16:31:50 +00:00
else if (nde->opcode == XP_AWK_BINOP_BXOR)
{
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
xp_long_t r =
((xp_awk_val_int_t*)left)->val ^
((xp_awk_val_int_t*)right)->val;
res = xp_awk_makeintval (awk, r);
}
}
2006-03-26 16:36:30 +00:00
2006-03-27 14:15:53 +00:00
xp_awk_refdownval (awk, left);
xp_awk_refdownval (awk, right);
2006-03-22 16:05:50 +00:00
return res;
}
2006-03-24 06:33:36 +00:00
static xp_awk_val_t* __eval_funccall (xp_awk_t* awk, xp_awk_nde_call_t* nde)
{
xp_awk_func_t* func;
xp_awk_pair_t* pair;
2006-03-25 17:04:36 +00:00
xp_awk_nde_t* p;
xp_size_t nargs, i;
xp_awk_val_t* v;
xp_size_t saved_stack_top;
2006-03-24 06:33:36 +00:00
pair = xp_awk_map_get (&awk->tree.funcs, nde->name);
if (pair == XP_NULL) return XP_NULL; /* no such function */
/*
* ---------------------
2006-03-26 16:36:30 +00:00
* locn <- stack top
* ---------------------
* ....
* ---------------------
* loc0 local variables are pushed by __run_block
* ---------------------
* argn
2006-03-24 06:33:36 +00:00
* ---------------------
* ....
* ---------------------
* arg1
* ---------------------
* arg0
* ---------------------
2006-03-25 17:04:36 +00:00
* nargs
* ---------------------
2006-03-24 06:33:36 +00:00
* return value
* ---------------------
* previous stack top
* ---------------------
2006-03-26 16:36:30 +00:00
* previous stack base <- stack base
2006-03-24 06:33:36 +00:00
* ---------------------
*/
2006-03-25 17:04:36 +00:00
xp_assert (xp_sizeof(void*) >= xp_sizeof(awk->run.stack_top));
xp_assert (xp_sizeof(void*) >= xp_sizeof(awk->run.stack_base));
saved_stack_top = awk->run.stack_top;
2006-03-26 14:03:08 +00:00
//xp_printf (XP_TEXT("setting up function stack frame stack_top = %ld stack_base = %ld\n"), awk->run.stack_top, awk->run.stack_base);
if (__raw_push(awk,(void*)awk->run.stack_base) == -1) return XP_NULL;
if (__raw_push(awk,(void*)saved_stack_top) == -1)
2006-03-25 17:04:36 +00:00
{
/* TODO: run.stack recovery */
return XP_NULL;
}
/* secure space for return value. */
2006-03-26 14:03:08 +00:00
if (__raw_push(awk,xp_awk_val_nil) == -1)
2006-03-25 17:04:36 +00:00
{
/* TODO: run.stack recovery */
return XP_NULL;
}
/* secure space for nargs */
2006-03-26 14:03:08 +00:00
if (__raw_push(awk,xp_awk_val_nil) == -1)
2006-03-25 17:04:36 +00:00
{
/* TODO: run.stack recovery */
return XP_NULL;
}
nargs = 0;
p = nde->args;
while (p != XP_NULL)
{
v = __eval_expression(awk,p);
if (v == XP_NULL)
{
/* TODO: run.stack recovery */
return XP_NULL;
}
2006-03-26 14:03:08 +00:00
if (__raw_push(awk,v) == -1)
2006-03-25 17:04:36 +00:00
{
/* TODO: run.stack recovery */
return XP_NULL;
}
xp_awk_refupval (v);
nargs++;
p = p->next;
}
awk->run.stack_base = saved_stack_top;
awk->run.stack[awk->run.stack_base+3] = (void*)nargs;
2006-03-24 06:33:36 +00:00
func = (xp_awk_func_t*)pair->val;
2006-03-25 17:04:36 +00:00
xp_assert (func != XP_NULL);
// TODO: do i need to check if the number of arguments matches the actual arguments...???? this might be the compiler job...
2006-03-26 14:03:08 +00:00
//xp_printf (XP_TEXT("running function body\n"));
2006-03-24 06:33:36 +00:00
2006-03-25 17:04:36 +00:00
xp_assert (func->body->type == XP_AWK_NDE_BLK);
2006-03-26 16:36:30 +00:00
if (__run_block(awk,(xp_awk_nde_blk_t*)func->body) == -1)
{
// TODO: handle error properly....
return XP_NULL;
}
2006-03-26 14:03:08 +00:00
//xp_printf (XP_TEXT("block run complete\n"));
2006-03-24 06:33:36 +00:00
2006-03-25 17:04:36 +00:00
/* refdown args in the run.stack */
2006-03-26 14:03:08 +00:00
nargs = STACK_NARGS(awk);
//xp_printf (XP_TEXT("block run complete nargs = %d\n"), nargs);
2006-03-25 17:04:36 +00:00
for (i = 0; i < nargs; i++)
{
2006-03-27 11:43:17 +00:00
xp_awk_refdownval (awk, STACK_ARG(awk,i));
2006-03-25 17:04:36 +00:00
}
2006-03-26 14:03:08 +00:00
//xp_printf (XP_TEXT("got return value\n"));
2006-03-26 16:36:30 +00:00
/* adjust the reference count of the return value.
* the value must not be freeed event if the reference count
* is decremented to zero. */
v = STACK_RETVAL(awk);
2006-03-27 11:43:17 +00:00
xp_awk_refdownval_nofree (awk, v);
2006-03-25 17:04:36 +00:00
awk->run.stack_top = (xp_size_t)awk->run.stack[awk->run.stack_base+1];
awk->run.stack_base = (xp_size_t)awk->run.stack[awk->run.stack_base+0];
2006-03-26 16:36:30 +00:00
if (awk->run.exit_level == EXIT_FUNCTION)
{
awk->run.exit_level = EXIT_NONE;
}
2006-03-26 14:03:08 +00:00
//xp_printf (XP_TEXT("returning from function stack_top=%ld, stack_base=%ld\n"), awk->run.stack_top, awk->run.stack_base);
2006-03-25 17:04:36 +00:00
return v;
2006-03-24 06:33:36 +00:00
}
2006-03-26 14:03:08 +00:00
static int __raw_push (xp_awk_t* awk, void* val)
2006-03-24 06:33:36 +00:00
{
2006-03-25 17:04:36 +00:00
if (awk->run.stack_top >= awk->run.stack_limit)
2006-03-24 06:33:36 +00:00
{
void* tmp;
xp_size_t n;
2006-03-25 17:04:36 +00:00
n = awk->run.stack_limit + STACK_INCREMENT;
tmp = (void**)xp_realloc (awk->run.stack, n * xp_sizeof(void*));
2006-03-24 06:33:36 +00:00
if (tmp == XP_NULL) return -1;
2006-03-25 17:04:36 +00:00
awk->run.stack = tmp;
awk->run.stack_limit = n;
2006-03-24 06:33:36 +00:00
}
2006-03-25 17:04:36 +00:00
awk->run.stack[awk->run.stack_top++] = val;
return 0;
2006-03-24 06:33:36 +00:00
}
2006-03-26 14:03:08 +00:00
2006-03-26 16:36:30 +00:00
static void __raw_pop (xp_awk_t* awk)
2006-03-26 14:03:08 +00:00
{
xp_assert (awk->run.stack_top > awk->run.stack_base);
awk->run.stack_top--;
}