qse/ase/awk/run.c

4956 lines
112 KiB
C
Raw Normal View History

2006-01-26 15:35:20 +00:00
/*
2006-08-27 15:29:21 +00:00
* $Id: run.c,v 1.181 2006-08-27 15:29:21 bacon Exp $
2006-01-26 15:35:20 +00:00
*/
2006-03-31 16:35:37 +00:00
#include <xp/awk/awk_i.h>
2006-03-02 15:36:30 +00:00
2006-04-14 10:56:42 +00:00
/* TODO: remove this dependency...*/
2006-04-11 15:44:30 +00:00
#include <math.h>
2006-04-16 04:31:38 +00:00
#ifndef XP_AWK_STAND_ALONE
2006-01-26 15:35:20 +00:00
#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-06-30 03:53:16 +00:00
#define DEF_BUF_CAPA 256
2006-03-24 06:33:36 +00:00
#define STACK_INCREMENT 512
2006-06-20 15:27:50 +00:00
#define STACK_AT(run,n) ((run)->stack[(run)->stack_base+(n)])
2006-04-21 17:24:31 +00:00
#define STACK_NARGS(run) (STACK_AT(run,3))
#define STACK_ARG(run,n) STACK_AT(run,3+1+(n))
#define STACK_LOCAL(run,n) STACK_AT(run,3+(xp_size_t)STACK_NARGS(run)+1+(n))
#define STACK_RETVAL(run) STACK_AT(run,2)
#define STACK_GLOBAL(run,n) ((run)->stack[(n)])
2006-06-19 09:10:57 +00:00
#define STACK_RETVAL_GLOBAL(run) ((run)->stack[(run)->awk->tree.nglobals+2])
2006-03-28 16:33:09 +00:00
2006-07-01 16:07:06 +00:00
enum
{
EXIT_NONE,
EXIT_BREAK,
EXIT_CONTINUE,
EXIT_FUNCTION,
EXIT_NEXT,
EXIT_GLOBAL,
EXIT_ABORT,
};
2006-03-26 14:03:08 +00:00
2006-04-21 17:24:31 +00:00
#define PANIC(run,code) \
2006-04-22 13:54:53 +00:00
do { (run)->errnum = (code); return XP_NULL; } while (0)
2006-07-26 16:43:35 +00:00
2006-04-21 17:24:31 +00:00
#define PANIC_I(run,code) \
2006-04-22 13:54:53 +00:00
do { (run)->errnum = (code); return -1; } while (0)
2006-04-21 17:24:31 +00:00
2006-08-10 16:06:52 +00:00
static void __add_run (xp_awk_t* awk, xp_awk_run_t* run);
static void __del_run (xp_awk_t* awk, xp_awk_run_t* run);
static int __init_run (
xp_awk_run_t* run, xp_awk_runios_t* runios, int* errnum);
static void __deinit_run (xp_awk_run_t* run);
2006-04-21 17:24:31 +00:00
static int __run_main (xp_awk_run_t* run);
2006-04-22 13:54:53 +00:00
static int __run_pattern_blocks (xp_awk_run_t* run);
2006-08-13 05:55:02 +00:00
static int __run_pattern_block_chain (
xp_awk_run_t* run, xp_awk_chain_t* chain);
static int __run_pattern_block (
xp_awk_run_t* run, xp_awk_chain_t* chain, xp_size_t block_no);
2006-04-21 17:24:31 +00:00
static int __run_block (xp_awk_run_t* run, xp_awk_nde_blk_t* nde);
static int __run_statement (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-06-27 14:18:19 +00:00
static int __run_if (xp_awk_run_t* run, xp_awk_nde_if_t* nde);
static int __run_while (xp_awk_run_t* run, xp_awk_nde_while_t* nde);
static int __run_for (xp_awk_run_t* run, xp_awk_nde_for_t* nde);
static int __run_foreach (xp_awk_run_t* run, xp_awk_nde_foreach_t* nde);
static int __run_break (xp_awk_run_t* run, xp_awk_nde_break_t* nde);
static int __run_continue (xp_awk_run_t* run, xp_awk_nde_continue_t* nde);
static int __run_return (xp_awk_run_t* run, xp_awk_nde_return_t* nde);
static int __run_exit (xp_awk_run_t* run, xp_awk_nde_exit_t* nde);
static int __run_next (xp_awk_run_t* run, xp_awk_nde_next_t* nde);
static int __run_nextfile (xp_awk_run_t* run, xp_awk_nde_nextfile_t* nde);
static int __run_delete (xp_awk_run_t* run, xp_awk_nde_delete_t* nde);
static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde);
2006-04-21 17:24:31 +00:00
2006-07-31 15:59:43 +00:00
static xp_awk_val_t* __eval_expression (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_expression0 (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-04-21 17:24:31 +00:00
2006-04-24 15:34:52 +00:00
static xp_awk_val_t* __eval_group (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_assignment (
xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-04-02 16:22:36 +00:00
static xp_awk_val_t* __do_assignment (
2006-07-06 13:57:32 +00:00
xp_awk_run_t* run, xp_awk_nde_t* var, xp_awk_val_t* val);
static xp_awk_val_t* __do_assignment_scalar (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_nde_var_t* var, xp_awk_val_t* val);
2006-04-18 11:26:48 +00:00
static xp_awk_val_t* __do_assignment_map (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_nde_var_t* var, xp_awk_val_t* val);
2006-07-06 13:57:32 +00:00
static xp_awk_val_t* __do_assignment_pos (
xp_awk_run_t* run, xp_awk_nde_pos_t* pos, xp_awk_val_t* val);
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_binary (
xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_lor (
2006-04-29 12:09:29 +00:00
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_land (
2006-04-29 12:09:29 +00:00
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right);
2006-04-25 15:20:10 +00:00
static xp_awk_val_t* __eval_binop_in (
2006-04-27 15:20:10 +00:00
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_bor (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_bxor (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_band (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_eq (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_ne (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_gt (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_ge (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_lt (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_le (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_lshift (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_rshift (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_plus (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_minus (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_mul (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_div (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-03 14:55:34 +00:00
static xp_awk_val_t* __eval_binop_mod (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-11 15:44:30 +00:00
static xp_awk_val_t* __eval_binop_exp (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-06-29 15:40:30 +00:00
static xp_awk_val_t* __eval_binop_concat (
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
2006-04-12 03:54:12 +00:00
static xp_awk_val_t* __eval_binop_ma (
2006-07-31 15:59:43 +00:00
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right);
2006-04-12 03:54:12 +00:00
static xp_awk_val_t* __eval_binop_nm (
2006-07-31 15:59:43 +00:00
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right);
2006-08-01 04:36:33 +00:00
static xp_awk_val_t* __eval_binop_match0 (
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right, int ret);
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_unary (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_incpre (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_incpst (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_cnd (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-06-20 15:27:50 +00:00
2006-06-19 15:43:27 +00:00
static xp_awk_val_t* __eval_bfn (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-06-20 15:27:50 +00:00
static xp_awk_val_t* __eval_afn (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_call (
2006-08-18 07:52:47 +00:00
xp_awk_run_t* run, xp_awk_nde_t* nde,
const xp_char_t* bfn_arg_spec, xp_awk_afn_t* afn);
2006-08-20 15:49:48 +00:00
static xp_awk_val_t** __get_reference (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t** __get_reference_indexed (
xp_awk_run_t* run, xp_awk_nde_var_t* nde, xp_awk_val_t** val);
2006-06-20 15:27:50 +00:00
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_int (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_real (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_str (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-04-24 11:22:42 +00:00
static xp_awk_val_t* __eval_rex (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_named (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_global (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_local (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_arg (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_namedidx (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_globalidx (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_localidx (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_argidx (xp_awk_run_t* run, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_pos (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-06-12 15:11:02 +00:00
static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-04-21 17:24:31 +00:00
static int __raw_push (xp_awk_run_t* run, void* val);
2006-08-18 07:52:47 +00:00
#define __raw_pop(run) \
do { \
xp_assert ((run)->stack_top > (run)->stack_base); \
(run)->stack_top--; \
} while (0)
2006-04-21 17:24:31 +00:00
static void __raw_pop_times (xp_awk_run_t* run, xp_size_t times);
2006-03-25 17:04:36 +00:00
2006-07-03 04:09:56 +00:00
static int __read_record (xp_awk_run_t* run);
2006-07-27 16:50:29 +00:00
static int __set_record (
xp_awk_run_t* run, const xp_char_t* str, xp_size_t len);
2006-07-05 16:20:23 +00:00
static int __split_record (xp_awk_run_t* run);
2006-08-27 15:29:21 +00:00
static int __clear_record (xp_awk_run_t* run, xp_bool_t noline);
static int __recomp_record_fields (
xp_awk_run_t* run, xp_size_t lv, xp_char_t* str, xp_size_t len);
2006-07-10 14:28:46 +00:00
2006-08-03 05:05:48 +00:00
static xp_char_t* __idxnde_to_str (
xp_awk_run_t* run, xp_awk_nde_t* nde, xp_size_t* len);
2006-04-16 04:31:38 +00:00
2006-04-03 14:55:34 +00:00
typedef xp_awk_val_t* (*binop_func_t) (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right);
typedef xp_awk_val_t* (*eval_expr_t) (xp_awk_run_t* run, xp_awk_nde_t* nde);
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
/* TODO: remove this function */
2006-04-30 17:12:51 +00:00
static int __printval (xp_awk_pair_t* pair, void* arg)
2006-03-05 17:07:33 +00:00
{
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("%s = "), (const xp_char_t*)pair->key);
2006-03-05 17:07:33 +00:00
xp_awk_printval ((xp_awk_val_t*)pair->val);
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("\n"));
2006-03-05 17:07:33 +00:00
return 0;
}
2006-06-20 15:27:50 +00:00
xp_size_t xp_awk_getnargs (void* run)
{
return (xp_size_t) STACK_NARGS ((xp_awk_run_t*)run);
}
xp_awk_val_t* xp_awk_getarg (void* run, xp_size_t idx)
{
return STACK_ARG ((xp_awk_run_t*)run, idx);
}
2006-08-24 03:30:32 +00:00
xp_awk_val_t* xp_awk_getglobal (void* run, xp_size_t idx)
2006-06-20 15:27:50 +00:00
{
2006-08-24 03:30:32 +00:00
return STACK_GLOBAL(((xp_awk_run_t*)run),idx);
2006-06-20 15:27:50 +00:00
}
2006-08-27 10:45:36 +00:00
int xp_awk_setglobal (void* run, xp_size_t idx, xp_awk_val_t* val)
2006-07-12 07:25:15 +00:00
{
2006-08-27 10:45:36 +00:00
xp_awk_val_t* old = STACK_GLOBAL((xp_awk_run_t*)run,idx);
if (old->type == XP_AWK_VAL_MAP)
{
/* once a variable becomes an array,
* it cannot be changed to a scalar variable */
PANIC_I ((xp_awk_run_t*)run, XP_AWK_EMAPTOSCALAR);
}
2006-08-27 15:29:21 +00:00
/* TODO: is this correct?? */
if (val->type == XP_AWK_VAL_MAP &&
(idx >= XP_AWK_GLOBAL_ARGC && idx <= XP_AWK_GLOBAL_SUBSEP) &&
idx != XP_AWK_GLOBAL_ARGV)
{
/* TODO: better error code */
PANIC_I ((xp_awk_run_t*)run, XP_AWK_ESCALARTOMAP);
}
if (idx == XP_AWK_GLOBAL_RS)
{
2006-08-27 10:45:36 +00:00
/* TODO: if idx == XP_AWK_GLOBAL_RS and it is multi-char sttring, compile it */
2006-08-27 15:29:21 +00:00
}
/* TODO: if idx == XP_AWK_GLOBAL_NF recompute $0, etc */
2006-08-27 10:45:36 +00:00
xp_awk_refdownval (run, old);
STACK_GLOBAL((xp_awk_run_t*)run,idx) = val;
xp_awk_refupval (val);
return 0;
2006-07-12 07:25:15 +00:00
}
2006-06-21 11:45:26 +00:00
void xp_awk_seterrnum (void* run, int errnum)
{
xp_awk_run_t* r = (xp_awk_run_t*)run;
r->errnum = errnum;
}
2006-08-24 03:30:32 +00:00
void xp_awk_setretval (void* run, xp_awk_val_t* val)
{
xp_awk_run_t* r = (xp_awk_run_t*)run;
xp_awk_refdownval (r, STACK_RETVAL(r));
STACK_RETVAL(r) = val;
/* should use the same trick as __run_return_statement */
xp_awk_refupval (val);
}
2006-08-04 17:54:52 +00:00
int xp_awk_run (xp_awk_t* awk, xp_awk_runios_t* runios, xp_awk_runcbs_t* runcbs)
2006-04-21 17:24:31 +00:00
{
2006-04-22 13:54:53 +00:00
xp_awk_run_t* run;
2006-08-10 16:06:52 +00:00
int n, errnum;
awk->errnum = XP_AWK_ENOERR;
2006-04-21 17:24:31 +00:00
2006-04-22 13:54:53 +00:00
run = (xp_awk_run_t*) xp_malloc (xp_sizeof(xp_awk_run_t));
if (run == XP_NULL)
{
awk->errnum = XP_AWK_ENOMEM;
return -1;
}
2006-04-21 17:24:31 +00:00
2006-08-10 16:06:52 +00:00
__add_run (awk, run);
if (__init_run (run, runios, &errnum) == -1)
2006-04-22 13:54:53 +00:00
{
2006-08-10 16:06:52 +00:00
awk->errnum = errnum;
__del_run (awk, run);
2006-04-22 13:54:53 +00:00
xp_free (run);
return -1;
}
2006-08-13 05:55:02 +00:00
if (runcbs != XP_NULL && runcbs->on_start != XP_NULL)
{
runcbs->on_start (awk, run, runcbs->custom_data);
}
2006-08-04 16:31:22 +00:00
2006-04-22 13:54:53 +00:00
n = __run_main (run);
2006-08-13 05:55:02 +00:00
if (n == -1)
{
/* if no callback is specified, awk's error number
* is updated with the run's error number */
awk->errnum = (runcbs == XP_NULL)? run->errnum: XP_AWK_ERUNTIME;
}
2006-04-22 13:54:53 +00:00
2006-08-13 05:55:02 +00:00
if (runcbs != XP_NULL && runcbs->on_end != XP_NULL)
{
runcbs->on_end (awk, run,
((n == -1)? run->errnum: XP_AWK_ENOERR),
runcbs->custom_data);
/* when using callbacks, the function always returns 0 after
* the start callbacks has been triggered */
n = 0;
}
2006-08-04 16:31:22 +00:00
2006-08-10 16:06:52 +00:00
__deinit_run (run);
__del_run (awk, run);
2006-04-22 13:54:53 +00:00
xp_free (run);
return n;
2006-04-21 17:24:31 +00:00
}
2006-08-04 17:36:40 +00:00
int xp_awk_stop (xp_awk_t* awk, void* run)
2006-08-04 16:31:22 +00:00
{
2006-08-10 16:06:52 +00:00
xp_awk_run_t* r;
int n = 0;
2006-08-04 16:31:22 +00:00
2006-08-10 16:06:52 +00:00
if (awk->thr.lks != XP_NULL)
awk->thr.lks->lock (awk, awk->thr.lks->custom_data);
/* check if the run handle given is valid */
for (r = awk->run.ptr; r != XP_NULL; r = r->next)
2006-08-04 16:31:22 +00:00
{
2006-08-10 16:06:52 +00:00
if (r == run)
{
xp_assert (r->awk == awk);
r->exit_level = EXIT_ABORT;
break;
}
2006-08-04 16:31:22 +00:00
}
2006-08-10 16:06:52 +00:00
if (r == XP_NULL)
{
/* if it is not found in the awk's run list,
* it is not a valid handle */
awk->errnum = XP_AWK_EINVAL;
n = -1;
}
if (awk->thr.lks != XP_NULL)
awk->thr.lks->unlock (awk, awk->thr.lks->custom_data);
return n;
}
2006-08-13 05:55:02 +00:00
void xp_awk_stopall (xp_awk_t* awk)
{
xp_awk_run_t* r;
if (awk->thr.lks != XP_NULL)
awk->thr.lks->lock (awk, awk->thr.lks->custom_data);
for (r = awk->run.ptr; r != XP_NULL; r = r->next)
{
r->exit_level = EXIT_ABORT;
}
if (awk->thr.lks != XP_NULL)
awk->thr.lks->unlock (awk, awk->thr.lks->custom_data);
}
2006-08-10 16:06:52 +00:00
int xp_awk_getrunerrnum (xp_awk_t* awk, void* run, int* errnum)
{
xp_awk_run_t* r;
int n = 0;
if (awk->thr.lks != XP_NULL)
awk->thr.lks->lock (awk, awk->thr.lks->custom_data);
for (r = awk->run.ptr; r != XP_NULL; r = r->next)
{
if (r == run)
{
xp_assert (r->awk == awk);
*errnum = r->errnum;
break;
}
}
if (r == XP_NULL)
{
awk->errnum = XP_AWK_EINVAL;
n = -1;
}
if (awk->thr.lks != XP_NULL)
awk->thr.lks->unlock (awk, awk->thr.lks->custom_data);
return n;
2006-08-04 16:31:22 +00:00
}
2006-04-24 14:38:46 +00:00
static void __free_namedval (void* run, void* val)
2006-04-21 17:24:31 +00:00
{
2006-04-24 14:38:46 +00:00
xp_awk_refdownval ((xp_awk_run_t*)run, val);
2006-04-21 17:24:31 +00:00
}
2006-08-10 16:06:52 +00:00
static void __add_run (xp_awk_t* awk, xp_awk_run_t* run)
2006-04-21 17:24:31 +00:00
{
2006-08-10 16:06:52 +00:00
if (awk->thr.lks != XP_NULL)
awk->thr.lks->lock (awk, awk->thr.lks->custom_data);
run->awk = awk;
run->prev = XP_NULL;
run->next = awk->run.ptr;
if (run->next != XP_NULL) run->next->prev = run;
awk->run.ptr = run;
awk->run.count++;
if (awk->thr.lks != XP_NULL)
awk->thr.lks->unlock (awk, awk->thr.lks->custom_data);
}
static void __del_run (xp_awk_t* awk, xp_awk_run_t* run)
{
if (awk->thr.lks != XP_NULL)
awk->thr.lks->lock (awk, awk->thr.lks->custom_data);
xp_assert (awk->run.ptr != XP_NULL);
if (run->prev == XP_NULL)
{
awk->run.ptr = run->next;
if (run->next != XP_NULL) run->next->prev = XP_NULL;
}
else
{
run->prev->next = run->next;
if (run->next != XP_NULL) run->next->prev = run->prev;
}
2006-06-16 07:37:27 +00:00
2006-08-10 16:06:52 +00:00
run->awk = XP_NULL;
awk->run.count--;
if (awk->thr.lks != XP_NULL)
awk->thr.lks->unlock (awk, awk->thr.lks->custom_data);
}
static int __init_run (
xp_awk_run_t* run, xp_awk_runios_t* runios, int* errnum)
{
2006-04-21 17:24:31 +00:00
run->stack = XP_NULL;
run->stack_top = 0;
run->stack_base = 0;
run->stack_limit = 0;
2006-04-22 13:54:53 +00:00
2006-08-04 16:31:22 +00:00
run->exit_level = EXIT_NONE;
2006-04-22 13:54:53 +00:00
2006-04-21 17:24:31 +00:00
run->icache_count = 0;
run->rcache_count = 0;
2006-08-21 02:53:42 +00:00
run->fcache_count = 0;
2006-04-21 17:24:31 +00:00
2006-04-22 13:54:53 +00:00
run->errnum = XP_AWK_ENOERR;
2006-04-21 17:24:31 +00:00
2006-07-03 04:09:56 +00:00
run->inrec.buf_pos = 0;
run->inrec.buf_len = 0;
run->inrec.flds = XP_NULL;
run->inrec.nflds = 0;
2006-07-07 09:48:23 +00:00
run->inrec.maxflds = 0;
2006-07-06 15:54:41 +00:00
run->inrec.d0 = xp_awk_val_nil;
2006-07-03 04:09:56 +00:00
if (xp_str_open (&run->inrec.line, DEF_BUF_CAPA) == XP_NULL)
2006-04-22 13:54:53 +00:00
{
2006-08-10 16:06:52 +00:00
*errnum = XP_AWK_ENOMEM;
2006-04-22 13:54:53 +00:00
return -1;
}
2006-04-21 17:24:31 +00:00
if (xp_awk_map_open (&run->named,
2006-06-30 03:53:16 +00:00
run, DEF_BUF_CAPA, __free_namedval) == XP_NULL)
2006-04-21 17:24:31 +00:00
{
2006-07-03 04:09:56 +00:00
xp_str_close (&run->inrec.line);
2006-08-10 16:06:52 +00:00
*errnum = XP_AWK_ENOMEM;
2006-04-21 17:24:31 +00:00
return -1;
}
2006-08-13 05:55:02 +00:00
run->pattern_range_state = (xp_byte_t*)
xp_calloc (run->awk->tree.chain_size, xp_sizeof(xp_byte_t));
if (run->pattern_range_state == XP_NULL)
{
xp_awk_map_close (&run->named);
xp_str_close (&run->inrec.line);
*errnum = XP_AWK_ENOMEM;
return -1;
}
2006-08-10 16:06:52 +00:00
run->extio.handler[XP_AWK_EXTIO_PIPE] = runios->pipe;
run->extio.handler[XP_AWK_EXTIO_COPROC] = runios->coproc;
run->extio.handler[XP_AWK_EXTIO_FILE] = runios->file;
run->extio.handler[XP_AWK_EXTIO_CONSOLE] = runios->console;
run->extio.chain = XP_NULL;
2006-04-21 17:24:31 +00:00
return 0;
}
2006-08-10 16:06:52 +00:00
static void __deinit_run (xp_awk_run_t* run)
2006-04-21 17:24:31 +00:00
{
2006-08-13 05:55:02 +00:00
xp_free (run->pattern_range_state);
2006-06-21 15:37:51 +00:00
/* close all pending eio's */
/* TODO: what if this operation fails? */
xp_awk_clearextio (run);
2006-08-10 16:06:52 +00:00
xp_assert (run->extio.chain == XP_NULL);
2006-06-21 15:37:51 +00:00
2006-07-25 16:41:40 +00:00
/* destroy input record. __clear_record should be called
* before the run stack has been destroyed because it may try
* to change the value to XP_AWK_GLOBAL_NF. */
2006-08-27 15:29:21 +00:00
__clear_record (run, xp_false);
2006-07-25 16:41:40 +00:00
if (run->inrec.flds != XP_NULL)
{
xp_free (run->inrec.flds);
run->inrec.flds = XP_NULL;
run->inrec.maxflds = 0;
}
xp_str_close (&run->inrec.line);
2006-04-21 17:24:31 +00:00
/* destroy run stack */
if (run->stack != XP_NULL)
{
xp_assert (run->stack_top == 0);
xp_free (run->stack);
run->stack = XP_NULL;
run->stack_top = 0;
run->stack_base = 0;
run->stack_limit = 0;
}
/* destroy named variables */
xp_awk_map_close (&run->named);
/* destroy values in free list */
while (run->icache_count > 0)
{
xp_awk_val_int_t* tmp = run->icache[--run->icache_count];
xp_awk_freeval (run, (xp_awk_val_t*)tmp, xp_false);
}
while (run->rcache_count > 0)
{
xp_awk_val_real_t* tmp = run->rcache[--run->rcache_count];
xp_awk_freeval (run, (xp_awk_val_t*)tmp, xp_false);
}
2006-08-21 02:53:42 +00:00
while (run->fcache_count > 0)
{
xp_awk_val_ref_t* tmp = run->fcache[--run->fcache_count];
xp_awk_freeval (run, (xp_awk_val_t*)tmp, xp_false);
}
2006-04-21 17:24:31 +00:00
}
static int __run_main (xp_awk_run_t* run)
2006-01-26 15:35:20 +00:00
{
2006-04-09 15:31:13 +00:00
xp_size_t nglobals, nargs, i;
xp_size_t saved_stack_top;
xp_awk_val_t* v;
2006-04-06 16:25:37 +00:00
int n = 0;
2006-03-28 11:32:58 +00:00
2006-04-21 17:24:31 +00:00
xp_assert (run->stack_base == 0 && run->stack_top == 0);
2006-03-28 16:33:09 +00:00
/* secure space for global variables */
2006-04-21 17:24:31 +00:00
saved_stack_top = run->stack_top;
2006-04-09 15:31:13 +00:00
2006-06-19 09:10:57 +00:00
nglobals = run->awk->tree.nglobals;
2006-06-19 04:38:51 +00:00
2006-03-28 11:32:58 +00:00
while (nglobals > 0)
{
--nglobals;
2006-04-21 17:24:31 +00:00
if (__raw_push(run,xp_awk_val_nil) == -1)
2006-03-28 11:32:58 +00:00
{
2006-04-09 15:31:13 +00:00
/* restore the stack_top with the saved value
* instead of calling __raw_pop as many times as
* the successful __raw_push. it is ok because
* the values pushed so fare are all xp_awk_val_nil */
2006-04-21 17:24:31 +00:00
run->stack_top = saved_stack_top;
PANIC_I (run, XP_AWK_ENOMEM);
2006-03-28 11:32:58 +00:00
}
}
2006-08-04 16:31:22 +00:00
run->exit_level = EXIT_NONE;
2006-08-04 17:36:40 +00:00
if (run->awk->option & XP_AWK_RUNMAIN)
2006-04-09 15:31:13 +00:00
{
2006-04-27 15:59:01 +00:00
/* TODO: should the main function be user-specifiable? */
2006-08-03 05:05:48 +00:00
xp_awk_nde_call_t nde;
2006-04-10 09:22:05 +00:00
2006-08-03 05:05:48 +00:00
nde.type = XP_AWK_NDE_AFN;
nde.next = XP_NULL;
nde.what.afn.name = XP_T("main");
nde.what.afn.name_len = 4;
nde.args = XP_NULL;
nde.nargs = 0;
2006-06-20 15:27:50 +00:00
v = __eval_afn (run, (xp_awk_nde_t*)&nde);
2006-04-10 09:22:05 +00:00
if (v == XP_NULL) n = -1;
else
{
/* destroy the return value if necessary */
xp_awk_refupval (v);
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, v);
2006-04-10 09:22:05 +00:00
}
2006-04-09 15:31:13 +00:00
}
else
2006-03-03 11:45:45 +00:00
{
2006-04-21 17:24:31 +00:00
saved_stack_top = run->stack_top;
if (__raw_push(run,(void*)run->stack_base) == -1)
2006-04-09 16:26:36 +00:00
{
/* restore the stack top in a cheesy(?) way */
2006-04-21 17:24:31 +00:00
run->stack_top = saved_stack_top;
2006-04-10 09:22:05 +00:00
/* pops off global variables in a decent way */
2006-06-19 09:10:57 +00:00
__raw_pop_times (run, run->awk->tree.nglobals);
2006-04-21 17:24:31 +00:00
PANIC_I (run, XP_AWK_ENOMEM);
2006-04-09 16:26:36 +00:00
}
2006-04-22 13:54:53 +00:00
2006-04-21 17:24:31 +00:00
if (__raw_push(run,(void*)saved_stack_top) == -1)
2006-04-09 16:26:36 +00:00
{
2006-04-21 17:24:31 +00:00
run->stack_top = saved_stack_top;
2006-06-19 09:10:57 +00:00
__raw_pop_times (run, run->awk->tree.nglobals);
2006-04-21 17:24:31 +00:00
PANIC_I (run, XP_AWK_ENOMEM);
2006-04-09 16:26:36 +00:00
}
/* secure space for a return value */
2006-04-21 17:24:31 +00:00
if (__raw_push(run,xp_awk_val_nil) == -1)
2006-04-09 16:26:36 +00:00
{
2006-04-21 17:24:31 +00:00
run->stack_top = saved_stack_top;
2006-06-19 09:10:57 +00:00
__raw_pop_times (run, run->awk->tree.nglobals);
2006-04-21 17:24:31 +00:00
PANIC_I (run, XP_AWK_ENOMEM);
2006-04-09 16:26:36 +00:00
}
/* secure space for nargs */
2006-04-21 17:24:31 +00:00
if (__raw_push(run,xp_awk_val_nil) == -1)
2006-04-09 16:26:36 +00:00
{
2006-04-21 17:24:31 +00:00
run->stack_top = saved_stack_top;
2006-06-19 09:10:57 +00:00
__raw_pop_times (run, run->awk->tree.nglobals);
2006-04-21 17:24:31 +00:00
PANIC_I (run, XP_AWK_ENOMEM);
2006-04-09 16:26:36 +00:00
}
2006-04-21 17:24:31 +00:00
run->stack_base = saved_stack_top;
2006-04-09 16:26:36 +00:00
/* set nargs to zero */
nargs = 0;
2006-04-21 17:24:31 +00:00
STACK_NARGS(run) = (void*)nargs;
2006-04-09 16:26:36 +00:00
/* stack set up properly. ready to exeucte statement blocks */
2006-08-04 16:31:22 +00:00
if (n == 0 &&
run->awk->tree.begin != XP_NULL &&
run->exit_level != EXIT_ABORT)
2006-04-09 15:31:13 +00:00
{
2006-07-01 16:07:06 +00:00
xp_awk_nde_blk_t* blk;
2006-04-10 09:22:05 +00:00
2006-07-01 16:07:06 +00:00
blk = (xp_awk_nde_blk_t*)run->awk->tree.begin;
xp_assert (blk->type == XP_AWK_NDE_BLK);
2006-04-10 09:22:05 +00:00
2006-08-03 15:50:04 +00:00
run->active_block = blk;
2006-07-01 16:07:06 +00:00
run->exit_level = EXIT_NONE;
if (__run_block (run, blk) == -1) n = -1;
2006-04-09 15:31:13 +00:00
}
2006-08-04 16:31:22 +00:00
if (n == 0 &&
run->awk->tree.chain != XP_NULL &&
run->exit_level != EXIT_ABORT)
2006-04-10 09:22:05 +00:00
{
2006-04-22 13:54:53 +00:00
if (__run_pattern_blocks (run) == -1) n = -1;
2006-04-10 09:22:05 +00:00
}
2006-04-09 15:31:13 +00:00
2006-08-04 16:31:22 +00:00
if (n == 0 &&
run->awk->tree.end != XP_NULL &&
run->exit_level != EXIT_ABORT)
2006-04-09 15:31:13 +00:00
{
2006-07-01 16:07:06 +00:00
xp_awk_nde_blk_t* blk;
2006-04-10 09:22:05 +00:00
2006-07-01 16:07:06 +00:00
blk = (xp_awk_nde_blk_t*)run->awk->tree.end;
xp_assert (blk->type == XP_AWK_NDE_BLK);
2006-04-10 09:22:05 +00:00
2006-08-03 15:50:04 +00:00
run->active_block = blk;
2006-07-01 16:07:06 +00:00
run->exit_level = EXIT_NONE;
if (__run_block (run, blk) == -1) n = -1;
2006-04-09 15:31:13 +00:00
}
2006-01-26 15:35:20 +00:00
2006-04-09 16:26:36 +00:00
/* restore stack */
2006-04-21 17:24:31 +00:00
nargs = (xp_size_t)STACK_NARGS(run);
2006-04-09 16:26:36 +00:00
xp_assert (nargs == 0);
for (i = 0; i < nargs; i++)
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, STACK_ARG(run,i));
2006-04-09 16:26:36 +00:00
}
2006-04-06 16:25:37 +00:00
2006-04-21 17:24:31 +00:00
v = STACK_RETVAL(run);
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("Return Value - "));
2006-04-10 09:22:05 +00:00
xp_awk_printval (v);
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("\n"));
2006-04-10 09:22:05 +00:00
/* the life of the global return value is over here
* unlike the return value of each function */
/*xp_awk_refdownval_nofree (awk, v);*/
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, v);
2006-04-09 15:31:13 +00:00
2006-04-21 17:24:31 +00:00
run->stack_top =
(xp_size_t)run->stack[run->stack_base+1];
run->stack_base =
(xp_size_t)run->stack[run->stack_base+0];
2006-04-09 16:26:36 +00:00
}
2006-04-09 15:31:13 +00:00
/* pops off the global variables */
2006-06-19 09:10:57 +00:00
nglobals = run->awk->tree.nglobals; /*run->nglobals */
2006-04-06 16:25:37 +00:00
while (nglobals > 0)
{
--nglobals;
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, STACK_GLOBAL(run,nglobals));
__raw_pop (run);
2006-01-26 15:35:20 +00:00
}
2006-04-10 09:22:05 +00:00
/* just reset the exit level */
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_NONE;
2006-04-10 09:22:05 +00:00
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("-[VARIABLES]------------------------\n"));
2006-04-30 17:12:51 +00:00
xp_awk_map_walk (&run->named, __printval, XP_NULL);
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("-[END VARIABLES]--------------------------\n"));
2006-04-10 09:22:05 +00:00
2006-04-06 16:25:37 +00:00
return n;
2006-01-26 15:35:20 +00:00
}
2006-05-09 15:21:26 +00:00
static int __run_pattern_blocks (xp_awk_run_t* run)
2006-04-22 13:54:53 +00:00
{
xp_ssize_t n;
2006-08-01 15:57:43 +00:00
xp_bool_t need_to_close = xp_false;
2006-04-22 13:54:53 +00:00
2006-07-03 04:09:56 +00:00
run->inrec.buf_pos = 0;
run->inrec.buf_len = 0;
run->inrec.eof = xp_false;
2006-04-22 13:54:53 +00:00
2006-08-02 14:36:23 +00:00
/* run each pattern block */
2006-04-22 13:54:53 +00:00
while (run->exit_level != EXIT_GLOBAL &&
run->exit_level != EXIT_ABORT)
{
int x;
2006-05-09 15:21:26 +00:00
2006-04-22 13:54:53 +00:00
run->exit_level = EXIT_NONE;
2006-07-05 16:20:23 +00:00
x = __read_record (run);
2006-04-22 13:54:53 +00:00
if (x == -1)
{
2006-08-27 15:29:21 +00:00
int saved = run->errnum;
2006-04-22 13:54:53 +00:00
/* don't care about the result of input close */
2006-08-02 11:26:11 +00:00
xp_awk_closeextio_read (
2006-08-27 15:29:21 +00:00
run, XP_AWK_IN_CONSOLE, XP_T(""));
run->errnum = saved;
2006-04-22 13:54:53 +00:00
return -1;
}
2006-08-01 15:57:43 +00:00
need_to_close = xp_true;
2006-04-22 13:54:53 +00:00
if (x == 0) break; /* end of input */
2006-06-19 09:10:57 +00:00
if (__run_pattern_block_chain (run, run->awk->tree.chain) == -1)
2006-05-09 15:21:26 +00:00
{
2006-08-27 15:29:21 +00:00
int saved = run->errnum;
2006-08-02 11:26:11 +00:00
xp_awk_closeextio_read (
2006-08-27 15:29:21 +00:00
run, XP_AWK_IN_CONSOLE, XP_T(""));
run->errnum = saved;
2006-05-09 15:21:26 +00:00
return -1;
}
2006-04-22 13:54:53 +00:00
}
2006-08-02 14:36:23 +00:00
/* In case of getline, the code would make getline return -1,
* set ERRNO, make this function return 0 after having checked
* if closextio has returned -1 and errnum has been set to
2006-08-23 15:42:16 +00:00
* XP_AWK_EIOHANDLER. But this part of the code ends the input for
2006-08-02 14:36:23 +00:00
* the implicit pattern-block loop, which is totally different
* from getline. so it returns -1 as long as closeextio returns
* -1 regardless of the value of errnum. */
2006-08-01 15:57:43 +00:00
if (need_to_close)
2006-07-27 16:50:29 +00:00
{
2006-08-02 11:26:11 +00:00
n = xp_awk_closeextio_read (
2006-08-27 15:29:21 +00:00
run, XP_AWK_IN_CONSOLE, XP_T(""));
2006-08-01 15:57:43 +00:00
if (n == -1)
{
2006-08-27 15:29:21 +00:00
if (run->errnum == XP_AWK_EIOHANDLER)
2006-08-03 09:54:16 +00:00
PANIC_I (run, XP_AWK_ECONINCLOSE);
2006-08-27 15:29:21 +00:00
else return -1;
2006-08-01 15:57:43 +00:00
}
2006-07-27 16:50:29 +00:00
}
2006-04-22 13:54:53 +00:00
return 0;
}
2006-05-09 15:21:26 +00:00
static int __run_pattern_block_chain (xp_awk_run_t* run, xp_awk_chain_t* chain)
{
2006-08-13 05:55:02 +00:00
xp_size_t block_no = 0;
2006-07-01 16:07:06 +00:00
while (run->exit_level != EXIT_GLOBAL &&
run->exit_level != EXIT_ABORT && chain != XP_NULL)
2006-05-09 15:21:26 +00:00
{
2006-07-01 16:07:06 +00:00
if (run->exit_level == EXIT_NEXT)
{
run->exit_level = EXIT_NONE;
break;
}
2006-08-13 05:55:02 +00:00
if (__run_pattern_block (run, chain, block_no) == -1) return -1;
chain = chain->next;
block_no++;
2006-08-02 14:36:23 +00:00
}
2006-05-09 15:21:26 +00:00
2006-08-02 14:36:23 +00:00
return 0;
}
2006-08-13 05:55:02 +00:00
static int __run_pattern_block (
xp_awk_run_t* run, xp_awk_chain_t* chain, xp_size_t block_no)
2006-08-02 14:36:23 +00:00
{
xp_awk_nde_t* ptn;
xp_awk_nde_blk_t* blk;
ptn = chain->pattern;
blk = (xp_awk_nde_blk_t*)chain->action;
if (ptn == XP_NULL)
{
/* just execute the block */
2006-08-03 15:50:04 +00:00
run->active_block = blk;
2006-08-02 14:36:23 +00:00
if (__run_block (run, blk) == -1) return -1;
}
else
{
if (ptn->next == XP_NULL)
2006-05-09 15:21:26 +00:00
{
2006-08-02 14:36:23 +00:00
/* pattern { ... } */
2006-05-09 15:21:26 +00:00
xp_awk_val_t* v1;
2006-08-02 14:36:23 +00:00
2006-05-09 15:21:26 +00:00
v1 = __eval_expression (run, ptn);
if (v1 == XP_NULL) return -1;
xp_awk_refupval (v1);
2006-08-02 14:36:23 +00:00
if (xp_awk_valtobool(v1))
2006-05-09 15:21:26 +00:00
{
2006-08-03 15:50:04 +00:00
run->active_block = blk;
2006-08-02 14:36:23 +00:00
if (__run_block (run, blk) == -1)
{
xp_awk_refdownval (run, v1);
return -1;
}
}
xp_awk_refdownval (run, v1);
}
else
{
/* pattern, pattern { ... } */
xp_assert (ptn->next->next == XP_NULL);
2006-08-13 05:55:02 +00:00
if (run->pattern_range_state[block_no] == 0)
2006-08-02 14:36:23 +00:00
{
xp_awk_val_t* v1;
v1 = __eval_expression (run, ptn);
if (v1 == XP_NULL) return -1;
xp_awk_refupval (v1);
2006-07-31 15:59:43 +00:00
if (xp_awk_valtobool(v1))
2006-05-09 15:21:26 +00:00
{
2006-08-03 15:50:04 +00:00
run->active_block = blk;
2006-07-01 16:07:06 +00:00
if (__run_block (run, blk) == -1)
2006-05-09 15:21:26 +00:00
{
xp_awk_refdownval (run, v1);
return -1;
}
2006-08-02 14:36:23 +00:00
2006-08-13 05:55:02 +00:00
run->pattern_range_state[block_no] = 1;
2006-05-09 15:21:26 +00:00
}
xp_awk_refdownval (run, v1);
}
2006-08-13 05:55:02 +00:00
else if (run->pattern_range_state[block_no] == 1)
2006-05-09 15:21:26 +00:00
{
2006-08-02 14:36:23 +00:00
xp_awk_val_t* v2;
2006-07-30 15:53:42 +00:00
2006-08-02 14:36:23 +00:00
v2 = __eval_expression (run, ptn->next);
if (v2 == XP_NULL) return -1;
xp_awk_refupval (v2);
2006-08-03 15:50:04 +00:00
run->active_block = blk;
2006-08-02 14:36:23 +00:00
if (__run_block (run, blk) == -1)
{
xp_awk_refdownval (run, v2);
return -1;
}
if (xp_awk_valtobool(v2))
2006-08-13 05:55:02 +00:00
run->pattern_range_state[block_no] = 0;
2006-08-02 14:36:23 +00:00
xp_awk_refdownval (run, v2);
2006-05-09 15:21:26 +00:00
}
}
}
return 0;
}
2006-04-21 17:24:31 +00:00
static int __run_block (xp_awk_run_t* run, 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-04-10 14:53:48 +00:00
xp_size_t saved_stack_top;
2006-04-06 16:25:37 +00:00
int n = 0;
2006-02-23 15:37:34 +00:00
2006-08-03 09:54:16 +00:00
if (nde == XP_NULL)
{
/* blockless pattern - execute print $0*/
xp_awk_refupval (run->inrec.d0);
n = xp_awk_writeextio_nl (run,
2006-08-27 15:29:21 +00:00
XP_AWK_OUT_CONSOLE, XP_T(""), run->inrec.d0);
2006-08-03 09:54:16 +00:00
if (n == -1)
{
xp_awk_refdownval (run, run->inrec.d0);
2006-08-27 15:29:21 +00:00
if (run->errnum == XP_AWK_EIOHANDLER)
2006-08-03 09:54:16 +00:00
PANIC_I (run, XP_AWK_ECONOUTDATA);
2006-08-27 15:29:21 +00:00
else return -1;
2006-08-03 09:54:16 +00:00
}
xp_awk_refdownval (run, run->inrec.d0);
return 0;
}
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-05-06 12:52:36 +00:00
/*xp_printf (XP_T("securing space for local variables nlocals = %d\n"), nlocals);*/
2006-04-21 17:24:31 +00:00
saved_stack_top = run->stack_top;
2006-04-10 14:53:48 +00:00
2006-03-26 14:03:08 +00:00
/* secure space for local variables */
while (nlocals > 0)
{
--nlocals;
2006-04-21 17:24:31 +00:00
if (__raw_push(run,xp_awk_val_nil) == -1)
2006-03-26 14:03:08 +00:00
{
2006-04-10 14:53:48 +00:00
/* restore stack top */
2006-04-21 17:24:31 +00:00
run->stack_top = saved_stack_top;
2006-03-26 14:03:08 +00:00
return -1;
}
/* refupval is not required for xp_awk_val_nil */
}
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("executing block statements\n"));*/
2006-04-21 17:24:31 +00:00
while (p != XP_NULL && run->exit_level == EXIT_NONE)
2006-03-03 11:45:45 +00:00
{
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("running a statement\n"));*/
2006-04-21 17:24:31 +00:00
if (__run_statement(run,p) == -1)
2006-04-06 16:25:37 +00:00
{
n = -1;
break;
}
2006-02-23 15:37:34 +00:00
p = p->next;
}
2006-03-26 14:03:08 +00:00
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("popping off local variables\n"));*/
2006-03-26 14:03:08 +00:00
/* pop off local variables */
nlocals = nde->nlocals;
while (nlocals > 0)
{
--nlocals;
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, STACK_LOCAL(run,nlocals));
__raw_pop (run);
2006-03-26 14:03:08 +00:00
}
2006-04-06 16:25:37 +00:00
return n;
2006-01-26 15:35:20 +00:00
}
2006-04-21 17:24:31 +00:00
static int __run_statement (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-01-26 15:35:20 +00:00
{
2006-03-03 11:45:45 +00:00
switch (nde->type)
{
2006-06-13 08:35:53 +00:00
case XP_AWK_NDE_NULL:
{
/* do nothing */
break;
}
case XP_AWK_NDE_BLK:
{
2006-06-30 11:31:51 +00:00
if (__run_block (run,
(xp_awk_nde_blk_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_IF:
{
2006-06-30 11:31:51 +00:00
if (__run_if (run,
(xp_awk_nde_if_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_WHILE:
case XP_AWK_NDE_DOWHILE:
{
2006-06-30 11:31:51 +00:00
if (__run_while (run,
(xp_awk_nde_while_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_FOR:
{
2006-06-30 11:31:51 +00:00
if (__run_for (run,
(xp_awk_nde_for_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_FOREACH:
{
2006-06-30 11:31:51 +00:00
if (__run_foreach (run,
(xp_awk_nde_foreach_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_BREAK:
{
2006-06-30 11:31:51 +00:00
if (__run_break (run,
(xp_awk_nde_break_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_CONTINUE:
{
2006-06-30 11:31:51 +00:00
if (__run_continue (run,
(xp_awk_nde_continue_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_RETURN:
{
2006-06-30 11:31:51 +00:00
if (__run_return (run,
(xp_awk_nde_return_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_EXIT:
{
2006-06-30 11:31:51 +00:00
if (__run_exit (run,
(xp_awk_nde_exit_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_NEXT:
{
2006-06-30 11:31:51 +00:00
if (__run_next (run,
(xp_awk_nde_next_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
case XP_AWK_NDE_NEXTFILE:
{
2006-06-30 11:31:51 +00:00
if (__run_nextfile (run,
(xp_awk_nde_nextfile_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-06-27 14:18:19 +00:00
case XP_AWK_NDE_DELETE:
{
2006-06-30 11:31:51 +00:00
if (__run_delete (run,
(xp_awk_nde_delete_t*)nde) == -1) return -1;
2006-06-27 14:18:19 +00:00
break;
}
2006-06-13 08:35:53 +00:00
case XP_AWK_NDE_PRINT:
{
2006-06-30 11:31:51 +00:00
if (__run_print (run,
(xp_awk_nde_print_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
default:
2006-05-11 18:16:04 +00:00
{
xp_awk_val_t* v;
v = __eval_expression(run,nde);
if (v == XP_NULL) return -1;
2006-07-06 15:54:41 +00:00
2006-05-11 18:16:04 +00:00
xp_awk_refupval (v);
xp_awk_refdownval (run, v);
2006-07-06 15:54:41 +00:00
2006-06-13 08:35:53 +00:00
break;
2006-05-11 18:16:04 +00:00
}
2006-03-04 15:54:37 +00:00
}
return 0;
}
2006-06-27 14:18:19 +00:00
static int __run_if (xp_awk_run_t* run, xp_awk_nde_if_t* nde)
2006-03-15 15:34:59 +00:00
{
xp_awk_val_t* test;
2006-03-27 11:43:17 +00:00
int n = 0;
2006-03-15 15:34:59 +00:00
2006-05-07 17:45:08 +00:00
/* the test expression for the if statement cannot have
* chained expressions. this should not be allowed by the
* parser first of all */
xp_assert (nde->test->next == XP_NULL);
2006-04-21 17:24:31 +00:00
test = __eval_expression (run, nde->test);
2006-03-23 13:26:04 +00:00
if (test == XP_NULL) return -1;
xp_awk_refupval (test);
2006-06-21 11:45:26 +00:00
if (xp_awk_valtobool(test))
2006-03-15 15:34:59 +00:00
{
2006-04-21 17:24:31 +00:00
n = __run_statement (run, nde->then_part);
2006-03-15 15:34:59 +00:00
}
else if (nde->else_part != XP_NULL)
{
2006-04-21 17:24:31 +00:00
n = __run_statement (run, nde->else_part);
2006-03-15 15:34:59 +00:00
}
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, test); /* TODO: is this correct?*/
2006-03-23 13:26:04 +00:00
return n;
}
2006-06-27 14:18:19 +00:00
static int __run_while (xp_awk_run_t* run, xp_awk_nde_while_t* nde)
2006-03-23 13:26:04 +00:00
{
xp_awk_val_t* test;
if (nde->type == XP_AWK_NDE_WHILE)
{
2006-05-07 17:45:08 +00:00
/* no chained expressions are allowed for the test
* expression of the while statement */
xp_assert (nde->test->next == XP_NULL);
2006-04-14 10:56:42 +00:00
/* TODO: handle run-time abortion... */
2006-03-23 13:26:04 +00:00
while (1)
{
2006-04-21 17:24:31 +00:00
test = __eval_expression (run, nde->test);
2006-03-23 13:26:04 +00:00
if (test == XP_NULL) return -1;
xp_awk_refupval (test);
2006-06-21 11:45:26 +00:00
if (xp_awk_valtobool(test))
2006-03-23 13:26:04 +00:00
{
2006-04-21 17:24:31 +00:00
if (__run_statement(run,nde->body) == -1)
2006-03-23 13:26:04 +00:00
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, test);
2006-03-23 13:26:04 +00:00
return -1;
}
}
else
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, test);
2006-03-23 13:26:04 +00:00
break;
}
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, test);
2006-03-27 11:43:17 +00:00
2006-04-21 17:24:31 +00:00
if (run->exit_level == EXIT_BREAK)
2006-03-27 11:43:17 +00:00
{
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_NONE;
2006-03-27 11:43:17 +00:00
break;
}
2006-04-21 17:24:31 +00:00
else if (run->exit_level == EXIT_CONTINUE)
2006-03-27 11:43:17 +00:00
{
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_NONE;
2006-03-27 11:43:17 +00:00
}
2006-04-21 17:24:31 +00:00
else if (run->exit_level != EXIT_NONE) break;
2006-03-23 13:26:04 +00:00
}
}
else if (nde->type == XP_AWK_NDE_DOWHILE)
{
2006-05-07 17:45:08 +00:00
/* no chained expressions are allowed for the test
* expression of the while statement */
xp_assert (nde->test->next == XP_NULL);
2006-04-14 10:56:42 +00:00
/* TODO: handle run-time abortion... */
2006-03-23 13:26:04 +00:00
do
{
2006-04-21 17:24:31 +00:00
if (__run_statement(run,nde->body) == -1) return -1;
2006-03-27 11:43:17 +00:00
2006-04-21 17:24:31 +00:00
if (run->exit_level == EXIT_BREAK)
2006-03-27 11:43:17 +00:00
{
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_NONE;
2006-03-27 11:43:17 +00:00
break;
}
2006-04-21 17:24:31 +00:00
else if (run->exit_level == EXIT_CONTINUE)
2006-03-27 11:43:17 +00:00
{
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_NONE;
2006-03-27 11:43:17 +00:00
}
2006-04-21 17:24:31 +00:00
else if (run->exit_level != EXIT_NONE) break;
2006-03-27 11:43:17 +00:00
2006-04-21 17:24:31 +00:00
test = __eval_expression (run, nde->test);
2006-03-23 13:26:04 +00:00
if (test == XP_NULL) return -1;
xp_awk_refupval (test);
2006-04-10 14:53:48 +00:00
2006-06-21 11:45:26 +00:00
if (!xp_awk_valtobool(test))
2006-03-23 13:26:04 +00:00
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, test);
2006-03-23 13:26:04 +00:00
break;
}
2006-04-10 14:53:48 +00:00
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, test);
2006-03-23 13:26:04 +00:00
}
while (1);
}
2006-03-23 15:36:20 +00:00
return 0;
}
2006-06-27 14:18:19 +00:00
static int __run_for (xp_awk_run_t* run, xp_awk_nde_for_t* nde)
2006-03-23 15:36:20 +00:00
{
2006-04-10 14:53:48 +00:00
xp_awk_val_t* val;
2006-03-23 15:36:20 +00:00
if (nde->init != XP_NULL)
{
2006-05-07 17:45:08 +00:00
xp_assert (nde->init->next == XP_NULL);
2006-04-21 17:24:31 +00:00
val = __eval_expression(run,nde->init);
2006-04-10 14:53:48 +00:00
if (val == XP_NULL) return -1;
xp_awk_refupval (val);
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, val);
2006-03-23 15:36:20 +00:00
}
while (1)
{
if (nde->test != XP_NULL)
{
xp_awk_val_t* test;
2006-05-07 17:45:08 +00:00
/* no chained expressions for the test expression of
* the for statement are allowed */
xp_assert (nde->test->next == XP_NULL);
2006-04-21 17:24:31 +00:00
test = __eval_expression (run, nde->test);
2006-03-23 15:36:20 +00:00
if (test == XP_NULL) return -1;
xp_awk_refupval (test);
2006-06-21 11:45:26 +00:00
if (xp_awk_valtobool(test))
2006-03-23 15:36:20 +00:00
{
2006-04-21 17:24:31 +00:00
if (__run_statement(run,nde->body) == -1)
2006-03-23 15:36:20 +00:00
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, test);
2006-03-23 15:36:20 +00:00
return -1;
}
}
else
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, test);
2006-03-23 15:36:20 +00:00
break;
}
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, test);
2006-03-23 15:36:20 +00:00
}
else
{
2006-04-21 17:24:31 +00:00
if (__run_statement(run,nde->body) == -1)
2006-03-23 15:36:20 +00:00
{
return -1;
}
}
2006-04-21 17:24:31 +00:00
if (run->exit_level == EXIT_BREAK)
2006-03-27 11:43:17 +00:00
{
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_NONE;
2006-03-27 11:43:17 +00:00
break;
}
2006-04-21 17:24:31 +00:00
else if (run->exit_level == EXIT_CONTINUE)
2006-03-27 11:43:17 +00:00
{
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_NONE;
2006-03-27 11:43:17 +00:00
}
2006-04-21 17:24:31 +00:00
else if (run->exit_level != EXIT_NONE) break;
2006-03-27 11:43:17 +00:00
2006-03-23 15:36:20 +00:00
if (nde->incr != XP_NULL)
{
2006-05-07 17:45:08 +00:00
xp_assert (nde->incr->next == XP_NULL);
2006-04-21 17:24:31 +00:00
val = __eval_expression(run,nde->incr);
2006-04-10 14:53:48 +00:00
if (val == XP_NULL) return -1;
xp_awk_refupval (val);
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, val);
2006-03-23 15:36:20 +00:00
}
}
return 0;
}
2006-04-30 17:12:51 +00:00
struct __foreach_walker_t
{
xp_awk_run_t* run;
2006-07-06 13:57:32 +00:00
xp_awk_nde_t* var;
2006-04-30 17:12:51 +00:00
xp_awk_nde_t* body;
};
static int __walk_foreach (xp_awk_pair_t* pair, void* arg)
2006-04-29 12:09:29 +00:00
{
2006-04-30 17:12:51 +00:00
struct __foreach_walker_t* w = (struct __foreach_walker_t*)arg;
xp_awk_val_t* str;
str = (xp_awk_val_t*)xp_awk_makestrval(pair->key,xp_strlen(pair->key));
if (str == XP_NULL) PANIC_I (w->run, XP_AWK_ENOMEM);
xp_awk_refupval (str);
if (__do_assignment (w->run, w->var, str) == XP_NULL)
{
xp_awk_refdownval (w->run, str);
return -1;
}
if (__run_statement (w->run, w->body) == -1)
{
xp_awk_refdownval (w->run, str);
return -1;
}
xp_awk_refdownval (w->run, str);
2006-04-29 12:09:29 +00:00
return 0;
}
2006-06-27 14:18:19 +00:00
static int __run_foreach (xp_awk_run_t* run, xp_awk_nde_foreach_t* nde)
2006-04-30 17:12:51 +00:00
{
int n;
xp_awk_nde_exp_t* test;
xp_awk_val_t* rv;
xp_awk_map_t* map;
struct __foreach_walker_t walker;
test = (xp_awk_nde_exp_t*)nde->test;
xp_assert (test->type == XP_AWK_NDE_EXP_BIN &&
test->opcode == XP_AWK_BINOP_IN);
2006-05-07 17:45:08 +00:00
/* chained expressions should not be allowed
* by the parser first of all */
xp_assert (test->right->next == XP_NULL);
2006-04-30 17:12:51 +00:00
rv = __eval_expression (run, test->right);
if (rv == XP_NULL) return -1;
xp_awk_refupval (rv);
if (rv->type != XP_AWK_VAL_MAP)
{
xp_awk_refdownval (run, rv);
PANIC_I (run, XP_AWK_ENOTINDEXABLE);
}
map = ((xp_awk_val_map_t*)rv)->map;
walker.run = run;
2006-07-06 13:57:32 +00:00
walker.var = test->left;
2006-04-30 17:12:51 +00:00
walker.body = nde->body;
n = xp_awk_map_walk (map, __walk_foreach, &walker);
xp_awk_refdownval (run, rv);
return n;
}
2006-06-27 14:18:19 +00:00
static int __run_break (xp_awk_run_t* run, xp_awk_nde_break_t* nde)
2006-03-23 15:36:20 +00:00
{
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_BREAK;
2006-03-23 15:36:20 +00:00
return 0;
}
2006-06-27 14:18:19 +00:00
static int __run_continue (xp_awk_run_t* run, xp_awk_nde_continue_t* nde)
2006-03-23 15:36:20 +00:00
{
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_CONTINUE;
2006-03-23 15:36:20 +00:00
return 0;
}
2006-06-27 14:18:19 +00:00
static int __run_return (xp_awk_run_t* run, xp_awk_nde_return_t* nde)
2006-03-23 15:36:20 +00:00
{
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-05-07 17:45:08 +00:00
/* chained expressions should not be allowed
* by the parser first of all */
xp_assert (nde->val->next == XP_NULL);
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("returning....\n"));*/
2006-08-18 17:46:07 +00:00
val = __eval_expression (run, nde->val);
2006-04-09 15:31:13 +00:00
if (val == XP_NULL) return -1;
2006-03-26 16:36:30 +00:00
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, STACK_RETVAL(run));
STACK_RETVAL(run) = val;
2006-06-20 15:27:50 +00:00
xp_awk_refupval (val); /* see __eval_call for the trick */
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("set return value....\n"));*/
2006-03-25 17:04:36 +00:00
}
2006-03-26 16:36:30 +00:00
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_FUNCTION;
2006-03-23 15:36:20 +00:00
return 0;
}
2006-06-27 14:18:19 +00:00
static int __run_exit (xp_awk_run_t* run, xp_awk_nde_exit_t* nde)
2006-03-23 15:36:20 +00:00
{
2006-03-27 10:19:33 +00:00
if (nde->val != XP_NULL)
{
xp_awk_val_t* val;
2006-05-07 17:45:08 +00:00
/* chained expressions should not be allowed
* by the parser first of all */
xp_assert (nde->val->next == XP_NULL);
2006-08-18 17:46:07 +00:00
val = __eval_expression (run, nde->val);
2006-04-10 09:22:05 +00:00
if (val == XP_NULL) return -1;
2006-03-27 10:19:33 +00:00
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, STACK_RETVAL_GLOBAL(run));
STACK_RETVAL_GLOBAL(run) = val; /* global return value */
2006-04-10 14:53:48 +00:00
2006-03-27 10:19:33 +00:00
xp_awk_refupval (val);
}
2006-04-21 17:24:31 +00:00
run->exit_level = EXIT_GLOBAL;
2006-03-23 15:36:20 +00:00
return 0;
2006-03-15 15:34:59 +00:00
}
2006-06-27 14:18:19 +00:00
static int __run_next (xp_awk_run_t* run, xp_awk_nde_next_t* nde)
2006-04-22 16:16:40 +00:00
{
2006-08-03 15:50:04 +00:00
/* the parser checks if next has been called in the begin/end
* block or whereever inappropriate. so the runtime doesn't
* check that explicitly */
if (run->active_block == (xp_awk_nde_blk_t*)run->awk->tree.begin ||
run->active_block == (xp_awk_nde_blk_t*)run->awk->tree.end)
{
PANIC_I (run, XP_AWK_ENEXTCALL);
}
2006-07-01 16:07:06 +00:00
run->exit_level = EXIT_NEXT;
return 0;
2006-04-22 16:16:40 +00:00
}
2006-06-27 14:18:19 +00:00
static int __run_nextfile (xp_awk_run_t* run, xp_awk_nde_nextfile_t* nde)
2006-04-22 16:16:40 +00:00
{
2006-07-28 10:34:22 +00:00
/* TODO: some extentions such as nextfile "in/out";
* what about awk -i in1,in2,in3 -o out1,out2,out3 ?
*/
2006-08-27 15:29:21 +00:00
int n;
2006-06-22 04:25:44 +00:00
2006-08-03 15:50:04 +00:00
if (run->active_block == (xp_awk_nde_blk_t*)run->awk->tree.begin ||
run->active_block == (xp_awk_nde_blk_t*)run->awk->tree.end)
{
PANIC_I (run, XP_AWK_ENEXTFILECALL);
}
2006-08-27 15:29:21 +00:00
n = xp_awk_nextextio_read (run, XP_AWK_IN_CONSOLE, XP_T(""));
2006-07-28 10:34:22 +00:00
if (n == -1)
{
2006-08-27 15:29:21 +00:00
if (run->errnum == XP_AWK_EIOHANDLER)
2006-08-03 09:54:16 +00:00
PANIC_I (run, XP_AWK_ECONINNEXT);
2006-08-27 15:29:21 +00:00
else return -1;
2006-07-28 10:34:22 +00:00
}
if (n == 0)
{
/* no more input file */
run->exit_level = EXIT_GLOBAL;
return 0;
}
/* TODO: update FILENAME, ARGIND. reset FNR to 1.
* some significant changes will be required to do this */
/* Consider using FILENAME_IN and FILENAME_OUT to accomplish nextfile in/out */
run->exit_level = EXIT_NEXT;
return 0;
2006-04-22 16:16:40 +00:00
}
2006-06-27 14:18:19 +00:00
static int __run_delete (xp_awk_run_t* run, xp_awk_nde_delete_t* nde)
{
2006-06-30 11:31:51 +00:00
xp_awk_nde_var_t* var;
var = (xp_awk_nde_var_t*) nde->var;
2006-07-01 07:57:10 +00:00
if (var->type == XP_AWK_NDE_NAMED ||
var->type == XP_AWK_NDE_NAMEDIDX)
2006-06-30 11:31:51 +00:00
{
xp_awk_pair_t* pair;
2006-07-01 07:57:10 +00:00
xp_assert ((var->type == XP_AWK_NDE_NAMED &&
var->idx == XP_NULL) ||
(var->type == XP_AWK_NDE_NAMEDIDX &&
var->idx != XP_NULL));
2006-08-03 05:05:48 +00:00
pair = xp_awk_map_get (
&run->named, var->id.name, var->id.name_len);
2006-06-30 16:46:34 +00:00
if (pair == XP_NULL)
2006-06-30 11:31:51 +00:00
{
2006-06-30 16:46:34 +00:00
xp_awk_val_t* tmp;
/* value not set for the named variable.
* create a map and assign it to the variable */
tmp = xp_awk_makemapval (run);
if (tmp == XP_NULL) PANIC_I (run, XP_AWK_ENOMEM);
2006-08-03 05:05:48 +00:00
if (xp_awk_map_put (&run->named,
var->id.name, var->id.name_len, tmp) == XP_NULL)
2006-06-30 16:46:34 +00:00
{
xp_awk_refupval (tmp);
xp_awk_refdownval (run, tmp);
PANIC_I (run, XP_AWK_ENOMEM);
}
xp_awk_refupval (tmp);
}
else
{
2006-07-01 07:57:10 +00:00
xp_awk_val_t* val;
xp_awk_map_t* map;
2006-06-30 16:46:34 +00:00
2006-07-01 07:57:10 +00:00
val = (xp_awk_val_t*)pair->val;
2006-06-30 16:46:34 +00:00
xp_assert (val != XP_NULL);
2006-07-01 07:57:10 +00:00
if (val->type != XP_AWK_VAL_MAP)
PANIC_I (run, XP_AWK_ENOTDELETABLE);
map = ((xp_awk_val_map_t*)val)->map;
if (var->type == XP_AWK_NDE_NAMEDIDX)
2006-06-30 11:31:51 +00:00
{
2006-07-01 07:57:10 +00:00
xp_char_t* key;
2006-08-03 05:05:48 +00:00
xp_size_t key_len;
2006-07-01 07:57:10 +00:00
xp_awk_val_t* idx;
2006-06-30 11:31:51 +00:00
2006-07-01 07:57:10 +00:00
xp_assert (var->idx != XP_NULL);
2006-06-30 17:07:52 +00:00
2006-07-01 07:57:10 +00:00
idx = __eval_expression (run, var->idx);
if (idx == XP_NULL) return -1;
2006-06-30 17:07:52 +00:00
2006-07-01 07:57:10 +00:00
xp_awk_refupval (idx);
2006-08-27 15:29:21 +00:00
key = xp_awk_valtostr (
idx, &run->errnum,
xp_true, XP_NULL, &key_len);
2006-07-01 07:57:10 +00:00
xp_awk_refdownval (run, idx);
2006-06-30 17:07:52 +00:00
2006-08-27 15:29:21 +00:00
if (key == XP_NULL) return -1;
2006-06-30 17:07:52 +00:00
2006-08-03 05:05:48 +00:00
xp_awk_map_remove (map, key, key_len);
2006-07-01 07:57:10 +00:00
xp_free (key);
2006-06-30 16:46:34 +00:00
}
else
{
2006-07-01 07:57:10 +00:00
xp_awk_map_clear (map);
2006-06-30 11:31:51 +00:00
}
}
}
2006-07-01 07:57:10 +00:00
else if (var->type == XP_AWK_NDE_GLOBAL ||
var->type == XP_AWK_NDE_LOCAL ||
var->type == XP_AWK_NDE_ARG ||
var->type == XP_AWK_NDE_GLOBALIDX ||
var->type == XP_AWK_NDE_LOCALIDX ||
var->type == XP_AWK_NDE_ARGIDX)
2006-06-30 17:07:52 +00:00
{
xp_awk_val_t* val;
2006-07-01 07:57:10 +00:00
if (var->type == XP_AWK_NDE_GLOBAL ||
var->type == XP_AWK_NDE_GLOBALIDX)
val = STACK_GLOBAL (run,var->id.idxa);
else if (var->type == XP_AWK_NDE_LOCAL ||
var->type == XP_AWK_NDE_LOCALIDX)
val = STACK_LOCAL (run,var->id.idxa);
else val = STACK_ARG (run,var->id.idxa);
2006-06-30 17:07:52 +00:00
xp_assert (val != XP_NULL);
if (val->type == XP_AWK_VAL_NIL)
{
xp_awk_val_t* tmp;
/* value not set for the named variable.
* create a map and assign it to the variable */
tmp = xp_awk_makemapval (run);
if (tmp == XP_NULL) PANIC_I (run, XP_AWK_ENOMEM);
/* no need to reduce the reference count of
* the previous value because it was nil. */
2006-07-01 07:57:10 +00:00
if (var->type == XP_AWK_NDE_GLOBAL ||
var->type == XP_AWK_NDE_GLOBALIDX)
2006-08-27 15:29:21 +00:00
{
if (xp_awk_setglobal (
run, var->id.idxa, tmp) == -1)
{
xp_awk_refupval (tmp);
xp_awk_refdownval (run, tmp);
return -1;
}
}
2006-07-01 07:57:10 +00:00
else if (var->type == XP_AWK_NDE_LOCAL ||
var->type == XP_AWK_NDE_LOCALIDX)
2006-08-27 15:29:21 +00:00
{
2006-07-01 07:57:10 +00:00
STACK_LOCAL(run,var->id.idxa) = tmp;
2006-08-27 15:29:21 +00:00
xp_awk_refupval (tmp);
}
else
{
STACK_ARG(run,var->id.idxa) = tmp;
xp_awk_refupval (tmp);
}
2006-06-30 17:07:52 +00:00
}
else
{
2006-07-01 07:57:10 +00:00
xp_awk_map_t* map;
if (val->type != XP_AWK_VAL_MAP)
PANIC_I (run, XP_AWK_ENOTDELETABLE);
map = ((xp_awk_val_map_t*)val)->map;
if (var->type == XP_AWK_NDE_GLOBALIDX ||
var->type == XP_AWK_NDE_LOCALIDX ||
var->type == XP_AWK_NDE_ARGIDX)
2006-06-30 17:07:52 +00:00
{
2006-07-01 07:57:10 +00:00
xp_char_t* key;
2006-08-03 05:05:48 +00:00
xp_size_t key_len;
2006-07-01 07:57:10 +00:00
xp_awk_val_t* idx;
xp_assert (var->idx != XP_NULL);
idx = __eval_expression (run, var->idx);
if (idx == XP_NULL) return -1;
xp_awk_refupval (idx);
2006-08-27 15:29:21 +00:00
key = xp_awk_valtostr (
idx, &run->errnum,
xp_true, XP_NULL, &key_len);
2006-07-01 07:57:10 +00:00
xp_awk_refdownval (run, idx);
2006-08-27 15:29:21 +00:00
if (key == XP_NULL) return -1;
2006-07-01 07:57:10 +00:00
2006-08-03 05:05:48 +00:00
xp_awk_map_remove (map, key, key_len);
2006-07-01 07:57:10 +00:00
xp_free (key);
2006-06-30 17:07:52 +00:00
}
else
{
2006-07-01 07:57:10 +00:00
xp_awk_map_clear (map);
2006-06-30 17:07:52 +00:00
}
}
}
2006-06-30 11:31:51 +00:00
else
{
2006-07-01 07:57:10 +00:00
xp_assert (!"should never happen - wrong variable type for delete");
2006-06-30 11:31:51 +00:00
PANIC_I (run, XP_AWK_EINTERNAL);
}
return 0;
2006-06-27 14:18:19 +00:00
}
static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde)
2006-06-13 08:35:53 +00:00
{
2006-06-22 14:15:02 +00:00
xp_awk_nde_print_t* p = (xp_awk_nde_print_t*)nde;
2006-06-26 15:09:28 +00:00
xp_char_t* out = XP_NULL;
const xp_char_t* dst;
2006-06-22 14:15:02 +00:00
xp_awk_val_t* v;
2006-06-26 15:09:28 +00:00
xp_awk_nde_t* np;
2006-06-28 03:44:40 +00:00
int errnum, n;
2006-06-26 15:09:28 +00:00
xp_assert (
2006-06-28 03:44:40 +00:00
(p->out_type == XP_AWK_OUT_PIPE && p->out != XP_NULL) ||
(p->out_type == XP_AWK_OUT_COPROC && p->out != XP_NULL) ||
(p->out_type == XP_AWK_OUT_FILE && p->out != XP_NULL) ||
2006-08-03 09:54:16 +00:00
(p->out_type == XP_AWK_OUT_FILE_APPEND && p->out != XP_NULL) ||
2006-06-28 03:44:40 +00:00
(p->out_type == XP_AWK_OUT_CONSOLE && p->out == XP_NULL));
2006-06-26 15:09:28 +00:00
if (p->out != XP_NULL)
{
2006-08-02 03:22:51 +00:00
xp_size_t len;
2006-06-26 15:09:28 +00:00
v = __eval_expression (run, p->out);
2006-06-27 10:53:04 +00:00
if (v == XP_NULL) return -1;
2006-06-22 14:15:02 +00:00
2006-06-26 15:09:28 +00:00
xp_awk_refupval (v);
2006-08-03 05:05:48 +00:00
out = xp_awk_valtostr (v, &errnum, xp_true, XP_NULL, &len);
2006-06-26 15:09:28 +00:00
if (out == XP_NULL)
2006-06-22 14:15:02 +00:00
{
2006-06-26 15:09:28 +00:00
xp_awk_refdownval (run, v);
2006-06-22 14:15:02 +00:00
PANIC_I (run, errnum);
}
2006-06-26 15:09:28 +00:00
xp_awk_refdownval (run, v);
2006-08-02 03:22:51 +00:00
2006-08-02 03:34:34 +00:00
if (len <= 0)
{
/* the output destination name is empty. */
xp_free (out);
n = -1;
goto skip_write;
}
2006-08-02 03:22:51 +00:00
while (len > 0)
{
if (out[--len] == XP_T('\0'))
{
/* the output destination name contains a null
2006-08-02 03:34:34 +00:00
* character. */
2006-08-02 03:22:51 +00:00
xp_free (out);
2006-08-22 15:11:13 +00:00
n = -1;
2006-08-02 03:22:51 +00:00
goto skip_write;
/* TODO: how to handle error???
* make print return -1??? not possible.
* throw an exception??
* set ERRNO or what??? this seems most
* reasonable. or can it have a global
* flag variable for print/printf such
* as PRINT_ERRNO? */
}
}
2006-06-26 15:09:28 +00:00
}
2006-06-22 14:15:02 +00:00
2006-06-26 15:09:28 +00:00
dst = (out == XP_NULL)? XP_T(""): out;
2006-06-22 14:15:02 +00:00
2006-06-26 15:09:28 +00:00
if (p->args == XP_NULL)
{
2006-07-30 15:53:42 +00:00
v = run->inrec.d0;
2006-06-22 14:15:02 +00:00
2006-06-26 15:09:28 +00:00
xp_awk_refupval (v);
2006-08-27 15:29:21 +00:00
n = xp_awk_writeextio (run, p->out_type, dst, v);
if (n < 0 && run->errnum != XP_AWK_EIOHANDLER)
2006-06-26 15:09:28 +00:00
{
if (out != XP_NULL) xp_free (out);
xp_awk_refdownval (run, v);
2006-08-27 15:29:21 +00:00
return -1;
2006-06-26 15:09:28 +00:00
}
xp_awk_refdownval (run, v);
2006-08-23 15:42:16 +00:00
/* TODO: how to handle n == -1 && errnum == XP_AWK_EIOHANDLER.
* that is the user handler returned an error... */
2006-06-22 14:15:02 +00:00
}
2006-06-26 15:09:28 +00:00
else
2006-06-25 15:26:57 +00:00
{
for (np = p->args; np != XP_NULL; np = np->next)
{
v = __eval_expression (run, np);
2006-06-26 15:09:28 +00:00
if (v == XP_NULL)
{
if (out != XP_NULL) xp_free (out);
return -1;
}
2006-06-25 15:26:57 +00:00
xp_awk_refupval (v);
2006-08-27 15:29:21 +00:00
n = xp_awk_writeextio (run, p->out_type, dst, v);
if (n < 0 && run->errnum != XP_AWK_EIOHANDLER)
2006-06-25 15:26:57 +00:00
{
2006-06-26 15:09:28 +00:00
if (out != XP_NULL) xp_free (out);
2006-06-25 15:26:57 +00:00
xp_awk_refdownval (run, v);
2006-08-27 15:29:21 +00:00
return -1;
2006-06-25 15:26:57 +00:00
}
2006-06-26 15:09:28 +00:00
xp_awk_refdownval (run, v);
2006-06-25 15:26:57 +00:00
2006-08-27 15:29:21 +00:00
/* TODO: how to handle n == -1 && run->errnum == XP_AWK_EIOHANDLER.
2006-08-23 15:42:16 +00:00
* that is the user handler returned an error... */
2006-06-27 10:53:04 +00:00
/* TODO: print proper field separator */
2006-06-26 15:09:28 +00:00
}
2006-07-30 15:53:42 +00:00
}
2006-06-26 15:09:28 +00:00
2006-08-03 09:54:16 +00:00
/* TODO: change xp_awk_val_nil to
* xp_awk_val_empty_string or something */
2006-08-27 15:29:21 +00:00
n = xp_awk_writeextio_nl (run, p->out_type, dst, xp_awk_val_nil);
if (n < 0 && run->errnum != XP_AWK_EIOHANDLER)
2006-07-30 15:53:42 +00:00
{
if (out != XP_NULL) xp_free (out);
2006-08-27 15:29:21 +00:00
return -1;
2006-06-22 14:15:02 +00:00
}
2006-07-30 15:53:42 +00:00
2006-08-23 15:42:16 +00:00
/* TODO: how to handle n == -1 && errnum == XP_AWK_EIOHANDLER.
* that is the user handler returned an error... */
2006-06-26 15:09:28 +00:00
if (out != XP_NULL) xp_free (out);
2006-08-02 03:22:51 +00:00
skip_write:
2006-06-26 15:09:28 +00:00
return 0;
2006-06-13 08:35:53 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_expression (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-07-31 15:59:43 +00:00
{
xp_awk_val_t* v;
int n, errnum;
v = __eval_expression0 (run, nde);
if (v == XP_NULL) return XP_NULL;
if (v->type == XP_AWK_VAL_REX)
{
xp_assert (run->inrec.d0->type == XP_AWK_VAL_STR);
xp_awk_refupval (v);
n = xp_awk_matchrex (
((xp_awk_val_rex_t*)v)->code,
((xp_awk_val_str_t*)run->inrec.d0)->buf,
((xp_awk_val_str_t*)run->inrec.d0)->len,
XP_NULL, XP_NULL, &errnum);
if (n == -1)
{
xp_awk_refdownval (run, v);
2006-08-10 16:06:52 +00:00
PANIC (run, errnum);
2006-07-31 15:59:43 +00:00
}
xp_awk_refdownval (run, v);
v = xp_awk_makeintval (run, (n != 0));
if (v == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
}
return v;
}
static xp_awk_val_t* __eval_expression0 (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-03-04 15:54:37 +00:00
{
2006-04-07 16:52:42 +00:00
static eval_expr_t __eval_func[] =
{
/* the order of functions here should match the order
* of node types declared in tree.h */
2006-04-24 15:34:52 +00:00
__eval_group,
2006-04-07 16:52:42 +00:00
__eval_assignment,
__eval_binary,
__eval_unary,
__eval_incpre,
__eval_incpst,
2006-04-11 09:16:20 +00:00
__eval_cnd,
2006-06-19 15:43:27 +00:00
__eval_bfn,
2006-06-20 15:27:50 +00:00
__eval_afn,
2006-04-07 16:52:42 +00:00
__eval_int,
__eval_real,
__eval_str,
2006-04-24 11:22:42 +00:00
__eval_rex,
2006-04-07 16:52:42 +00:00
__eval_named,
__eval_global,
__eval_local,
__eval_arg,
__eval_namedidx,
__eval_globalidx,
__eval_localidx,
__eval_argidx,
2006-06-12 15:11:02 +00:00
__eval_pos,
__eval_getline
2006-04-07 16:52:42 +00:00
};
2006-04-25 15:20:10 +00:00
xp_assert (nde->type >= XP_AWK_NDE_GRP &&
(nde->type - XP_AWK_NDE_GRP) < xp_countof(__eval_func));
2006-07-31 15:59:43 +00:00
2006-04-25 15:20:10 +00:00
return __eval_func[nde->type-XP_AWK_NDE_GRP] (run, nde);
2006-01-26 15:35:20 +00:00
}
2006-02-23 15:37:34 +00:00
2006-04-24 15:34:52 +00:00
static xp_awk_val_t* __eval_group (xp_awk_run_t* run, xp_awk_nde_t* nde)
{
2006-04-27 15:20:10 +00:00
/* __eval_binop_in evaluates the XP_AWK_NDE_GRP specially.
* so this function should never be reached. */
xp_assert (!"should never happen - NDE_GRP only for in");
2006-04-24 15:36:08 +00:00
PANIC (run, XP_AWK_EINTERNAL);
2006-04-24 15:34:52 +00:00
return XP_NULL;
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_assignment (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-02-23 15:37:34 +00:00
{
2006-07-13 03:10:35 +00:00
xp_awk_val_t* val, * ret;
2006-04-07 16:52:42 +00:00
xp_awk_nde_ass_t* ass = (xp_awk_nde_ass_t*)nde;
2006-03-05 17:07:33 +00:00
2006-04-07 16:52:42 +00:00
xp_assert (ass->left != XP_NULL && ass->right != XP_NULL);
2006-03-25 17:04:36 +00:00
2006-05-07 17:45:08 +00:00
xp_assert (ass->right->next == XP_NULL);
2006-07-31 15:59:43 +00:00
val = __eval_expression (run, ass->right);
2006-03-25 17:04:36 +00:00
if (val == XP_NULL) return XP_NULL;
2006-03-04 15:54:37 +00:00
2006-04-16 04:31:38 +00:00
xp_awk_refupval (val);
if (ass->opcode != XP_AWK_ASSOP_NONE)
{
xp_awk_val_t* val2, * tmp;
2006-05-07 17:45:08 +00:00
xp_assert (ass->left->next == XP_NULL);
2006-04-21 17:24:31 +00:00
val2 = __eval_expression (run, ass->left);
2006-04-16 04:31:38 +00:00
if (val2 == XP_NULL)
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, val);
2006-04-16 04:31:38 +00:00
return XP_NULL;
}
xp_awk_refupval (val2);
if (ass->opcode == XP_AWK_ASSOP_PLUS)
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_plus (run, val2, val);
2006-04-16 04:31:38 +00:00
}
else if (ass->opcode == XP_AWK_ASSOP_MINUS)
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_minus (run, val2, val);
2006-04-16 04:31:38 +00:00
}
else if (ass->opcode == XP_AWK_ASSOP_MUL)
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_mul (run, val2, val);
2006-04-16 04:31:38 +00:00
}
else if (ass->opcode == XP_AWK_ASSOP_DIV)
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_div (run, val2, val);
2006-04-16 04:31:38 +00:00
}
else if (ass->opcode == XP_AWK_ASSOP_MOD)
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_mod (run, val2, val);
2006-04-16 04:31:38 +00:00
}
else if (ass->opcode == XP_AWK_ASSOP_EXP)
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_exp (run, val2, val);
2006-04-16 04:31:38 +00:00
}
else
{
xp_assert (!"should never happen - invalid assignment opcode");
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EINTERNAL);
2006-04-16 04:31:38 +00:00
}
if (tmp == XP_NULL)
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, val);
xp_awk_refdownval (run, val2);
2006-04-16 04:31:38 +00:00
return XP_NULL;
}
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, val);
2006-04-16 04:31:38 +00:00
val = tmp;
xp_awk_refupval (val);
}
2006-07-13 03:10:35 +00:00
ret = __do_assignment (run, ass->left, val);
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, val);
2006-04-16 04:31:38 +00:00
2006-07-13 03:10:35 +00:00
return ret;
2006-04-02 16:22:36 +00:00
}
static xp_awk_val_t* __do_assignment (
2006-07-06 13:57:32 +00:00
xp_awk_run_t* run, xp_awk_nde_t* var, xp_awk_val_t* val)
2006-04-02 16:22:36 +00:00
{
2006-07-13 03:10:35 +00:00
xp_awk_val_t* ret;
2006-04-16 16:30:59 +00:00
if (val->type == XP_AWK_VAL_MAP)
{
/* a map cannot be assigned to a variable */
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_ENOTASSIGNABLE);
2006-04-16 16:30:59 +00:00
}
2006-07-06 13:57:32 +00:00
if (var->type == XP_AWK_NDE_NAMED ||
var->type == XP_AWK_NDE_GLOBAL ||
var->type == XP_AWK_NDE_LOCAL ||
var->type == XP_AWK_NDE_ARG)
{
2006-07-13 03:10:35 +00:00
ret = __do_assignment_scalar (run, (xp_awk_nde_var_t*)var, val);
2006-07-06 13:57:32 +00:00
}
else if (var->type == XP_AWK_NDE_NAMEDIDX ||
var->type == XP_AWK_NDE_GLOBALIDX ||
var->type == XP_AWK_NDE_LOCALIDX ||
var->type == XP_AWK_NDE_ARGIDX)
{
2006-07-13 03:10:35 +00:00
ret = __do_assignment_map (run, (xp_awk_nde_var_t*)var, val);
2006-07-06 13:57:32 +00:00
}
else if (var->type == XP_AWK_NDE_POS)
{
2006-07-13 03:10:35 +00:00
ret = __do_assignment_pos (run, (xp_awk_nde_pos_t*)var, val);
2006-07-06 13:57:32 +00:00
}
else
{
xp_assert (!"should never happen - invalid variable type");
PANIC (run, XP_AWK_EINTERNAL);
}
2006-07-13 03:10:35 +00:00
return ret;
2006-07-06 13:57:32 +00:00
}
static xp_awk_val_t* __do_assignment_scalar (
xp_awk_run_t* run, xp_awk_nde_var_t* var, xp_awk_val_t* val)
{
xp_assert (
(var->type == XP_AWK_NDE_NAMED ||
var->type == XP_AWK_NDE_GLOBAL ||
var->type == XP_AWK_NDE_LOCAL ||
var->type == XP_AWK_NDE_ARG) && var->idx == XP_NULL);
xp_assert (val->type != XP_AWK_VAL_MAP);
2006-04-02 16:22:36 +00:00
if (var->type == XP_AWK_NDE_NAMED)
2006-03-03 11:45:45 +00:00
{
2006-07-01 16:07:06 +00:00
xp_awk_pair_t* pair;
2006-04-19 03:42:08 +00:00
int n;
2006-02-23 15:37:34 +00:00
2006-08-03 05:05:48 +00:00
pair = xp_awk_map_get (
&run->named, var->id.name, var->id.name_len);
2006-07-01 16:07:06 +00:00
if (pair != XP_NULL &&
((xp_awk_val_t*)pair->val)->type == XP_AWK_VAL_MAP)
{
/* once a variable becomes an array,
* it cannot be changed to a scalar variable */
2006-08-20 15:49:48 +00:00
PANIC (run, XP_AWK_EMAPTOSCALAR);
2006-07-01 16:07:06 +00:00
}
2006-08-03 05:05:48 +00:00
n = xp_awk_map_putx (&run->named,
var->id.name, var->id.name_len, val, XP_NULL);
2006-04-21 17:24:31 +00:00
if (n < 0) PANIC (run, XP_AWK_ENOMEM);
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-04-02 16:22:36 +00:00
else if (var->type == XP_AWK_NDE_GLOBAL)
2006-03-03 11:45:45 +00:00
{
2006-08-27 10:45:36 +00:00
if (xp_awk_setglobal (
run, var->id.idxa, val) == -1) return XP_NULL;
2006-02-23 15:37:34 +00:00
}
2006-04-02 16:22:36 +00:00
else if (var->type == XP_AWK_NDE_LOCAL)
2006-03-03 11:45:45 +00:00
{
2006-07-01 16:07:06 +00:00
xp_awk_val_t* old = STACK_LOCAL(run,var->id.idxa);
if (old->type == XP_AWK_VAL_MAP)
{
/* once the variable becomes an array,
* it cannot be changed to a scalar variable */
2006-08-20 15:49:48 +00:00
PANIC (run, XP_AWK_EMAPTOSCALAR);
2006-07-01 16:07:06 +00:00
}
xp_awk_refdownval (run, old);
2006-04-21 17:24:31 +00:00
STACK_LOCAL(run,var->id.idxa) = val;
2006-03-26 14:03:08 +00:00
xp_awk_refupval (val);
2006-02-23 15:37:34 +00:00
}
2006-07-06 13:57:32 +00:00
else /* if (var->type == XP_AWK_NDE_ARG) */
2006-03-03 11:45:45 +00:00
{
2006-07-01 16:07:06 +00:00
xp_awk_val_t* old = STACK_ARG(run,var->id.idxa);
if (old->type == XP_AWK_VAL_MAP)
{
/* once the variable becomes an array,
* it cannot be changed to a scalar variable */
2006-08-20 15:49:48 +00:00
PANIC (run, XP_AWK_EMAPTOSCALAR);
2006-07-01 16:07:06 +00:00
}
xp_awk_refdownval (run, old);
2006-04-21 17:24:31 +00:00
STACK_ARG(run,var->id.idxa) = val;
2006-03-25 17:04:36 +00:00
xp_awk_refupval (val);
2006-02-23 15:37:34 +00:00
}
2006-04-16 16:30:59 +00:00
2006-04-17 16:12:02 +00:00
return val;
}
2006-04-16 16:30:59 +00:00
2006-04-18 11:26:48 +00:00
static xp_awk_val_t* __do_assignment_map (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_nde_var_t* var, xp_awk_val_t* val)
2006-04-17 16:12:02 +00:00
{
xp_awk_val_map_t* map;
2006-05-03 15:40:19 +00:00
xp_char_t* str;
2006-08-03 05:05:48 +00:00
xp_size_t len;
2006-04-18 14:49:42 +00:00
int n;
2006-04-16 16:30:59 +00:00
2006-04-18 11:26:48 +00:00
xp_assert (
2006-04-19 16:09:51 +00:00
(var->type == XP_AWK_NDE_NAMEDIDX ||
var->type == XP_AWK_NDE_GLOBALIDX ||
2006-04-18 11:26:48 +00:00
var->type == XP_AWK_NDE_LOCALIDX ||
var->type == XP_AWK_NDE_ARGIDX) && var->idx != XP_NULL);
2006-07-06 13:57:32 +00:00
xp_assert (val->type != XP_AWK_VAL_MAP);
2006-04-18 11:26:48 +00:00
2006-04-19 16:09:51 +00:00
if (var->type == XP_AWK_NDE_NAMEDIDX)
{
xp_awk_pair_t* pair;
2006-08-03 05:05:48 +00:00
pair = xp_awk_map_get (
&run->named, var->id.name, var->id.name_len);
2006-04-27 15:20:10 +00:00
map = (pair == XP_NULL)?
(xp_awk_val_map_t*)xp_awk_val_nil:
(xp_awk_val_map_t*)pair->val;
2006-04-19 16:09:51 +00:00
}
else
{
map = (var->type == XP_AWK_NDE_GLOBALIDX)?
2006-04-21 17:24:31 +00:00
(xp_awk_val_map_t*)STACK_GLOBAL(run,var->id.idxa):
2006-04-19 16:09:51 +00:00
(var->type == XP_AWK_NDE_LOCALIDX)?
2006-04-21 17:24:31 +00:00
(xp_awk_val_map_t*)STACK_LOCAL(run,var->id.idxa):
(xp_awk_val_map_t*)STACK_ARG(run,var->id.idxa);
2006-04-19 16:09:51 +00:00
}
2006-04-16 16:30:59 +00:00
2006-04-17 16:12:02 +00:00
if (map->type == XP_AWK_VAL_NIL)
{
/* the map is not initialized yet */
xp_awk_val_t* tmp;
2006-04-16 16:30:59 +00:00
2006-04-21 17:24:31 +00:00
tmp = xp_awk_makemapval (run);
if (tmp == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-17 16:12:02 +00:00
2006-04-19 16:09:51 +00:00
if (var->type == XP_AWK_NDE_NAMEDIDX)
{
/* doesn't have to decrease the reference count
* of the previous value here as it is done by
* xp_awk_map_put */
2006-08-03 05:05:48 +00:00
if (xp_awk_map_put (&run->named,
var->id.name, var->id.name_len, tmp) == XP_NULL)
2006-04-19 16:09:51 +00:00
{
xp_awk_refupval (tmp);
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, tmp);
PANIC (run, XP_AWK_ENOMEM);
2006-04-19 16:09:51 +00:00
}
2006-08-27 15:29:21 +00:00
xp_awk_refupval (tmp);
2006-04-19 16:09:51 +00:00
}
else if (var->type == XP_AWK_NDE_GLOBALIDX)
{
/* decrease the reference count of the previous value.
* in fact, this is not necessary as map is always
* xp_awk_val_nil here. */
2006-08-27 15:29:21 +00:00
/*
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, (xp_awk_val_t*)map);
STACK_GLOBAL(run,var->id.idxa) = tmp;
2006-08-27 15:29:21 +00:00
xp_awk_refupval (tmp);
*/
if (xp_awk_setglobal (run, var->id.idxa, tmp) == -1)
{
xp_awk_refupval (tmp);
xp_awk_refdownval (run, tmp);
return XP_NULL;
}
2006-04-19 16:09:51 +00:00
}
2006-04-18 11:26:48 +00:00
else if (var->type == XP_AWK_NDE_LOCALIDX)
2006-04-19 16:09:51 +00:00
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, (xp_awk_val_t*)map);
STACK_LOCAL(run,var->id.idxa) = tmp;
2006-08-27 15:29:21 +00:00
xp_awk_refupval (tmp);
2006-04-19 16:09:51 +00:00
}
2006-07-06 13:57:32 +00:00
else /* if (var->type == XP_AWK_NDE_ARGIDX) */
2006-04-19 16:09:51 +00:00
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, (xp_awk_val_t*)map);
STACK_ARG(run,var->id.idxa) = tmp;
2006-08-27 15:29:21 +00:00
xp_awk_refupval (tmp);
2006-04-19 16:09:51 +00:00
}
2006-04-18 11:26:48 +00:00
2006-04-17 16:12:02 +00:00
map = (xp_awk_val_map_t*) tmp;
}
else if (map->type != XP_AWK_VAL_MAP)
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_ENOTINDEXABLE);
2006-04-17 16:12:02 +00:00
}
2006-08-03 05:05:48 +00:00
str = __idxnde_to_str (run, var->idx, &len);
2006-05-03 15:40:19 +00:00
if (str == XP_NULL) return XP_NULL;
2006-04-17 16:12:02 +00:00
2006-04-18 10:28:03 +00:00
/*
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("**** index str=>%s, map->ref=%d, map->type=%d\n"), str, map->ref, map->type);
2006-04-18 10:28:03 +00:00
*/
2006-08-03 05:05:48 +00:00
n = xp_awk_map_putx (map->map, str, len, val, XP_NULL);
2006-04-18 14:49:42 +00:00
if (n < 0)
2006-03-03 11:45:45 +00:00
{
2006-05-03 15:40:19 +00:00
xp_free (str);
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_ENOMEM);
2006-02-23 15:37:34 +00:00
}
2006-04-17 16:12:02 +00:00
2006-05-03 15:40:19 +00:00
xp_free (str);
2006-04-17 16:12:02 +00:00
xp_awk_refupval (val);
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
2006-07-06 13:57:32 +00:00
static xp_awk_val_t* __do_assignment_pos (
xp_awk_run_t* run, xp_awk_nde_pos_t* pos, xp_awk_val_t* val)
{
xp_awk_val_t* v;
xp_long_t lv;
xp_real_t rv;
2006-07-06 15:54:41 +00:00
xp_char_t* str;
2006-07-13 03:10:35 +00:00
xp_size_t len;
2006-07-06 13:57:32 +00:00
int n, errnum;
2006-07-06 15:54:41 +00:00
/* get the position number */
2006-07-06 13:57:32 +00:00
v = __eval_expression (run, pos->val);
if (v == XP_NULL) return XP_NULL;
xp_awk_refupval (v);
2006-07-17 04:17:40 +00:00
n = xp_awk_valtonum (v, &lv, &rv);
2006-07-06 13:57:32 +00:00
xp_awk_refdownval (run, v);
2006-08-13 16:05:04 +00:00
if (n == -1) PANIC (run, XP_AWK_EPOSIDX);
2006-07-06 13:57:32 +00:00
if (n == 1) lv = (xp_long_t)rv;
2006-07-09 16:06:05 +00:00
if (lv < 0) PANIC (run, XP_AWK_EPOSIDX);
2006-07-06 13:57:32 +00:00
2006-07-06 15:54:41 +00:00
/* convert the value to the string */
2006-08-03 05:05:48 +00:00
str = xp_awk_valtostr (val, &errnum, xp_true, XP_NULL, &len);
2006-07-06 15:54:41 +00:00
if (str == XP_NULL) PANIC (run, errnum);
2006-07-06 13:57:32 +00:00
if (lv == 0)
{
2006-08-27 15:29:21 +00:00
if (__clear_record (run, xp_false) == -1)
{
xp_free (str);
return XP_NULL;
}
2006-07-07 09:48:23 +00:00
2006-07-13 03:10:35 +00:00
if (xp_str_ncpy (&run->inrec.line, str, len) == (xp_size_t)-1)
2006-07-06 13:57:32 +00:00
{
2006-07-06 15:54:41 +00:00
xp_free (str);
2006-08-27 15:29:21 +00:00
PANIC (run, XP_AWK_ENOMEM);
2006-07-06 13:57:32 +00:00
}
2006-07-06 15:54:41 +00:00
xp_free (str);
2006-07-06 13:57:32 +00:00
2006-07-25 16:41:40 +00:00
if (val->type == XP_AWK_VAL_STR)
{
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = val;
xp_awk_refupval (val);
}
else
{
v = xp_awk_makestrval (
XP_STR_BUF(&run->inrec.line),
XP_STR_LEN(&run->inrec.line));
if (v == XP_NULL)
{
__clear_record (run, xp_false);
PANIC (run, XP_AWK_ENOMEM);
}
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = v;
xp_awk_refupval (v);
val = v;
}
2006-07-09 16:06:05 +00:00
if (__split_record (run) == -1)
{
2006-08-27 15:29:21 +00:00
errnum = run->errnum;
2006-07-09 16:06:05 +00:00
__clear_record (run, xp_false);
2006-08-27 15:29:21 +00:00
run->errnum = errnum;
2006-07-09 16:06:05 +00:00
return XP_NULL;
}
2006-07-06 13:57:32 +00:00
}
else
{
2006-08-27 15:29:21 +00:00
if (__recomp_record_fields (run, (xp_size_t)lv, str, len) == -1)
2006-07-09 16:06:05 +00:00
{
2006-08-27 15:29:21 +00:00
errnum = run->errnum;
2006-07-10 04:51:38 +00:00
xp_free (str);
__clear_record (run, xp_false);
2006-08-27 15:29:21 +00:00
run->errnum = errnum;
return XP_NULL;
2006-07-09 16:06:05 +00:00
}
2006-07-10 04:51:38 +00:00
xp_free (str);
2006-07-09 16:06:05 +00:00
2006-07-10 04:51:38 +00:00
/* recompose $0 */
v = xp_awk_makestrval (
XP_STR_BUF(&run->inrec.line),
XP_STR_LEN(&run->inrec.line));
if (v == XP_NULL)
2006-07-06 15:54:41 +00:00
{
2006-07-09 16:06:05 +00:00
__clear_record (run, xp_false);
2006-07-06 15:54:41 +00:00
PANIC (run, XP_AWK_ENOMEM);
}
2006-07-10 04:51:38 +00:00
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = v;
xp_awk_refupval (v);
2006-07-13 03:10:35 +00:00
val = v;
2006-07-10 04:51:38 +00:00
}
return val;
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_binary (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-03-07 15:55:14 +00:00
{
2006-04-03 14:55:34 +00:00
static binop_func_t __binop_func[] =
{
2006-04-29 12:09:29 +00:00
/* the order of the functions should be inline with
* the operator declaration in run.h */
XP_NULL, /* __eval_binop_lor */
XP_NULL, /* __eval_binop_land */
XP_NULL, /* __eval_binop_in */
2006-04-03 14:55:34 +00:00
__eval_binop_bor,
__eval_binop_bxor,
__eval_binop_band,
__eval_binop_eq,
__eval_binop_ne,
__eval_binop_gt,
__eval_binop_ge,
__eval_binop_lt,
__eval_binop_le,
__eval_binop_lshift,
__eval_binop_rshift,
__eval_binop_plus,
__eval_binop_minus,
__eval_binop_mul,
__eval_binop_div,
2006-04-11 15:44:30 +00:00
__eval_binop_mod,
2006-04-12 03:54:12 +00:00
__eval_binop_exp,
2006-06-29 15:40:30 +00:00
__eval_binop_concat,
2006-07-31 15:59:43 +00:00
XP_NULL, /* __eval_binop_ma */
XP_NULL /* __eval_binop_nm */
2006-04-03 14:55:34 +00:00
};
2006-04-27 15:20:10 +00:00
2006-04-07 16:52:42 +00:00
xp_awk_nde_exp_t* exp = (xp_awk_nde_exp_t*)nde;
xp_awk_val_t* left, * right, * res;
2006-03-07 15:55:14 +00:00
2006-04-07 16:52:42 +00:00
xp_assert (exp->type == XP_AWK_NDE_EXP_BIN);
2006-03-07 15:55:14 +00:00
2006-04-29 12:09:29 +00:00
if (exp->opcode == XP_AWK_BINOP_LAND)
{
res = __eval_binop_land (run, exp->left, exp->right);
}
else if (exp->opcode == XP_AWK_BINOP_LOR)
{
res = __eval_binop_lor (run, exp->left, exp->right);
}
else if (exp->opcode == XP_AWK_BINOP_IN)
2006-03-07 15:55:14 +00:00
{
2006-04-27 15:20:10 +00:00
/* treat the in operator specially */
res = __eval_binop_in (run, exp->left, exp->right);
2006-03-07 15:55:14 +00:00
}
2006-07-31 15:59:43 +00:00
else if (exp->opcode == XP_AWK_BINOP_NM)
{
res = __eval_binop_nm (run, exp->left, exp->right);
}
else if (exp->opcode == XP_AWK_BINOP_MA)
{
res = __eval_binop_ma (run, exp->left, exp->right);
}
2006-04-27 15:20:10 +00:00
else
{
2006-05-07 17:45:08 +00:00
xp_assert (exp->left->next == XP_NULL);
2006-04-27 15:20:10 +00:00
left = __eval_expression (run, exp->left);
if (left == XP_NULL) return XP_NULL;
2006-03-07 15:55:14 +00:00
2006-04-27 15:20:10 +00:00
xp_awk_refupval (left);
2006-03-23 15:36:20 +00:00
2006-05-07 17:45:08 +00:00
xp_assert (exp->right->next == XP_NULL);
2006-04-27 15:20:10 +00:00
right = __eval_expression (run, exp->right);
if (right == XP_NULL)
{
xp_awk_refdownval (run, left);
return XP_NULL;
}
2006-04-03 15:31:33 +00:00
2006-04-27 15:20:10 +00:00
xp_awk_refupval (right);
2006-03-22 16:05:50 +00:00
2006-04-27 15:20:10 +00:00
xp_assert (exp->opcode >= 0 &&
exp->opcode < xp_countof(__binop_func));
xp_assert (__binop_func[exp->opcode] != XP_NULL);
res = __binop_func[exp->opcode] (run, left, right);
xp_awk_refdownval (run, left);
xp_awk_refdownval (run, right);
}
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_lor (
2006-04-29 12:09:29 +00:00
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-29 12:09:29 +00:00
/*
2006-04-03 14:55:34 +00:00
xp_awk_val_t* res = XP_NULL;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run,
2006-06-21 11:45:26 +00:00
xp_awk_valtobool(left) || xp_awk_valtobool(right));
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
2006-04-29 12:09:29 +00:00
*/
/* short-circuit evaluation required special treatment */
xp_awk_val_t* lv, * rv, * res;
2006-05-07 17:45:08 +00:00
xp_assert (left->next == XP_NULL);
2006-04-29 12:09:29 +00:00
lv = __eval_expression (run, left);
if (lv == XP_NULL) return XP_NULL;
xp_awk_refupval (lv);
2006-06-21 11:45:26 +00:00
if (xp_awk_valtobool(lv))
2006-04-29 12:09:29 +00:00
{
res = xp_awk_makeintval (run, 1);
}
else
{
2006-05-07 17:45:08 +00:00
xp_assert (right->next == XP_NULL);
2006-04-29 12:09:29 +00:00
rv = __eval_expression (run, right);
if (rv == XP_NULL)
{
xp_awk_refdownval (run, lv);
return XP_NULL;
}
xp_awk_refupval (rv);
2006-06-21 11:45:26 +00:00
res = xp_awk_makeintval (run, (xp_awk_valtobool(rv)? 1: 0));
2006-04-29 12:09:29 +00:00
xp_awk_refdownval (run, rv);
}
xp_awk_refdownval (run, lv);
2006-06-28 14:19:01 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-29 12:09:29 +00:00
return res;
2006-04-03 14:55:34 +00:00
}
static xp_awk_val_t* __eval_binop_land (
2006-04-29 12:09:29 +00:00
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-29 12:09:29 +00:00
/*
2006-04-03 14:55:34 +00:00
xp_awk_val_t* res = XP_NULL;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run,
2006-06-21 11:45:26 +00:00
xp_awk_valtobool(left) && xp_awk_valtobool(right));
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
2006-04-29 12:09:29 +00:00
*/
/* short-circuit evaluation required special treatment */
xp_awk_val_t* lv, * rv, * res;
2006-05-07 17:45:08 +00:00
xp_assert (left->next == XP_NULL);
2006-04-29 12:09:29 +00:00
lv = __eval_expression (run, left);
if (lv == XP_NULL) return XP_NULL;
xp_awk_refupval (lv);
2006-06-21 11:45:26 +00:00
if (!xp_awk_valtobool(lv))
2006-04-29 12:09:29 +00:00
{
res = xp_awk_makeintval (run, 0);
}
else
{
2006-05-07 17:45:08 +00:00
xp_assert (right->next == XP_NULL);
2006-04-29 12:09:29 +00:00
rv = __eval_expression (run, right);
if (rv == XP_NULL)
{
xp_awk_refdownval (run, lv);
return XP_NULL;
}
xp_awk_refupval (rv);
2006-06-21 11:45:26 +00:00
res = xp_awk_makeintval (run, (xp_awk_valtobool(rv)? 1: 0));
2006-04-29 12:09:29 +00:00
xp_awk_refdownval (run, rv);
}
xp_awk_refdownval (run, lv);
2006-06-28 14:19:01 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-29 12:09:29 +00:00
return res;
}
2006-06-13 15:11:39 +00:00
2006-04-29 12:09:29 +00:00
static xp_awk_val_t* __eval_binop_in (
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right)
{
2006-05-03 15:54:20 +00:00
xp_awk_val_t* rv, * res;
xp_char_t* str;
2006-08-03 05:05:48 +00:00
xp_size_t len;
2006-04-29 12:09:29 +00:00
if (right->type != XP_AWK_NDE_GLOBAL &&
right->type != XP_AWK_NDE_LOCAL &&
right->type != XP_AWK_NDE_ARG &&
right->type != XP_AWK_NDE_NAMED)
{
/* the compiler should have handled this case */
xp_assert (!"should never happen - in needs a plain variable");
PANIC (run, XP_AWK_EINTERNAL);
return XP_NULL;
}
/* evaluate the left-hand side of the operator */
2006-05-03 15:54:20 +00:00
str = (left->type == XP_AWK_NDE_GRP)?
2006-08-03 05:05:48 +00:00
__idxnde_to_str (run, ((xp_awk_nde_grp_t*)left)->body, &len):
__idxnde_to_str (run, left, &len);
2006-05-03 15:54:20 +00:00
if (str == XP_NULL) return XP_NULL;
2006-04-29 12:09:29 +00:00
/* evaluate the right-hand side of the operator */
2006-05-07 17:45:08 +00:00
xp_assert (right->next == XP_NULL);
2006-04-29 12:09:29 +00:00
rv = __eval_expression (run, right);
if (rv == XP_NULL)
{
2006-05-03 15:54:20 +00:00
xp_free (str);
2006-04-29 12:09:29 +00:00
return XP_NULL;
}
xp_awk_refupval (rv);
if (rv->type == XP_AWK_VAL_NIL)
{
res = xp_awk_makeintval (run, 0);
if (res == XP_NULL)
{
2006-05-03 15:54:20 +00:00
xp_free (str);
2006-04-29 12:09:29 +00:00
xp_awk_refdownval (run, rv);
PANIC (run, XP_AWK_ENOMEM);
}
2006-05-03 15:54:20 +00:00
xp_free (str);
2006-04-29 12:09:29 +00:00
xp_awk_refdownval (run, rv);
return res;
}
else if (rv->type == XP_AWK_VAL_MAP)
{
xp_long_t r;
2006-08-03 05:05:48 +00:00
r = (xp_long_t)(xp_awk_map_get(((xp_awk_val_map_t*)rv)->map,str,len) != XP_NULL);
2006-04-29 12:09:29 +00:00
res = xp_awk_makeintval (run, r);
if (res == XP_NULL)
{
xp_free (str);
xp_awk_refdownval (run, rv);
PANIC (run, XP_AWK_ENOMEM);
}
xp_free (str);
xp_awk_refdownval (run, rv);
return res;
}
/* need an array */
2006-08-03 05:05:48 +00:00
/* TODO: change the error code to make it clearer */
PANIC (run, XP_AWK_EOPERAND);
2006-04-29 12:09:29 +00:00
return XP_NULL;
2006-04-03 14:55:34 +00:00
}
static xp_awk_val_t* __eval_binop_bor (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
xp_awk_val_t* res = XP_NULL;
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
2006-03-27 14:59:57 +00:00
{
2006-04-03 14:55:34 +00:00
xp_long_t r =
((xp_awk_val_int_t*)left)->val |
((xp_awk_val_int_t*)right)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
2006-04-07 04:23:11 +00:00
}
else
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
}
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_bxor (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
xp_awk_val_t* res = XP_NULL;
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-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
2006-04-07 04:23:11 +00:00
}
else
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-03-27 14:59:57 +00:00
}
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_band (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
xp_awk_val_t* res = XP_NULL;
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
2006-03-27 14:59:57 +00:00
{
2006-04-03 14:55:34 +00:00
xp_long_t r =
((xp_awk_val_int_t*)left)->val &
((xp_awk_val_int_t*)right)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
2006-04-07 04:23:11 +00:00
}
else
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-03-27 14:59:57 +00:00
}
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_eq (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
xp_awk_val_t* res = XP_NULL;
2006-04-06 16:25:37 +00:00
xp_long_t r = 0;
2006-04-03 14:55:34 +00:00
2006-04-09 16:26:36 +00:00
if (left->type == XP_AWK_VAL_NIL ||
right->type == XP_AWK_VAL_NIL)
{
r = (left->type == right->type);
}
else if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
2006-03-27 14:59:57 +00:00
{
2006-04-06 16:25:37 +00:00
r = ((xp_awk_val_int_t*)left)->val ==
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_real_t*)left)->val ==
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_int_t*)left)->val ==
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_INT)
{
r = ((xp_awk_val_real_t*)left)->val ==
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_STR &&
right->type == XP_AWK_VAL_STR)
{
r = xp_strxncmp (
((xp_awk_val_str_t*)left)->buf,
((xp_awk_val_str_t*)left)->len,
((xp_awk_val_str_t*)right)->buf,
((xp_awk_val_str_t*)right)->len) == 0;
}
else
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-03-27 14:59:57 +00:00
}
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-06-28 14:19:01 +00:00
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_ne (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
xp_awk_val_t* res = XP_NULL;
2006-04-06 16:25:37 +00:00
xp_long_t r = 0;
2006-04-03 14:55:34 +00:00
2006-04-09 16:26:36 +00:00
if (left->type == XP_AWK_VAL_NIL ||
right->type == XP_AWK_VAL_NIL)
{
r = (left->type != right->type);
}
else if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
2006-03-30 16:31:50 +00:00
{
2006-04-06 16:25:37 +00:00
r = ((xp_awk_val_int_t*)left)->val !=
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_real_t*)left)->val !=
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_int_t*)left)->val !=
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_INT)
{
r = ((xp_awk_val_real_t*)left)->val !=
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_STR &&
right->type == XP_AWK_VAL_STR)
{
r = xp_strxncmp (
((xp_awk_val_str_t*)left)->buf,
((xp_awk_val_str_t*)left)->len,
((xp_awk_val_str_t*)right)->buf,
((xp_awk_val_str_t*)right)->len) != 0;
}
else
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-03-30 16:31:50 +00:00
}
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_gt (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
xp_awk_val_t* res = XP_NULL;
2006-04-06 16:25:37 +00:00
xp_long_t r = 0;
2006-04-03 14:55:34 +00:00
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
2006-04-06 16:25:37 +00:00
r = ((xp_awk_val_int_t*)left)->val >
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_real_t*)left)->val >
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_int_t*)left)->val >
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_INT)
{
r = ((xp_awk_val_real_t*)left)->val >
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_STR &&
right->type == XP_AWK_VAL_STR)
{
r = xp_strxncmp (
((xp_awk_val_str_t*)left)->buf,
((xp_awk_val_str_t*)left)->len,
((xp_awk_val_str_t*)right)->buf,
((xp_awk_val_str_t*)right)->len) > 0;
}
else
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
}
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_ge (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
xp_awk_val_t* res = XP_NULL;
2006-04-06 16:25:37 +00:00
xp_long_t r = 0;
2006-04-03 14:55:34 +00:00
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
2006-04-06 16:25:37 +00:00
r = ((xp_awk_val_int_t*)left)->val >=
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_real_t*)left)->val >=
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_int_t*)left)->val >=
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_INT)
{
r = ((xp_awk_val_real_t*)left)->val >=
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_STR &&
right->type == XP_AWK_VAL_STR)
{
r = xp_strxncmp (
((xp_awk_val_str_t*)left)->buf,
((xp_awk_val_str_t*)left)->len,
((xp_awk_val_str_t*)right)->buf,
((xp_awk_val_str_t*)right)->len) >= 0;
}
else
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
}
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_lt (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
xp_awk_val_t* res = XP_NULL;
2006-04-06 16:25:37 +00:00
xp_long_t r = 0;
2006-04-03 14:55:34 +00:00
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
2006-04-06 16:25:37 +00:00
r = ((xp_awk_val_int_t*)left)->val <
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_real_t*)left)->val <
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_int_t*)left)->val <
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_INT)
{
r = ((xp_awk_val_real_t*)left)->val <
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_STR &&
right->type == XP_AWK_VAL_STR)
{
r = xp_strxncmp (
((xp_awk_val_str_t*)left)->buf,
((xp_awk_val_str_t*)left)->len,
((xp_awk_val_str_t*)right)->buf,
((xp_awk_val_str_t*)right)->len) < 0;
}
else
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
}
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_le (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
xp_awk_val_t* res = XP_NULL;
2006-04-06 16:25:37 +00:00
xp_long_t r = 0;
2006-04-03 14:55:34 +00:00
if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_INT)
{
2006-04-06 16:25:37 +00:00
r = ((xp_awk_val_int_t*)left)->val <=
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_real_t*)left)->val <=
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_INT &&
right->type == XP_AWK_VAL_REAL)
{
r = ((xp_awk_val_int_t*)left)->val <=
((xp_awk_val_real_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_REAL &&
right->type == XP_AWK_VAL_INT)
{
r = ((xp_awk_val_real_t*)left)->val <=
((xp_awk_val_int_t*)right)->val;
}
else if (left->type == XP_AWK_VAL_STR &&
right->type == XP_AWK_VAL_STR)
{
r = xp_strxncmp (
((xp_awk_val_str_t*)left)->buf,
((xp_awk_val_str_t*)left)->len,
((xp_awk_val_str_t*)right)->buf,
((xp_awk_val_str_t*)right)->len) <= 0;
}
else
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
}
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_lshift (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 13:30:19 +00:00
int n1, n2, n3;
xp_long_t l1, l2;
xp_real_t r1, r2;
xp_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-07-17 04:17:40 +00:00
n1 = xp_awk_valtonum (left, &l1, &r1);
n2 = xp_awk_valtonum (right, &l2, &r2);
2006-04-16 13:30:19 +00:00
2006-08-13 16:05:04 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, XP_AWK_EOPERAND);
2006-04-16 13:30:19 +00:00
n3 = n1 + (n2 << 1);
if (n3 == 0)
2006-04-06 16:25:37 +00:00
{
2006-04-21 17:24:31 +00:00
if (l2 == 0) PANIC (run, XP_AWK_EDIVBYZERO);
res = xp_awk_makeintval (run, (xp_long_t)l1 << (xp_long_t)l2);
2006-04-03 14:55:34 +00:00
}
2006-04-21 17:24:31 +00:00
else PANIC (run, XP_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_rshift (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 13:30:19 +00:00
int n1, n2, n3;
xp_long_t l1, l2;
xp_real_t r1, r2;
xp_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-07-17 04:17:40 +00:00
n1 = xp_awk_valtonum (left, &l1, &r1);
n2 = xp_awk_valtonum (right, &l2, &r2);
2006-04-16 13:30:19 +00:00
2006-08-13 16:05:04 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, XP_AWK_EOPERAND);
2006-04-16 13:30:19 +00:00
n3 = n1 + (n2 << 1);
if (n3 == 0)
2006-04-06 16:25:37 +00:00
{
2006-04-21 17:24:31 +00:00
if (l2 == 0) PANIC (run, XP_AWK_EDIVBYZERO);
res = xp_awk_makeintval (run, (xp_long_t)l1 >> (xp_long_t)l2);
2006-04-03 14:55:34 +00:00
}
2006-04-21 17:24:31 +00:00
else PANIC (run, XP_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_plus (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
xp_long_t l1, l2;
xp_real_t r1, r2;
xp_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-07-17 04:17:40 +00:00
n1 = xp_awk_valtonum (left, &l1, &r1);
n2 = xp_awk_valtonum (right, &l2, &r2);
2006-04-16 06:16:42 +00:00
2006-08-13 16:05:04 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, XP_AWK_EOPERAND);
2006-04-16 06:16:42 +00:00
/*
n1 n2 n3
0 0 = 0
1 0 = 1
0 1 = 2
1 1 = 3
*/
n3 = n1 + (n2 << 1);
2006-04-16 13:30:19 +00:00
xp_assert (n3 >= 0 && n3 <= 3);
2006-04-21 17:24:31 +00:00
res = (n3 == 0)? xp_awk_makeintval(run,(xp_long_t)l1+(xp_long_t)l2):
(n3 == 1)? xp_awk_makerealval(run,(xp_real_t)r1+(xp_real_t)l2):
(n3 == 2)? xp_awk_makerealval(run,(xp_real_t)l1+(xp_real_t)r2):
xp_awk_makerealval(run,(xp_real_t)r1+(xp_real_t)r2);
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_minus (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
xp_long_t l1, l2;
xp_real_t r1, r2;
xp_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-07-17 04:17:40 +00:00
n1 = xp_awk_valtonum (left, &l1, &r1);
n2 = xp_awk_valtonum (right, &l2, &r2);
2006-04-16 06:16:42 +00:00
2006-04-21 17:24:31 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, XP_AWK_EOPERAND);
2006-04-16 06:16:42 +00:00
n3 = n1 + (n2 << 1);
2006-04-16 13:30:19 +00:00
xp_assert (n3 >= 0 && n3 <= 3);
2006-04-21 17:24:31 +00:00
res = (n3 == 0)? xp_awk_makeintval(run,(xp_long_t)l1-(xp_long_t)l2):
(n3 == 1)? xp_awk_makerealval(run,(xp_real_t)r1-(xp_real_t)l2):
(n3 == 2)? xp_awk_makerealval(run,(xp_real_t)l1-(xp_real_t)r2):
xp_awk_makerealval(run,(xp_real_t)r1-(xp_real_t)r2);
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_mul (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
xp_long_t l1, l2;
xp_real_t r1, r2;
xp_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-07-17 04:17:40 +00:00
n1 = xp_awk_valtonum (left, &l1, &r1);
n2 = xp_awk_valtonum (right, &l2, &r2);
2006-04-06 16:25:37 +00:00
2006-04-21 17:24:31 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, XP_AWK_EOPERAND);
2006-04-16 06:16:42 +00:00
n3 = n1 + (n2 << 1);
2006-04-16 13:30:19 +00:00
xp_assert (n3 >= 0 && n3 <= 3);
2006-04-21 17:24:31 +00:00
res = (n3 == 0)? xp_awk_makeintval(run,(xp_long_t)l1*(xp_long_t)l2):
(n3 == 1)? xp_awk_makerealval(run,(xp_real_t)r1*(xp_real_t)l2):
(n3 == 2)? xp_awk_makerealval(run,(xp_real_t)l1*(xp_real_t)r2):
xp_awk_makerealval(run,(xp_real_t)r1*(xp_real_t)r2);
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_div (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
xp_long_t l1, l2;
xp_real_t r1, r2;
xp_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-07-17 04:17:40 +00:00
n1 = xp_awk_valtonum (left, &l1, &r1);
n2 = xp_awk_valtonum (right, &l2, &r2);
2006-04-05 15:56:20 +00:00
2006-04-21 17:24:31 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, XP_AWK_EOPERAND);
2006-04-05 15:56:20 +00:00
2006-04-16 06:16:42 +00:00
n3 = n1 + (n2 << 1);
if (n3 == 0)
2006-04-05 15:56:20 +00:00
{
2006-04-21 17:24:31 +00:00
if (l2 == 0) PANIC (run, XP_AWK_EDIVBYZERO);
res = xp_awk_makeintval (run, (xp_long_t)l1 / (xp_long_t)l2);
2006-04-05 15:56:20 +00:00
}
2006-04-16 06:16:42 +00:00
else if (n3 == 1)
2006-04-05 15:56:20 +00:00
{
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, (xp_real_t)r1 / (xp_real_t)l2);
2006-04-05 15:56:20 +00:00
}
2006-04-16 06:16:42 +00:00
else if (n3 == 2)
2006-04-05 15:56:20 +00:00
{
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, (xp_real_t)l1 / (xp_real_t)r2);
2006-04-05 15:56:20 +00:00
}
else
{
2006-04-16 06:16:42 +00:00
xp_assert (n3 == 3);
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, (xp_real_t)r1 / (xp_real_t)r2);
2006-04-05 15:56:20 +00:00
}
2006-04-03 14:55:34 +00:00
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
static xp_awk_val_t* __eval_binop_mod (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
xp_long_t l1, l2;
xp_real_t r1, r2;
xp_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-07-17 04:17:40 +00:00
n1 = xp_awk_valtonum (left, &l1, &r1);
n2 = xp_awk_valtonum (right, &l2, &r2);
2006-04-05 15:56:20 +00:00
2006-04-21 17:24:31 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, XP_AWK_EOPERAND);
2006-04-05 15:56:20 +00:00
2006-04-16 06:16:42 +00:00
n3 = n1 + (n2 << 1);
if (n3 == 0)
2006-04-05 15:56:20 +00:00
{
2006-04-21 17:24:31 +00:00
if (l2 == 0) PANIC (run, XP_AWK_EDIVBYZERO);
res = xp_awk_makeintval (run, (xp_long_t)l1 % (xp_long_t)l2);
2006-04-05 15:56:20 +00:00
}
2006-04-21 17:24:31 +00:00
else PANIC (run, XP_AWK_EOPERAND);
2006-03-22 16:05:50 +00:00
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-03-22 16:05:50 +00:00
return res;
}
2006-04-11 15:44:30 +00:00
static xp_awk_val_t* __eval_binop_exp (
2006-04-21 17:24:31 +00:00
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
2006-04-11 15:44:30 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
xp_long_t l1, l2;
xp_real_t r1, r2;
xp_awk_val_t* res;
2006-04-11 15:44:30 +00:00
2006-07-17 04:17:40 +00:00
n1 = xp_awk_valtonum (left, &l1, &r1);
n2 = xp_awk_valtonum (right, &l2, &r2);
2006-04-11 15:44:30 +00:00
2006-04-21 17:24:31 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, XP_AWK_EOPERAND);
2006-04-16 06:16:42 +00:00
n3 = n1 + (n2 << 1);
if (n3 == 0)
2006-04-11 15:44:30 +00:00
{
2006-04-16 06:16:42 +00:00
xp_long_t v = 1;
while (l2-- > 0) v *= l1;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, v);
2006-04-11 15:44:30 +00:00
}
2006-04-16 06:16:42 +00:00
else if (n3 == 1)
2006-04-11 15:44:30 +00:00
{
2006-04-19 16:09:51 +00:00
/*res = xp_awk_makerealval (
2006-04-21 17:24:31 +00:00
run, pow((xp_real_t)r1,(xp_real_t)l2));*/
2006-04-19 16:09:51 +00:00
xp_real_t v = 1.0;
while (l2-- > 0) v *= r1;
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, v);
2006-04-11 15:44:30 +00:00
}
2006-04-16 06:16:42 +00:00
else if (n3 == 2)
2006-04-11 15:44:30 +00:00
{
2006-04-16 06:16:42 +00:00
res = xp_awk_makerealval (
2006-04-21 17:24:31 +00:00
run, pow((xp_real_t)l1,(xp_real_t)r2));
2006-04-11 15:44:30 +00:00
}
else
{
2006-04-16 06:16:42 +00:00
xp_assert (n3 == 3);
res = xp_awk_makerealval (
2006-04-21 17:24:31 +00:00
run, pow((xp_real_t)r1,(xp_real_t)r2));
2006-04-11 15:44:30 +00:00
}
2006-04-21 17:24:31 +00:00
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-11 15:44:30 +00:00
return res;
}
2006-06-29 15:40:30 +00:00
static xp_awk_val_t* __eval_binop_concat (
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right)
{
2006-08-03 05:05:48 +00:00
xp_char_t* strl, * strr;
xp_size_t strl_len, strr_len;
2006-06-30 03:53:16 +00:00
xp_awk_val_t* res;
int errnum;
2006-08-03 05:05:48 +00:00
strl = xp_awk_valtostr (left, &errnum, xp_true, XP_NULL, &strl_len);
if (strl == XP_NULL) PANIC (run, errnum);
2006-06-30 03:53:16 +00:00
2006-08-03 05:05:48 +00:00
strr = xp_awk_valtostr (right, &errnum, xp_true, XP_NULL, &strr_len);
if (strr == XP_NULL)
2006-06-30 03:53:16 +00:00
{
2006-08-03 05:05:48 +00:00
xp_free (strl);
2006-06-30 03:53:16 +00:00
PANIC (run, errnum);
}
2006-08-03 05:05:48 +00:00
res = xp_awk_makestrval2 (strl, strl_len, strr, strr_len);
2006-06-30 03:53:16 +00:00
if (res == XP_NULL)
{
2006-08-03 05:05:48 +00:00
xp_free (strl);
xp_free (strr);
2006-06-30 03:53:16 +00:00
PANIC (run, XP_AWK_ENOMEM);
}
2006-08-03 05:05:48 +00:00
xp_free (strl);
xp_free (strr);
2006-06-30 03:53:16 +00:00
return res;
2006-06-29 15:40:30 +00:00
}
2006-04-12 03:54:12 +00:00
static xp_awk_val_t* __eval_binop_ma (
2006-07-31 15:59:43 +00:00
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right)
2006-04-12 03:54:12 +00:00
{
2006-07-31 15:59:43 +00:00
xp_awk_val_t* lv, * rv, * res;
xp_assert (left->next == XP_NULL);
xp_assert (right->next == XP_NULL);
lv = __eval_expression (run, left);
2006-08-03 09:54:16 +00:00
if (lv == XP_NULL)
{
return XP_NULL;
}
2006-07-31 15:59:43 +00:00
xp_awk_refupval (lv);
2006-08-01 15:57:43 +00:00
2006-07-31 15:59:43 +00:00
rv = __eval_expression0 (run, right);
if (rv == XP_NULL)
{
xp_awk_refdownval (run, lv);
return XP_NULL;
}
xp_awk_refupval (rv);
2006-08-01 04:36:33 +00:00
res = __eval_binop_match0 (run, lv, rv, 1);
2006-07-31 15:59:43 +00:00
xp_awk_refdownval (run, lv);
xp_awk_refdownval (run, rv);
return res;
2006-04-12 03:54:12 +00:00
}
2006-07-31 15:59:43 +00:00
static xp_awk_val_t* __eval_binop_nm (
xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right)
{
xp_awk_val_t* lv, * rv, * res;
xp_assert (left->next == XP_NULL);
xp_assert (right->next == XP_NULL);
lv = __eval_expression (run, left);
if (lv == XP_NULL) return XP_NULL;
xp_awk_refupval (lv);
2006-08-01 15:57:43 +00:00
2006-07-31 15:59:43 +00:00
rv = __eval_expression0 (run, right);
if (rv == XP_NULL)
2006-04-24 11:22:42 +00:00
{
2006-07-31 15:59:43 +00:00
xp_awk_refdownval (run, lv);
return XP_NULL;
2006-04-24 11:22:42 +00:00
}
2006-07-31 15:59:43 +00:00
xp_awk_refupval (rv);
2006-08-01 04:36:33 +00:00
res = __eval_binop_match0 (run, lv, rv, 0);
2006-07-31 15:59:43 +00:00
xp_awk_refdownval (run, lv);
xp_awk_refdownval (run, rv);
return res;
}
2006-08-01 04:36:33 +00:00
static xp_awk_val_t* __eval_binop_match0 (
xp_awk_run_t* run, xp_awk_val_t* left, xp_awk_val_t* right, int ret)
2006-07-31 15:59:43 +00:00
{
xp_awk_val_t* res;
int n, errnum;
xp_char_t* str;
xp_size_t len;
2006-08-01 04:36:33 +00:00
void* rex_code;
2006-07-31 15:59:43 +00:00
if (right->type == XP_AWK_VAL_REX)
2006-04-24 11:22:42 +00:00
{
2006-08-01 04:36:33 +00:00
rex_code = ((xp_awk_val_rex_t*)right)->code;
}
else if (right->type == XP_AWK_VAL_STR)
{
rex_code = xp_awk_buildrex (
((xp_awk_val_str_t*)right)->buf,
((xp_awk_val_str_t*)right)->len, &errnum);
if (rex_code == XP_NULL)
2006-08-10 16:06:52 +00:00
PANIC (run, errnum);
2006-08-01 04:36:33 +00:00
}
else
{
2006-08-03 05:05:48 +00:00
str = xp_awk_valtostr (right, &errnum, xp_true, XP_NULL, &len);
2006-08-01 04:36:33 +00:00
if (str == XP_NULL) PANIC (run, errnum);
2006-07-31 15:59:43 +00:00
2006-08-01 04:36:33 +00:00
rex_code = xp_awk_buildrex (str, len, &errnum);
if (rex_code == XP_NULL)
{
xp_free (str);
2006-08-10 16:06:52 +00:00
PANIC (run, errnum);
2006-07-31 15:59:43 +00:00
}
2006-08-01 04:36:33 +00:00
xp_free (str);
}
if (left->type == XP_AWK_VAL_STR)
{
n = xp_awk_matchrex (
rex_code,
((xp_awk_val_str_t*)left)->buf,
((xp_awk_val_str_t*)left)->len,
XP_NULL, XP_NULL, &errnum);
if (n == -1)
2006-07-31 15:59:43 +00:00
{
2006-08-01 04:36:33 +00:00
if (right->type != XP_AWK_VAL_REX) xp_free (rex_code);
2006-08-10 16:06:52 +00:00
PANIC (run, errnum);
2006-08-01 04:36:33 +00:00
}
2006-07-31 15:59:43 +00:00
2006-08-01 04:36:33 +00:00
res = xp_awk_makeintval (run, (n == ret));
if (res == XP_NULL)
{
if (right->type != XP_AWK_VAL_REX) xp_free (rex_code);
PANIC (run, XP_AWK_ENOMEM);
}
}
else
{
2006-08-03 09:54:16 +00:00
str = xp_awk_valtostr (left, &errnum, xp_true, XP_NULL, &len);
2006-08-01 04:36:33 +00:00
if (str == XP_NULL)
{
if (right->type != XP_AWK_VAL_REX) xp_free (rex_code);
PANIC (run, errnum);
}
2006-07-31 15:59:43 +00:00
2006-08-01 04:36:33 +00:00
n = xp_awk_matchrex (
rex_code, str, len, XP_NULL, XP_NULL, &errnum);
if (n == -1)
{
xp_free (str);
if (right->type != XP_AWK_VAL_REX) xp_free (rex_code);
2006-08-10 16:06:52 +00:00
PANIC (run, errnum);
2006-08-01 04:36:33 +00:00
}
2006-07-31 15:59:43 +00:00
2006-08-01 04:36:33 +00:00
res = xp_awk_makeintval (run, (n == ret));
if (res == XP_NULL)
{
2006-07-31 15:59:43 +00:00
xp_free (str);
2006-08-01 04:36:33 +00:00
if (right->type != XP_AWK_VAL_REX) xp_free (rex_code);
PANIC (run, XP_AWK_ENOMEM);
2006-07-31 15:59:43 +00:00
}
2006-08-01 04:36:33 +00:00
xp_free (str);
2006-04-24 11:22:42 +00:00
}
2006-08-01 04:36:33 +00:00
if (right->type != XP_AWK_VAL_REX) xp_free (rex_code);
return res;
2006-04-12 03:54:12 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_unary (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-02 12:45:04 +00:00
{
2006-04-09 15:31:13 +00:00
xp_awk_val_t* left, * res = XP_NULL;
2006-04-07 16:52:42 +00:00
xp_awk_nde_exp_t* exp = (xp_awk_nde_exp_t*)nde;
2006-04-02 12:45:04 +00:00
2006-04-07 16:52:42 +00:00
xp_assert (exp->type == XP_AWK_NDE_EXP_UNR);
xp_assert (exp->left != XP_NULL && exp->right == XP_NULL);
2006-04-02 12:45:04 +00:00
2006-05-07 17:45:08 +00:00
xp_assert (exp->left->next == XP_NULL);
2006-04-21 17:24:31 +00:00
left = __eval_expression (run, exp->left);
2006-04-02 12:45:04 +00:00
if (left == XP_NULL) return XP_NULL;
xp_awk_refupval (left);
2006-04-07 16:52:42 +00:00
if (exp->opcode == XP_AWK_UNROP_PLUS)
2006-04-02 12:45:04 +00:00
{
if (left->type == XP_AWK_VAL_INT)
{
xp_long_t r = ((xp_awk_val_int_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
2006-04-02 12:45:04 +00:00
}
else if (left->type == XP_AWK_VAL_REAL)
{
xp_real_t r = ((xp_awk_val_real_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, r);
2006-04-02 12:45:04 +00:00
}
else
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EOPERAND);
2006-04-02 12:45:04 +00:00
}
}
2006-04-07 16:52:42 +00:00
else if (exp->opcode == XP_AWK_UNROP_MINUS)
2006-04-02 12:45:04 +00:00
{
if (left->type == XP_AWK_VAL_INT)
{
xp_long_t r = ((xp_awk_val_int_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, -r);
2006-04-02 12:45:04 +00:00
}
else if (left->type == XP_AWK_VAL_REAL)
{
xp_real_t r = ((xp_awk_val_real_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, -r);
2006-04-02 12:45:04 +00:00
}
else
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EOPERAND);
2006-04-02 12:45:04 +00:00
}
}
2006-04-07 16:52:42 +00:00
else if (exp->opcode == XP_AWK_UNROP_NOT)
2006-04-02 12:45:04 +00:00
{
if (left->type == XP_AWK_VAL_INT)
{
xp_long_t r = ((xp_awk_val_int_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, !r);
2006-04-02 12:45:04 +00:00
}
else if (left->type == XP_AWK_VAL_REAL)
{
xp_real_t r = ((xp_awk_val_real_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, !r);
2006-04-02 12:45:04 +00:00
}
else
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EOPERAND);
2006-04-02 12:45:04 +00:00
}
}
2006-04-07 16:52:42 +00:00
else if (exp->opcode == XP_AWK_UNROP_BNOT)
2006-04-02 12:45:04 +00:00
{
if (left->type == XP_AWK_VAL_INT)
{
xp_long_t r = ((xp_awk_val_int_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, ~r);
2006-04-02 12:45:04 +00:00
}
else
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EOPERAND);
2006-04-02 12:45:04 +00:00
}
}
2006-04-09 15:31:13 +00:00
if (res == XP_NULL)
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
2006-04-09 15:31:13 +00:00
}
2006-04-02 12:45:04 +00:00
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
2006-04-02 12:45:04 +00:00
return res;
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_incpre (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-02 12:45:04 +00:00
{
xp_awk_val_t* left, * res;
2006-04-07 16:52:42 +00:00
xp_awk_nde_exp_t* exp = (xp_awk_nde_exp_t*)nde;
2006-04-02 12:45:04 +00:00
2006-04-07 16:52:42 +00:00
xp_assert (exp->type == XP_AWK_NDE_EXP_INCPRE);
xp_assert (exp->left != XP_NULL && exp->right == XP_NULL);
2006-04-02 16:22:36 +00:00
2006-07-31 04:25:17 +00:00
/* this way of checking if the l-value is assignable is
* ugly as it is dependent of the values defined in tree.h.
* but let's keep going this way for the time being. */
2006-04-09 16:26:36 +00:00
if (exp->left->type < XP_AWK_NDE_NAMED ||
exp->left->type > XP_AWK_NDE_ARGIDX)
2006-04-07 16:52:42 +00:00
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-04-02 16:22:36 +00:00
}
2006-04-02 12:45:04 +00:00
2006-05-07 17:45:08 +00:00
xp_assert (exp->left->next == XP_NULL);
2006-04-21 17:24:31 +00:00
left = __eval_expression (run, exp->left);
2006-04-02 12:45:04 +00:00
if (left == XP_NULL) return XP_NULL;
xp_awk_refupval (left);
2006-04-07 16:52:42 +00:00
if (exp->opcode == XP_AWK_INCOP_PLUS)
2006-04-02 12:45:04 +00:00
{
if (left->type == XP_AWK_VAL_INT)
{
xp_long_t r = ((xp_awk_val_int_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r + 1);
2006-07-31 04:25:17 +00:00
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-02 12:45:04 +00:00
}
else if (left->type == XP_AWK_VAL_REAL)
{
xp_real_t r = ((xp_awk_val_real_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, r + 1.0);
2006-07-31 04:25:17 +00:00
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-02 12:45:04 +00:00
}
else
{
2006-07-31 04:25:17 +00:00
xp_long_t v1;
xp_real_t v2;
int n;
n = xp_awk_valtonum (left, &v1, &v2);
if (n == -1)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EOPERAND);
}
if (n == 0)
{
res = xp_awk_makeintval (run, v1 + 1);
}
else /* if (n == 1) */
{
xp_assert (n == 1);
res = xp_awk_makerealval (run, v2 + 1.0);
}
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-02 12:45:04 +00:00
}
}
2006-04-07 16:52:42 +00:00
else if (exp->opcode == XP_AWK_INCOP_MINUS)
2006-04-02 12:45:04 +00:00
{
if (left->type == XP_AWK_VAL_INT)
{
xp_long_t r = ((xp_awk_val_int_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r - 1);
2006-07-31 04:25:17 +00:00
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-02 12:45:04 +00:00
}
else if (left->type == XP_AWK_VAL_REAL)
{
xp_real_t r = ((xp_awk_val_real_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, r - 1.0);
2006-07-31 04:25:17 +00:00
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-02 12:45:04 +00:00
}
else
{
2006-07-31 04:25:17 +00:00
xp_long_t v1;
xp_real_t v2;
int n;
n = xp_awk_valtonum (left, &v1, &v2);
if (n == -1)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EOPERAND);
}
if (n == 0)
{
res = xp_awk_makeintval (run, v1 - 1);
}
else /* if (n == 1) */
{
xp_assert (n == 1);
res = xp_awk_makerealval (run, v2 - 1.0);
}
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-02 12:45:04 +00:00
}
}
2006-04-07 16:52:42 +00:00
else
{
xp_assert (!"should never happen - invalid opcode");
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EINTERNAL);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
2006-07-06 13:57:32 +00:00
if (__do_assignment (run, exp->left, res) == XP_NULL)
2006-04-02 16:22:36 +00:00
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
2006-04-02 16:22:36 +00:00
return XP_NULL;
}
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
2006-04-02 12:45:04 +00:00
return res;
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_incpst (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-02 12:45:04 +00:00
{
2006-04-02 16:22:36 +00:00
xp_awk_val_t* left, * res, * res2;
2006-04-07 16:52:42 +00:00
xp_awk_nde_exp_t* exp = (xp_awk_nde_exp_t*)nde;
2006-04-02 12:45:04 +00:00
2006-04-07 16:52:42 +00:00
xp_assert (exp->type == XP_AWK_NDE_EXP_INCPST);
xp_assert (exp->left != XP_NULL && exp->right == XP_NULL);
2006-04-02 16:22:36 +00:00
2006-07-31 04:25:17 +00:00
/* this way of checking if the l-value is assignable is
* ugly as it is dependent of the values defined in tree.h.
* but let's keep going this way for the time being. */
2006-04-09 16:26:36 +00:00
if (exp->left->type < XP_AWK_NDE_NAMED ||
exp->left->type > XP_AWK_NDE_ARGIDX)
2006-04-07 16:52:42 +00:00
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_EOPERAND);
2006-04-02 16:22:36 +00:00
}
2006-05-07 17:45:08 +00:00
xp_assert (exp->left->next == XP_NULL);
2006-04-21 17:24:31 +00:00
left = __eval_expression (run, exp->left);
2006-04-02 16:22:36 +00:00
if (left == XP_NULL) return XP_NULL;
xp_awk_refupval (left);
2006-04-02 12:45:04 +00:00
2006-04-07 16:52:42 +00:00
if (exp->opcode == XP_AWK_INCOP_PLUS)
2006-04-02 12:45:04 +00:00
{
if (left->type == XP_AWK_VAL_INT)
{
xp_long_t r = ((xp_awk_val_int_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
2006-07-31 04:25:17 +00:00
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-02 12:45:04 +00:00
2006-04-21 17:24:31 +00:00
res2 = xp_awk_makeintval (run, r + 1);
2006-04-07 16:52:42 +00:00
if (res2 == XP_NULL)
{
2006-07-31 04:25:17 +00:00
xp_awk_refdownval (run, left);
2006-04-21 17:24:31 +00:00
xp_awk_freeval (run, res, xp_true);
PANIC (run, XP_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
}
else if (left->type == XP_AWK_VAL_REAL)
{
xp_real_t r = ((xp_awk_val_real_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, r);
2006-07-31 04:25:17 +00:00
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-02 12:45:04 +00:00
2006-04-21 17:24:31 +00:00
res2 = xp_awk_makerealval (run, r + 1.0);
2006-04-07 16:52:42 +00:00
if (res2 == XP_NULL)
{
2006-07-31 04:25:17 +00:00
xp_awk_refdownval (run, left);
2006-04-21 17:24:31 +00:00
xp_awk_freeval (run, res, xp_true);
PANIC (run, XP_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
}
else
{
2006-07-31 04:25:17 +00:00
xp_long_t v1;
xp_real_t v2;
int n;
n = xp_awk_valtonum (left, &v1, &v2);
if (n == -1)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EOPERAND);
}
if (n == 0)
{
res = xp_awk_makeintval (run, v1);
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
res2 = xp_awk_makeintval (run, v1 + 1);
if (res2 == XP_NULL)
{
xp_awk_refdownval (run, left);
xp_awk_freeval (run, res, xp_true);
PANIC (run, XP_AWK_ENOMEM);
}
}
else /* if (n == 1) */
{
xp_assert (n == 1);
res = xp_awk_makerealval (run, v2);
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
res2 = xp_awk_makerealval (run, v2 + 1.0);
if (res2 == XP_NULL)
{
xp_awk_refdownval (run, left);
xp_awk_freeval (run, res, xp_true);
PANIC (run, XP_AWK_ENOMEM);
}
}
2006-04-02 12:45:04 +00:00
}
}
2006-04-07 16:52:42 +00:00
else if (exp->opcode == XP_AWK_INCOP_MINUS)
2006-04-02 12:45:04 +00:00
{
if (left->type == XP_AWK_VAL_INT)
{
xp_long_t r = ((xp_awk_val_int_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makeintval (run, r);
2006-07-31 04:25:17 +00:00
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-07 16:52:42 +00:00
2006-04-21 17:24:31 +00:00
res2 = xp_awk_makeintval (run, r - 1);
2006-04-07 16:52:42 +00:00
if (res2 == XP_NULL)
{
2006-07-31 04:25:17 +00:00
xp_awk_refdownval (run, left);
2006-04-21 17:24:31 +00:00
xp_awk_freeval (run, res, xp_true);
PANIC (run, XP_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
}
else if (left->type == XP_AWK_VAL_REAL)
{
xp_real_t r = ((xp_awk_val_real_t*)left)->val;
2006-04-21 17:24:31 +00:00
res = xp_awk_makerealval (run, r);
2006-07-31 04:25:17 +00:00
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
2006-04-07 16:52:42 +00:00
2006-04-21 17:24:31 +00:00
res2 = xp_awk_makerealval (run, r - 1.0);
2006-04-07 16:52:42 +00:00
if (res2 == XP_NULL)
{
2006-07-31 04:25:17 +00:00
xp_awk_refdownval (run, left);
2006-04-21 17:24:31 +00:00
xp_awk_freeval (run, res, xp_true);
PANIC (run, XP_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
}
else
{
2006-07-31 04:25:17 +00:00
xp_long_t v1;
xp_real_t v2;
int n;
n = xp_awk_valtonum (left, &v1, &v2);
if (n == -1)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EOPERAND);
}
if (n == 0)
{
res = xp_awk_makeintval (run, v1);
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
res2 = xp_awk_makeintval (run, v1 - 1);
if (res2 == XP_NULL)
{
xp_awk_refdownval (run, left);
xp_awk_freeval (run, res, xp_true);
PANIC (run, XP_AWK_ENOMEM);
}
}
else /* if (n == 1) */
{
xp_assert (n == 1);
res = xp_awk_makerealval (run, v2);
if (res == XP_NULL)
{
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_ENOMEM);
}
res2 = xp_awk_makerealval (run, v2 - 1.0);
if (res2 == XP_NULL)
{
xp_awk_refdownval (run, left);
xp_awk_freeval (run, res, xp_true);
PANIC (run, XP_AWK_ENOMEM);
}
}
2006-04-02 12:45:04 +00:00
}
}
2006-04-07 16:52:42 +00:00
else
{
xp_assert (!"should never happen - invalid opcode");
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
PANIC (run, XP_AWK_EINTERNAL);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
2006-07-06 13:57:32 +00:00
if (__do_assignment (run, exp->left, res2) == XP_NULL)
2006-04-02 16:22:36 +00:00
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
2006-04-02 16:22:36 +00:00
return XP_NULL;
}
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, left);
2006-04-02 12:45:04 +00:00
return res;
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_cnd (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-11 09:16:20 +00:00
{
xp_awk_val_t* tv, * v;
xp_awk_nde_cnd_t* cnd = (xp_awk_nde_cnd_t*)nde;
2006-05-07 17:45:08 +00:00
xp_assert (cnd->test->next == XP_NULL);
2006-04-21 17:24:31 +00:00
tv = __eval_expression (run, cnd->test);
2006-04-11 09:16:20 +00:00
if (tv == XP_NULL) return XP_NULL;
xp_awk_refupval (tv);
2006-05-07 17:45:08 +00:00
xp_assert (cnd->left->next == XP_NULL &&
cnd->right->next == XP_NULL);
2006-06-21 11:45:26 +00:00
v = (xp_awk_valtobool(tv))?
2006-04-21 17:24:31 +00:00
__eval_expression (run, cnd->left):
__eval_expression (run, cnd->right);
2006-04-11 09:16:20 +00:00
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, tv);
2006-04-11 09:16:20 +00:00
return v;
}
2006-06-19 15:43:27 +00:00
static xp_awk_val_t* __eval_bfn (xp_awk_run_t* run, xp_awk_nde_t* nde)
{
2006-06-21 15:37:51 +00:00
xp_awk_nde_call_t* call = (xp_awk_nde_call_t*)nde;
/* built-in function */
2006-07-14 04:19:22 +00:00
if (call->nargs < call->what.bfn.min_args)
2006-06-21 15:37:51 +00:00
{
PANIC (run, XP_AWK_ETOOFEWARGS);
}
2006-07-14 04:19:22 +00:00
if (call->nargs > call->what.bfn.max_args)
2006-06-21 15:37:51 +00:00
{
PANIC (run, XP_AWK_ETOOMANYARGS);
}
2006-08-18 07:52:47 +00:00
return __eval_call (run, nde, call->what.bfn.arg_spec, XP_NULL);
2006-06-19 15:43:27 +00:00
}
2006-06-20 15:27:50 +00:00
static xp_awk_val_t* __eval_afn (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-03-24 06:33:36 +00:00
{
2006-04-07 16:52:42 +00:00
xp_awk_nde_call_t* call = (xp_awk_nde_call_t*)nde;
2006-06-20 15:27:50 +00:00
xp_awk_afn_t* afn;
xp_awk_pair_t* pair;
2006-03-24 06:33:36 +00:00
2006-08-03 05:05:48 +00:00
pair = xp_awk_map_get (&run->awk->tree.afns,
call->what.afn.name, call->what.afn.name_len);
2006-04-21 17:24:31 +00:00
if (pair == XP_NULL) PANIC (run, XP_AWK_ENOSUCHFUNC);
2006-03-24 06:33:36 +00:00
2006-06-20 15:27:50 +00:00
afn = (xp_awk_afn_t*)pair->val;
xp_assert (afn != XP_NULL);
2006-04-18 16:04:59 +00:00
2006-06-20 15:27:50 +00:00
if (call->nargs > afn->nargs)
2006-04-18 16:04:59 +00:00
{
/* TODO: is this correct? what if i want to allow arbitarary numbers of arguments? */
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_ETOOMANYARGS);
2006-04-18 16:04:59 +00:00
}
2006-08-18 07:52:47 +00:00
return __eval_call (run, nde, XP_NULL, afn);
2006-06-20 15:27:50 +00:00
}
2006-08-18 07:52:47 +00:00
/* run->stack_base has not been set for this
* stack frame. so STACK_ARG cannot be used */
/*xp_awk_refdownval (run, STACK_ARG(run,nargs));*/
#define UNWIND_RUN_STACK(run,nargs) \
do { \
while ((nargs) > 0) \
{ \
--(nargs); \
xp_awk_refdownval ((run), \
(run)->stack[(run)->stack_top-1]); \
__raw_pop (run); \
} \
__raw_pop (run); \
__raw_pop (run); \
__raw_pop (run); \
} while (0)
2006-06-20 15:27:50 +00:00
static xp_awk_val_t* __eval_call (
2006-08-18 07:52:47 +00:00
xp_awk_run_t* run, xp_awk_nde_t* nde,
const xp_char_t* bfn_arg_spec, xp_awk_afn_t* afn)
2006-06-20 15:27:50 +00:00
{
xp_awk_nde_call_t* call = (xp_awk_nde_call_t*)nde;
xp_size_t saved_stack_top;
xp_size_t nargs, i;
xp_awk_nde_t* p;
xp_awk_val_t* v;
int n;
2006-03-24 06:33:36 +00:00
/*
* ---------------------
2006-04-09 15:34:38 +00:00
* localn <- stack top
2006-03-26 16:36:30 +00:00
* ---------------------
* ....
* ---------------------
2006-04-09 15:34:38 +00:00
* local0 local variables are pushed by __run_block
* =====================
2006-03-26 16:36:30 +00:00
* 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-04-09 15:34:38 +00:00
* =====================
* 0 (nargs) <- stack top
* ---------------------
* return value
* ---------------------
* previous stack top
* ---------------------
* previous stack base <- stack base
* =====================
* globaln
* ---------------------
* ....
* ---------------------
* global0
2006-03-24 06:33:36 +00:00
* ---------------------
*/
2006-04-21 17:24:31 +00:00
xp_assert (xp_sizeof(void*) >= xp_sizeof(run->stack_top));
xp_assert (xp_sizeof(void*) >= xp_sizeof(run->stack_base));
2006-03-25 17:04:36 +00:00
2006-04-21 17:24:31 +00:00
saved_stack_top = run->stack_top;
2006-03-25 17:04:36 +00:00
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("setting up function stack frame stack_top = %ld stack_base = %ld\n"), run->stack_top, run->stack_base); */
2006-04-21 17:24:31 +00:00
if (__raw_push(run,(void*)run->stack_base) == -1)
2006-04-09 15:31:13 +00:00
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_ENOMEM);
2006-04-09 15:31:13 +00:00
}
2006-08-18 07:52:47 +00:00
2006-04-21 17:24:31 +00:00
if (__raw_push(run,(void*)saved_stack_top) == -1)
2006-03-25 17:04:36 +00:00
{
2006-04-21 17:24:31 +00:00
__raw_pop (run);
PANIC (run, XP_AWK_ENOMEM);
2006-03-25 17:04:36 +00:00
}
2006-04-09 15:31:13 +00:00
/* secure space for a return value. */
2006-04-21 17:24:31 +00:00
if (__raw_push(run,xp_awk_val_nil) == -1)
2006-03-25 17:04:36 +00:00
{
2006-04-21 17:24:31 +00:00
__raw_pop (run);
__raw_pop (run);
PANIC (run, XP_AWK_ENOMEM);
2006-03-25 17:04:36 +00:00
}
/* secure space for nargs */
2006-04-21 17:24:31 +00:00
if (__raw_push(run,xp_awk_val_nil) == -1)
2006-03-25 17:04:36 +00:00
{
2006-04-21 17:24:31 +00:00
__raw_pop (run);
__raw_pop (run);
__raw_pop (run);
PANIC (run, XP_AWK_ENOMEM);
2006-03-25 17:04:36 +00:00
}
nargs = 0;
2006-04-07 16:52:42 +00:00
p = call->args;
2006-03-25 17:04:36 +00:00
while (p != XP_NULL)
{
2006-08-18 17:46:07 +00:00
v = __eval_expression (run, p);
2006-03-25 17:04:36 +00:00
if (v == XP_NULL)
{
2006-08-18 07:52:47 +00:00
UNWIND_RUN_STACK (run, nargs);
2006-03-25 17:04:36 +00:00
return XP_NULL;
}
2006-08-18 07:52:47 +00:00
if (bfn_arg_spec != XP_NULL)
{
xp_char_t spec;
2006-08-18 17:46:07 +00:00
/* TODO: spec length check */
2006-08-18 07:52:47 +00:00
spec = bfn_arg_spec[nargs];
2006-08-20 15:49:48 +00:00
if (spec == XP_T('r'))
2006-08-18 07:52:47 +00:00
{
2006-08-20 15:49:48 +00:00
xp_awk_val_t** ref;
2006-08-19 16:34:24 +00:00
xp_awk_val_t* tmp;
ref = __get_reference (run, p);
if (ref == XP_NULL)
2006-08-18 07:52:47 +00:00
{
2006-08-18 17:46:07 +00:00
xp_awk_refupval (v);
xp_awk_refdownval (run, v);
UNWIND_RUN_STACK (run, nargs);
2006-08-20 15:49:48 +00:00
return XP_NULL;
2006-08-18 07:52:47 +00:00
}
2006-08-18 17:46:07 +00:00
2006-08-20 15:49:48 +00:00
/* p->type-XP_AWK_NDE_NAMED assumes that the
* derived value matches XP_AWK_VAL_REF_XXX */
tmp = xp_awk_makerefval (
run, p->type-XP_AWK_NDE_NAMED, ref);
2006-08-19 16:34:24 +00:00
if (tmp == XP_NULL)
2006-08-18 17:46:07 +00:00
{
xp_awk_refupval (v);
xp_awk_refdownval (run, v);
2006-08-19 16:34:24 +00:00
UNWIND_RUN_STACK (run, nargs);
PANIC (run, XP_AWK_ENOMEM);
2006-08-18 17:46:07 +00:00
}
2006-08-19 16:34:24 +00:00
xp_awk_refupval (v);
xp_awk_refdownval (run, v);
v = tmp;
2006-08-18 17:46:07 +00:00
}
2006-08-18 07:52:47 +00:00
}
2006-04-21 17:24:31 +00:00
if (__raw_push(run,v) == -1)
2006-03-25 17:04:36 +00:00
{
2006-04-09 15:31:13 +00:00
/* ugly - v needs to be freed if it doesn't have
* any reference. but its reference has not been
2006-08-18 07:52:47 +00:00
* updated yet as it is carried out after the
* successful stack push. so it adds up a reference
* and dereferences it */
2006-04-09 15:31:13 +00:00
xp_awk_refupval (v);
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, v);
2006-04-09 15:31:13 +00:00
2006-08-18 07:52:47 +00:00
UNWIND_RUN_STACK (run, nargs);
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_ENOMEM);
2006-03-25 17:04:36 +00:00
}
xp_awk_refupval (v);
nargs++;
p = p->next;
}
2006-04-18 16:04:59 +00:00
xp_assert (nargs == call->nargs);
2006-03-24 06:33:36 +00:00
2006-06-20 15:27:50 +00:00
if (afn != XP_NULL)
2006-04-18 16:04:59 +00:00
{
2006-06-21 15:37:51 +00:00
/* extra step for normal awk functions */
2006-06-20 15:27:50 +00:00
while (nargs < afn->nargs)
2006-04-18 16:04:59 +00:00
{
2006-06-20 15:27:50 +00:00
/* push as many nils as the number of missing actual arguments */
if (__raw_push(run,xp_awk_val_nil) == -1)
2006-04-18 16:04:59 +00:00
{
2006-08-18 07:52:47 +00:00
UNWIND_RUN_STACK (run, nargs);
2006-06-20 15:27:50 +00:00
PANIC (run, XP_AWK_ENOMEM);
}
2006-03-25 17:04:36 +00:00
2006-06-20 15:27:50 +00:00
nargs++;
2006-04-18 16:04:59 +00:00
}
}
2006-04-21 17:24:31 +00:00
run->stack_base = saved_stack_top;
STACK_NARGS(run) = (void*)nargs;
2006-03-25 17:04:36 +00:00
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("running function body\n")); */
2006-03-24 06:33:36 +00:00
2006-06-20 15:27:50 +00:00
if (afn != XP_NULL)
{
/* normal awk function */
xp_assert (afn->body->type == XP_AWK_NDE_BLK);
n = __run_block(run,(xp_awk_nde_blk_t*)afn->body);
}
else
{
n = 0;
/* built-in function */
2006-07-14 04:19:22 +00:00
xp_assert (call->nargs >= call->what.bfn.min_args &&
call->nargs <= call->what.bfn.max_args);
2006-06-21 15:37:51 +00:00
2006-07-14 04:19:22 +00:00
if (call->what.bfn.handler != XP_NULL)
2006-08-13 16:05:04 +00:00
n = call->what.bfn.handler (run->awk, run);
2006-06-20 15:27:50 +00:00
}
2006-03-26 16:36:30 +00:00
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("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-04-21 17:24:31 +00:00
nargs = (xp_size_t)STACK_NARGS(run);
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("block run complete nargs = %d\n"), nargs); */
2006-03-25 17:04:36 +00:00
for (i = 0; i < nargs; i++)
{
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, STACK_ARG(run,i));
2006-03-25 17:04:36 +00:00
}
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("got return value\n")); */
2006-03-26 16:36:30 +00:00
2006-06-27 14:18:19 +00:00
/* this is the trick mentioned in __run_return.
2006-04-09 15:31:13 +00:00
* adjust the reference count of the return value.
2006-06-20 15:27:50 +00:00
* the value must not be freed even if the reference count
* is decremented to zero because its reference has been incremented
2006-06-27 14:18:19 +00:00
* in __run_return regardless of its reference count. */
2006-04-21 17:24:31 +00:00
v = STACK_RETVAL(run);
xp_awk_refdownval_nofree (run, v);
2006-03-25 17:04:36 +00:00
2006-04-21 17:24:31 +00:00
run->stack_top = (xp_size_t)run->stack[run->stack_base+1];
run->stack_base = (xp_size_t)run->stack[run->stack_base+0];
2006-03-25 17:04:36 +00:00
2006-08-18 17:46:07 +00:00
if (run->exit_level == EXIT_FUNCTION) run->exit_level = EXIT_NONE;
2006-03-26 16:36:30 +00:00
2006-05-06 12:52:36 +00:00
/*xp_printf (XP_T("returning from function stack_top=%ld, stack_base=%ld\n"), run->stack_top, run->stack_base); */
2006-04-09 15:31:13 +00:00
return (n == -1)? XP_NULL: v;
2006-03-24 06:33:36 +00:00
}
2006-08-20 15:49:48 +00:00
static xp_awk_val_t** __get_reference (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-08-19 16:34:24 +00:00
{
xp_awk_nde_var_t* tgt = (xp_awk_nde_var_t*)nde;
2006-08-20 15:49:48 +00:00
/* refer to __eval_indexed for application of similar concept */
if (nde->type == XP_AWK_NDE_NAMED)
{
xp_awk_pair_t* pair;
pair = xp_awk_map_get (
&run->named, tgt->id.name, tgt->id.name_len);
if (pair == XP_NULL)
{
/* it is bad that the named variable has to be
* created in the function named "__get_refernce".
* would there be any better ways to avoid this? */
pair = xp_awk_map_put (
&run->named, tgt->id.name,
tgt->id.name_len, xp_awk_val_nil);
if (pair == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
}
return (xp_awk_val_t**)&pair->val;
}
2006-08-19 16:34:24 +00:00
if (nde->type == XP_AWK_NDE_GLOBAL)
2006-08-20 15:49:48 +00:00
{
return (xp_awk_val_t**)&STACK_GLOBAL(run,tgt->id.idxa);
}
2006-08-19 16:34:24 +00:00
if (nde->type == XP_AWK_NDE_LOCAL)
2006-08-20 15:49:48 +00:00
{
return (xp_awk_val_t**)&STACK_LOCAL(run,tgt->id.idxa);
}
2006-08-19 16:34:24 +00:00
if (nde->type == XP_AWK_NDE_ARG)
2006-08-20 15:49:48 +00:00
{
return (xp_awk_val_t**)&STACK_ARG(run,tgt->id.idxa);
}
2006-08-19 16:34:24 +00:00
2006-08-20 15:49:48 +00:00
if (nde->type == XP_AWK_NDE_NAMEDIDX)
{
xp_awk_pair_t* pair;
2006-08-19 16:34:24 +00:00
2006-08-20 15:49:48 +00:00
pair = xp_awk_map_get (
&run->named, tgt->id.name, tgt->id.name_len);
if (pair == XP_NULL)
{
pair = xp_awk_map_put (
&run->named, tgt->id.name,
tgt->id.name_len, xp_awk_val_nil);
if (pair == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
}
return __get_reference_indexed (
run, tgt, (xp_awk_val_t**)&pair->val);
}
if (nde->type == XP_AWK_NDE_GLOBALIDX)
{
return __get_reference_indexed (run, tgt,
(xp_awk_val_t**)&STACK_GLOBAL(run,tgt->id.idxa));
}
if (nde->type == XP_AWK_NDE_LOCALIDX)
{
return __get_reference_indexed (run, tgt,
(xp_awk_val_t**)&STACK_LOCAL(run,tgt->id.idxa));
}
if (nde->type == XP_AWK_NDE_ARGIDX)
{
return __get_reference_indexed (run, tgt,
(xp_awk_val_t**)&STACK_ARG(run,tgt->id.idxa));
}
PANIC (run, XP_AWK_ENOTREFERENCEABLE);
2006-08-19 16:34:24 +00:00
}
2006-08-20 15:49:48 +00:00
static xp_awk_val_t** __get_reference_indexed (
xp_awk_run_t* run, xp_awk_nde_var_t* nde, xp_awk_val_t** val)
{
xp_awk_pair_t* pair;
xp_char_t* str;
xp_size_t len;
xp_assert (val != XP_NULL);
if ((*val)->type == XP_AWK_VAL_NIL)
{
xp_awk_val_t* tmp;
tmp = xp_awk_makemapval (run);
if (tmp == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
xp_awk_refdownval (run, *val);
*val = tmp;
xp_awk_refupval ((xp_awk_val_t*)*val);
}
else if ((*val)->type != XP_AWK_VAL_MAP)
{
PANIC (run, XP_AWK_ENOTINDEXABLE);
}
xp_assert (nde->idx != XP_NULL);
str = __idxnde_to_str (run, nde->idx, &len);
if (str == XP_NULL) return XP_NULL;
pair = xp_awk_map_get ((*(xp_awk_val_map_t**)val)->map, str, len);
if (pair == XP_NULL)
{
pair = xp_awk_map_put (
(*(xp_awk_val_map_t**)val)->map,
str, len, xp_awk_val_nil);
if (pair == XP_NULL)
{
xp_free (str);
PANIC (run, XP_AWK_ENOMEM);
}
xp_awk_refupval (pair->val);
}
xp_free (str);
return (xp_awk_val_t**)&pair->val;
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_int (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
xp_awk_val_t* val;
val = xp_awk_makeintval (
2006-04-21 17:24:31 +00:00
run, ((xp_awk_nde_int_t*)nde)->val);
if (val == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
return val;
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_real (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
xp_awk_val_t* val;
val = xp_awk_makerealval (
2006-04-21 17:24:31 +00:00
run, ((xp_awk_nde_real_t*)nde)->val);
if (val == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
return val;
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_str (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
xp_awk_val_t* val;
2006-07-25 16:41:40 +00:00
2006-04-07 16:52:42 +00:00
val = xp_awk_makestrval (
((xp_awk_nde_str_t*)nde)->buf,
((xp_awk_nde_str_t*)nde)->len);
2006-04-21 17:24:31 +00:00
if (val == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
2006-07-25 16:41:40 +00:00
2006-04-07 16:52:42 +00:00
return val;
}
2006-04-24 11:22:42 +00:00
static xp_awk_val_t* __eval_rex (xp_awk_run_t* run, xp_awk_nde_t* nde)
{
2006-07-25 16:41:40 +00:00
xp_awk_val_t* val;
val = xp_awk_makerexval (
((xp_awk_nde_rex_t*)nde)->buf,
2006-07-26 15:00:01 +00:00
((xp_awk_nde_rex_t*)nde)->len,
((xp_awk_nde_rex_t*)nde)->code);
2006-07-25 16:41:40 +00:00
if (val == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
return val;
2006-04-24 11:22:42 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_named (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
xp_awk_pair_t* pair;
2006-08-20 15:49:48 +00:00
pair = xp_awk_map_get (&run->named,
((xp_awk_nde_var_t*)nde)->id.name,
((xp_awk_nde_var_t*)nde)->id.name_len);
2006-04-07 16:52:42 +00:00
return (pair == XP_NULL)? xp_awk_val_nil: pair->val;
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_global (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-08-20 15:49:48 +00:00
return STACK_GLOBAL(run,((xp_awk_nde_var_t*)nde)->id.idxa);
2006-04-07 16:52:42 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_local (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-08-20 15:49:48 +00:00
return STACK_LOCAL(run,((xp_awk_nde_var_t*)nde)->id.idxa);
2006-04-07 16:52:42 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_arg (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-08-20 15:49:48 +00:00
return STACK_ARG(run,((xp_awk_nde_var_t*)nde)->id.idxa);
2006-04-07 16:52:42 +00:00
}
2006-04-18 11:26:48 +00:00
static xp_awk_val_t* __eval_indexed (
2006-08-20 15:49:48 +00:00
xp_awk_run_t* run, xp_awk_nde_var_t* nde, xp_awk_val_t** val)
2006-04-07 16:52:42 +00:00
{
2006-04-16 16:30:59 +00:00
xp_awk_pair_t* pair;
2006-05-03 15:40:19 +00:00
xp_char_t* str;
2006-08-03 05:05:48 +00:00
xp_size_t len;
2006-04-16 13:30:19 +00:00
2006-08-20 15:49:48 +00:00
xp_assert (val != XP_NULL);
if ((*val)->type == XP_AWK_VAL_NIL)
{
xp_awk_val_t* tmp;
tmp = xp_awk_makemapval (run);
if (tmp == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
xp_awk_refdownval (run, *val);
*val = tmp;
xp_awk_refupval ((xp_awk_val_t*)*val);
}
else if ((*val)->type != XP_AWK_VAL_MAP)
2006-04-18 11:26:48 +00:00
{
2006-04-21 17:24:31 +00:00
PANIC (run, XP_AWK_ENOTINDEXABLE);
2006-04-18 11:26:48 +00:00
}
2006-04-16 16:30:59 +00:00
2006-04-18 11:26:48 +00:00
xp_assert (nde->idx != XP_NULL);
2006-04-16 16:30:59 +00:00
2006-08-03 05:05:48 +00:00
str = __idxnde_to_str (run, nde->idx, &len);
2006-05-03 15:40:19 +00:00
if (str == XP_NULL) return XP_NULL;
2006-04-16 13:30:19 +00:00
2006-08-20 15:49:48 +00:00
pair = xp_awk_map_get ((*(xp_awk_val_map_t**)val)->map, str, len);
2006-05-03 15:40:19 +00:00
xp_free (str);
2006-08-20 15:49:48 +00:00
return (pair == XP_NULL)? xp_awk_val_nil: (xp_awk_val_t*)pair->val;
2006-04-07 16:52:42 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_namedidx (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-19 16:09:51 +00:00
{
xp_awk_nde_var_t* tgt = (xp_awk_nde_var_t*)nde;
xp_awk_pair_t* pair;
2006-08-03 05:05:48 +00:00
pair = xp_awk_map_get (&run->named, tgt->id.name, tgt->id.name_len);
2006-08-20 15:49:48 +00:00
if (pair == XP_NULL)
{
pair = xp_awk_map_put (&run->named,
tgt->id.name, tgt->id.name_len, xp_awk_val_nil);
if (pair == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
xp_awk_refupval (pair->val);
}
return __eval_indexed (run, tgt, (xp_awk_val_t**)&pair->val);
2006-04-19 16:09:51 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_globalidx (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-18 11:26:48 +00:00
{
2006-08-20 15:49:48 +00:00
return __eval_indexed (run, (xp_awk_nde_var_t*)nde,
(xp_awk_val_t**)&STACK_GLOBAL(run,((xp_awk_nde_var_t*)nde)->id.idxa));
2006-04-18 11:26:48 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_localidx (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-08-20 15:49:48 +00:00
return __eval_indexed (run, (xp_awk_nde_var_t*)nde,
(xp_awk_val_t**)&STACK_LOCAL(run,((xp_awk_nde_var_t*)nde)->id.idxa));
2006-04-07 16:52:42 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_argidx (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-08-20 15:49:48 +00:00
return __eval_indexed (run, (xp_awk_nde_var_t*)nde,
(xp_awk_val_t**)&STACK_ARG(run,((xp_awk_nde_var_t*)nde)->id.idxa));
2006-04-07 16:52:42 +00:00
}
2006-04-21 17:24:31 +00:00
static xp_awk_val_t* __eval_pos (xp_awk_run_t* run, xp_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-07-05 16:20:23 +00:00
xp_awk_nde_pos_t* pos = (xp_awk_nde_pos_t*)nde;
xp_awk_val_t* v;
xp_long_t lv;
xp_real_t rv;
int n;
v = __eval_expression (run, pos->val);
if (v == XP_NULL) return XP_NULL;
xp_awk_refupval (v);
2006-07-17 04:17:40 +00:00
n = xp_awk_valtonum (v, &lv, &rv);
2006-07-05 16:20:23 +00:00
xp_awk_refdownval (run, v);
if (n == -1) PANIC (run, XP_AWK_EPOSIDX);
if (n == 1) lv = (xp_long_t)rv;
2006-07-09 16:06:05 +00:00
if (lv < 0) PANIC (run, XP_AWK_EPOSIDX);
2006-07-06 15:54:41 +00:00
if (lv == 0) v = run->inrec.d0;
else if (lv > 0 && lv <= run->inrec.nflds)
v = run->inrec.flds[lv-1].val;
2006-07-10 04:51:38 +00:00
else v = xp_awk_val_zls; /*xp_awk_val_nil;*/
2006-07-05 16:20:23 +00:00
return v;
2006-04-07 16:52:42 +00:00
}
2006-06-12 15:11:02 +00:00
static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde)
{
2006-06-28 14:19:01 +00:00
xp_awk_nde_getline_t* p;
2006-06-29 14:38:01 +00:00
xp_awk_val_t* v, * res;
xp_char_t* in = XP_NULL;
const xp_char_t* dst;
2006-06-28 14:19:01 +00:00
xp_str_t buf;
2006-06-28 10:40:24 +00:00
int errnum, n;
2006-06-16 07:37:27 +00:00
2006-06-28 14:19:01 +00:00
p = (xp_awk_nde_getline_t*)nde;
2006-06-29 14:38:01 +00:00
xp_assert ((p->in_type == XP_AWK_IN_PIPE && p->in != XP_NULL) ||
(p->in_type == XP_AWK_IN_COPROC && p->in != XP_NULL) ||
(p->in_type == XP_AWK_IN_FILE && p->in != XP_NULL) ||
(p->in_type == XP_AWK_IN_CONSOLE && p->in == XP_NULL));
2006-06-16 07:37:27 +00:00
2006-06-29 14:38:01 +00:00
if (p->in != XP_NULL)
2006-06-16 07:37:27 +00:00
{
2006-08-02 03:22:51 +00:00
xp_size_t len;
2006-06-29 14:38:01 +00:00
v = __eval_expression (run, p->in);
if (v == XP_NULL) return XP_NULL;
2006-08-02 03:22:51 +00:00
/* TODO: distinction between v->type == XP_AWK_VAL_STR
* and v->type != XP_AWK_VAL_STR
* if you use the buffer the v directly when
* v->type == XP_AWK_VAL_STR, xp_awk_refdownval(v)
* should not be called immediately below */
2006-06-29 14:38:01 +00:00
xp_awk_refupval (v);
2006-08-03 05:05:48 +00:00
in = xp_awk_valtostr (v, &errnum, xp_true, XP_NULL, &len);
2006-06-29 14:38:01 +00:00
if (in == XP_NULL)
{
xp_awk_refdownval (run, v);
PANIC (run, errnum);
}
xp_awk_refdownval (run, v);
2006-08-02 03:22:51 +00:00
2006-08-02 03:34:34 +00:00
if (len <= 0)
{
/* the input source name is empty.
* make getline return -1 */
xp_free (in);
n = -1;
goto skip_read;
}
2006-08-02 03:22:51 +00:00
while (len > 0)
{
if (in[--len] == XP_T('\0'))
{
/* the input source name contains a null
* character. make getline return -1 */
2006-08-22 15:11:13 +00:00
/* TODO: set ERRNO */
2006-08-02 03:22:51 +00:00
xp_free (in);
n = -1;
goto skip_read;
}
}
2006-06-28 10:40:24 +00:00
}
2006-06-29 14:38:01 +00:00
dst = (in == XP_NULL)? XP_T(""): in;
2006-06-19 09:10:57 +00:00
2006-07-01 16:07:06 +00:00
/* TODO: optimize the line buffer management */
2006-06-30 03:53:16 +00:00
if (xp_str_open (&buf, DEF_BUF_CAPA) == XP_NULL)
2006-06-28 14:19:01 +00:00
{
2006-06-29 14:38:01 +00:00
if (in != XP_NULL) xp_free (in);
2006-06-28 14:19:01 +00:00
PANIC (run, XP_AWK_ENOMEM);
}
2006-08-27 15:29:21 +00:00
n = xp_awk_readextio (run, p->in_type, dst, &buf);
2006-06-29 14:38:01 +00:00
if (in != XP_NULL) xp_free (in);
2006-06-19 09:10:57 +00:00
2006-07-28 10:34:22 +00:00
if (n < 0)
2006-06-28 14:19:01 +00:00
{
2006-08-27 15:29:21 +00:00
if (run->errnum != XP_AWK_EIOHANDLER)
2006-07-28 10:34:22 +00:00
{
xp_str_close (&buf);
2006-08-27 15:29:21 +00:00
return XP_NULL;
2006-07-28 10:34:22 +00:00
}
2006-08-27 15:29:21 +00:00
/* if run->errnum == XP_AWK_EIOHANDLER,
* make getline return -1 */
2006-07-28 10:34:22 +00:00
n = -1;
2006-06-28 14:19:01 +00:00
}
2006-06-22 14:15:02 +00:00
2006-06-28 14:19:01 +00:00
if (n > 0)
{
if (p->var == XP_NULL)
{
2006-07-27 16:50:29 +00:00
/* set $0 with the input value */
2006-08-27 15:29:21 +00:00
if (__clear_record (run, xp_false) == -1)
{
xp_str_close (&buf);
return XP_NULL;
}
2006-07-27 16:50:29 +00:00
if (__set_record (run,
XP_STR_BUF(&buf), XP_STR_LEN(&buf)) == -1)
{
xp_str_close (&buf);
return XP_NULL;
}
2006-06-28 14:19:01 +00:00
xp_str_close (&buf);
}
else
{
xp_awk_val_t* v;
v = xp_awk_makestrval (
XP_STR_BUF(&buf), XP_STR_LEN(&buf));
xp_str_close (&buf);
if (v == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
xp_awk_refupval (v);
2006-07-06 13:57:32 +00:00
if (__do_assignment(run, p->var, v) == XP_NULL)
2006-06-28 14:19:01 +00:00
{
xp_awk_refdownval (run, v);
return XP_NULL;
}
xp_awk_refdownval (run, v);
}
}
2006-07-27 16:50:29 +00:00
else
{
xp_str_close (&buf);
}
2006-06-28 14:19:01 +00:00
2006-08-02 03:22:51 +00:00
skip_read:
2006-06-28 14:19:01 +00:00
res = xp_awk_makeintval (run, n);
if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
return res;
2006-06-12 15:11:02 +00:00
}
2006-04-21 17:24:31 +00:00
static int __raw_push (xp_awk_run_t* run, void* val)
2006-03-24 06:33:36 +00:00
{
2006-04-21 17:24:31 +00:00
if (run->stack_top >= run->stack_limit)
2006-03-24 06:33:36 +00:00
{
2006-08-16 11:35:54 +00:00
void** tmp;
2006-03-24 06:33:36 +00:00
xp_size_t n;
2006-04-21 17:24:31 +00:00
n = run->stack_limit + STACK_INCREMENT;
2006-03-24 06:33:36 +00:00
2006-08-16 11:35:54 +00:00
#ifndef XP_AWK_NTDDK
tmp = (void**) xp_realloc (
run->stack, n * xp_sizeof(void*));
if (tmp == XP_NULL) return -1;
#else
tmp = (void**) xp_malloc (n * xp_sizeof(void*));
if (tmp == XP_NULL) return -1;
if (run->stack != XP_NULL)
{
xp_memcpy (tmp, run->stack,
run->stack_limit * xp_sizeof(void*));
xp_free (run->stack);
}
#endif
2006-04-21 17:24:31 +00:00
run->stack = tmp;
run->stack_limit = n;
2006-03-24 06:33:36 +00:00
}
2006-04-21 17:24:31 +00:00
run->stack[run->stack_top++] = val;
2006-03-25 17:04:36 +00:00
return 0;
2006-03-24 06:33:36 +00:00
}
2006-03-26 14:03:08 +00:00
2006-04-21 17:24:31 +00:00
static void __raw_pop_times (xp_awk_run_t* run, xp_size_t times)
2006-04-09 15:31:13 +00:00
{
while (times > 0)
{
--times;
2006-04-21 17:24:31 +00:00
__raw_pop (run);
2006-04-09 15:31:13 +00:00
}
}
2006-04-16 04:31:38 +00:00
2006-07-03 04:09:56 +00:00
static int __read_record (xp_awk_run_t* run)
2006-04-22 13:54:53 +00:00
{
xp_ssize_t n;
2006-08-27 15:29:21 +00:00
if (__clear_record (run, xp_false) == -1) return -1;
2006-04-22 13:54:53 +00:00
2006-07-27 16:50:29 +00:00
n = xp_awk_readextio (
2006-08-27 15:29:21 +00:00
run, XP_AWK_IN_CONSOLE, XP_T(""), &run->inrec.line);
2006-07-27 16:50:29 +00:00
if (n < 0)
2006-04-22 13:54:53 +00:00
{
2006-08-27 15:29:21 +00:00
if (run->errnum == XP_AWK_EIOHANDLER)
2006-08-03 09:54:16 +00:00
PANIC_I (run, XP_AWK_ECONINDATA);
2006-08-27 15:29:21 +00:00
else return -1;
2006-07-27 16:50:29 +00:00
}
if (n == 0)
{
xp_assert (XP_STR_LEN(&run->inrec.line) == 0);
return 0;
}
2006-04-22 13:54:53 +00:00
2006-07-27 16:50:29 +00:00
if (__set_record (run,
XP_STR_BUF(&run->inrec.line),
XP_STR_LEN(&run->inrec.line)) == -1) return -1;
2006-04-22 13:54:53 +00:00
2006-07-27 16:50:29 +00:00
return 1;
}
2006-07-03 04:09:56 +00:00
2006-07-27 16:50:29 +00:00
static int __set_record (xp_awk_run_t* run, const xp_char_t* str, xp_size_t len)
{
xp_awk_val_t* v;
2006-08-27 15:29:21 +00:00
int errnum;
2006-04-22 13:54:53 +00:00
2006-07-27 16:50:29 +00:00
v = xp_awk_makestrval (str, len);
2006-07-06 15:54:41 +00:00
if (v == XP_NULL)
{
2006-07-09 16:06:05 +00:00
__clear_record (run, xp_false);
2006-07-06 15:54:41 +00:00
PANIC_I (run, XP_AWK_ENOMEM);
}
2006-07-07 09:48:23 +00:00
xp_assert (run->inrec.d0 == xp_awk_val_nil);
2006-07-27 16:50:29 +00:00
/* the record should be clear cleared before this function is called
* as it doesn't call xp_awk_refdownval on run->inrec.d0 */
2006-07-06 15:54:41 +00:00
run->inrec.d0 = v;
xp_awk_refupval (v);
if (__split_record (run) == -1)
{
2006-08-27 15:29:21 +00:00
errnum = run->errnum;
2006-07-09 16:06:05 +00:00
__clear_record (run, xp_false);
2006-08-27 15:29:21 +00:00
run->errnum = errnum;
2006-07-06 15:54:41 +00:00
return -1;
}
2006-07-05 16:20:23 +00:00
2006-07-27 16:50:29 +00:00
return 0; /* success */
2006-07-05 16:20:23 +00:00
}
static int __split_record (xp_awk_run_t* run)
{
/* TODO: support FS and regular expression */
2006-07-06 15:54:41 +00:00
/* TODO: set NR after split */
2006-07-05 16:20:23 +00:00
xp_char_t* p, * tok;
2006-07-07 09:48:23 +00:00
xp_size_t len, tok_len, nflds;
2006-07-12 07:25:15 +00:00
xp_awk_val_t* v;
2006-07-05 16:20:23 +00:00
2006-07-07 09:48:23 +00:00
/* inrec should be cleared before __split_record is called */
2006-07-05 16:20:23 +00:00
xp_assert (run->inrec.nflds == 0);
/* scan the input record to count the fields */
p = XP_STR_BUF(&run->inrec.line);
len = XP_STR_LEN(&run->inrec.line);
nflds = 0;
while (p != XP_NULL)
{
2006-07-07 09:48:23 +00:00
p = xp_strxtok (p, len, XP_T(" \t"), &tok, &tok_len);
if (nflds == 0 && p == XP_NULL && tok_len == 0)
{
2006-08-17 14:10:21 +00:00
/* there are no fields. it can just return here
* as __clear_record has been called before this */
2006-07-07 09:48:23 +00:00
return 0;
}
xp_assert ((tok != XP_NULL && tok_len > 0) || tok_len == 0);
2006-07-05 16:20:23 +00:00
nflds++;
2006-07-07 09:48:23 +00:00
len = XP_STR_LEN(&run->inrec.line) -
(p - XP_STR_BUF(&run->inrec.line));
2006-07-05 16:20:23 +00:00
}
/* allocate space */
2006-07-07 09:48:23 +00:00
if (nflds > run->inrec.maxflds)
{
void* tmp = xp_malloc (
xp_sizeof(*run->inrec.flds) * nflds);
if (tmp == XP_NULL) PANIC_I (run, XP_AWK_ENOMEM);
2006-07-10 14:28:46 +00:00
if (run->inrec.flds != NULL)
xp_free (run->inrec.flds);
2006-07-07 09:48:23 +00:00
run->inrec.flds = tmp;
run->inrec.maxflds = nflds;
}
2006-07-05 16:20:23 +00:00
/* scan again and split it */
p = XP_STR_BUF(&run->inrec.line);
len = XP_STR_LEN(&run->inrec.line);
while (p != XP_NULL)
{
2006-07-07 09:48:23 +00:00
p = xp_strxtok (p, len, XP_T(" \t"), &tok, &tok_len);
xp_assert ((tok != XP_NULL && tok_len > 0) || tok_len == 0);
2006-07-05 16:20:23 +00:00
run->inrec.flds[run->inrec.nflds].ptr = tok;
run->inrec.flds[run->inrec.nflds].len = tok_len;
2006-07-06 15:54:41 +00:00
run->inrec.flds[run->inrec.nflds].val =
xp_awk_makestrval (tok, tok_len);
2006-07-12 07:25:15 +00:00
2006-07-06 15:54:41 +00:00
if (run->inrec.flds[run->inrec.nflds].val == XP_NULL)
PANIC_I (run, XP_AWK_ENOMEM);
xp_awk_refupval (run->inrec.flds[run->inrec.nflds].val);
2006-07-05 16:20:23 +00:00
run->inrec.nflds++;
2006-07-06 15:54:41 +00:00
2006-07-07 09:48:23 +00:00
len = XP_STR_LEN(&run->inrec.line) -
(p - XP_STR_BUF(&run->inrec.line));
2006-07-05 16:20:23 +00:00
}
2006-07-12 07:25:15 +00:00
/* set the number of fields */
v = xp_awk_makeintval (run, (xp_long_t)nflds);
if (v == XP_NULL) PANIC_I (run, XP_AWK_ENOMEM);
2006-08-27 15:29:21 +00:00
if (xp_awk_setglobal (run, XP_AWK_GLOBAL_NF, v) == -1) return -1;
2006-07-12 07:25:15 +00:00
2006-07-05 16:20:23 +00:00
xp_assert (nflds == run->inrec.nflds);
return 0;
2006-04-22 13:54:53 +00:00
}
2006-08-27 15:29:21 +00:00
static int __clear_record (xp_awk_run_t* run, xp_bool_t noline)
2006-07-07 09:48:23 +00:00
{
xp_size_t i;
2006-08-27 15:29:21 +00:00
int n = 0;
2006-07-07 09:48:23 +00:00
xp_awk_refdownval (run, run->inrec.d0);
run->inrec.d0 = xp_awk_val_nil;
if (run->inrec.nflds > 0)
{
xp_assert (run->inrec.flds != XP_NULL);
for (i = 0; i < run->inrec.nflds; i++)
{
xp_assert (run->inrec.flds[i].val != XP_NULL);
xp_awk_refdownval (run, run->inrec.flds[i].val);
}
run->inrec.nflds = 0;
2006-08-27 15:29:21 +00:00
if (xp_awk_setglobal (
run, XP_AWK_GLOBAL_NF, xp_awk_val_zero) == -1)
{
/* first of all, this should never happen.
* if it happened, it would return an error
* after all the clearance tasks */
n = -1;
}
2006-07-07 09:48:23 +00:00
}
xp_assert (run->inrec.nflds == 0);
2006-07-09 16:06:05 +00:00
if (!noline) xp_str_clear (&run->inrec.line);
2006-08-27 15:29:21 +00:00
return n;
2006-07-07 09:48:23 +00:00
}
2006-08-27 15:29:21 +00:00
static int __recomp_record_fields (
xp_awk_run_t* run, xp_size_t lv, xp_char_t* str, xp_size_t len)
2006-07-10 14:28:46 +00:00
{
2006-07-12 07:25:15 +00:00
xp_awk_val_t* v;
2006-07-13 15:43:39 +00:00
xp_char_t* ofsp = XP_NULL, * ofs;
xp_size_t ofs_len;
xp_size_t max, i, nflds;
2006-07-10 14:28:46 +00:00
xp_assert (lv > 0);
max = (lv > run->inrec.nflds)? lv: run->inrec.nflds;
nflds = run->inrec.nflds;
if (max > run->inrec.maxflds)
{
2006-08-16 11:35:54 +00:00
void* tmp;
#ifndef XP_AWK_NTDDK
tmp = xp_realloc (
2006-07-10 14:28:46 +00:00
run->inrec.flds, xp_sizeof(*run->inrec.flds) * max);
2006-07-13 15:43:39 +00:00
if (tmp == XP_NULL)
{
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-07-13 15:43:39 +00:00
return -1;
}
2006-08-16 11:35:54 +00:00
#else
tmp = xp_malloc (xp_sizeof(*run->inrec.flds) * max);
if (tmp == XP_NULL)
{
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-08-16 11:35:54 +00:00
return -1;
}
if (run->inrec.flds != XP_NULL)
{
xp_memcpy (tmp, run->inrec.flds,
xp_sizeof(*run->inrec.flds) * run->inrec.maxflds);
xp_free (run->inrec.flds);
}
#endif
2006-07-10 14:28:46 +00:00
run->inrec.flds = tmp;
run->inrec.maxflds = max;
}
lv = lv - 1; /* adjust the value to 0-based index */
xp_str_clear (&run->inrec.line);
2006-07-13 15:43:39 +00:00
if (max > 1)
{
v = STACK_GLOBAL(run, XP_AWK_GLOBAL_OFS);
if (v != xp_awk_val_nil)
{
2006-08-03 05:05:48 +00:00
ofsp = xp_awk_valtostr (
2006-08-27 15:29:21 +00:00
v, &run->errnum, xp_true, XP_NULL, &ofs_len);
2006-07-13 15:43:39 +00:00
if (ofsp == XP_NULL) return -1;
ofs = ofsp;
}
else
{
/* OFS not set */
ofs = XP_T(" ");
ofs_len = 1;
}
}
2006-07-10 14:28:46 +00:00
for (i = 0; i < max; i++)
{
if (i > 0)
{
2006-07-13 15:43:39 +00:00
if (xp_str_ncat (
2006-07-31 15:59:43 +00:00
&run->inrec.line,
ofs, ofs_len) == (xp_size_t)-1)
2006-07-10 14:28:46 +00:00
{
2006-07-13 15:43:39 +00:00
if (ofsp != XP_NULL) xp_free (ofsp);
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-07-10 14:28:46 +00:00
return -1;
}
}
if (i == lv)
{
xp_awk_val_t* tmp;
run->inrec.flds[i].ptr =
XP_STR_BUF(&run->inrec.line) +
XP_STR_LEN(&run->inrec.line);
run->inrec.flds[i].len = len;
if (xp_str_ncat (
&run->inrec.line, str, len) == (xp_size_t)-1)
{
2006-07-13 15:43:39 +00:00
if (ofsp != XP_NULL) xp_free (ofsp);
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-07-10 14:28:46 +00:00
return -1;
}
tmp = xp_awk_makestrval (str,len);
if (tmp == XP_NULL)
{
2006-07-13 15:43:39 +00:00
if (ofsp != XP_NULL) xp_free (ofsp);
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-07-10 14:28:46 +00:00
return -1;
}
if (i < nflds)
xp_awk_refdownval (run, run->inrec.flds[i].val);
else run->inrec.nflds++;
run->inrec.flds[i].val = tmp;
xp_awk_refupval (tmp);
}
else if (i >= nflds)
{
run->inrec.flds[i].ptr =
XP_STR_BUF(&run->inrec.line) +
XP_STR_LEN(&run->inrec.line);
run->inrec.flds[i].len = 0;
if (xp_str_cat (
&run->inrec.line, XP_T("")) == (xp_size_t)-1)
{
2006-07-13 15:43:39 +00:00
if (ofsp != XP_NULL) xp_free (ofsp);
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-07-10 14:28:46 +00:00
return -1;
}
2006-07-13 15:43:39 +00:00
/* xp_awk_refdownval should not be called over
* run->inrec.flds[i].val as it is not initialized
* to any valid values */
2006-07-10 14:28:46 +00:00
/*xp_awk_refdownval (run, run->inrec.flds[i].val);*/
run->inrec.flds[i].val = xp_awk_val_zls;
xp_awk_refupval (xp_awk_val_zls);
run->inrec.nflds++;
}
else
{
xp_awk_val_str_t* tmp;
tmp = (xp_awk_val_str_t*)run->inrec.flds[i].val;
run->inrec.flds[i].ptr =
XP_STR_BUF(&run->inrec.line) +
XP_STR_LEN(&run->inrec.line);
run->inrec.flds[i].len = tmp->len;
if (xp_str_ncat (&run->inrec.line,
tmp->buf, tmp->len) == (xp_size_t)-1)
{
2006-07-13 15:43:39 +00:00
if (ofsp != XP_NULL) xp_free (ofsp);
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-07-10 14:28:46 +00:00
return -1;
}
}
}
2006-07-13 15:43:39 +00:00
if (ofsp != XP_NULL) xp_free (ofsp);
2006-07-12 07:25:15 +00:00
v = STACK_GLOBAL(run, XP_AWK_GLOBAL_NF);
xp_assert (v->type == XP_AWK_VAL_INT);
if (((xp_awk_val_int_t*)v)->val != max)
{
v = xp_awk_makeintval (run, (xp_long_t)max);
2006-07-13 15:43:39 +00:00
if (v == XP_NULL)
{
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-07-13 15:43:39 +00:00
return -1;
}
2006-08-27 15:29:21 +00:00
if (xp_awk_setglobal (
run, XP_AWK_GLOBAL_NF, v) == -1) return -1;
2006-07-12 07:25:15 +00:00
}
2006-07-10 14:28:46 +00:00
return 0;
}
2006-08-03 05:05:48 +00:00
static xp_char_t* __idxnde_to_str (
xp_awk_run_t* run, xp_awk_nde_t* nde, xp_size_t* len)
2006-05-03 15:40:19 +00:00
{
xp_char_t* str;
xp_awk_val_t* idx;
int errnum;
xp_assert (nde != XP_NULL);
if (nde->next == XP_NULL)
{
/* single node index */
idx = __eval_expression (run, nde);
if (idx == XP_NULL) return XP_NULL;
xp_awk_refupval (idx);
2006-08-03 05:05:48 +00:00
str = xp_awk_valtostr (idx, &errnum, xp_true, XP_NULL, len);
2006-05-03 15:40:19 +00:00
if (str == XP_NULL)
{
xp_awk_refdownval (run, idx);
PANIC (run, errnum);
}
xp_awk_refdownval (run, idx);
}
else
{
/* multidimensional index */
xp_str_t idxstr;
2006-06-30 03:53:16 +00:00
if (xp_str_open (&idxstr, DEF_BUF_CAPA) == XP_NULL)
2006-05-03 15:40:19 +00:00
{
PANIC (run, XP_AWK_ENOMEM);
}
while (nde != XP_NULL)
{
idx = __eval_expression (run, nde);
if (idx == XP_NULL)
{
xp_str_close (&idxstr);
return XP_NULL;
}
xp_awk_refupval (idx);
2006-07-31 15:59:43 +00:00
/* TODO: configurable index seperator, not just a comma */
2006-05-03 15:40:19 +00:00
if (XP_STR_LEN(&idxstr) > 0 &&
2006-05-06 12:52:36 +00:00
xp_str_cat (&idxstr, XP_T(",")) == (xp_size_t)-1)
2006-05-03 15:40:19 +00:00
{
2006-05-04 15:59:43 +00:00
xp_awk_refdownval (run, idx);
2006-05-03 15:40:19 +00:00
xp_str_close (&idxstr);
PANIC (run, XP_AWK_ENOMEM);
}
2006-08-03 05:05:48 +00:00
if (xp_awk_valtostr (idx, &errnum,
xp_false, &idxstr, XP_NULL) == XP_NULL)
2006-05-03 15:40:19 +00:00
{
2006-05-04 15:59:43 +00:00
xp_awk_refdownval (run, idx);
2006-05-03 15:40:19 +00:00
xp_str_close (&idxstr);
2006-05-04 15:59:43 +00:00
PANIC (run, errnum);
2006-05-03 15:40:19 +00:00
}
2006-05-04 15:59:43 +00:00
xp_awk_refdownval (run, idx);
2006-05-03 15:40:19 +00:00
nde = nde->next;
}
str = XP_STR_BUF(&idxstr);
2006-08-03 05:05:48 +00:00
*len = XP_STR_LEN(&idxstr);
2006-05-03 15:40:19 +00:00
xp_str_forfeit (&idxstr);
}
return str;
}
2006-08-10 16:06:52 +00:00