qse/ase/awk/run.c

5246 lines
124 KiB
C
Raw Normal View History

2006-01-26 15:35:20 +00:00
/*
2006-10-24 04:10:12 +00:00
* $Id: run.c,v 1.243 2006-10-24 04:10:12 bacon Exp $
2006-01-26 15:35:20 +00:00
*/
2006-10-24 04:10:12 +00:00
#include <ase/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-10-04 14:52:12 +00:00
#define CMP_ERROR -99
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))
2006-10-24 04:10:12 +00:00
#define STACK_LOCAL(run,n) STACK_AT(run,3+(ase_size_t)STACK_NARGS(run)+1+(n))
2006-04-21 17:24:31 +00:00
#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-10-24 04:10:12 +00:00
do { (run)->errnum = (code); return ASE_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-10-24 04:10:12 +00:00
#define DEFAULT_CONVFMT ASE_T("%.6g")
#define DEFAULT_OFMT ASE_T("%.6g")
#define DEFAULT_OFS ASE_T(" ")
#define DEFAULT_ORS ASE_T("\n")
#define DEFAULT_SUBSEP ASE_T("\034")
2006-09-22 14:05:30 +00:00
2006-10-01 14:48:48 +00:00
/* the index of a positional variable should be a positive interger
* equal to or less than the maximum value of the type by which
* the index is represented. but it has an extra check against the
2006-10-24 04:10:12 +00:00
* maximum value of ase_size_t as the reference is represented
* in a pointer variable of ase_awk_val_ref_t and sizeof(void*) is
* equal to sizeof(ase_size_t). */
2006-10-01 14:48:48 +00:00
#define IS_VALID_POSIDX(idx) \
((idx) >= 0 && \
2006-10-24 04:10:12 +00:00
(idx) < ASE_TYPE_MAX(ase_long_t) && \
(idx) < ASE_TYPE_MAX(ase_size_t))
2006-10-01 14:48:48 +00:00
2006-10-24 04:10:12 +00:00
static void __add_run (ase_awk_t* awk, ase_awk_run_t* run);
static void __del_run (ase_awk_t* awk, ase_awk_run_t* run);
2006-08-10 16:06:52 +00:00
static int __init_run (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_runios_t* runios, int* errnum);
static void __deinit_run (ase_awk_run_t* run);
2006-10-16 08:48:19 +00:00
2006-10-24 04:10:12 +00:00
static int __build_runarg (ase_awk_run_t* run, ase_awk_runarg_t* runarg);
static int __set_globals_to_default (ase_awk_run_t* run);
2006-04-21 17:24:31 +00:00
2006-10-24 04:10:12 +00:00
static int __run_main (ase_awk_run_t* run, ase_awk_runarg_t* runarg);
static int __run_pattern_blocks (ase_awk_run_t* run);
2006-08-13 05:55:02 +00:00
static int __run_pattern_block_chain (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_chain_t* chain);
2006-08-13 05:55:02 +00:00
static int __run_pattern_block (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_chain_t* chain, ase_size_t block_no);
static int __run_block (ase_awk_run_t* run, ase_awk_nde_blk_t* nde);
static int __run_statement (ase_awk_run_t* run, ase_awk_nde_t* nde);
static int __run_if (ase_awk_run_t* run, ase_awk_nde_if_t* nde);
static int __run_while (ase_awk_run_t* run, ase_awk_nde_while_t* nde);
static int __run_for (ase_awk_run_t* run, ase_awk_nde_for_t* nde);
static int __run_foreach (ase_awk_run_t* run, ase_awk_nde_foreach_t* nde);
static int __run_break (ase_awk_run_t* run, ase_awk_nde_break_t* nde);
static int __run_continue (ase_awk_run_t* run, ase_awk_nde_continue_t* nde);
static int __run_return (ase_awk_run_t* run, ase_awk_nde_return_t* nde);
static int __run_exit (ase_awk_run_t* run, ase_awk_nde_exit_t* nde);
static int __run_next (ase_awk_run_t* run, ase_awk_nde_next_t* nde);
static int __run_nextfile (ase_awk_run_t* run, ase_awk_nde_nextfile_t* nde);
static int __run_delete (ase_awk_run_t* run, ase_awk_nde_delete_t* nde);
static int __run_print (ase_awk_run_t* run, ase_awk_nde_print_t* nde);
static ase_awk_val_t* __eval_expression (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_expression0 (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_group (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_assignment (
ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __do_assignment (
ase_awk_run_t* run, ase_awk_nde_t* var, ase_awk_val_t* val);
static ase_awk_val_t* __do_assignment_scalar (
ase_awk_run_t* run, ase_awk_nde_var_t* var, ase_awk_val_t* val);
static ase_awk_val_t* __do_assignment_map (
ase_awk_run_t* run, ase_awk_nde_var_t* var, ase_awk_val_t* val);
static ase_awk_val_t* __do_assignment_pos (
ase_awk_run_t* run, ase_awk_nde_pos_t* pos, ase_awk_val_t* val);
static ase_awk_val_t* __eval_binary (
ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_binop_lor (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right);
static ase_awk_val_t* __eval_binop_land (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right);
static ase_awk_val_t* __eval_binop_in (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right);
static ase_awk_val_t* __eval_binop_bor (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_bxor (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_band (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_eq (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_ne (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_gt (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_ge (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_lt (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_le (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_lshift (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_rshift (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_plus (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_minus (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_mul (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_div (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_mod (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_exp (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_concat (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
static ase_awk_val_t* __eval_binop_ma (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right);
static ase_awk_val_t* __eval_binop_nm (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right);
static ase_awk_val_t* __eval_binop_match0 (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right, int ret);
static ase_awk_val_t* __eval_unary (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_incpre (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_incpst (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_cnd (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_bfn (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_afn (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_call (
ase_awk_run_t* run, ase_awk_nde_t* nde,
const ase_char_t* bfn_arg_spec, ase_awk_afn_t* afn);
2006-08-20 15:49:48 +00:00
2006-10-01 14:48:48 +00:00
static int __get_reference (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_nde_t* nde, ase_awk_val_t*** ref);
static ase_awk_val_t** __get_reference_indexed (
ase_awk_run_t* run, ase_awk_nde_var_t* nde, ase_awk_val_t** val);
static ase_awk_val_t* __eval_int (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_real (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_str (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_rex (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_named (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_global (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_local (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_arg (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_namedidx (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_globalidx (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_localidx (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_argidx (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_pos (ase_awk_run_t* run, ase_awk_nde_t* nde);
static ase_awk_val_t* __eval_getline (ase_awk_run_t* run, ase_awk_nde_t* nde);
static int __raw_push (ase_awk_run_t* run, void* val);
2006-08-18 07:52:47 +00:00
#define __raw_pop(run) \
do { \
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, (run)->stack_top > (run)->stack_base); \
2006-08-18 07:52:47 +00:00
(run)->stack_top--; \
} while (0)
2006-10-24 04:10:12 +00:00
static void __raw_pop_times (ase_awk_run_t* run, ase_size_t times);
2006-03-25 17:04:36 +00:00
2006-10-24 04:10:12 +00:00
static int __read_record (ase_awk_run_t* run);
static int __shorten_record (ase_awk_run_t* run, ase_size_t nflds);
2006-07-10 14:28:46 +00:00
2006-10-24 04:10:12 +00:00
static ase_char_t* __idxnde_to_str (
ase_awk_run_t* run, ase_awk_nde_t* nde, ase_size_t* len);
2006-04-16 04:31:38 +00:00
2006-10-24 04:10:12 +00:00
typedef ase_awk_val_t* (*binop_func_t) (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right);
typedef ase_awk_val_t* (*eval_expr_t) (ase_awk_run_t* run, ase_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-10-24 04:10:12 +00:00
static int __printval (ase_awk_pair_t* pair, void* arg)
2006-03-05 17:07:33 +00:00
{
2006-10-24 04:10:12 +00:00
xp_printf (ASE_T("%s = "), (const ase_char_t*)pair->key);
ase_awk_printval ((ase_awk_val_t*)pair->val);
xp_printf (ASE_T("\n"));
2006-03-05 17:07:33 +00:00
return 0;
}
2006-10-24 04:10:12 +00:00
ase_size_t ase_awk_getnargs (ase_awk_run_t* run)
2006-06-20 15:27:50 +00:00
{
2006-10-24 04:10:12 +00:00
return (ase_size_t) STACK_NARGS (run);
2006-06-20 15:27:50 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_val_t* ase_awk_getarg (ase_awk_run_t* run, ase_size_t idx)
2006-06-20 15:27:50 +00:00
{
2006-10-03 14:38:26 +00:00
return STACK_ARG (run, idx);
2006-06-20 15:27:50 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_val_t* ase_awk_getglobal (ase_awk_run_t* run, ase_size_t idx)
2006-06-20 15:27:50 +00:00
{
2006-10-03 14:38:26 +00:00
return STACK_GLOBAL (run, idx);
2006-06-20 15:27:50 +00:00
}
2006-10-24 04:10:12 +00:00
int ase_awk_setglobal (ase_awk_run_t* run, ase_size_t idx, ase_awk_val_t* val)
2006-07-12 07:25:15 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* old = STACK_GLOBAL (run, idx);
if (old->type == ASE_AWK_VAL_MAP)
2006-08-27 10:45:36 +00:00
{
/* once a variable becomes an array,
* it cannot be changed to a scalar variable */
2006-10-24 04:10:12 +00:00
PANIC_I (run, ASE_AWK_EMAPTOSCALAR);
2006-08-27 10:45:36 +00:00
}
2006-08-27 15:29:21 +00:00
/* TODO: is this correct?? */
2006-10-24 04:10:12 +00:00
if (val->type == ASE_AWK_VAL_MAP &&
(idx >= ASE_AWK_GLOBAL_ARGC && idx <= ASE_AWK_GLOBAL_SUBSEP) &&
idx != ASE_AWK_GLOBAL_ARGV)
2006-08-27 15:29:21 +00:00
{
/* TODO: better error code */
2006-10-24 04:10:12 +00:00
PANIC_I (run, ASE_AWK_ESCALARTOMAP);
2006-08-27 15:29:21 +00:00
}
2006-10-24 04:10:12 +00:00
if (idx == ASE_AWK_GLOBAL_CONVFMT)
2006-10-11 15:02:26 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* convfmt_ptr;
ase_size_t convfmt_len;
2006-10-11 15:02:26 +00:00
2006-10-24 04:10:12 +00:00
convfmt_ptr = ase_awk_valtostr (
run, val, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &convfmt_len);
if (convfmt_ptr == ASE_NULL) return -1;
2006-10-11 15:02:26 +00:00
2006-10-24 04:10:12 +00:00
if (run->global.convfmt.ptr != ASE_NULL)
ASE_AWK_FREE (run->awk, run->global.convfmt.ptr);
2006-10-11 15:02:26 +00:00
run->global.convfmt.ptr = convfmt_ptr;
run->global.convfmt.len = convfmt_len;
}
2006-10-24 04:10:12 +00:00
else if (idx == ASE_AWK_GLOBAL_FS)
2006-09-05 04:11:11 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* fs_ptr;
ase_size_t fs_len;
2006-09-05 04:11:11 +00:00
2006-10-24 04:10:12 +00:00
if (val->type == ASE_AWK_VAL_STR)
2006-09-05 04:11:11 +00:00
{
2006-10-24 04:10:12 +00:00
fs_ptr = ((ase_awk_val_str_t*)val)->buf;
fs_len = ((ase_awk_val_str_t*)val)->len;
2006-09-05 04:11:11 +00:00
}
else
{
2006-10-22 12:39:30 +00:00
/* due to the expression evaluation rule, the
* regular expression can not be an assigned value */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, val->type != ASE_AWK_VAL_REX);
2006-09-05 04:11:11 +00:00
2006-10-24 04:10:12 +00:00
fs_ptr = ase_awk_valtostr (
run, val, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &fs_len);
if (fs_ptr == ASE_NULL) return -1;
2006-09-05 04:11:11 +00:00
}
if (fs_len > 1)
{
void* rex;
2006-10-22 12:39:30 +00:00
/* compile the regular expression */
2006-09-05 04:11:11 +00:00
/* TODO: use safebuild */
2006-10-24 04:10:12 +00:00
rex = ase_awk_buildrex (
2006-10-03 14:38:26 +00:00
run->awk, fs_ptr, fs_len, &run->errnum);
2006-10-24 04:10:12 +00:00
if (rex == ASE_NULL)
2006-09-05 04:11:11 +00:00
{
2006-10-24 04:10:12 +00:00
if (val->type != ASE_AWK_VAL_STR)
ASE_AWK_FREE (run->awk, fs_ptr);
2006-09-05 04:11:11 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (run->global.fs != ASE_NULL)
2006-09-05 04:11:11 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_freerex (run->awk, run->global.fs);
2006-09-05 04:11:11 +00:00
}
2006-10-03 14:38:26 +00:00
run->global.fs = rex;
2006-09-05 04:11:11 +00:00
}
2006-10-24 04:10:12 +00:00
if (val->type != ASE_AWK_VAL_STR) ASE_AWK_FREE (run->awk, fs_ptr);
2006-09-05 04:11:11 +00:00
}
2006-10-24 04:10:12 +00:00
else if (idx == ASE_AWK_GLOBAL_IGNORECASE)
2006-09-08 15:26:49 +00:00
{
2006-10-24 04:10:12 +00:00
if ((val->type == ASE_AWK_VAL_INT &&
((ase_awk_val_int_t*)val)->val == 0) ||
(val->type == ASE_AWK_VAL_REAL &&
((ase_awk_val_real_t*)val)->val == 0.0) ||
(val->type == ASE_AWK_VAL_STR &&
((ase_awk_val_str_t*)val)->len == 0))
2006-09-08 15:26:49 +00:00
{
2006-10-03 14:38:26 +00:00
run->global.ignorecase = 0;
2006-09-08 15:26:49 +00:00
}
else
{
2006-10-03 14:38:26 +00:00
run->global.ignorecase = 1;
2006-09-08 15:26:49 +00:00
}
}
2006-10-24 04:10:12 +00:00
else if (idx == ASE_AWK_GLOBAL_NF)
2006-09-10 15:50:34 +00:00
{
int n;
2006-10-24 04:10:12 +00:00
ase_long_t lv;
ase_real_t rv;
2006-08-27 15:29:21 +00:00
2006-10-24 04:10:12 +00:00
n = ase_awk_valtonum (run, val, &lv, &rv);
2006-09-10 15:50:34 +00:00
if (n == -1) return -1;
2006-10-24 04:10:12 +00:00
if (n == 1) lv = (ase_long_t)rv;
2006-09-10 15:50:34 +00:00
2006-10-03 14:38:26 +00:00
if (lv < run->inrec.nflds)
2006-09-10 15:50:34 +00:00
{
2006-10-24 04:10:12 +00:00
if (__shorten_record (run, (ase_size_t)lv) == -1) return -1;
2006-09-10 15:50:34 +00:00
}
}
2006-10-24 04:10:12 +00:00
else if (idx == ASE_AWK_GLOBAL_OFMT)
2006-09-29 11:18:13 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* ofmt_ptr;
ase_size_t ofmt_len;
2006-09-29 11:18:13 +00:00
2006-10-24 04:10:12 +00:00
ofmt_ptr = ase_awk_valtostr (
run, val, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &ofmt_len);
if (ofmt_ptr == ASE_NULL) return -1;
2006-09-29 11:18:13 +00:00
2006-10-24 04:10:12 +00:00
if (run->global.ofmt.ptr != ASE_NULL)
ASE_AWK_FREE (run->awk, run->global.ofmt.ptr);
2006-10-03 14:38:26 +00:00
run->global.ofmt.ptr = ofmt_ptr;
run->global.ofmt.len = ofmt_len;
2006-09-29 11:18:13 +00:00
}
2006-10-24 04:10:12 +00:00
else if (idx == ASE_AWK_GLOBAL_OFS)
2006-09-22 14:05:30 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* ofs_ptr;
ase_size_t ofs_len;
2006-09-22 14:05:30 +00:00
2006-10-24 04:10:12 +00:00
ofs_ptr = ase_awk_valtostr (
run, val, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &ofs_len);
if (ofs_ptr == ASE_NULL) return -1;
2006-09-22 14:05:30 +00:00
2006-10-24 04:10:12 +00:00
if (run->global.ofs.ptr != ASE_NULL)
ASE_AWK_FREE (run->awk, run->global.ofs.ptr);
2006-10-03 14:38:26 +00:00
run->global.ofs.ptr = ofs_ptr;
run->global.ofs.len = ofs_len;
2006-09-22 14:05:30 +00:00
}
2006-10-24 04:10:12 +00:00
else if (idx == ASE_AWK_GLOBAL_ORS)
2006-09-29 11:18:13 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* ors_ptr;
ase_size_t ors_len;
2006-09-29 11:18:13 +00:00
2006-10-24 04:10:12 +00:00
ors_ptr = ase_awk_valtostr (
run, val, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &ors_len);
if (ors_ptr == ASE_NULL) return -1;
2006-09-29 11:18:13 +00:00
2006-10-24 04:10:12 +00:00
if (run->global.ors.ptr != ASE_NULL)
ASE_AWK_FREE (run->awk, run->global.ors.ptr);
2006-10-03 14:38:26 +00:00
run->global.ors.ptr = ors_ptr;
run->global.ors.len = ors_len;
2006-09-29 11:18:13 +00:00
}
2006-10-24 04:10:12 +00:00
else if (idx == ASE_AWK_GLOBAL_RS)
2006-09-29 11:18:13 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* rs_ptr;
ase_size_t rs_len;
2006-09-29 11:18:13 +00:00
2006-10-24 04:10:12 +00:00
if (val->type == ASE_AWK_VAL_STR)
2006-09-29 11:18:13 +00:00
{
2006-10-24 04:10:12 +00:00
rs_ptr = ((ase_awk_val_str_t*)val)->buf;
rs_len = ((ase_awk_val_str_t*)val)->len;
2006-09-29 11:18:13 +00:00
}
else
{
2006-10-22 12:39:30 +00:00
/* due to the expression evaluation rule, the
* regular expression can not be an assigned value */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, val->type != ASE_AWK_VAL_REX);
2006-09-29 11:18:13 +00:00
2006-10-24 04:10:12 +00:00
rs_ptr = ase_awk_valtostr (
run, val, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &rs_len);
if (rs_ptr == ASE_NULL) return -1;
2006-09-29 11:18:13 +00:00
}
if (rs_len > 1)
{
void* rex;
2006-10-22 12:39:30 +00:00
/* compile the regular expression */
2006-09-29 11:18:13 +00:00
/* TODO: use safebuild */
2006-10-24 04:10:12 +00:00
rex = ase_awk_buildrex (
2006-10-03 14:38:26 +00:00
run->awk, rs_ptr, rs_len, &run->errnum);
2006-10-24 04:10:12 +00:00
if (rex == ASE_NULL)
2006-09-29 11:18:13 +00:00
{
2006-10-24 04:10:12 +00:00
if (val->type != ASE_AWK_VAL_STR)
ASE_AWK_FREE (run->awk, rs_ptr);
2006-09-29 11:18:13 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (run->global.rs != ASE_NULL)
2006-09-29 11:18:13 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_freerex (run->awk, run->global.rs);
2006-09-29 11:18:13 +00:00
}
2006-10-03 14:38:26 +00:00
run->global.rs = rex;
2006-09-29 11:18:13 +00:00
}
2006-10-24 04:10:12 +00:00
if (val->type != ASE_AWK_VAL_STR) ASE_AWK_FREE (run->awk, rs_ptr);
2006-09-29 11:18:13 +00:00
}
2006-10-24 04:10:12 +00:00
else if (idx == ASE_AWK_GLOBAL_SUBSEP)
2006-09-22 14:05:30 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* subsep_ptr;
ase_size_t subsep_len;
2006-09-22 14:05:30 +00:00
2006-10-24 04:10:12 +00:00
subsep_ptr = ase_awk_valtostr (
run, val, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &subsep_len);
if (subsep_ptr == ASE_NULL) return -1;
2006-09-22 14:05:30 +00:00
2006-10-24 04:10:12 +00:00
if (run->global.subsep.ptr != ASE_NULL)
ASE_AWK_FREE (run->awk, run->global.subsep.ptr);
2006-10-03 14:38:26 +00:00
run->global.subsep.ptr = subsep_ptr;
run->global.subsep.len = subsep_len;
2006-09-22 14:05:30 +00:00
}
2006-08-27 15:29:21 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, old);
2006-10-03 14:38:26 +00:00
STACK_GLOBAL(run,idx) = val;
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val);
2006-08-27 10:45:36 +00:00
return 0;
2006-07-12 07:25:15 +00:00
}
2006-10-24 04:10:12 +00:00
void ase_awk_setretval (ase_awk_run_t* run, ase_awk_val_t* val)
2006-08-24 03:30:32 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, STACK_RETVAL(run));
2006-10-03 14:38:26 +00:00
STACK_RETVAL(run) = val;
2006-08-24 03:30:32 +00:00
/* should use the same trick as __run_return_statement */
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val);
2006-08-24 03:30:32 +00:00
}
2006-10-24 04:10:12 +00:00
int ase_awk_setconsolename (
ase_awk_run_t* run, const ase_char_t* name, ase_size_t len)
2006-10-16 14:39:21 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tmp;
2006-10-16 14:39:21 +00:00
int n;
2006-10-24 04:10:12 +00:00
if (len == 0) tmp = ase_awk_val_zls;
2006-10-16 14:39:21 +00:00
else
{
2006-10-24 04:10:12 +00:00
tmp = ase_awk_makestrval (run, name, len);
if (tmp == ASE_NULL)
2006-10-16 14:39:21 +00:00
{
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_ENOMEM;
2006-10-16 14:39:21 +00:00
return -1;
}
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
n = ase_awk_setglobal (run, ASE_AWK_GLOBAL_FILENAME, tmp);
ase_awk_refdownval (run, tmp);
2006-10-16 14:39:21 +00:00
return n;
}
2006-10-24 04:10:12 +00:00
int ase_awk_getrunerrnum (ase_awk_run_t* run)
2006-10-10 07:02:38 +00:00
{
return run->errnum;
}
2006-10-24 04:10:12 +00:00
void ase_awk_setrunerrnum (ase_awk_run_t* run, int errnum)
2006-10-10 07:02:38 +00:00
{
run->errnum = errnum;
}
2006-10-24 04:10:12 +00:00
int ase_awk_run (ase_awk_t* awk,
ase_awk_runios_t* runios,
ase_awk_runcbs_t* runcbs,
ase_awk_runarg_t* runarg)
2006-04-21 17:24:31 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run;
2006-08-10 16:06:52 +00:00
int n, errnum;
2006-10-24 04:10:12 +00:00
awk->errnum = ASE_AWK_ENOERR;
2006-04-21 17:24:31 +00:00
2006-10-24 04:10:12 +00:00
run = (ase_awk_run_t*) ASE_AWK_MALLOC (awk, ase_sizeof(ase_awk_run_t));
if (run == ASE_NULL)
2006-04-22 13:54:53 +00:00
{
2006-10-24 04:10:12 +00:00
awk->errnum = ASE_AWK_ENOMEM;
2006-04-22 13:54:53 +00:00
return -1;
}
2006-04-21 17:24:31 +00:00
2006-10-24 04:10:12 +00:00
ASE_AWK_MEMSET (awk, run, 0, ase_sizeof(ase_awk_run_t));
2006-10-16 08:48:19 +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-10-24 04:10:12 +00:00
ASE_AWK_FREE (awk, run);
2006-04-22 13:54:53 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (runcbs != ASE_NULL && runcbs->on_start != ASE_NULL)
2006-08-13 05:55:02 +00:00
{
runcbs->on_start (awk, run, runcbs->custom_data);
}
2006-08-04 16:31:22 +00:00
2006-10-11 03:19:08 +00:00
n = __run_main (run, runarg);
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 */
2006-10-24 04:10:12 +00:00
awk->errnum = (runcbs == ASE_NULL)? run->errnum: ASE_AWK_ERUNTIME;
2006-08-13 05:55:02 +00:00
}
2006-04-22 13:54:53 +00:00
2006-10-24 04:10:12 +00:00
if (runcbs != ASE_NULL && runcbs->on_end != ASE_NULL)
2006-08-13 05:55:02 +00:00
{
runcbs->on_end (awk, run,
2006-10-24 04:10:12 +00:00
((n == -1)? run->errnum: ASE_AWK_ENOERR),
2006-08-13 05:55:02 +00:00
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-10-24 04:10:12 +00:00
ASE_AWK_FREE (awk, run);
2006-04-22 13:54:53 +00:00
return n;
2006-04-21 17:24:31 +00:00
}
2006-10-24 04:10:12 +00:00
int ase_awk_stop (ase_awk_t* awk, ase_awk_run_t* run)
2006-08-04 16:31:22 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_run_t* r;
2006-08-10 16:06:52 +00:00
int n = 0;
2006-08-04 16:31:22 +00:00
2006-10-24 04:10:12 +00:00
ASE_AWK_LOCK (awk);
2006-08-10 16:06:52 +00:00
/* check if the run handle given is valid */
2006-10-24 04:10:12 +00:00
for (r = awk->run.ptr; r != ASE_NULL; r = r->next)
2006-08-04 16:31:22 +00:00
{
2006-08-10 16:06:52 +00:00
if (r == run)
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, r->awk == awk);
2006-08-10 16:06:52 +00:00
r->exit_level = EXIT_ABORT;
break;
}
2006-08-04 16:31:22 +00:00
}
2006-10-24 04:10:12 +00:00
if (r == ASE_NULL)
2006-08-10 16:06:52 +00:00
{
/* if it is not found in the awk's run list,
* it is not a valid handle */
2006-10-24 04:10:12 +00:00
awk->errnum = ASE_AWK_EINVAL;
2006-08-10 16:06:52 +00:00
n = -1;
}
2006-10-24 04:10:12 +00:00
ASE_AWK_UNLOCK (awk);
2006-08-10 16:06:52 +00:00
return n;
}
2006-10-24 04:10:12 +00:00
void ase_awk_stopall (ase_awk_t* awk)
2006-08-13 05:55:02 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_run_t* r;
2006-08-13 05:55:02 +00:00
2006-10-24 04:10:12 +00:00
ASE_AWK_LOCK (awk);
2006-08-13 05:55:02 +00:00
2006-10-24 04:10:12 +00:00
for (r = awk->run.ptr; r != ASE_NULL; r = r->next)
2006-08-13 05:55:02 +00:00
{
r->exit_level = EXIT_ABORT;
}
2006-10-24 04:10:12 +00:00
ASE_AWK_UNLOCK (awk);
2006-08-13 05:55:02 +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-10-24 04:10:12 +00:00
ase_awk_refdownval ((ase_awk_run_t*)run, val);
2006-04-21 17:24:31 +00:00
}
2006-10-24 04:10:12 +00:00
static void __add_run (ase_awk_t* awk, ase_awk_run_t* run)
2006-04-21 17:24:31 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_LOCK (awk);
2006-08-10 16:06:52 +00:00
run->awk = awk;
2006-10-24 04:10:12 +00:00
run->prev = ASE_NULL;
2006-08-10 16:06:52 +00:00
run->next = awk->run.ptr;
2006-10-24 04:10:12 +00:00
if (run->next != ASE_NULL) run->next->prev = run;
2006-08-10 16:06:52 +00:00
awk->run.ptr = run;
awk->run.count++;
2006-10-24 04:10:12 +00:00
ASE_AWK_UNLOCK (awk);
2006-08-10 16:06:52 +00:00
}
2006-10-24 04:10:12 +00:00
static void __del_run (ase_awk_t* awk, ase_awk_run_t* run)
2006-08-10 16:06:52 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_LOCK (awk);
2006-08-10 16:06:52 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, awk->run.ptr != ASE_NULL);
2006-08-10 16:06:52 +00:00
2006-10-24 04:10:12 +00:00
if (run->prev == ASE_NULL)
2006-08-10 16:06:52 +00:00
{
awk->run.ptr = run->next;
2006-10-24 04:10:12 +00:00
if (run->next != ASE_NULL) run->next->prev = ASE_NULL;
2006-08-10 16:06:52 +00:00
}
else
{
run->prev->next = run->next;
2006-10-24 04:10:12 +00:00
if (run->next != ASE_NULL) run->next->prev = run->prev;
2006-08-10 16:06:52 +00:00
}
2006-06-16 07:37:27 +00:00
2006-10-24 04:10:12 +00:00
run->awk = ASE_NULL;
2006-08-10 16:06:52 +00:00
awk->run.count--;
2006-10-24 04:10:12 +00:00
ASE_AWK_UNLOCK (awk);
2006-08-10 16:06:52 +00:00
}
2006-10-24 04:10:12 +00:00
static int __init_run (ase_awk_run_t* run, ase_awk_runios_t* runios, int* errnum)
2006-08-10 16:06:52 +00:00
{
2006-10-24 04:10:12 +00:00
run->stack = ASE_NULL;
2006-04-21 17:24:31 +00:00
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-10-24 04:10:12 +00:00
run->errnum = ASE_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;
2006-10-24 04:10:12 +00:00
run->inrec.flds = ASE_NULL;
2006-07-03 04:09:56 +00:00
run->inrec.nflds = 0;
2006-07-07 09:48:23 +00:00
run->inrec.maxflds = 0;
2006-10-24 04:10:12 +00:00
run->inrec.d0 = ase_awk_val_nil;
if (ase_awk_str_open (
&run->inrec.line, DEF_BUF_CAPA, run->awk) == ASE_NULL)
2006-04-22 13:54:53 +00:00
{
2006-10-24 04:10:12 +00:00
*errnum = ASE_AWK_ENOMEM;
2006-04-22 13:54:53 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (ase_awk_map_open (&run->named,
run, DEF_BUF_CAPA, __free_namedval, run->awk) == ASE_NULL)
2006-04-21 17:24:31 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_str_close (&run->inrec.line);
*errnum = ASE_AWK_ENOMEM;
2006-04-21 17:24:31 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
run->pattern_range_state = (ase_byte_t*) ASE_AWK_MALLOC (
run->awk, run->awk->tree.chain_size * ase_sizeof(ase_byte_t));
if (run->pattern_range_state == ASE_NULL)
2006-08-13 05:55:02 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_map_close (&run->named);
ase_awk_str_close (&run->inrec.line);
*errnum = ASE_AWK_ENOMEM;
2006-08-13 05:55:02 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ASE_AWK_MEMSET (run->awk, run->pattern_range_state, 0,
run->awk->tree.chain_size * ase_sizeof(ase_byte_t));
2006-09-01 03:44:51 +00:00
2006-10-24 04:10:12 +00:00
run->extio.handler[ASE_AWK_EXTIO_PIPE] = runios->pipe;
run->extio.handler[ASE_AWK_EXTIO_COPROC] = runios->coproc;
run->extio.handler[ASE_AWK_EXTIO_FILE] = runios->file;
run->extio.handler[ASE_AWK_EXTIO_CONSOLE] = runios->console;
2006-10-13 10:18:39 +00:00
run->extio.custom_data = runios->custom_data;
2006-10-24 04:10:12 +00:00
run->extio.chain = ASE_NULL;
2006-09-05 04:11:11 +00:00
2006-10-24 04:10:12 +00:00
run->global.rs = ASE_NULL;
run->global.fs = ASE_NULL;
2006-09-22 14:05:30 +00:00
run->global.ignorecase = 0;
2006-08-10 16:06:52 +00:00
2006-04-21 17:24:31 +00:00
return 0;
}
2006-10-24 04:10:12 +00:00
static void __deinit_run (ase_awk_run_t* run)
2006-04-21 17:24:31 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->pattern_range_state);
2006-08-13 05:55:02 +00:00
2006-06-21 15:37:51 +00:00
/* close all pending eio's */
/* TODO: what if this operation fails? */
2006-10-24 04:10:12 +00:00
ase_awk_clearextio (run);
ase_awk_assert (run->awk, run->extio.chain == ASE_NULL);
2006-09-05 04:11:11 +00:00
2006-10-24 04:10:12 +00:00
if (run->global.rs != ASE_NULL)
2006-09-05 04:11:11 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->global.rs);
run->global.rs = ASE_NULL;
2006-09-05 04:11:11 +00:00
}
2006-10-24 04:10:12 +00:00
if (run->global.fs != ASE_NULL)
2006-09-22 14:05:30 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->global.fs);
run->global.fs = ASE_NULL;
2006-09-22 14:05:30 +00:00
}
2006-10-24 04:10:12 +00:00
if (run->global.convfmt.ptr != ASE_NULL &&
2006-10-11 15:02:26 +00:00
run->global.convfmt.ptr != DEFAULT_CONVFMT)
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->global.convfmt.ptr);
run->global.convfmt.ptr = ASE_NULL;
2006-10-11 15:02:26 +00:00
run->global.convfmt.len = 0;
}
2006-10-24 04:10:12 +00:00
if (run->global.ofmt.ptr != ASE_NULL &&
2006-09-29 11:18:13 +00:00
run->global.ofmt.ptr != DEFAULT_OFMT)
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->global.ofmt.ptr);
run->global.ofmt.ptr = ASE_NULL;
2006-09-29 11:18:13 +00:00
run->global.ofmt.len = 0;
}
2006-10-24 04:10:12 +00:00
if (run->global.ofs.ptr != ASE_NULL &&
2006-09-25 14:53:10 +00:00
run->global.ofs.ptr != DEFAULT_OFS)
2006-08-29 15:01:45 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->global.ofs.ptr);
run->global.ofs.ptr = ASE_NULL;
2006-09-22 14:05:30 +00:00
run->global.ofs.len = 0;
}
2006-10-24 04:10:12 +00:00
if (run->global.ors.ptr != ASE_NULL &&
2006-09-29 11:18:13 +00:00
run->global.ors.ptr != DEFAULT_ORS)
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->global.ors.ptr);
run->global.ors.ptr = ASE_NULL;
2006-09-29 11:18:13 +00:00
run->global.ors.len = 0;
}
2006-10-24 04:10:12 +00:00
if (run->global.subsep.ptr != ASE_NULL &&
2006-09-22 14:05:30 +00:00
run->global.subsep.ptr != DEFAULT_SUBSEP)
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->global.subsep.ptr);
run->global.subsep.ptr = ASE_NULL;
2006-09-22 14:05:30 +00:00
run->global.subsep.len = 0;
2006-08-29 15:01:45 +00:00
}
2006-06-21 15:37:51 +00:00
2006-10-24 04:10:12 +00:00
/* destroy input record. ase_awk_clrrec should be called
2006-07-25 16:41:40 +00:00
* before the run stack has been destroyed because it may try
2006-10-24 04:10:12 +00:00
* to change the value to ASE_AWK_GLOBAL_NF. */
ase_awk_clrrec (run, ase_false);
if (run->inrec.flds != ASE_NULL)
2006-07-25 16:41:40 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->inrec.flds);
run->inrec.flds = ASE_NULL;
2006-07-25 16:41:40 +00:00
run->inrec.maxflds = 0;
}
2006-10-24 04:10:12 +00:00
ase_awk_str_close (&run->inrec.line);
2006-07-25 16:41:40 +00:00
2006-04-21 17:24:31 +00:00
/* destroy run stack */
2006-10-24 04:10:12 +00:00
if (run->stack != ASE_NULL)
2006-04-21 17:24:31 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, run->stack_top == 0);
2006-04-21 17:24:31 +00:00
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->stack);
run->stack = ASE_NULL;
2006-04-21 17:24:31 +00:00
run->stack_top = 0;
run->stack_base = 0;
run->stack_limit = 0;
}
/* destroy named variables */
2006-10-24 04:10:12 +00:00
ase_awk_map_close (&run->named);
2006-04-21 17:24:31 +00:00
/* destroy values in free list */
while (run->icache_count > 0)
{
2006-10-24 04:10:12 +00:00
ase_awk_val_int_t* tmp = run->icache[--run->icache_count];
ase_awk_freeval (run, (ase_awk_val_t*)tmp, ase_false);
2006-04-21 17:24:31 +00:00
}
while (run->rcache_count > 0)
{
2006-10-24 04:10:12 +00:00
ase_awk_val_real_t* tmp = run->rcache[--run->rcache_count];
ase_awk_freeval (run, (ase_awk_val_t*)tmp, ase_false);
2006-04-21 17:24:31 +00:00
}
2006-08-21 02:53:42 +00:00
while (run->fcache_count > 0)
{
2006-10-24 04:10:12 +00:00
ase_awk_val_ref_t* tmp = run->fcache[--run->fcache_count];
ase_awk_freeval (run, (ase_awk_val_t*)tmp, ase_false);
2006-08-21 02:53:42 +00:00
}
2006-04-21 17:24:31 +00:00
}
2006-10-24 04:10:12 +00:00
static int __build_runarg (ase_awk_run_t* run, ase_awk_runarg_t* runarg)
2006-10-10 14:09:23 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_runarg_t* p = runarg;
ase_size_t argc;
ase_awk_val_t* v_argc;
ase_awk_val_t* v_argv;
ase_awk_val_t* v_tmp;
ase_char_t key[ase_sizeof(ase_long_t)*8+2];
ase_size_t key_len;
v_argv = ase_awk_makemapval (run);
if (v_argv == ASE_NULL)
{
run->errnum = ASE_AWK_ENOMEM;
2006-10-10 14:09:23 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v_argv);
2006-10-10 14:09:23 +00:00
2006-10-24 04:10:12 +00:00
if (runarg == ASE_NULL) argc = 0;
2006-10-13 10:18:39 +00:00
else
2006-10-10 14:09:23 +00:00
{
2006-10-24 04:10:12 +00:00
for (argc = 0, p = runarg; p->ptr != ASE_NULL; argc++, p++)
2006-10-11 03:19:08 +00:00
{
2006-10-24 04:10:12 +00:00
v_tmp = ase_awk_makestrval (run, p->ptr, p->len);
if (v_tmp == ASE_NULL)
2006-10-13 10:18:39 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v_argv);
run->errnum = ASE_AWK_ENOMEM;
2006-10-13 10:18:39 +00:00
return -1;
}
2006-10-11 03:19:08 +00:00
2006-10-24 04:10:12 +00:00
key_len = ase_awk_longtostr (
argc, 10, ASE_NULL, key, ase_countof(key));
ase_awk_assert (run->awk, key_len != (ase_size_t)-1);
2006-10-11 03:19:08 +00:00
2006-10-13 10:18:39 +00:00
/* increment reference count of v_tmp in advance as if
* it has successfully been assigned into ARGV. */
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v_tmp);
2006-10-11 03:19:08 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_map_putx (
((ase_awk_val_map_t*)v_argv)->map,
key, key_len, v_tmp, ASE_NULL) == -1)
2006-10-13 10:18:39 +00:00
{
/* if the assignment operation fails, decrements
* the reference of v_tmp to free it */
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v_tmp);
2006-10-11 03:19:08 +00:00
2006-10-13 10:18:39 +00:00
/* the values previously assigned into the
* map will be freeed when v_argv is freed */
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v_argv);
2006-10-11 03:19:08 +00:00
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_ENOMEM;
2006-10-13 10:18:39 +00:00
return -1;
}
2006-10-11 03:19:08 +00:00
}
2006-10-10 14:09:23 +00:00
}
2006-10-24 04:10:12 +00:00
v_argc = ase_awk_makeintval (run, (ase_long_t)argc);
if (v_argc == ASE_NULL)
2006-10-10 14:09:23 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v_argv);
run->errnum = ASE_AWK_ENOMEM;
2006-10-10 14:09:23 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v_argc);
2006-10-10 14:09:23 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk,
STACK_GLOBAL(run,ASE_AWK_GLOBAL_ARGC) == ase_awk_val_nil);
2006-10-11 03:19:08 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (run, ASE_AWK_GLOBAL_ARGC, v_argc) == -1)
2006-10-10 14:09:23 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v_argc);
ase_awk_refdownval (run, v_argv);
2006-10-10 14:09:23 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (run, ASE_AWK_GLOBAL_ARGV, v_argv) == -1)
2006-10-10 14:09:23 +00:00
{
2006-10-11 03:19:08 +00:00
/* ARGC is assigned nil when ARGV assignment has failed.
* However, this requires preconditions, as follows:
* 1. __build_runarg should be called in a proper place
* as it is not a generic-purpose routine.
* 2. ARGC should be nil before __build_runarg is called
* If the restoration fails, nothing can salvage it. */
2006-10-24 04:10:12 +00:00
ase_awk_setglobal (run, ASE_AWK_GLOBAL_ARGC, ase_awk_val_nil);
ase_awk_refdownval (run, v_argc);
ase_awk_refdownval (run, v_argv);
2006-10-10 14:09:23 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v_argc);
ase_awk_refdownval (run, v_argv);
2006-10-10 14:09:23 +00:00
return 0;
}
2006-10-24 04:10:12 +00:00
static int __update_fnr (ase_awk_run_t* run, ase_size_t fnr)
2006-10-17 09:36:09 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tmp;
2006-10-17 09:36:09 +00:00
2006-10-24 04:10:12 +00:00
tmp = ase_awk_makeintval (run, fnr);
if (tmp == ASE_NULL)
2006-10-17 09:36:09 +00:00
{
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_ENOMEM;
2006-10-17 09:36:09 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
if (ase_awk_setglobal (run, ASE_AWK_GLOBAL_FNR, tmp) == -1)
2006-10-17 09:36:09 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, tmp);
2006-10-17 09:36:09 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, tmp);
2006-10-17 09:36:09 +00:00
run->global.fnr = fnr;
return 0;
}
2006-10-24 04:10:12 +00:00
static int __set_globals_to_default (ase_awk_run_t* run)
2006-10-16 08:48:19 +00:00
{
struct __gtab_t
{
int idx;
2006-10-24 04:10:12 +00:00
const ase_char_t* str;
2006-10-16 08:48:19 +00:00
};
static struct __gtab_t gtab[] =
{
2006-10-24 04:10:12 +00:00
{ ASE_AWK_GLOBAL_CONVFMT, DEFAULT_CONVFMT },
{ ASE_AWK_GLOBAL_FILENAME, ASE_NULL },
{ ASE_AWK_GLOBAL_OFMT, DEFAULT_OFMT },
{ ASE_AWK_GLOBAL_OFS, DEFAULT_OFS },
{ ASE_AWK_GLOBAL_ORS, DEFAULT_ORS },
{ ASE_AWK_GLOBAL_SUBSEP, DEFAULT_SUBSEP },
2006-10-16 08:48:19 +00:00
};
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tmp;
ase_size_t i, j;
2006-10-16 08:48:19 +00:00
2006-10-24 04:10:12 +00:00
for (i = 0; i < ase_countof(gtab); i++)
2006-10-16 08:48:19 +00:00
{
2006-10-24 04:10:12 +00:00
if (gtab[i].str == ASE_NULL || gtab[i].str[0] == ASE_T('\0'))
2006-10-16 08:48:19 +00:00
{
2006-10-24 04:10:12 +00:00
tmp = ase_awk_val_zls;
2006-10-16 14:39:21 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
tmp = ase_awk_makestrval0 (run, gtab[i].str);
if (tmp == ASE_NULL)
2006-10-16 14:39:21 +00:00
{
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_ENOMEM;
2006-10-16 14:39:21 +00:00
return -1;
}
2006-10-16 08:48:19 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
2006-10-16 08:48:19 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk,
STACK_GLOBAL(run,gtab[i].idx) == ase_awk_val_nil);
2006-10-16 08:48:19 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (run, gtab[i].idx, tmp) == -1)
2006-10-16 08:48:19 +00:00
{
for (j = 0; j < i; j++)
{
2006-10-24 04:10:12 +00:00
ase_awk_setglobal (
run, gtab[i].idx, ase_awk_val_nil);
2006-10-16 08:48:19 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, tmp);
2006-10-16 08:48:19 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, tmp);
2006-10-16 08:48:19 +00:00
}
return 0;
}
2006-10-24 04:10:12 +00:00
static int __run_main (ase_awk_run_t* run, ase_awk_runarg_t* runarg)
2006-01-26 15:35:20 +00:00
{
2006-10-24 04:10:12 +00:00
ase_size_t nglobals, nargs, i;
ase_size_t saved_stack_top;
ase_awk_val_t* v;
2006-10-16 08:48:19 +00:00
int n;
2006-03-28 11:32:58 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, 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-10-24 04:10:12 +00:00
if (__raw_push(run,ase_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
2006-10-24 04:10:12 +00:00
* the values pushed so far are all ase_awk_val_nil */
2006-04-21 17:24:31 +00:00
run->stack_top = saved_stack_top;
2006-10-24 04:10:12 +00:00
PANIC_I (run, ASE_AWK_ENOMEM);
2006-03-28 11:32:58 +00:00
}
}
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (run, ASE_AWK_GLOBAL_NR, ase_awk_val_zero) == -1)
2006-08-30 07:15:14 +00:00
{
/* it can simply restore the top of the stack this way
* because the values pused onto the stack so far are
2006-10-24 04:10:12 +00:00
* all ase_awk_val_nils */
2006-08-30 07:15:14 +00:00
run->stack_top = saved_stack_top;
return -1;
}
2006-10-16 08:48:19 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (run, ASE_AWK_GLOBAL_NF, ase_awk_val_zero) == -1)
2006-09-02 14:59:15 +00:00
{
/* it can simply restore the top of the stack this way
* because the values pused onto the stack so far are
2006-10-24 04:10:12 +00:00
* all ase_awk_val_nils and ase_awk_val_zeros */
2006-09-02 14:59:15 +00:00
run->stack_top = saved_stack_top;
return -1;
}
2006-10-11 03:19:08 +00:00
2006-10-24 04:10:12 +00:00
if (runarg != ASE_NULL && __build_runarg (run, runarg) == -1)
2006-10-11 03:19:08 +00:00
{
/* it can simply restore the top of the stack this way
* because the values pused onto the stack so far are
2006-10-24 04:10:12 +00:00
* all ase_awk_val_nils and ase_awk_val_zeros and
2006-10-11 03:19:08 +00:00
* __build_runarg doesn't push other values than them
* when it has failed */
run->stack_top = saved_stack_top;
return -1;
}
2006-08-30 07:15:14 +00:00
2006-08-04 16:31:22 +00:00
run->exit_level = EXIT_NONE;
2006-10-17 09:36:09 +00:00
n = __update_fnr (run, 0);
if (n == 0) n = __set_globals_to_default (run);
2006-10-24 04:10:12 +00:00
if (n == 0 && (run->awk->option & ASE_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-10-24 04:10:12 +00:00
ase_awk_nde_call_t nde;
2006-04-10 09:22:05 +00:00
2006-10-24 04:10:12 +00:00
nde.type = ASE_AWK_NDE_AFN;
nde.next = ASE_NULL;
nde.what.afn.name = ASE_T("main");
2006-08-03 05:05:48 +00:00
nde.what.afn.name_len = 4;
2006-10-24 04:10:12 +00:00
nde.args = ASE_NULL;
2006-08-03 05:05:48 +00:00
nde.nargs = 0;
2006-10-24 04:10:12 +00:00
v = __eval_afn (run, (ase_awk_nde_t*)&nde);
if (v == ASE_NULL) n = -1;
2006-04-10 09:22:05 +00:00
else
{
/* destroy the return value if necessary */
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
ase_awk_refdownval (run, v);
2006-04-10 09:22:05 +00:00
}
2006-04-09 15:31:13 +00:00
}
2006-10-16 08:48:19 +00:00
else if (n == 0)
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-10-24 04:10:12 +00:00
PANIC_I (run, ASE_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-10-24 04:10:12 +00:00
PANIC_I (run, ASE_AWK_ENOMEM);
2006-04-09 16:26:36 +00:00
}
/* secure space for a return value */
2006-10-24 04:10:12 +00:00
if (__raw_push(run,ase_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-10-24 04:10:12 +00:00
PANIC_I (run, ASE_AWK_ENOMEM);
2006-04-09 16:26:36 +00:00
}
/* secure space for nargs */
2006-10-24 04:10:12 +00:00
if (__raw_push(run,ase_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-10-24 04:10:12 +00:00
PANIC_I (run, ASE_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 &&
2006-10-24 04:10:12 +00:00
run->awk->tree.begin != ASE_NULL &&
2006-08-04 16:31:22 +00:00
run->exit_level != EXIT_ABORT)
2006-04-09 15:31:13 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_blk_t* blk;
2006-04-10 09:22:05 +00:00
2006-10-24 04:10:12 +00:00
blk = (ase_awk_nde_blk_t*)run->awk->tree.begin;
ase_awk_assert (run->awk, blk->type == ASE_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 &&
2006-10-24 04:10:12 +00:00
run->awk->tree.chain != ASE_NULL &&
2006-08-04 16:31:22 +00:00
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 &&
2006-10-24 04:10:12 +00:00
run->awk->tree.end != ASE_NULL &&
2006-08-04 16:31:22 +00:00
run->exit_level != EXIT_ABORT)
2006-04-09 15:31:13 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_blk_t* blk;
2006-04-10 09:22:05 +00:00
2006-10-24 04:10:12 +00:00
blk = (ase_awk_nde_blk_t*)run->awk->tree.end;
ase_awk_assert (run->awk, blk->type == ASE_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-10-24 04:10:12 +00:00
nargs = (ase_size_t)STACK_NARGS(run);
ase_awk_assert (run->awk, nargs == 0);
2006-04-09 16:26:36 +00:00
for (i = 0; i < nargs; i++)
{
2006-10-24 04:10:12 +00:00
ase_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-10-24 04:10:12 +00:00
xp_printf (ASE_T("Return Value - "));
ase_awk_printval (v);
xp_printf (ASE_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 */
2006-10-24 04:10:12 +00:00
/*ase_awk_refdownval_nofree (awk, v);*/
ase_awk_refdownval (run, v);
2006-04-09 15:31:13 +00:00
2006-04-21 17:24:31 +00:00
run->stack_top =
2006-10-24 04:10:12 +00:00
(ase_size_t)run->stack[run->stack_base+1];
2006-04-21 17:24:31 +00:00
run->stack_base =
2006-10-24 04:10:12 +00:00
(ase_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-10-24 04:10:12 +00:00
ase_awk_refdownval (run, STACK_GLOBAL(run,nglobals));
2006-04-21 17:24:31 +00:00
__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-10-24 04:10:12 +00:00
xp_printf (ASE_T("-[VARIABLES]------------------------\n"));
ase_awk_map_walk (&run->named, __printval, ASE_NULL);
xp_printf (ASE_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-10-24 04:10:12 +00:00
static int __run_pattern_blocks (ase_awk_run_t* run)
2006-04-22 13:54:53 +00:00
{
2006-10-24 04:10:12 +00:00
ase_ssize_t n;
ase_bool_t need_to_close = ase_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;
2006-10-24 04:10:12 +00:00
run->inrec.eof = ase_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-10-24 04:10:12 +00:00
ase_awk_closeextio_read (
run, ASE_AWK_IN_CONSOLE, ASE_T(""));
2006-08-27 15:29:21 +00:00
run->errnum = saved;
2006-04-22 13:54:53 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
need_to_close = ase_true;
2006-04-22 13:54:53 +00:00
if (x == 0) break; /* end of input */
2006-10-17 09:36:09 +00:00
__update_fnr (run, run->global.fnr + 1);
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-10-24 04:10:12 +00:00
ase_awk_closeextio_read (
run, ASE_AWK_IN_CONSOLE, ASE_T(""));
2006-08-27 15:29:21 +00:00
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-10-24 04:10:12 +00:00
* ASE_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-10-24 04:10:12 +00:00
n = ase_awk_closeextio_read (
run, ASE_AWK_IN_CONSOLE, ASE_T(""));
2006-08-01 15:57:43 +00:00
if (n == -1)
{
2006-10-24 04:10:12 +00:00
if (run->errnum == ASE_AWK_EIOHANDLER)
PANIC_I (run, ASE_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-10-24 04:10:12 +00:00
static int __run_pattern_block_chain (ase_awk_run_t* run, ase_awk_chain_t* chain)
2006-05-09 15:21:26 +00:00
{
2006-10-24 04:10:12 +00:00
ase_size_t block_no = 0;
2006-08-13 05:55:02 +00:00
2006-07-01 16:07:06 +00:00
while (run->exit_level != EXIT_GLOBAL &&
2006-10-24 04:10:12 +00:00
run->exit_level != EXIT_ABORT && chain != ASE_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 (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_chain_t* chain, ase_size_t block_no)
2006-08-02 14:36:23 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_t* ptn;
ase_awk_nde_blk_t* blk;
2006-08-02 14:36:23 +00:00
ptn = chain->pattern;
2006-10-24 04:10:12 +00:00
blk = (ase_awk_nde_blk_t*)chain->action;
2006-08-02 14:36:23 +00:00
2006-10-24 04:10:12 +00:00
if (ptn == ASE_NULL)
2006-08-02 14:36:23 +00:00
{
/* 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
{
2006-10-24 04:10:12 +00:00
if (ptn->next == ASE_NULL)
2006-05-09 15:21:26 +00:00
{
2006-08-02 14:36:23 +00:00
/* pattern { ... } */
2006-10-24 04:10:12 +00:00
ase_awk_val_t* v1;
2006-08-02 14:36:23 +00:00
2006-10-22 12:39:30 +00:00
v1 = __eval_expression (run, ptn);
2006-10-24 04:10:12 +00:00
if (v1 == ASE_NULL) return -1;
2006-05-09 15:21:26 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v1);
2006-05-09 15:21:26 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_valtobool (run, 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)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v1);
2006-08-02 14:36:23 +00:00
return -1;
}
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v1);
2006-08-02 14:36:23 +00:00
}
else
{
/* pattern, pattern { ... } */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, ptn->next->next == ASE_NULL);
2006-08-02 14:36:23 +00:00
2006-08-13 05:55:02 +00:00
if (run->pattern_range_state[block_no] == 0)
2006-08-02 14:36:23 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* v1;
2006-08-02 14:36:23 +00:00
2006-10-22 12:39:30 +00:00
v1 = __eval_expression (run, ptn);
2006-10-24 04:10:12 +00:00
if (v1 == ASE_NULL) return -1;
ase_awk_refupval (v1);
2006-08-02 14:36:23 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_valtobool (run, 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
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v1);
2006-05-09 15:21:26 +00:00
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
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v1);
2006-05-09 15:21:26 +00:00
}
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-10-24 04:10:12 +00:00
ase_awk_val_t* v2;
2006-07-30 15:53:42 +00:00
2006-10-22 12:39:30 +00:00
v2 = __eval_expression (run, ptn->next);
2006-10-24 04:10:12 +00:00
if (v2 == ASE_NULL) return -1;
ase_awk_refupval (v2);
2006-08-02 14:36:23 +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)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v2);
2006-08-02 14:36:23 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (ase_awk_valtobool (run, v2))
2006-08-13 05:55:02 +00:00
run->pattern_range_state[block_no] = 0;
2006-08-02 14:36:23 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v2);
2006-05-09 15:21:26 +00:00
}
}
}
return 0;
}
2006-10-24 04:10:12 +00:00
static int __run_block (ase_awk_run_t* run, ase_awk_nde_blk_t* nde)
2006-01-26 15:35:20 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_t* p;
ase_size_t nlocals;
ase_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-10-24 04:10:12 +00:00
if (nde == ASE_NULL)
2006-08-03 09:54:16 +00:00
{
/* blockless pattern - execute print $0*/
2006-10-24 04:10:12 +00:00
ase_awk_refupval (run->inrec.d0);
/*n = ase_awk_writeextio_val (run,
ASE_AWK_OUT_CONSOLE, ASE_T(""), run->inrec.d0);*/
n = ase_awk_writeextio_str (run,
ASE_AWK_OUT_CONSOLE, ASE_T(""),
ASE_AWK_STR_BUF(&run->inrec.line),
ASE_AWK_STR_LEN(&run->inrec.line));
2006-08-03 09:54:16 +00:00
if (n == -1)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, run->inrec.d0);
2006-08-03 09:54:16 +00:00
2006-10-24 04:10:12 +00:00
if (run->errnum == ASE_AWK_EIOHANDLER)
PANIC_I (run, ASE_AWK_ECONOUTDATA);
2006-08-27 15:29:21 +00:00
else return -1;
2006-08-03 09:54:16 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, run->inrec.d0);
2006-08-03 09:54:16 +00:00
return 0;
}
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->type == ASE_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-10-24 04:10:12 +00:00
/*xp_printf (ASE_T("securing space for local variables nlocals = %d\n"), (int)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-10-24 04:10:12 +00:00
if (__raw_push(run,ase_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;
}
2006-10-24 04:10:12 +00:00
/* refupval is not required for ase_awk_val_nil */
2006-03-26 14:03:08 +00:00
}
2006-10-24 04:10:12 +00:00
/*xp_printf (ASE_T("executing block statements\n"));*/
while (p != ASE_NULL && run->exit_level == EXIT_NONE)
2006-03-03 11:45:45 +00:00
{
2006-10-24 04:10:12 +00:00
/*xp_printf (ASE_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-10-24 04:10:12 +00:00
/*xp_printf (ASE_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-10-24 04:10:12 +00:00
ase_awk_refdownval (run, STACK_LOCAL(run,nlocals));
2006-04-21 17:24:31 +00:00
__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-10-24 04:10:12 +00:00
static int __run_statement (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-01-26 15:35:20 +00:00
{
2006-03-03 11:45:45 +00:00
switch (nde->type)
{
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_NULL:
2006-06-13 08:35:53 +00:00
{
/* do nothing */
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_BLK:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_block (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_blk_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_IF:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_if (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_if_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_WHILE:
case ASE_AWK_NDE_DOWHILE:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_while (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_while_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_FOR:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_for (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_for_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_FOREACH:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_foreach (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_foreach_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_BREAK:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_break (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_break_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_CONTINUE:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_continue (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_continue_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_RETURN:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_return (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_return_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_EXIT:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_exit (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_exit_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_NEXT:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_next (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_next_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_NEXTFILE:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_nextfile (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_nextfile_t*)nde) == -1) return -1;
2006-06-13 08:35:53 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_DELETE:
2006-06-27 14:18:19 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_delete (run,
2006-10-24 04:10:12 +00:00
(ase_awk_nde_delete_t*)nde) == -1) return -1;
2006-06-27 14:18:19 +00:00
break;
}
2006-10-24 04:10:12 +00:00
case ASE_AWK_NDE_PRINT:
2006-06-13 08:35:53 +00:00
{
2006-06-30 11:31:51 +00:00
if (__run_print (run,
2006-10-24 04:10:12 +00:00
(ase_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
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* v;
2006-10-22 12:39:30 +00:00
v = __eval_expression(run,nde);
2006-10-24 04:10:12 +00:00
if (v == ASE_NULL) return -1;
2006-07-06 15:54:41 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
ase_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-10-24 04:10:12 +00:00
static int __run_if (ase_awk_run_t* run, ase_awk_nde_if_t* nde)
2006-03-15 15:34:59 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* test;
2006-03-27 11:43:17 +00:00
int n = 0;
2006-03-15 15:34:59 +00:00
2006-10-22 12:39:30 +00:00
/* the test expression for the if statement cannot have
* chained expressions. this should not be allowed by the
2006-05-07 17:45:08 +00:00
* parser first of all */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->test->next == ASE_NULL);
2006-05-07 17:45:08 +00:00
2006-10-22 12:39:30 +00:00
test = __eval_expression (run, nde->test);
2006-10-24 04:10:12 +00:00
if (test == ASE_NULL) return -1;
2006-03-23 13:26:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (test);
if (ase_awk_valtobool (run, 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
}
2006-10-24 04:10:12 +00:00
else if (nde->else_part != ASE_NULL)
2006-03-15 15:34:59 +00:00
{
2006-04-21 17:24:31 +00:00
n = __run_statement (run, nde->else_part);
2006-03-15 15:34:59 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, test); /* TODO: is this correct?*/
2006-03-23 13:26:04 +00:00
return n;
}
2006-10-24 04:10:12 +00:00
static int __run_while (ase_awk_run_t* run, ase_awk_nde_while_t* nde)
2006-03-23 13:26:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* test;
2006-03-23 13:26:04 +00:00
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_WHILE)
2006-03-23 13:26:04 +00:00
{
2006-10-22 12:39:30 +00:00
/* no chained expressions are allowed for the test
* expression of the while statement */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->test->next == ASE_NULL);
2006-05-07 17:45:08 +00:00
2006-04-14 10:56:42 +00:00
/* TODO: handle run-time abortion... */
2006-03-23 13:26:04 +00:00
while (1)
{
2006-10-22 12:39:30 +00:00
test = __eval_expression (run, nde->test);
2006-10-24 04:10:12 +00:00
if (test == ASE_NULL) return -1;
2006-03-23 13:26:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (test);
2006-03-23 13:26:04 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_valtobool (run, 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-10-24 04:10:12 +00:00
ase_awk_refdownval (run, test);
2006-03-23 13:26:04 +00:00
return -1;
}
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, test);
2006-03-23 13:26:04 +00:00
break;
}
2006-10-24 04:10:12 +00:00
ase_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
}
}
2006-10-24 04:10:12 +00:00
else if (nde->type == ASE_AWK_NDE_DOWHILE)
2006-03-23 13:26:04 +00:00
{
2006-10-22 12:39:30 +00:00
/* no chained expressions are allowed for the test
* expression of the while statement */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->test->next == ASE_NULL);
2006-05-07 17:45:08 +00:00
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-10-22 12:39:30 +00:00
test = __eval_expression (run, nde->test);
2006-10-24 04:10:12 +00:00
if (test == ASE_NULL) return -1;
2006-03-23 13:26:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (test);
2006-04-10 14:53:48 +00:00
2006-10-24 04:10:12 +00:00
if (!ase_awk_valtobool (run, test))
2006-03-23 13:26:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, test);
2006-03-23 13:26:04 +00:00
break;
}
2006-04-10 14:53:48 +00:00
2006-10-24 04:10:12 +00:00
ase_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-10-24 04:10:12 +00:00
static int __run_for (ase_awk_run_t* run, ase_awk_nde_for_t* nde)
2006-03-23 15:36:20 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val;
2006-04-10 14:53:48 +00:00
2006-10-24 04:10:12 +00:00
if (nde->init != ASE_NULL)
2006-03-23 15:36:20 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->init->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
val = __eval_expression(run,nde->init);
2006-10-24 04:10:12 +00:00
if (val == ASE_NULL) return -1;
2006-04-10 14:53:48 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val);
ase_awk_refdownval (run, val);
2006-03-23 15:36:20 +00:00
}
while (1)
{
2006-10-24 04:10:12 +00:00
if (nde->test != ASE_NULL)
2006-03-23 15:36:20 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* test;
2006-03-23 15:36:20 +00:00
2006-10-22 12:39:30 +00:00
/* no chained expressions for the test expression of
2006-05-07 17:45:08 +00:00
* the for statement are allowed */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->test->next == ASE_NULL);
2006-05-07 17:45:08 +00:00
2006-10-22 12:39:30 +00:00
test = __eval_expression (run, nde->test);
2006-10-24 04:10:12 +00:00
if (test == ASE_NULL) return -1;
2006-03-23 15:36:20 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (test);
if (ase_awk_valtobool (run, 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-10-24 04:10:12 +00:00
ase_awk_refdownval (run, test);
2006-03-23 15:36:20 +00:00
return -1;
}
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, test);
2006-03-23 15:36:20 +00:00
break;
}
2006-10-24 04:10:12 +00:00
ase_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-10-24 04:10:12 +00:00
if (nde->incr != ASE_NULL)
2006-03-23 15:36:20 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->incr->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
val = __eval_expression(run,nde->incr);
2006-10-24 04:10:12 +00:00
if (val == ASE_NULL) return -1;
2006-04-10 14:53:48 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val);
ase_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
{
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run;
ase_awk_nde_t* var;
ase_awk_nde_t* body;
2006-04-30 17:12:51 +00:00
};
2006-10-24 04:10:12 +00:00
static int __walk_foreach (ase_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;
2006-10-24 04:10:12 +00:00
ase_awk_val_t* str;
2006-04-30 17:12:51 +00:00
2006-10-24 04:10:12 +00:00
str = (ase_awk_val_t*) ase_awk_makestrval (
w->run, pair->key, ase_awk_strlen(pair->key));
if (str == ASE_NULL) PANIC_I (w->run, ASE_AWK_ENOMEM);
2006-04-30 17:12:51 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (str);
if (__do_assignment (w->run, w->var, str) == ASE_NULL)
2006-04-30 17:12:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (w->run, str);
2006-04-30 17:12:51 +00:00
return -1;
}
if (__run_statement (w->run, w->body) == -1)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (w->run, str);
2006-04-30 17:12:51 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (w->run, str);
2006-04-29 12:09:29 +00:00
return 0;
}
2006-10-24 04:10:12 +00:00
static int __run_foreach (ase_awk_run_t* run, ase_awk_nde_foreach_t* nde)
2006-04-30 17:12:51 +00:00
{
int n;
2006-10-24 04:10:12 +00:00
ase_awk_nde_exp_t* test;
ase_awk_val_t* rv;
ase_awk_map_t* map;
2006-04-30 17:12:51 +00:00
struct __foreach_walker_t walker;
2006-10-24 04:10:12 +00:00
test = (ase_awk_nde_exp_t*)nde->test;
ase_awk_assert (run->awk, test->type == ASE_AWK_NDE_EXP_BIN &&
test->opcode == ASE_AWK_BINOP_IN);
2006-04-30 17:12:51 +00:00
2006-10-22 12:39:30 +00:00
/* chained expressions should not be allowed
2006-05-07 17:45:08 +00:00
* by the parser first of all */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, test->right->next == ASE_NULL);
2006-05-07 17:45:08 +00:00
2006-10-22 12:39:30 +00:00
rv = __eval_expression (run, test->right);
2006-10-24 04:10:12 +00:00
if (rv == ASE_NULL) return -1;
2006-04-30 17:12:51 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (rv);
if (rv->type != ASE_AWK_VAL_MAP)
2006-04-30 17:12:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, rv);
PANIC_I (run, ASE_AWK_ENOTINDEXABLE);
2006-04-30 17:12:51 +00:00
}
2006-10-24 04:10:12 +00:00
map = ((ase_awk_val_map_t*)rv)->map;
2006-04-30 17:12:51 +00:00
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;
2006-10-24 04:10:12 +00:00
n = ase_awk_map_walk (map, __walk_foreach, &walker);
2006-04-30 17:12:51 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, rv);
2006-04-30 17:12:51 +00:00
return n;
}
2006-10-24 04:10:12 +00:00
static int __run_break (ase_awk_run_t* run, ase_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-10-24 04:10:12 +00:00
static int __run_continue (ase_awk_run_t* run, ase_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-10-24 04:10:12 +00:00
static int __run_return (ase_awk_run_t* run, ase_awk_nde_return_t* nde)
2006-03-23 15:36:20 +00:00
{
2006-10-24 04:10:12 +00:00
if (nde->val != ASE_NULL)
2006-03-25 17:04:36 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val;
2006-05-07 17:45:08 +00:00
2006-10-22 12:39:30 +00:00
/* chained expressions should not be allowed
2006-05-07 17:45:08 +00:00
* by the parser first of all */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->val->next == ASE_NULL);
2006-05-07 17:45:08 +00:00
2006-10-24 04:10:12 +00:00
/*xp_printf (ASE_T("returning....\n"));*/
2006-10-22 12:39:30 +00:00
val = __eval_expression (run, nde->val);
2006-10-24 04:10:12 +00:00
if (val == ASE_NULL) return -1;
2006-03-26 16:36:30 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, STACK_RETVAL(run));
2006-04-21 17:24:31 +00:00
STACK_RETVAL(run) = val;
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val); /* see __eval_call for the trick */
/*xp_printf (ASE_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-10-24 04:10:12 +00:00
static int __run_exit (ase_awk_run_t* run, ase_awk_nde_exit_t* nde)
2006-03-23 15:36:20 +00:00
{
2006-10-24 04:10:12 +00:00
if (nde->val != ASE_NULL)
2006-03-27 10:19:33 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val;
2006-03-27 10:19:33 +00:00
2006-10-22 12:39:30 +00:00
/* chained expressions should not be allowed
2006-05-07 17:45:08 +00:00
* by the parser first of all */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->val->next == ASE_NULL);
2006-05-07 17:45:08 +00:00
2006-10-22 12:39:30 +00:00
val = __eval_expression (run, nde->val);
2006-10-24 04:10:12 +00:00
if (val == ASE_NULL) return -1;
2006-03-27 10:19:33 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, STACK_RETVAL_GLOBAL(run));
2006-04-21 17:24:31 +00:00
STACK_RETVAL_GLOBAL(run) = val; /* global return value */
2006-04-10 14:53:48 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val);
2006-03-27 10:19:33 +00:00
}
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-10-24 04:10:12 +00:00
static int __run_next (ase_awk_run_t* run, ase_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
2006-10-22 12:39:30 +00:00
* check that explicitly */
2006-08-03 15:50:04 +00:00
2006-10-24 04:10:12 +00:00
if (run->active_block == (ase_awk_nde_blk_t*)run->awk->tree.begin ||
run->active_block == (ase_awk_nde_blk_t*)run->awk->tree.end)
2006-08-03 15:50:04 +00:00
{
2006-10-24 04:10:12 +00:00
PANIC_I (run, ASE_AWK_ENEXTCALL);
2006-08-03 15:50:04 +00:00
}
2006-07-01 16:07:06 +00:00
run->exit_level = EXIT_NEXT;
return 0;
2006-04-22 16:16:40 +00:00
}
2006-10-24 04:10:12 +00:00
static int __run_nextfile (ase_awk_run_t* run, ase_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-10-24 04:10:12 +00:00
if (run->active_block == (ase_awk_nde_blk_t*)run->awk->tree.begin ||
run->active_block == (ase_awk_nde_blk_t*)run->awk->tree.end)
2006-08-03 15:50:04 +00:00
{
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_ENEXTFILECALL;
2006-10-17 09:36:09 +00:00
return -1;
2006-08-03 15:50:04 +00:00
}
2006-10-24 04:10:12 +00:00
n = ase_awk_nextextio_read (run, ASE_AWK_IN_CONSOLE, ASE_T(""));
2006-07-28 10:34:22 +00:00
if (n == -1)
{
2006-10-24 04:10:12 +00:00
if (run->errnum == ASE_AWK_EIOHANDLER)
run->errnum = ASE_AWK_ECONINNEXT;
2006-10-17 09:36:09 +00:00
return -1;
2006-07-28 10:34:22 +00:00
}
if (n == 0)
{
/* no more input file */
run->exit_level = EXIT_GLOBAL;
return 0;
}
2006-10-17 09:36:09 +00:00
if (__update_fnr (run, 0) == -1) return -1;
/* TODO: Consider using FILENAME_IN and FILENAME_OUT to accomplish nextfile in/out */
2006-07-28 10:34:22 +00:00
run->exit_level = EXIT_NEXT;
return 0;
2006-04-22 16:16:40 +00:00
}
2006-10-24 04:10:12 +00:00
static int __run_delete (ase_awk_run_t* run, ase_awk_nde_delete_t* nde)
2006-06-27 14:18:19 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_var_t* var;
2006-06-30 11:31:51 +00:00
2006-10-24 04:10:12 +00:00
var = (ase_awk_nde_var_t*) nde->var;
2006-06-30 11:31:51 +00:00
2006-10-24 04:10:12 +00:00
if (var->type == ASE_AWK_NDE_NAMED ||
var->type == ASE_AWK_NDE_NAMEDIDX)
2006-06-30 11:31:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_pair_t* pair;
2006-06-30 11:31:51 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, (var->type == ASE_AWK_NDE_NAMED &&
var->idx == ASE_NULL) ||
(var->type == ASE_AWK_NDE_NAMEDIDX &&
var->idx != ASE_NULL));
2006-07-01 07:57:10 +00:00
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_get (
2006-08-03 05:05:48 +00:00
&run->named, var->id.name, var->id.name_len);
2006-10-24 04:10:12 +00:00
if (pair == ASE_NULL)
2006-06-30 11:31:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tmp;
2006-06-30 16:46:34 +00:00
/* value not set for the named variable.
* create a map and assign it to the variable */
2006-10-24 04:10:12 +00:00
tmp = ase_awk_makemapval (run);
if (tmp == ASE_NULL) PANIC_I (run, ASE_AWK_ENOMEM);
2006-06-30 16:46:34 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_map_put (&run->named,
var->id.name, var->id.name_len, tmp) == ASE_NULL)
2006-06-30 16:46:34 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
ase_awk_refdownval (run, tmp);
PANIC_I (run, ASE_AWK_ENOMEM);
2006-06-30 16:46:34 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
2006-06-30 16:46:34 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val;
ase_awk_map_t* map;
2006-06-30 16:46:34 +00:00
2006-10-24 04:10:12 +00:00
val = (ase_awk_val_t*)pair->val;
ase_awk_assert (run->awk, val != ASE_NULL);
2006-07-01 07:57:10 +00:00
2006-10-24 04:10:12 +00:00
if (val->type != ASE_AWK_VAL_MAP)
PANIC_I (run, ASE_AWK_ENOTDELETABLE);
2006-07-01 07:57:10 +00:00
2006-10-24 04:10:12 +00:00
map = ((ase_awk_val_map_t*)val)->map;
if (var->type == ASE_AWK_NDE_NAMEDIDX)
2006-06-30 11:31:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* key;
ase_size_t key_len;
ase_awk_val_t* idx;
2006-06-30 11:31:51 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, var->idx != ASE_NULL);
2006-06-30 17:07:52 +00:00
2006-10-22 12:39:30 +00:00
idx = __eval_expression (run, var->idx);
2006-10-24 04:10:12 +00:00
if (idx == ASE_NULL) return -1;
2006-06-30 17:07:52 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (idx);
key = ase_awk_valtostr (
run, idx, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &key_len);
ase_awk_refdownval (run, idx);
2006-06-30 17:07:52 +00:00
2006-10-24 04:10:12 +00:00
if (key == ASE_NULL) return -1;
2006-06-30 17:07:52 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_map_remove (map, key, key_len);
ASE_AWK_FREE (run->awk, key);
2006-06-30 16:46:34 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_map_clear (map);
2006-06-30 11:31:51 +00:00
}
}
}
2006-10-24 04:10:12 +00:00
else if (var->type == ASE_AWK_NDE_GLOBAL ||
var->type == ASE_AWK_NDE_LOCAL ||
var->type == ASE_AWK_NDE_ARG ||
var->type == ASE_AWK_NDE_GLOBALIDX ||
var->type == ASE_AWK_NDE_LOCALIDX ||
var->type == ASE_AWK_NDE_ARGIDX)
2006-06-30 17:07:52 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val;
2006-06-30 17:07:52 +00:00
2006-10-24 04:10:12 +00:00
if (var->type == ASE_AWK_NDE_GLOBAL ||
var->type == ASE_AWK_NDE_GLOBALIDX)
2006-07-01 07:57:10 +00:00
val = STACK_GLOBAL (run,var->id.idxa);
2006-10-24 04:10:12 +00:00
else if (var->type == ASE_AWK_NDE_LOCAL ||
var->type == ASE_AWK_NDE_LOCALIDX)
2006-07-01 07:57:10 +00:00
val = STACK_LOCAL (run,var->id.idxa);
else val = STACK_ARG (run,var->id.idxa);
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, val != ASE_NULL);
2006-06-30 17:07:52 +00:00
2006-10-24 04:10:12 +00:00
if (val->type == ASE_AWK_VAL_NIL)
2006-06-30 17:07:52 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tmp;
2006-06-30 17:07:52 +00:00
/* value not set for the named variable.
* create a map and assign it to the variable */
2006-10-24 04:10:12 +00:00
tmp = ase_awk_makemapval (run);
if (tmp == ASE_NULL) PANIC_I (run, ASE_AWK_ENOMEM);
2006-06-30 17:07:52 +00:00
/* no need to reduce the reference count of
* the previous value because it was nil. */
2006-10-24 04:10:12 +00:00
if (var->type == ASE_AWK_NDE_GLOBAL ||
var->type == ASE_AWK_NDE_GLOBALIDX)
2006-08-27 15:29:21 +00:00
{
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (
2006-08-27 15:29:21 +00:00
run, var->id.idxa, tmp) == -1)
{
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
ase_awk_refdownval (run, tmp);
2006-08-27 15:29:21 +00:00
return -1;
}
}
2006-10-24 04:10:12 +00:00
else if (var->type == ASE_AWK_NDE_LOCAL ||
var->type == ASE_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-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
2006-08-27 15:29:21 +00:00
}
else
{
STACK_ARG(run,var->id.idxa) = tmp;
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
2006-08-27 15:29:21 +00:00
}
2006-06-30 17:07:52 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_map_t* map;
2006-07-01 07:57:10 +00:00
2006-10-24 04:10:12 +00:00
if (val->type != ASE_AWK_VAL_MAP)
PANIC_I (run, ASE_AWK_ENOTDELETABLE);
2006-07-01 07:57:10 +00:00
2006-10-24 04:10:12 +00:00
map = ((ase_awk_val_map_t*)val)->map;
if (var->type == ASE_AWK_NDE_GLOBALIDX ||
var->type == ASE_AWK_NDE_LOCALIDX ||
var->type == ASE_AWK_NDE_ARGIDX)
2006-06-30 17:07:52 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* key;
ase_size_t key_len;
ase_awk_val_t* idx;
2006-07-01 07:57:10 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, var->idx != ASE_NULL);
2006-07-01 07:57:10 +00:00
2006-10-22 12:39:30 +00:00
idx = __eval_expression (run, var->idx);
2006-10-24 04:10:12 +00:00
if (idx == ASE_NULL) return -1;
2006-07-01 07:57:10 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (idx);
key = ase_awk_valtostr (
run, idx, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &key_len);
ase_awk_refdownval (run, idx);
2006-07-01 07:57:10 +00:00
2006-10-24 04:10:12 +00:00
if (key == ASE_NULL) return -1;
2006-07-01 07:57:10 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_map_remove (map, key, key_len);
ASE_AWK_FREE (run->awk, key);
2006-06-30 17:07:52 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_map_clear (map);
2006-06-30 17:07:52 +00:00
}
}
}
2006-06-30 11:31:51 +00:00
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, !"should never happen - wrong variable type for delete");
PANIC_I (run, ASE_AWK_EINTERNAL);
2006-06-30 11:31:51 +00:00
}
return 0;
2006-06-27 14:18:19 +00:00
}
2006-10-24 04:10:12 +00:00
static int __run_print (ase_awk_run_t* run, ase_awk_nde_print_t* nde)
2006-06-13 08:35:53 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_print_t* p = (ase_awk_nde_print_t*)nde;
ase_char_t* out = ASE_NULL;
const ase_char_t* dst;
ase_awk_val_t* v;
ase_awk_nde_t* np;
2006-08-31 15:39:14 +00:00
int n;
2006-06-26 15:09:28 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk,
(p->out_type == ASE_AWK_OUT_PIPE && p->out != ASE_NULL) ||
(p->out_type == ASE_AWK_OUT_COPROC && p->out != ASE_NULL) ||
(p->out_type == ASE_AWK_OUT_FILE && p->out != ASE_NULL) ||
(p->out_type == ASE_AWK_OUT_FILE_APPEND && p->out != ASE_NULL) ||
(p->out_type == ASE_AWK_OUT_CONSOLE && p->out == ASE_NULL));
2006-06-26 15:09:28 +00:00
2006-10-24 04:10:12 +00:00
if (p->out != ASE_NULL)
2006-06-26 15:09:28 +00:00
{
2006-10-24 04:10:12 +00:00
ase_size_t len;
2006-08-02 03:22:51 +00:00
2006-10-22 12:39:30 +00:00
v = __eval_expression (run, p->out);
2006-10-24 04:10:12 +00:00
if (v == ASE_NULL) return -1;
2006-06-22 14:15:02 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
out = ase_awk_valtostr (
run, v, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &len);
if (out == ASE_NULL)
2006-06-22 14:15:02 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v);
2006-08-31 15:39:14 +00:00
return -1;
2006-06-22 14:15:02 +00:00
}
2006-10-24 04:10:12 +00:00
ase_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. */
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, out);
2006-08-02 03:34:34 +00:00
n = -1;
goto skip_write;
}
2006-08-02 03:22:51 +00:00
while (len > 0)
{
2006-10-24 04:10:12 +00:00
if (out[--len] == ASE_T('\0'))
2006-08-02 03:22:51 +00:00
{
/* the output destination name contains a null
2006-08-02 03:34:34 +00:00
* character. */
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, 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-10-24 04:10:12 +00:00
dst = (out == ASE_NULL)? ASE_T(""): out;
2006-06-22 14:15:02 +00:00
2006-10-24 04:10:12 +00:00
if (p->args == ASE_NULL)
2006-06-26 15:09:28 +00:00
{
2006-09-30 17:03:11 +00:00
/*
2006-07-30 15:53:42 +00:00
v = run->inrec.d0;
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
n = ase_awk_writeextio_val (run, p->out_type, dst, v);
if (n < 0 && run->errnum != ASE_AWK_EIOHANDLER)
2006-06-26 15:09:28 +00:00
{
2006-10-24 04:10:12 +00:00
if (out != ASE_NULL) ASE_AWK_FREE (run->awk, out);
ase_awk_refdownval (run, v);
2006-08-27 15:29:21 +00:00
return -1;
2006-06-26 15:09:28 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v);
2006-09-30 17:03:11 +00:00
*/
2006-10-24 04:10:12 +00:00
n = ase_awk_writeextio_str (
2006-09-30 17:03:11 +00:00
run, p->out_type, dst,
2006-10-24 04:10:12 +00:00
ASE_AWK_STR_BUF(&run->inrec.line),
ASE_AWK_STR_LEN(&run->inrec.line));
if (n < 0 && run->errnum != ASE_AWK_EIOHANDLER)
2006-09-30 17:03:11 +00:00
{
2006-10-24 04:10:12 +00:00
if (out != ASE_NULL) ASE_AWK_FREE (run->awk, out);
2006-09-30 17:03:11 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
/* TODO: how to handle n == -1 && errnum == ASE_AWK_EIOHANDLER.
2006-08-23 15:42:16 +00:00
* 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
{
2006-10-24 04:10:12 +00:00
for (np = p->args; np != ASE_NULL; np = np->next)
2006-06-25 15:26:57 +00:00
{
2006-09-30 17:03:11 +00:00
if (np != p->args)
{
2006-10-24 04:10:12 +00:00
n = ase_awk_writeextio_str (
2006-09-30 17:03:11 +00:00
run, p->out_type, dst,
2006-10-12 04:17:58 +00:00
run->global.ofs.ptr,
run->global.ofs.len);
2006-10-24 04:10:12 +00:00
if (n < 0 && run->errnum != ASE_AWK_EIOHANDLER)
2006-09-30 17:03:11 +00:00
{
2006-10-24 04:10:12 +00:00
if (out != ASE_NULL) ASE_AWK_FREE (run->awk, out);
2006-09-30 17:03:11 +00:00
return -1;
}
}
2006-10-22 12:39:30 +00:00
v = __eval_expression (run, np);
2006-10-24 04:10:12 +00:00
if (v == ASE_NULL)
2006-06-26 15:09:28 +00:00
{
2006-10-24 04:10:12 +00:00
if (out != ASE_NULL) ASE_AWK_FREE (run->awk, out);
2006-06-26 15:09:28 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
2006-06-25 15:26:57 +00:00
2006-10-24 04:10:12 +00:00
n = ase_awk_writeextio_val (run, p->out_type, dst, v);
if (n < 0 && run->errnum != ASE_AWK_EIOHANDLER)
2006-06-25 15:26:57 +00:00
{
2006-10-24 04:10:12 +00:00
if (out != ASE_NULL) ASE_AWK_FREE (run->awk, out);
ase_awk_refdownval (run, v);
2006-08-27 15:29:21 +00:00
return -1;
2006-06-25 15:26:57 +00:00
}
2006-10-13 10:18:39 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v);
2006-06-25 15:26:57 +00:00
2006-09-29 11:18:13 +00:00
2006-10-24 04:10:12 +00:00
/* TODO: how to handle n == -1 && run->errnum == ASE_AWK_EIOHANDLER.
2006-08-23 15:42:16 +00:00
* that is the user handler returned an error... */
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-10-24 04:10:12 +00:00
n = ase_awk_writeextio_str (
2006-09-29 11:18:13 +00:00
run, p->out_type, dst,
run->global.ors.ptr, run->global.ors.len);
2006-10-24 04:10:12 +00:00
if (n < 0 && run->errnum != ASE_AWK_EIOHANDLER)
2006-07-30 15:53:42 +00:00
{
2006-10-24 04:10:12 +00:00
if (out != ASE_NULL) ASE_AWK_FREE (run->awk, 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-10-24 04:10:12 +00:00
/* TODO: how to handle n == -1 && errnum == ASE_AWK_EIOHANDLER.
2006-08-23 15:42:16 +00:00
* that is the user handler returned an error... */
2006-06-26 15:09:28 +00:00
2006-10-24 04:10:12 +00:00
if (out != ASE_NULL) ASE_AWK_FREE (run->awk, 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-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_expression (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-07-31 15:59:43 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* v;
2006-07-31 15:59:43 +00:00
int n, errnum;
2006-10-22 12:39:30 +00:00
v = __eval_expression0 (run, nde);
2006-10-24 04:10:12 +00:00
if (v == ASE_NULL) return ASE_NULL;
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
if (v->type == ASE_AWK_VAL_REX)
2006-07-31 15:59:43 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
2006-09-02 14:59:15 +00:00
2006-10-24 04:10:12 +00:00
if (run->inrec.d0->type == ASE_AWK_VAL_NIL)
2006-08-30 07:15:14 +00:00
{
/* the record has never been read.
* probably, this functions has been triggered
* by the statements in the BEGIN block */
2006-10-24 04:10:12 +00:00
n = ase_awk_isemptyrex (run->awk, ((ase_awk_val_rex_t*)v)->code)? 1: 0;
2006-08-30 07:15:14 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, run->inrec.d0->type == ASE_AWK_VAL_STR);
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
n = ase_awk_matchrex (
((ase_awk_run_t*)run)->awk,
((ase_awk_val_rex_t*)v)->code,
((((ase_awk_run_t*)run)->global.ignorecase)? ASE_AWK_REX_IGNORECASE: 0),
((ase_awk_val_str_t*)run->inrec.d0)->buf,
((ase_awk_val_str_t*)run->inrec.d0)->len,
ASE_NULL, ASE_NULL, &errnum);
2006-08-30 07:15:14 +00:00
if (n == -1)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v);
2006-08-30 07:15:14 +00:00
PANIC (run, errnum);
}
}
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v);
2006-09-02 14:59:15 +00:00
2006-10-24 04:10:12 +00:00
v = ase_awk_makeintval (run, (n != 0));
if (v == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 15:59:43 +00:00
}
return v;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_expression0 (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-03-04 15:54:37 +00:00
{
2006-10-22 12:39:30 +00:00
static eval_expr_t __eval_func[] =
2006-04-07 16:52:42 +00:00
{
/* 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-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->type >= ASE_AWK_NDE_GRP &&
(nde->type - ASE_AWK_NDE_GRP) < ase_countof(__eval_func));
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
return __eval_func[nde->type-ASE_AWK_NDE_GRP] (run, nde);
2006-01-26 15:35:20 +00:00
}
2006-02-23 15:37:34 +00:00
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_group (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-24 15:34:52 +00:00
{
2006-10-24 04:10:12 +00:00
/* __eval_binop_in evaluates the ASE_AWK_NDE_GRP specially.
2006-04-27 15:20:10 +00:00
* so this function should never be reached. */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, !"should never happen - NDE_GRP only for in");
PANIC (run, ASE_AWK_EINTERNAL);
return ASE_NULL;
2006-04-24 15:34:52 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_assignment (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-02-23 15:37:34 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val, * ret;
ase_awk_nde_ass_t* ass = (ase_awk_nde_ass_t*)nde;
2006-03-05 17:07:33 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, ass->left != ASE_NULL && ass->right != ASE_NULL);
2006-03-25 17:04:36 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, ass->right->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
val = __eval_expression (run, ass->right);
2006-10-24 04:10:12 +00:00
if (val == ASE_NULL) return ASE_NULL;
2006-03-04 15:54:37 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val);
2006-04-16 04:31:38 +00:00
2006-10-24 04:10:12 +00:00
if (ass->opcode != ASE_AWK_ASSOP_NONE)
2006-04-16 04:31:38 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val2, * tmp;
2006-04-16 04:31:38 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, ass->left->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
val2 = __eval_expression (run, ass->left);
2006-10-24 04:10:12 +00:00
if (val2 == ASE_NULL)
2006-04-16 04:31:38 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, val);
return ASE_NULL;
2006-04-16 04:31:38 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val2);
2006-04-16 04:31:38 +00:00
2006-10-24 04:10:12 +00:00
if (ass->opcode == ASE_AWK_ASSOP_PLUS)
2006-04-16 04:31:38 +00:00
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_plus (run, val2, val);
2006-04-16 04:31:38 +00:00
}
2006-10-24 04:10:12 +00:00
else if (ass->opcode == ASE_AWK_ASSOP_MINUS)
2006-04-16 04:31:38 +00:00
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_minus (run, val2, val);
2006-04-16 04:31:38 +00:00
}
2006-10-24 04:10:12 +00:00
else if (ass->opcode == ASE_AWK_ASSOP_MUL)
2006-04-16 04:31:38 +00:00
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_mul (run, val2, val);
2006-04-16 04:31:38 +00:00
}
2006-10-24 04:10:12 +00:00
else if (ass->opcode == ASE_AWK_ASSOP_DIV)
2006-04-16 04:31:38 +00:00
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_div (run, val2, val);
2006-04-16 04:31:38 +00:00
}
2006-10-24 04:10:12 +00:00
else if (ass->opcode == ASE_AWK_ASSOP_MOD)
2006-04-16 04:31:38 +00:00
{
2006-04-21 17:24:31 +00:00
tmp = __eval_binop_mod (run, val2, val);
2006-04-16 04:31:38 +00:00
}
2006-10-24 04:10:12 +00:00
else if (ass->opcode == ASE_AWK_ASSOP_EXP)
2006-04-16 04:31:38 +00:00
{
2006-10-22 12:39:30 +00:00
tmp = __eval_binop_exp (run, val2, val);
2006-04-16 04:31:38 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, !"should never happen - invalid assignment opcode");
PANIC (run, ASE_AWK_EINTERNAL);
2006-04-16 04:31:38 +00:00
}
2006-10-24 04:10:12 +00:00
if (tmp == ASE_NULL)
2006-04-16 04:31:38 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, val);
ase_awk_refdownval (run, val2);
return ASE_NULL;
2006-04-16 04:31:38 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, val);
2006-04-16 04:31:38 +00:00
val = tmp;
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val);
2006-04-16 04:31:38 +00:00
}
2006-07-13 03:10:35 +00:00
ret = __do_assignment (run, ass->left, val);
2006-10-24 04:10:12 +00:00
ase_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
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __do_assignment (
ase_awk_run_t* run, ase_awk_nde_t* var, ase_awk_val_t* val)
2006-04-02 16:22:36 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* ret;
2006-07-13 03:10:35 +00:00
2006-10-24 04:10:12 +00:00
if (val->type == ASE_AWK_VAL_MAP)
2006-04-16 16:30:59 +00:00
{
/* a map cannot be assigned to a variable */
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ENOTASSIGNABLE);
2006-04-16 16:30:59 +00:00
}
2006-10-24 04:10:12 +00:00
if (var->type == ASE_AWK_NDE_NAMED ||
var->type == ASE_AWK_NDE_GLOBAL ||
var->type == ASE_AWK_NDE_LOCAL ||
var->type == ASE_AWK_NDE_ARG)
2006-07-06 13:57:32 +00:00
{
2006-10-24 04:10:12 +00:00
ret = __do_assignment_scalar (run, (ase_awk_nde_var_t*)var, val);
2006-07-06 13:57:32 +00:00
}
2006-10-24 04:10:12 +00:00
else if (var->type == ASE_AWK_NDE_NAMEDIDX ||
var->type == ASE_AWK_NDE_GLOBALIDX ||
var->type == ASE_AWK_NDE_LOCALIDX ||
var->type == ASE_AWK_NDE_ARGIDX)
2006-07-06 13:57:32 +00:00
{
2006-10-24 04:10:12 +00:00
ret = __do_assignment_map (run, (ase_awk_nde_var_t*)var, val);
2006-07-06 13:57:32 +00:00
}
2006-10-24 04:10:12 +00:00
else if (var->type == ASE_AWK_NDE_POS)
2006-07-06 13:57:32 +00:00
{
2006-10-24 04:10:12 +00:00
ret = __do_assignment_pos (run, (ase_awk_nde_pos_t*)var, val);
2006-07-06 13:57:32 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, !"should never happen - invalid variable type");
PANIC (run, ASE_AWK_EINTERNAL);
2006-07-06 13:57:32 +00:00
}
2006-07-13 03:10:35 +00:00
return ret;
2006-07-06 13:57:32 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __do_assignment_scalar (
ase_awk_run_t* run, ase_awk_nde_var_t* var, ase_awk_val_t* val)
2006-07-06 13:57:32 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk,
(var->type == ASE_AWK_NDE_NAMED ||
var->type == ASE_AWK_NDE_GLOBAL ||
var->type == ASE_AWK_NDE_LOCAL ||
var->type == ASE_AWK_NDE_ARG) && var->idx == ASE_NULL);
2006-07-06 13:57:32 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, val->type != ASE_AWK_VAL_MAP);
2006-07-06 13:57:32 +00:00
2006-10-24 04:10:12 +00:00
if (var->type == ASE_AWK_NDE_NAMED)
2006-03-03 11:45:45 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_pair_t* pair;
2006-04-19 03:42:08 +00:00
int n;
2006-02-23 15:37:34 +00:00
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_get (
2006-08-03 05:05:48 +00:00
&run->named, var->id.name, var->id.name_len);
2006-10-24 04:10:12 +00:00
if (pair != ASE_NULL &&
((ase_awk_val_t*)pair->val)->type == ASE_AWK_VAL_MAP)
2006-07-01 16:07:06 +00:00
{
/* once a variable becomes an array,
* it cannot be changed to a scalar variable */
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_EMAPTOSCALAR);
2006-07-01 16:07:06 +00:00
}
2006-10-24 04:10:12 +00:00
n = ase_awk_map_putx (&run->named,
var->id.name, var->id.name_len, val, ASE_NULL);
if (n < 0) PANIC (run, ASE_AWK_ENOMEM);
2006-03-04 15:54:37 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val);
2006-02-23 15:37:34 +00:00
}
2006-10-24 04:10:12 +00:00
else if (var->type == ASE_AWK_NDE_GLOBAL)
2006-03-03 11:45:45 +00:00
{
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (
run, var->id.idxa, val) == -1) return ASE_NULL;
2006-02-23 15:37:34 +00:00
}
2006-10-24 04:10:12 +00:00
else if (var->type == ASE_AWK_NDE_LOCAL)
2006-03-03 11:45:45 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* old = STACK_LOCAL(run,var->id.idxa);
if (old->type == ASE_AWK_VAL_MAP)
2006-07-01 16:07:06 +00:00
{
/* once the variable becomes an array,
* it cannot be changed to a scalar variable */
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_EMAPTOSCALAR);
2006-07-01 16:07:06 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, old);
2006-04-21 17:24:31 +00:00
STACK_LOCAL(run,var->id.idxa) = val;
2006-10-24 04:10:12 +00:00
ase_awk_refupval (val);
2006-02-23 15:37:34 +00:00
}
2006-10-24 04:10:12 +00:00
else /* if (var->type == ASE_AWK_NDE_ARG) */
2006-03-03 11:45:45 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* old = STACK_ARG(run,var->id.idxa);
if (old->type == ASE_AWK_VAL_MAP)
2006-07-01 16:07:06 +00:00
{
/* once the variable becomes an array,
* it cannot be changed to a scalar variable */
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_EMAPTOSCALAR);
2006-07-01 16:07:06 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, old);
2006-04-21 17:24:31 +00:00
STACK_ARG(run,var->id.idxa) = val;
2006-10-24 04:10:12 +00:00
ase_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-10-24 04:10:12 +00:00
static ase_awk_val_t* __do_assignment_map (
ase_awk_run_t* run, ase_awk_nde_var_t* var, ase_awk_val_t* val)
2006-04-17 16:12:02 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_map_t* map;
ase_char_t* str;
ase_size_t len;
2006-04-18 14:49:42 +00:00
int n;
2006-04-16 16:30:59 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk,
(var->type == ASE_AWK_NDE_NAMEDIDX ||
var->type == ASE_AWK_NDE_GLOBALIDX ||
var->type == ASE_AWK_NDE_LOCALIDX ||
var->type == ASE_AWK_NDE_ARGIDX) && var->idx != ASE_NULL);
ase_awk_assert (run->awk, val->type != ASE_AWK_VAL_MAP);
2006-04-18 11:26:48 +00:00
2006-10-24 04:10:12 +00:00
if (var->type == ASE_AWK_NDE_NAMEDIDX)
2006-04-19 16:09:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_pair_t* pair;
pair = ase_awk_map_get (
2006-08-03 05:05:48 +00:00
&run->named, var->id.name, var->id.name_len);
2006-10-24 04:10:12 +00:00
map = (pair == ASE_NULL)?
(ase_awk_val_map_t*)ase_awk_val_nil:
(ase_awk_val_map_t*)pair->val;
2006-04-19 16:09:51 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
map = (var->type == ASE_AWK_NDE_GLOBALIDX)?
(ase_awk_val_map_t*)STACK_GLOBAL(run,var->id.idxa):
(var->type == ASE_AWK_NDE_LOCALIDX)?
(ase_awk_val_map_t*)STACK_LOCAL(run,var->id.idxa):
(ase_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-10-24 04:10:12 +00:00
if (map->type == ASE_AWK_VAL_NIL)
2006-04-17 16:12:02 +00:00
{
/* the map is not initialized yet */
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tmp;
2006-04-16 16:30:59 +00:00
2006-10-24 04:10:12 +00:00
tmp = ase_awk_makemapval (run);
if (tmp == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-17 16:12:02 +00:00
2006-10-24 04:10:12 +00:00
if (var->type == ASE_AWK_NDE_NAMEDIDX)
2006-04-19 16:09:51 +00:00
{
/* doesn't have to decrease the reference count
* of the previous value here as it is done by
2006-10-24 04:10:12 +00:00
* ase_awk_map_put */
if (ase_awk_map_put (&run->named,
var->id.name, var->id.name_len, tmp) == ASE_NULL)
2006-04-19 16:09:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
ase_awk_refdownval (run, tmp);
PANIC (run, ASE_AWK_ENOMEM);
2006-04-19 16:09:51 +00:00
}
2006-08-27 15:29:21 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
2006-04-19 16:09:51 +00:00
}
2006-10-24 04:10:12 +00:00
else if (var->type == ASE_AWK_NDE_GLOBALIDX)
2006-04-19 16:09:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
if (ase_awk_setglobal (run, var->id.idxa, tmp) == -1)
2006-08-27 15:29:21 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, tmp);
return ASE_NULL;
2006-08-27 15:29:21 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, tmp);
2006-04-19 16:09:51 +00:00
}
2006-10-24 04:10:12 +00:00
else if (var->type == ASE_AWK_NDE_LOCALIDX)
2006-04-19 16:09:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, (ase_awk_val_t*)map);
2006-04-21 17:24:31 +00:00
STACK_LOCAL(run,var->id.idxa) = tmp;
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
2006-04-19 16:09:51 +00:00
}
2006-10-24 04:10:12 +00:00
else /* if (var->type == ASE_AWK_NDE_ARGIDX) */
2006-04-19 16:09:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, (ase_awk_val_t*)map);
2006-04-21 17:24:31 +00:00
STACK_ARG(run,var->id.idxa) = tmp;
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tmp);
2006-04-19 16:09:51 +00:00
}
2006-04-18 11:26:48 +00:00
2006-10-24 04:10:12 +00:00
map = (ase_awk_val_map_t*) tmp;
2006-04-17 16:12:02 +00:00
}
2006-10-24 04:10:12 +00:00
else if (map->type != ASE_AWK_VAL_MAP)
2006-04-17 16:12:02 +00:00
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_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-10-24 04:10:12 +00:00
if (str == ASE_NULL) return ASE_NULL;
2006-04-17 16:12:02 +00:00
2006-04-18 10:28:03 +00:00
/*
2006-10-24 04:10:12 +00:00
xp_printf (ASE_T("**** index str=>%s, map->ref=%d, map->type=%d\n"), str, (int)map->ref, (int)map->type);
2006-04-18 10:28:03 +00:00
*/
2006-10-24 04:10:12 +00:00
n = ase_awk_map_putx (map->map, str, len, val, ASE_NULL);
2006-04-18 14:49:42 +00:00
if (n < 0)
2006-03-03 11:45:45 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
PANIC (run, ASE_AWK_ENOMEM);
2006-02-23 15:37:34 +00:00
}
2006-04-17 16:12:02 +00:00
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
ase_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-10-24 04:10:12 +00:00
static ase_awk_val_t* __do_assignment_pos (
ase_awk_run_t* run, ase_awk_nde_pos_t* pos, ase_awk_val_t* val)
2006-07-06 13:57:32 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* v;
ase_long_t lv;
ase_real_t rv;
ase_char_t* str;
ase_size_t len;
2006-10-03 14:38:26 +00:00
int n;
2006-07-06 13:57:32 +00:00
2006-10-22 12:39:30 +00:00
v = __eval_expression (run, pos->val);
2006-10-24 04:10:12 +00:00
if (v == ASE_NULL) return ASE_NULL;
2006-07-06 13:57:32 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
n = ase_awk_valtonum (run, v, &lv, &rv);
ase_awk_refdownval (run, v);
2006-07-06 13:57:32 +00:00
2006-10-24 04:10:12 +00:00
if (n == -1) PANIC (run, ASE_AWK_EPOSIDX);
if (n == 1) lv = (ase_long_t)rv;
if (!IS_VALID_POSIDX(lv)) PANIC (run, ASE_AWK_EPOSIDX);
2006-07-06 13:57:32 +00:00
2006-10-24 04:10:12 +00:00
if (val->type == ASE_AWK_VAL_STR)
2006-10-03 14:38:26 +00:00
{
2006-10-24 04:10:12 +00:00
str = ((ase_awk_val_str_t*)val)->buf;
len = ((ase_awk_val_str_t*)val)->len;
2006-10-03 14:38:26 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
str = ase_awk_valtostr (run, val, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &len);
if (str == ASE_NULL) return ASE_NULL;
2006-10-03 14:38:26 +00:00
}
2006-10-24 04:10:12 +00:00
n = ase_awk_setrec (run, (ase_size_t)lv, str, len);
2006-10-03 14:38:26 +00:00
2006-10-24 04:10:12 +00:00
if (val->type != ASE_AWK_VAL_STR) ASE_AWK_FREE (run->awk, str);
2006-10-03 14:38:26 +00:00
2006-10-24 04:10:12 +00:00
if (n == -1) return ASE_NULL;
2006-10-03 14:38:26 +00:00
return (lv == 0)? run->inrec.d0: run->inrec.flds[lv-1].val;
2006-07-10 04:51:38 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binary (ase_awk_run_t* run, ase_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 */
2006-10-24 04:10:12 +00:00
ASE_NULL, /* __eval_binop_lor */
ASE_NULL, /* __eval_binop_land */
ASE_NULL, /* __eval_binop_in */
2006-04-29 12:09:29 +00:00
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-10-22 12:39:30 +00:00
__eval_binop_exp,
2006-04-12 03:54:12 +00:00
2006-06-29 15:40:30 +00:00
__eval_binop_concat,
2006-10-24 04:10:12 +00:00
ASE_NULL, /* __eval_binop_ma */
ASE_NULL /* __eval_binop_nm */
2006-04-03 14:55:34 +00:00
};
2006-04-27 15:20:10 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_nde_exp_t* exp = (ase_awk_nde_exp_t*)nde;
ase_awk_val_t* left, * right, * res;
2006-03-07 15:55:14 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->type == ASE_AWK_NDE_EXP_BIN);
2006-03-07 15:55:14 +00:00
2006-10-24 04:10:12 +00:00
if (exp->opcode == ASE_AWK_BINOP_LAND)
2006-04-29 12:09:29 +00:00
{
2006-10-22 12:39:30 +00:00
res = __eval_binop_land (run, exp->left, exp->right);
2006-04-29 12:09:29 +00:00
}
2006-10-24 04:10:12 +00:00
else if (exp->opcode == ASE_AWK_BINOP_LOR)
2006-04-29 12:09:29 +00:00
{
2006-10-22 12:39:30 +00:00
res = __eval_binop_lor (run, exp->left, exp->right);
2006-04-29 12:09:29 +00:00
}
2006-10-24 04:10:12 +00:00
else if (exp->opcode == ASE_AWK_BINOP_IN)
2006-03-07 15:55:14 +00:00
{
2006-04-27 15:20:10 +00:00
/* treat the in operator specially */
2006-10-22 12:39:30 +00:00
res = __eval_binop_in (run, exp->left, exp->right);
2006-03-07 15:55:14 +00:00
}
2006-10-24 04:10:12 +00:00
else if (exp->opcode == ASE_AWK_BINOP_NM)
2006-07-31 15:59:43 +00:00
{
2006-10-22 12:39:30 +00:00
res = __eval_binop_nm (run, exp->left, exp->right);
2006-07-31 15:59:43 +00:00
}
2006-10-24 04:10:12 +00:00
else if (exp->opcode == ASE_AWK_BINOP_MA)
2006-07-31 15:59:43 +00:00
{
2006-10-22 12:39:30 +00:00
res = __eval_binop_ma (run, exp->left, exp->right);
2006-07-31 15:59:43 +00:00
}
2006-04-27 15:20:10 +00:00
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->left->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
left = __eval_expression (run, exp->left);
2006-10-24 04:10:12 +00:00
if (left == ASE_NULL) return ASE_NULL;
2006-03-07 15:55:14 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (left);
2006-03-23 15:36:20 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->right->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
right = __eval_expression (run, exp->right);
2006-10-24 04:10:12 +00:00
if (right == ASE_NULL)
2006-04-27 15:20:10 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
return ASE_NULL;
2006-04-27 15:20:10 +00:00
}
2006-04-03 15:31:33 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (right);
2006-03-22 16:05:50 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->opcode >= 0 &&
exp->opcode < ase_countof(__binop_func));
ase_awk_assert (run->awk, __binop_func[exp->opcode] != ASE_NULL);
2006-04-27 15:20:10 +00:00
2006-10-22 12:39:30 +00:00
res = __binop_func[exp->opcode] (run, left, right);
2006-04-27 15:20:10 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
ase_awk_refdownval (run, right);
2006-04-27 15:20:10 +00:00
}
2006-04-03 14:55:34 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_lor (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-29 12:09:29 +00:00
/*
2006-10-24 04:10:12 +00:00
ase_awk_val_t* res = ASE_NULL;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
res = ase_awk_makeintval (run,
ase_awk_valtobool(run left) || ase_awk_valtobool(run,right));
if (res == ASE_NULL) PANIC (run, ASE_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 */
2006-10-24 04:10:12 +00:00
ase_awk_val_t* lv, * rv, * res;
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, left->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
lv = __eval_expression (run, left);
2006-10-24 04:10:12 +00:00
if (lv == ASE_NULL) return ASE_NULL;
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (lv);
if (ase_awk_valtobool (run, lv))
2006-04-29 12:09:29 +00:00
{
2006-10-24 04:10:12 +00:00
/*res = ase_awk_makeintval (run, 1);*/
res = ase_awk_val_one;
2006-04-29 12:09:29 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, right->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
rv = __eval_expression (run, right);
2006-10-24 04:10:12 +00:00
if (rv == ASE_NULL)
2006-04-29 12:09:29 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, lv);
return ASE_NULL;
2006-04-29 12:09:29 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (rv);
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
/*res = ase_awk_makeintval (run, (ase_awk_valtobool(run,rv)? 1: 0));*/
res = ase_awk_valtobool(run,rv)? ase_awk_val_one: ase_awk_val_zero;
ase_awk_refdownval (run, rv);
2006-04-29 12:09:29 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, lv);
2006-06-28 14:19:01 +00:00
2006-10-24 04:10:12 +00:00
/*if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);*/
2006-04-29 12:09:29 +00:00
return res;
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_land (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-29 12:09:29 +00:00
/*
2006-10-24 04:10:12 +00:00
ase_awk_val_t* res = ASE_NULL;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
res = ase_awk_makeintval (run,
ase_awk_valtobool(run,left) && ase_awk_valtobool(run,right));
if (res == ASE_NULL) PANIC (run, ASE_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 */
2006-10-24 04:10:12 +00:00
ase_awk_val_t* lv, * rv, * res;
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, left->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
lv = __eval_expression (run, left);
2006-10-24 04:10:12 +00:00
if (lv == ASE_NULL) return ASE_NULL;
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (lv);
if (!ase_awk_valtobool (run, lv))
2006-04-29 12:09:29 +00:00
{
2006-10-24 04:10:12 +00:00
/*res = ase_awk_makeintval (run, 0);*/
res = ase_awk_val_zero;
2006-04-29 12:09:29 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, right->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
rv = __eval_expression (run, right);
2006-10-24 04:10:12 +00:00
if (rv == ASE_NULL)
2006-04-29 12:09:29 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, lv);
return ASE_NULL;
2006-04-29 12:09:29 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (rv);
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
/*res = ase_awk_makeintval (run, (ase_awk_valtobool(run,rv)? 1: 0));*/
res = ase_awk_valtobool(run,rv)? ase_awk_val_one: ase_awk_val_zero;
ase_awk_refdownval (run, rv);
2006-04-29 12:09:29 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, lv);
2006-06-28 14:19:01 +00:00
2006-10-24 04:10:12 +00:00
/*if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);*/
2006-04-29 12:09:29 +00:00
return res;
}
2006-06-13 15:11:39 +00:00
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_in (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right)
2006-04-29 12:09:29 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* rv;
ase_char_t* str;
ase_size_t len;
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
if (right->type != ASE_AWK_NDE_GLOBAL &&
right->type != ASE_AWK_NDE_LOCAL &&
right->type != ASE_AWK_NDE_ARG &&
right->type != ASE_AWK_NDE_NAMED)
2006-04-29 12:09:29 +00:00
{
/* the compiler should have handled this case */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, !"should never happen - in needs a plain variable");
PANIC (run, ASE_AWK_EINTERNAL);
return ASE_NULL;
2006-04-29 12:09:29 +00:00
}
/* evaluate the left-hand side of the operator */
2006-10-24 04:10:12 +00:00
str = (left->type == ASE_AWK_NDE_GRP)?
__idxnde_to_str (run, ((ase_awk_nde_grp_t*)left)->body, &len):
2006-08-03 05:05:48 +00:00
__idxnde_to_str (run, left, &len);
2006-10-24 04:10:12 +00:00
if (str == ASE_NULL) return ASE_NULL;
2006-04-29 12:09:29 +00:00
/* evaluate the right-hand side of the operator */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, right->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
rv = __eval_expression (run, right);
2006-10-24 04:10:12 +00:00
if (rv == ASE_NULL)
2006-04-29 12:09:29 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
return ASE_NULL;
2006-04-29 12:09:29 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (rv);
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
if (rv->type == ASE_AWK_VAL_NIL)
2006-04-29 12:09:29 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
ase_awk_refdownval (run, rv);
return ase_awk_val_zero;
2006-04-29 12:09:29 +00:00
}
2006-10-24 04:10:12 +00:00
else if (rv->type == ASE_AWK_VAL_MAP)
2006-04-29 12:09:29 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* res;
ase_awk_map_t* map;
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
map = ((ase_awk_val_map_t*)rv)->map;
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
/*r = (ase_long_t)(ase_awk_map_get (map, str, len) != ASE_NULL);
res = ase_awk_makeintval (run, r);
if (res == ASE_NULL)
2006-04-29 12:09:29 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
ase_awk_refdownval (run, rv);
PANIC (run, ASE_AWK_ENOMEM);
2006-09-28 04:39:42 +00:00
}*/
2006-10-24 04:10:12 +00:00
res = (ase_awk_map_get (map, str, len) == ASE_NULL)?
ase_awk_val_zero: ase_awk_val_one;
2006-04-29 12:09:29 +00:00
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
ase_awk_refdownval (run, rv);
2006-04-29 12:09:29 +00:00
return res;
}
/* need an array */
2006-08-03 05:05:48 +00:00
/* TODO: change the error code to make it clearer */
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_EOPERAND);
return ASE_NULL;
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_bor (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* res = ASE_NULL;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT &&
right->type == ASE_AWK_VAL_INT)
2006-03-27 14:59:57 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r =
((ase_awk_val_int_t*)left)->val |
((ase_awk_val_int_t*)right)->val;
res = ase_awk_makeintval (run, r);
2006-04-07 04:23:11 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_bxor (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* res = ASE_NULL;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT &&
right->type == ASE_AWK_VAL_INT)
2006-04-03 14:55:34 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r =
((ase_awk_val_int_t*)left)->val ^
((ase_awk_val_int_t*)right)->val;
res = ase_awk_makeintval (run, r);
2006-04-07 04:23:11 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_EOPERAND);
2006-03-27 14:59:57 +00:00
}
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_band (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* res = ASE_NULL;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT &&
right->type == ASE_AWK_VAL_INT)
2006-03-27 14:59:57 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r =
((ase_awk_val_int_t*)left)->val &
((ase_awk_val_int_t*)right)->val;
res = ase_awk_makeintval (run, r);
2006-04-07 04:23:11 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_EOPERAND);
2006-03-27 14:59:57 +00:00
}
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
2006-09-28 04:39:42 +00:00
static int __cmp_nil_nil (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-09-28 04:39:42 +00:00
return 0;
}
2006-04-03 14:55:34 +00:00
2006-09-28 04:39:42 +00:00
static int __cmp_nil_int (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_int_t*)right)->val < 0) return 1;
if (((ase_awk_val_int_t*)right)->val > 0) return -1;
2006-09-28 04:39:42 +00:00
return 0;
}
static int __cmp_nil_real (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_real_t*)right)->val < 0) return 1;
if (((ase_awk_val_real_t*)right)->val > 0) return -1;
2006-09-28 04:39:42 +00:00
return 0;
}
static int __cmp_nil_str (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
return (((ase_awk_val_str_t*)right)->len == 0)? 0: -1;
2006-09-28 04:39:42 +00:00
}
static int __cmp_int_nil (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_int_t*)left)->val > 0) return 1;
if (((ase_awk_val_int_t*)left)->val < 0) return -1;
2006-09-28 04:39:42 +00:00
return 0;
}
static int __cmp_int_int (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_int_t*)left)->val >
((ase_awk_val_int_t*)right)->val) return 1;
if (((ase_awk_val_int_t*)left)->val <
((ase_awk_val_int_t*)right)->val) return -1;
2006-09-28 04:39:42 +00:00
return 0;
}
static int __cmp_int_real (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_int_t*)left)->val >
((ase_awk_val_real_t*)right)->val) return 1;
if (((ase_awk_val_int_t*)left)->val <
((ase_awk_val_real_t*)right)->val) return -1;
2006-09-28 04:39:42 +00:00
return 0;
}
static int __cmp_int_str (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* str;
ase_size_t len;
ase_long_t r;
ase_real_t rr;
2006-10-04 14:52:12 +00:00
int n;
2006-10-24 04:10:12 +00:00
r = ase_awk_strxtolong (run->awk,
((ase_awk_val_str_t*)right)->buf,
((ase_awk_val_str_t*)right)->len, 0, (const ase_char_t**)&str);
if (str == ((ase_awk_val_str_t*)right)->buf +
((ase_awk_val_str_t*)right)->len)
2006-10-05 14:20:57 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_int_t*)left)->val > r) return 1;
if (((ase_awk_val_int_t*)left)->val < r) return -1;
2006-10-05 14:20:57 +00:00
return 0;
}
2006-10-06 14:35:17 +00:00
/* TODO: should i do this??? conversion to real and comparision... */
2006-10-24 04:10:12 +00:00
else if (*str == ASE_T('.') || *str == ASE_T('E') || *str == ASE_T('e'))
2006-10-05 14:20:57 +00:00
{
2006-10-24 04:10:12 +00:00
rr = ase_awk_strxtoreal (run->awk,
((ase_awk_val_str_t*)right)->buf,
((ase_awk_val_str_t*)right)->len,
(const ase_char_t**)&str);
if (str == ((ase_awk_val_str_t*)right)->buf +
((ase_awk_val_str_t*)right)->len)
2006-10-06 03:37:40 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_int_t*)left)->val > rr) return 1;
if (((ase_awk_val_int_t*)left)->val < rr) return -1;
2006-10-06 03:37:40 +00:00
return 0;
}
2006-10-05 14:20:57 +00:00
}
2006-10-24 04:10:12 +00:00
str = ase_awk_valtostr (run, left, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &len);
if (str == ASE_NULL)
2006-10-04 14:52:12 +00:00
{
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_ENOMEM;
2006-10-04 14:52:12 +00:00
return CMP_ERROR;
}
if (run->global.ignorecase)
{
2006-10-24 04:10:12 +00:00
n = ase_awk_strxncasecmp (
2006-10-04 14:52:12 +00:00
run->awk, str, len,
2006-10-24 04:10:12 +00:00
((ase_awk_val_str_t*)right)->buf,
((ase_awk_val_str_t*)right)->len);
2006-10-04 14:52:12 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
n = ase_awk_strxncmp (
2006-10-04 14:52:12 +00:00
str, len,
2006-10-24 04:10:12 +00:00
((ase_awk_val_str_t*)right)->buf,
((ase_awk_val_str_t*)right)->len);
2006-10-04 14:52:12 +00:00
}
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
2006-10-04 14:52:12 +00:00
return n;
2006-09-28 04:39:42 +00:00
}
static int __cmp_real_nil (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_real_t*)left)->val > 0) return 1;
if (((ase_awk_val_real_t*)left)->val < 0) return -1;
2006-09-28 04:39:42 +00:00
return 0;
}
static int __cmp_real_int (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_real_t*)left)->val >
((ase_awk_val_int_t*)right)->val) return 1;
if (((ase_awk_val_real_t*)left)->val <
((ase_awk_val_int_t*)right)->val) return -1;
2006-09-28 04:39:42 +00:00
return 0;
}
static int __cmp_real_real (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_real_t*)left)->val >
((ase_awk_val_real_t*)right)->val) return 1;
if (((ase_awk_val_real_t*)left)->val <
((ase_awk_val_real_t*)right)->val) return -1;
2006-09-28 04:39:42 +00:00
return 0;
}
static int __cmp_real_str (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* str;
ase_size_t len;
ase_real_t rr;
2006-10-04 14:52:12 +00:00
int n;
2006-10-24 04:10:12 +00:00
rr = ase_awk_strxtoreal (run->awk,
((ase_awk_val_str_t*)right)->buf,
((ase_awk_val_str_t*)right)->len,
(const ase_char_t**)&str);
if (str == ((ase_awk_val_str_t*)right)->buf +
((ase_awk_val_str_t*)right)->len)
2006-10-06 14:35:17 +00:00
{
2006-10-24 04:10:12 +00:00
if (((ase_awk_val_real_t*)left)->val > rr) return 1;
if (((ase_awk_val_real_t*)left)->val < rr) return -1;
2006-10-06 14:35:17 +00:00
return 0;
}
2006-10-24 04:10:12 +00:00
str = ase_awk_valtostr (run, left, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &len);
if (str == ASE_NULL)
2006-10-04 14:52:12 +00:00
{
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_ENOMEM;
2006-10-04 14:52:12 +00:00
return CMP_ERROR;
}
if (run->global.ignorecase)
{
2006-10-24 04:10:12 +00:00
n = ase_awk_strxncasecmp (
2006-10-04 14:52:12 +00:00
run->awk, str, len,
2006-10-24 04:10:12 +00:00
((ase_awk_val_str_t*)right)->buf,
((ase_awk_val_str_t*)right)->len);
2006-10-04 14:52:12 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
n = ase_awk_strxncmp (
2006-10-04 14:52:12 +00:00
str, len,
2006-10-24 04:10:12 +00:00
((ase_awk_val_str_t*)right)->buf,
((ase_awk_val_str_t*)right)->len);
2006-10-04 14:52:12 +00:00
}
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
2006-10-04 14:52:12 +00:00
return n;
2006-09-28 04:39:42 +00:00
}
static int __cmp_str_nil (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
return (((ase_awk_val_str_t*)left)->len == 0)? 0: 1;
2006-09-28 04:39:42 +00:00
}
static int __cmp_str_int (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-08 05:46:41 +00:00
return -__cmp_int_str (run, right, left);
2006-09-28 04:39:42 +00:00
}
static int __cmp_str_real (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-08 05:46:41 +00:00
return -__cmp_real_str (run, right, left);
2006-09-28 04:39:42 +00:00
}
static int __cmp_str_str (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
int n;
2006-10-24 04:10:12 +00:00
ase_awk_val_str_t* ls, * rs;
2006-09-28 04:39:42 +00:00
2006-10-24 04:10:12 +00:00
ls = (ase_awk_val_str_t*)left;
rs = (ase_awk_val_str_t*)right;
2006-09-28 04:39:42 +00:00
if (run->global.ignorecase)
2006-04-09 16:26:36 +00:00
{
2006-10-24 04:10:12 +00:00
n = ase_awk_strxncasecmp (run->awk,
2006-09-28 04:39:42 +00:00
ls->buf, ls->len, rs->buf, rs->len);
2006-04-09 16:26:36 +00:00
}
2006-09-28 04:39:42 +00:00
else
2006-03-27 14:59:57 +00:00
{
2006-10-24 04:10:12 +00:00
n = ase_awk_strxncmp (
2006-09-28 04:39:42 +00:00
ls->buf, ls->len, rs->buf, rs->len);
2006-04-06 16:25:37 +00:00
}
2006-09-28 04:39:42 +00:00
return n;
}
static int __cmp_val (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-10-24 04:10:12 +00:00
typedef int (*cmp_val_t) (ase_awk_run_t*, ase_awk_val_t*, ase_awk_val_t*);
2006-09-28 04:39:42 +00:00
static cmp_val_t func[] =
2006-04-06 16:25:37 +00:00
{
2006-09-28 06:58:10 +00:00
/* this table must be synchronized with
2006-10-24 04:10:12 +00:00
* the ASE_AWK_VAL_XXX values in val.h */
2006-09-28 04:39:42 +00:00
__cmp_nil_nil, __cmp_nil_int, __cmp_nil_real, __cmp_nil_str,
__cmp_int_nil, __cmp_int_int, __cmp_int_real, __cmp_int_str,
__cmp_real_nil, __cmp_real_int, __cmp_real_real, __cmp_real_str,
__cmp_str_nil, __cmp_str_int, __cmp_str_real, __cmp_str_str,
};
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_MAP || right->type == ASE_AWK_VAL_MAP)
2006-04-06 16:25:37 +00:00
{
2006-09-28 04:39:42 +00:00
/* a map can't be compared againt other values */
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_EOPERAND;
2006-10-04 14:52:12 +00:00
return CMP_ERROR;
2006-04-06 16:25:37 +00:00
}
2006-09-28 04:39:42 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk,
left->type >= ASE_AWK_VAL_NIL &&
left->type <= ASE_AWK_VAL_STR);
ase_awk_assert (run->awk,
right->type >= ASE_AWK_VAL_NIL &&
right->type <= ASE_AWK_VAL_STR);
2006-09-28 06:58:10 +00:00
2006-09-28 04:39:42 +00:00
return func[left->type*4+right->type] (run, left, right);
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_eq (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-09-28 04:39:42 +00:00
{
2006-09-28 06:58:10 +00:00
int n = __cmp_val (run, left, right);
2006-10-24 04:10:12 +00:00
if (n == CMP_ERROR) return ASE_NULL;
return (n == 0)? ase_awk_val_one: ase_awk_val_zero;
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_ne (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-09-28 06:58:10 +00:00
int n = __cmp_val (run, left, right);
2006-10-24 04:10:12 +00:00
if (n == CMP_ERROR) return ASE_NULL;
return (n != 0)? ase_awk_val_one: ase_awk_val_zero;
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_gt (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-09-28 06:58:10 +00:00
int n = __cmp_val (run, left, right);
2006-10-24 04:10:12 +00:00
if (n == CMP_ERROR) return ASE_NULL;
return (n > 0)? ase_awk_val_one: ase_awk_val_zero;
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_ge (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-09-28 06:58:10 +00:00
int n = __cmp_val (run, left, right);
2006-10-24 04:10:12 +00:00
if (n == CMP_ERROR) return ASE_NULL;
return (n >= 0)? ase_awk_val_one: ase_awk_val_zero;
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_lt (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-09-28 06:58:10 +00:00
int n = __cmp_val (run, left, right);
2006-10-24 04:10:12 +00:00
if (n == CMP_ERROR) return ASE_NULL;
return (n < 0)? ase_awk_val_one: ase_awk_val_zero;
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_le (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-09-28 06:58:10 +00:00
int n = __cmp_val (run, left, right);
2006-10-24 04:10:12 +00:00
if (n == CMP_ERROR) return ASE_NULL;
return (n <= 0)? ase_awk_val_one: ase_awk_val_zero;
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_lshift (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 13:30:19 +00:00
int n1, n2, n3;
2006-10-24 04:10:12 +00:00
ase_long_t l1, l2;
ase_real_t r1, r2;
ase_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
n1 = ase_awk_valtonum (run, left, &l1, &r1);
n2 = ase_awk_valtonum (run, right, &l2, &r2);
2006-04-16 13:30:19 +00:00
2006-10-24 04:10:12 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, ASE_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-10-24 04:10:12 +00:00
if (l2 == 0) PANIC (run, ASE_AWK_EDIVBYZERO);
res = ase_awk_makeintval (run, (ase_long_t)l1 << (ase_long_t)l2);
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
else PANIC (run, ASE_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_rshift (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 13:30:19 +00:00
int n1, n2, n3;
2006-10-24 04:10:12 +00:00
ase_long_t l1, l2;
ase_real_t r1, r2;
ase_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
n1 = ase_awk_valtonum (run, left, &l1, &r1);
n2 = ase_awk_valtonum (run, right, &l2, &r2);
2006-04-16 13:30:19 +00:00
2006-10-24 04:10:12 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, ASE_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-10-24 04:10:12 +00:00
if (l2 == 0) PANIC (run, ASE_AWK_EDIVBYZERO);
res = ase_awk_makeintval (run, (ase_long_t)l1 >> (ase_long_t)l2);
2006-04-03 14:55:34 +00:00
}
2006-10-24 04:10:12 +00:00
else PANIC (run, ASE_AWK_EOPERAND);
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_plus (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
2006-10-24 04:10:12 +00:00
ase_long_t l1, l2;
ase_real_t r1, r2;
ase_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
n1 = ase_awk_valtonum (run, left, &l1, &r1);
n2 = ase_awk_valtonum (run, right, &l2, &r2);
2006-04-16 06:16:42 +00:00
2006-10-24 04:10:12 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, ASE_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-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, n3 >= 0 && n3 <= 3);
res = (n3 == 0)? ase_awk_makeintval(run,(ase_long_t)l1+(ase_long_t)l2):
(n3 == 1)? ase_awk_makerealval(run,(ase_real_t)r1+(ase_real_t)l2):
(n3 == 2)? ase_awk_makerealval(run,(ase_real_t)l1+(ase_real_t)r2):
ase_awk_makerealval(run,(ase_real_t)r1+(ase_real_t)r2);
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_minus (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
2006-10-24 04:10:12 +00:00
ase_long_t l1, l2;
ase_real_t r1, r2;
ase_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
n1 = ase_awk_valtonum (run, left, &l1, &r1);
n2 = ase_awk_valtonum (run, right, &l2, &r2);
2006-04-16 06:16:42 +00:00
2006-10-24 04:10:12 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, ASE_AWK_EOPERAND);
2006-04-16 06:16:42 +00:00
n3 = n1 + (n2 << 1);
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, n3 >= 0 && n3 <= 3);
res = (n3 == 0)? ase_awk_makeintval(run,(ase_long_t)l1-(ase_long_t)l2):
(n3 == 1)? ase_awk_makerealval(run,(ase_real_t)r1-(ase_real_t)l2):
(n3 == 2)? ase_awk_makerealval(run,(ase_real_t)l1-(ase_real_t)r2):
ase_awk_makerealval(run,(ase_real_t)r1-(ase_real_t)r2);
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_mul (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
2006-10-24 04:10:12 +00:00
ase_long_t l1, l2;
ase_real_t r1, r2;
ase_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
n1 = ase_awk_valtonum (run, left, &l1, &r1);
n2 = ase_awk_valtonum (run, right, &l2, &r2);
2006-04-06 16:25:37 +00:00
2006-10-24 04:10:12 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, ASE_AWK_EOPERAND);
2006-04-16 06:16:42 +00:00
n3 = n1 + (n2 << 1);
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, n3 >= 0 && n3 <= 3);
res = (n3 == 0)? ase_awk_makeintval(run,(ase_long_t)l1*(ase_long_t)l2):
(n3 == 1)? ase_awk_makerealval(run,(ase_real_t)r1*(ase_real_t)l2):
(n3 == 2)? ase_awk_makerealval(run,(ase_real_t)l1*(ase_real_t)r2):
ase_awk_makerealval(run,(ase_real_t)r1*(ase_real_t)r2);
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_div (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
2006-10-24 04:10:12 +00:00
ase_long_t l1, l2;
ase_real_t r1, r2;
ase_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
n1 = ase_awk_valtonum (run, left, &l1, &r1);
n2 = ase_awk_valtonum (run, right, &l2, &r2);
2006-04-05 15:56:20 +00:00
2006-10-24 04:10:12 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, ASE_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-10-24 04:10:12 +00:00
if (l2 == 0) PANIC (run, ASE_AWK_EDIVBYZERO);
res = ase_awk_makeintval (run, (ase_long_t)l1 / (ase_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-10-24 04:10:12 +00:00
res = ase_awk_makerealval (run, (ase_real_t)r1 / (ase_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-10-24 04:10:12 +00:00
res = ase_awk_makerealval (run, (ase_real_t)l1 / (ase_real_t)r2);
2006-04-05 15:56:20 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, n3 == 3);
res = ase_awk_makerealval (run, (ase_real_t)r1 / (ase_real_t)r2);
2006-04-05 15:56:20 +00:00
}
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-03 14:55:34 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_mod (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-03 14:55:34 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
2006-10-24 04:10:12 +00:00
ase_long_t l1, l2;
ase_real_t r1, r2;
ase_awk_val_t* res;
2006-04-03 14:55:34 +00:00
2006-10-24 04:10:12 +00:00
n1 = ase_awk_valtonum (run, left, &l1, &r1);
n2 = ase_awk_valtonum (run, right, &l2, &r2);
2006-04-05 15:56:20 +00:00
2006-10-24 04:10:12 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, ASE_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-10-24 04:10:12 +00:00
if (l2 == 0) PANIC (run, ASE_AWK_EDIVBYZERO);
res = ase_awk_makeintval (run, (ase_long_t)l1 % (ase_long_t)l2);
2006-04-05 15:56:20 +00:00
}
2006-10-24 04:10:12 +00:00
else PANIC (run, ASE_AWK_EOPERAND);
2006-03-22 16:05:50 +00:00
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-03-22 16:05:50 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_exp (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-04-11 15:44:30 +00:00
{
2006-04-16 06:16:42 +00:00
int n1, n2, n3;
2006-10-24 04:10:12 +00:00
ase_long_t l1, l2;
ase_real_t r1, r2;
ase_awk_val_t* res;
2006-04-11 15:44:30 +00:00
2006-10-24 04:10:12 +00:00
n1 = ase_awk_valtonum (run, left, &l1, &r1);
n2 = ase_awk_valtonum (run, right, &l2, &r2);
2006-04-11 15:44:30 +00:00
2006-10-24 04:10:12 +00:00
if (n1 == -1 || n2 == -1) PANIC (run, ASE_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-10-24 04:10:12 +00:00
ase_long_t v = 1;
2006-04-16 06:16:42 +00:00
while (l2-- > 0) v *= l1;
2006-10-24 04:10:12 +00:00
res = ase_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-10-24 04:10:12 +00:00
/*res = ase_awk_makerealval (
run, pow((ase_real_t)r1,(ase_real_t)l2));*/
ase_real_t v = 1.0;
2006-04-19 16:09:51 +00:00
while (l2-- > 0) v *= r1;
2006-10-24 04:10:12 +00:00
res = ase_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-10-24 04:10:12 +00:00
res = ase_awk_makerealval (
run, pow((ase_real_t)l1,(ase_real_t)r2));
2006-04-11 15:44:30 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, n3 == 3);
res = ase_awk_makerealval (
run, pow((ase_real_t)r1,(ase_real_t)r2));
2006-04-11 15:44:30 +00:00
}
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-04-11 15:44:30 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_concat (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right)
2006-06-29 15:40:30 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* strl, * strr;
ase_size_t strl_len, strr_len;
ase_awk_val_t* res;
2006-06-30 03:53:16 +00:00
2006-10-24 04:10:12 +00:00
strl = ase_awk_valtostr (
run, left, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &strl_len);
if (strl == ASE_NULL) return ASE_NULL;
2006-06-30 03:53:16 +00:00
2006-10-24 04:10:12 +00:00
strr = ase_awk_valtostr (
run, right, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &strr_len);
if (strr == ASE_NULL)
2006-06-30 03:53:16 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, strl);
return ASE_NULL;
2006-06-30 03:53:16 +00:00
}
2006-10-24 04:10:12 +00:00
res = ase_awk_makestrval2 (run, strl, strl_len, strr, strr_len);
if (res == ASE_NULL)
2006-06-30 03:53:16 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, strl);
ASE_AWK_FREE (run->awk, strr);
PANIC (run, ASE_AWK_ENOMEM);
2006-06-30 03:53:16 +00:00
}
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, strl);
ASE_AWK_FREE (run->awk, strr);
2006-06-30 03:53:16 +00:00
return res;
2006-06-29 15:40:30 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_ma (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right)
2006-04-12 03:54:12 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* lv, * rv, * res;
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, left->next == ASE_NULL);
ase_awk_assert (run->awk, right->next == ASE_NULL);
2006-07-31 15:59:43 +00:00
2006-10-22 12:39:30 +00:00
lv = __eval_expression (run, left);
2006-10-24 04:10:12 +00:00
if (lv == ASE_NULL)
2006-08-03 09:54:16 +00:00
{
2006-10-24 04:10:12 +00:00
return ASE_NULL;
2006-08-03 09:54:16 +00:00
}
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (lv);
2006-08-01 15:57:43 +00:00
2006-10-22 12:39:30 +00:00
rv = __eval_expression0 (run, right);
2006-10-24 04:10:12 +00:00
if (rv == ASE_NULL)
2006-07-31 15:59:43 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, lv);
return ASE_NULL;
2006-07-31 15:59:43 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (rv);
2006-07-31 15:59:43 +00:00
2006-08-01 04:36:33 +00:00
res = __eval_binop_match0 (run, lv, rv, 1);
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, lv);
ase_awk_refdownval (run, rv);
2006-07-31 15:59:43 +00:00
return res;
2006-04-12 03:54:12 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_nm (
ase_awk_run_t* run, ase_awk_nde_t* left, ase_awk_nde_t* right)
2006-07-31 15:59:43 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* lv, * rv, * res;
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, left->next == ASE_NULL);
ase_awk_assert (run->awk, right->next == ASE_NULL);
2006-07-31 15:59:43 +00:00
2006-10-22 12:39:30 +00:00
lv = __eval_expression (run, left);
2006-10-24 04:10:12 +00:00
if (lv == ASE_NULL) return ASE_NULL;
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (lv);
2006-08-01 15:57:43 +00:00
2006-10-22 12:39:30 +00:00
rv = __eval_expression0 (run, right);
2006-10-24 04:10:12 +00:00
if (rv == ASE_NULL)
2006-04-24 11:22:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, lv);
return ASE_NULL;
2006-04-24 11:22:42 +00:00
}
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (rv);
2006-07-31 15:59:43 +00:00
2006-08-01 04:36:33 +00:00
res = __eval_binop_match0 (run, lv, rv, 0);
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, lv);
ase_awk_refdownval (run, rv);
2006-07-31 15:59:43 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_binop_match0 (
ase_awk_run_t* run, ase_awk_val_t* left, ase_awk_val_t* right, int ret)
2006-07-31 15:59:43 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* res;
2006-07-31 15:59:43 +00:00
int n, errnum;
2006-10-24 04:10:12 +00:00
ase_char_t* str;
ase_size_t len;
2006-08-01 04:36:33 +00:00
void* rex_code;
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
if (right->type == ASE_AWK_VAL_REX)
2006-04-24 11:22:42 +00:00
{
2006-10-24 04:10:12 +00:00
rex_code = ((ase_awk_val_rex_t*)right)->code;
2006-08-01 04:36:33 +00:00
}
2006-10-24 04:10:12 +00:00
else if (right->type == ASE_AWK_VAL_STR)
2006-08-01 04:36:33 +00:00
{
2006-10-24 04:10:12 +00:00
rex_code = ase_awk_buildrex (
2006-09-01 03:44:51 +00:00
run->awk,
2006-10-24 04:10:12 +00:00
((ase_awk_val_str_t*)right)->buf,
((ase_awk_val_str_t*)right)->len, &errnum);
if (rex_code == ASE_NULL)
2006-08-10 16:06:52 +00:00
PANIC (run, errnum);
2006-08-01 04:36:33 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
str = ase_awk_valtostr (
run, right, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &len);
if (str == ASE_NULL) return ASE_NULL;
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
rex_code = ase_awk_buildrex (run->awk, str, len, &errnum);
if (rex_code == ASE_NULL)
2006-08-01 04:36:33 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, 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
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
2006-08-01 04:36:33 +00:00
}
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_STR)
2006-08-01 04:36:33 +00:00
{
2006-10-24 04:10:12 +00:00
n = ase_awk_matchrex (
2006-09-01 03:44:51 +00:00
run->awk, rex_code,
2006-10-24 04:10:12 +00:00
((run->global.ignorecase)? ASE_AWK_REX_IGNORECASE: 0),
((ase_awk_val_str_t*)left)->buf,
((ase_awk_val_str_t*)left)->len,
ASE_NULL, ASE_NULL, &errnum);
2006-08-01 04:36:33 +00:00
if (n == -1)
2006-07-31 15:59:43 +00:00
{
2006-10-24 04:10:12 +00:00
if (right->type != ASE_AWK_VAL_REX)
ASE_AWK_FREE (run->awk, 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-10-24 04:10:12 +00:00
res = ase_awk_makeintval (run, (n == ret));
if (res == ASE_NULL)
2006-08-01 04:36:33 +00:00
{
2006-10-24 04:10:12 +00:00
if (right->type != ASE_AWK_VAL_REX)
ASE_AWK_FREE (run->awk, rex_code);
PANIC (run, ASE_AWK_ENOMEM);
2006-08-01 04:36:33 +00:00
}
}
else
{
2006-10-24 04:10:12 +00:00
str = ase_awk_valtostr (
run, left, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &len);
if (str == ASE_NULL)
2006-08-01 04:36:33 +00:00
{
2006-10-24 04:10:12 +00:00
if (right->type != ASE_AWK_VAL_REX)
ASE_AWK_FREE (run->awk, rex_code);
return ASE_NULL;
2006-08-01 04:36:33 +00:00
}
2006-07-31 15:59:43 +00:00
2006-10-24 04:10:12 +00:00
n = ase_awk_matchrex (
2006-09-01 03:44:51 +00:00
run->awk, rex_code,
2006-10-24 04:10:12 +00:00
((run->global.ignorecase)? ASE_AWK_REX_IGNORECASE: 0),
str, len, ASE_NULL, ASE_NULL, &errnum);
2006-08-01 04:36:33 +00:00
if (n == -1)
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
if (right->type != ASE_AWK_VAL_REX)
ASE_AWK_FREE (run->awk, 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-10-24 04:10:12 +00:00
res = ase_awk_makeintval (run, (n == ret));
if (res == ASE_NULL)
2006-08-01 04:36:33 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
if (right->type != ASE_AWK_VAL_REX)
ASE_AWK_FREE (run->awk, rex_code);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 15:59:43 +00:00
}
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
2006-04-24 11:22:42 +00:00
}
2006-10-24 04:10:12 +00:00
if (right->type != ASE_AWK_VAL_REX) ASE_AWK_FREE (run->awk, rex_code);
2006-08-01 04:36:33 +00:00
return res;
2006-04-12 03:54:12 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_unary (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* left, * res = ASE_NULL;
ase_awk_nde_exp_t* exp = (ase_awk_nde_exp_t*)nde;
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->type == ASE_AWK_NDE_EXP_UNR);
ase_awk_assert (run->awk, exp->left != ASE_NULL && exp->right == ASE_NULL);
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->left->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
left = __eval_expression (run, exp->left);
2006-10-24 04:10:12 +00:00
if (left == ASE_NULL) return ASE_NULL;
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (left);
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
if (exp->opcode == ASE_AWK_UNROP_PLUS)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r = ((ase_awk_val_int_t*)left)->val;
res = ase_awk_makeintval (run, r);
2006-04-02 12:45:04 +00:00
}
2006-10-24 04:10:12 +00:00
else if (left->type == ASE_AWK_VAL_REAL)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_real_t r = ((ase_awk_val_real_t*)left)->val;
res = ase_awk_makerealval (run, r);
2006-04-02 12:45:04 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EOPERAND);
2006-04-02 12:45:04 +00:00
}
}
2006-10-24 04:10:12 +00:00
else if (exp->opcode == ASE_AWK_UNROP_MINUS)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r = ((ase_awk_val_int_t*)left)->val;
res = ase_awk_makeintval (run, -r);
2006-04-02 12:45:04 +00:00
}
2006-10-24 04:10:12 +00:00
else if (left->type == ASE_AWK_VAL_REAL)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_real_t r = ((ase_awk_val_real_t*)left)->val;
res = ase_awk_makerealval (run, -r);
2006-04-02 12:45:04 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EOPERAND);
2006-04-02 12:45:04 +00:00
}
}
2006-10-24 04:10:12 +00:00
else if (exp->opcode == ASE_AWK_UNROP_NOT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r = ((ase_awk_val_int_t*)left)->val;
res = ase_awk_makeintval (run, !r);
2006-04-02 12:45:04 +00:00
}
2006-10-24 04:10:12 +00:00
else if (left->type == ASE_AWK_VAL_REAL)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_real_t r = ((ase_awk_val_real_t*)left)->val;
res = ase_awk_makerealval (run, !r);
2006-04-02 12:45:04 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EOPERAND);
2006-04-02 12:45:04 +00:00
}
}
2006-10-24 04:10:12 +00:00
else if (exp->opcode == ASE_AWK_UNROP_BNOT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r = ((ase_awk_val_int_t*)left)->val;
res = ase_awk_makeintval (run, ~r);
2006-04-02 12:45:04 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EOPERAND);
2006-04-02 12:45:04 +00:00
}
}
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL)
2006-04-09 15:31:13 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-04-09 15:31:13 +00:00
}
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
2006-04-02 12:45:04 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_incpre (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* left, * res;
ase_awk_nde_exp_t* exp = (ase_awk_nde_exp_t*)nde;
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->type == ASE_AWK_NDE_EXP_INCPRE);
ase_awk_assert (run->awk, exp->left != ASE_NULL && exp->right == ASE_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-10-24 04:10:12 +00:00
if (exp->left->type < ASE_AWK_NDE_NAMED ||
exp->left->type > ASE_AWK_NDE_ARGIDX)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_EOPERAND);
2006-04-02 16:22:36 +00:00
}
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->left->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
left = __eval_expression (run, exp->left);
2006-10-24 04:10:12 +00:00
if (left == ASE_NULL) return ASE_NULL;
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (left);
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
if (exp->opcode == ASE_AWK_INCOP_PLUS)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r = ((ase_awk_val_int_t*)left)->val;
res = ase_awk_makeintval (run, r + 1);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-02 12:45:04 +00:00
}
2006-10-24 04:10:12 +00:00
else if (left->type == ASE_AWK_VAL_REAL)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_real_t r = ((ase_awk_val_real_t*)left)->val;
res = ase_awk_makerealval (run, r + 1.0);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-02 12:45:04 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_long_t v1;
ase_real_t v2;
2006-07-31 04:25:17 +00:00
int n;
2006-10-24 04:10:12 +00:00
n = ase_awk_valtonum (run, left, &v1, &v2);
2006-07-31 04:25:17 +00:00
if (n == -1)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EOPERAND);
2006-07-31 04:25:17 +00:00
}
if (n == 0)
{
2006-10-24 04:10:12 +00:00
res = ase_awk_makeintval (run, v1 + 1);
2006-07-31 04:25:17 +00:00
}
else /* if (n == 1) */
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, n == 1);
res = ase_awk_makerealval (run, v2 + 1.0);
2006-07-31 04:25:17 +00:00
}
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-02 12:45:04 +00:00
}
}
2006-10-24 04:10:12 +00:00
else if (exp->opcode == ASE_AWK_INCOP_MINUS)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r = ((ase_awk_val_int_t*)left)->val;
res = ase_awk_makeintval (run, r - 1);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-02 12:45:04 +00:00
}
2006-10-24 04:10:12 +00:00
else if (left->type == ASE_AWK_VAL_REAL)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_real_t r = ((ase_awk_val_real_t*)left)->val;
res = ase_awk_makerealval (run, r - 1.0);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-02 12:45:04 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_long_t v1;
ase_real_t v2;
2006-07-31 04:25:17 +00:00
int n;
2006-10-24 04:10:12 +00:00
n = ase_awk_valtonum (run, left, &v1, &v2);
2006-07-31 04:25:17 +00:00
if (n == -1)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EOPERAND);
2006-07-31 04:25:17 +00:00
}
if (n == 0)
{
2006-10-24 04:10:12 +00:00
res = ase_awk_makeintval (run, v1 - 1);
2006-07-31 04:25:17 +00:00
}
else /* if (n == 1) */
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, n == 1);
res = ase_awk_makerealval (run, v2 - 1.0);
2006-07-31 04:25:17 +00:00
}
2006-10-24 04:10:12 +00:00
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-02 12:45:04 +00:00
}
}
2006-04-07 16:52:42 +00:00
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, !"should never happen - invalid opcode");
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EINTERNAL);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
if (__do_assignment (run, exp->left, res) == ASE_NULL)
2006-04-02 16:22:36 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
return ASE_NULL;
2006-04-02 16:22:36 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
2006-04-02 12:45:04 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_incpst (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* left, * res, * res2;
ase_awk_nde_exp_t* exp = (ase_awk_nde_exp_t*)nde;
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->type == ASE_AWK_NDE_EXP_INCPST);
ase_awk_assert (run->awk, exp->left != ASE_NULL && exp->right == ASE_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-10-24 04:10:12 +00:00
if (exp->left->type < ASE_AWK_NDE_NAMED ||
exp->left->type > ASE_AWK_NDE_ARGIDX)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_EOPERAND);
2006-04-02 16:22:36 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, exp->left->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
left = __eval_expression (run, exp->left);
2006-10-24 04:10:12 +00:00
if (left == ASE_NULL) return ASE_NULL;
2006-04-02 16:22:36 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (left);
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
if (exp->opcode == ASE_AWK_INCOP_PLUS)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r = ((ase_awk_val_int_t*)left)->val;
res = ase_awk_makeintval (run, r);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
res2 = ase_awk_makeintval (run, r + 1);
if (res2 == ASE_NULL)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
ase_awk_freeval (run, res, ase_true);
PANIC (run, ASE_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
}
2006-10-24 04:10:12 +00:00
else if (left->type == ASE_AWK_VAL_REAL)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_real_t r = ((ase_awk_val_real_t*)left)->val;
res = ase_awk_makerealval (run, r);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
res2 = ase_awk_makerealval (run, r + 1.0);
if (res2 == ASE_NULL)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
ase_awk_freeval (run, res, ase_true);
PANIC (run, ASE_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_long_t v1;
ase_real_t v2;
2006-07-31 04:25:17 +00:00
int n;
2006-10-24 04:10:12 +00:00
n = ase_awk_valtonum (run, left, &v1, &v2);
2006-07-31 04:25:17 +00:00
if (n == -1)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EOPERAND);
2006-07-31 04:25:17 +00:00
}
if (n == 0)
{
2006-10-24 04:10:12 +00:00
res = ase_awk_makeintval (run, v1);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-10-24 04:10:12 +00:00
res2 = ase_awk_makeintval (run, v1 + 1);
if (res2 == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
ase_awk_freeval (run, res, ase_true);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
}
else /* if (n == 1) */
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, n == 1);
res = ase_awk_makerealval (run, v2);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-10-24 04:10:12 +00:00
res2 = ase_awk_makerealval (run, v2 + 1.0);
if (res2 == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
ase_awk_freeval (run, res, ase_true);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
}
2006-04-02 12:45:04 +00:00
}
}
2006-10-24 04:10:12 +00:00
else if (exp->opcode == ASE_AWK_INCOP_MINUS)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
if (left->type == ASE_AWK_VAL_INT)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_long_t r = ((ase_awk_val_int_t*)left)->val;
res = ase_awk_makeintval (run, r);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-07 16:52:42 +00:00
2006-10-24 04:10:12 +00:00
res2 = ase_awk_makeintval (run, r - 1);
if (res2 == ASE_NULL)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
ase_awk_freeval (run, res, ase_true);
PANIC (run, ASE_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
}
2006-10-24 04:10:12 +00:00
else if (left->type == ASE_AWK_VAL_REAL)
2006-04-02 12:45:04 +00:00
{
2006-10-24 04:10:12 +00:00
ase_real_t r = ((ase_awk_val_real_t*)left)->val;
res = ase_awk_makerealval (run, r);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-04-07 16:52:42 +00:00
2006-10-24 04:10:12 +00:00
res2 = ase_awk_makerealval (run, r - 1.0);
if (res2 == ASE_NULL)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
ase_awk_freeval (run, res, ase_true);
PANIC (run, ASE_AWK_ENOMEM);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_long_t v1;
ase_real_t v2;
2006-07-31 04:25:17 +00:00
int n;
2006-10-24 04:10:12 +00:00
n = ase_awk_valtonum (run, left, &v1, &v2);
2006-07-31 04:25:17 +00:00
if (n == -1)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EOPERAND);
2006-07-31 04:25:17 +00:00
}
if (n == 0)
{
2006-10-24 04:10:12 +00:00
res = ase_awk_makeintval (run, v1);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-10-24 04:10:12 +00:00
res2 = ase_awk_makeintval (run, v1 - 1);
if (res2 == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
ase_awk_freeval (run, res, ase_true);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
}
else /* if (n == 1) */
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, n == 1);
res = ase_awk_makerealval (run, v2);
if (res == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
2006-10-24 04:10:12 +00:00
res2 = ase_awk_makerealval (run, v2 - 1.0);
if (res2 == ASE_NULL)
2006-07-31 04:25:17 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
ase_awk_freeval (run, res, ase_true);
PANIC (run, ASE_AWK_ENOMEM);
2006-07-31 04:25:17 +00:00
}
}
2006-04-02 12:45:04 +00:00
}
}
2006-04-07 16:52:42 +00:00
else
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, !"should never happen - invalid opcode");
ase_awk_refdownval (run, left);
PANIC (run, ASE_AWK_EINTERNAL);
2006-04-07 16:52:42 +00:00
}
2006-04-02 12:45:04 +00:00
2006-10-24 04:10:12 +00:00
if (__do_assignment (run, exp->left, res2) == ASE_NULL)
2006-04-02 16:22:36 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
return ASE_NULL;
2006-04-02 16:22:36 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, left);
2006-04-02 12:45:04 +00:00
return res;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_cnd (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-11 09:16:20 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tv, * v;
ase_awk_nde_cnd_t* cnd = (ase_awk_nde_cnd_t*)nde;
2006-04-11 09:16:20 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, cnd->test->next == ASE_NULL);
2006-10-22 12:39:30 +00:00
tv = __eval_expression (run, cnd->test);
2006-10-24 04:10:12 +00:00
if (tv == ASE_NULL) return ASE_NULL;
2006-04-11 09:16:20 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (tv);
2006-04-11 09:16:20 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, cnd->left->next == ASE_NULL &&
cnd->right->next == ASE_NULL);
v = (ase_awk_valtobool (run, tv))?
2006-10-22 12:39:30 +00:00
__eval_expression (run, cnd->left):
__eval_expression (run, cnd->right);
2006-04-11 09:16:20 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, tv);
2006-04-11 09:16:20 +00:00
return v;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_bfn (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-06-19 15:43:27 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_call_t* call = (ase_awk_nde_call_t*)nde;
2006-06-21 15:37:51 +00:00
/* 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
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ETOOFEWARGS);
2006-06-21 15:37:51 +00:00
}
2006-07-14 04:19:22 +00:00
if (call->nargs > call->what.bfn.max_args)
2006-06-21 15:37:51 +00:00
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ETOOMANYARGS);
2006-06-21 15:37:51 +00:00
}
2006-10-24 04:10:12 +00:00
return __eval_call (run, nde, call->what.bfn.arg_spec, ASE_NULL);
2006-06-19 15:43:27 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_afn (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-03-24 06:33:36 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_call_t* call = (ase_awk_nde_call_t*)nde;
ase_awk_afn_t* afn;
ase_awk_pair_t* pair;
2006-03-24 06:33:36 +00:00
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_get (&run->awk->tree.afns,
2006-08-03 05:05:48 +00:00
call->what.afn.name, call->what.afn.name_len);
2006-10-24 04:10:12 +00:00
if (pair == ASE_NULL) PANIC (run, ASE_AWK_ENOSUCHFUNC);
2006-03-24 06:33:36 +00:00
2006-10-24 04:10:12 +00:00
afn = (ase_awk_afn_t*)pair->val;
ase_awk_assert (run->awk, afn != ASE_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-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ETOOMANYARGS);
2006-04-18 16:04:59 +00:00
}
2006-10-24 04:10:12 +00:00
return __eval_call (run, nde, ASE_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 */
2006-10-24 04:10:12 +00:00
/*ase_awk_refdownval (run, STACK_ARG(run,nargs));*/
2006-08-18 07:52:47 +00:00
#define UNWIND_RUN_STACK(run,nargs) \
do { \
while ((nargs) > 0) \
{ \
--(nargs); \
2006-10-24 04:10:12 +00:00
ase_awk_refdownval ((run), \
2006-08-18 07:52:47 +00:00
(run)->stack[(run)->stack_top-1]); \
__raw_pop (run); \
} \
__raw_pop (run); \
__raw_pop (run); \
__raw_pop (run); \
} while (0)
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_call (
ase_awk_run_t* run, ase_awk_nde_t* nde,
const ase_char_t* bfn_arg_spec, ase_awk_afn_t* afn)
2006-06-20 15:27:50 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_call_t* call = (ase_awk_nde_call_t*)nde;
ase_size_t saved_stack_top;
ase_size_t nargs, i;
ase_awk_nde_t* p;
ase_awk_val_t* v;
2006-06-20 15:27:50 +00:00
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-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, ase_sizeof(void*) >= ase_sizeof(run->stack_top));
ase_awk_assert (run->awk, ase_sizeof(void*) >= ase_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-10-24 04:10:12 +00:00
/*xp_printf (ASE_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-10-24 04:10:12 +00:00
PANIC (run, ASE_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);
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_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-10-24 04:10:12 +00:00
if (__raw_push(run,ase_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);
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ENOMEM);
2006-03-25 17:04:36 +00:00
}
/* secure space for nargs */
2006-10-24 04:10:12 +00:00
if (__raw_push(run,ase_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);
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ENOMEM);
2006-03-25 17:04:36 +00:00
}
nargs = 0;
2006-04-07 16:52:42 +00:00
p = call->args;
2006-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-03-25 17:04:36 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, bfn_arg_spec == ASE_NULL ||
(bfn_arg_spec != ASE_NULL &&
ase_awk_strlen(bfn_arg_spec) > nargs));
2006-09-13 14:16:35 +00:00
2006-10-24 04:10:12 +00:00
if (bfn_arg_spec != ASE_NULL &&
bfn_arg_spec[nargs] == ASE_T('r'))
2006-09-30 17:03:11 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t** ref;
2006-09-30 17:03:11 +00:00
2006-10-01 14:48:48 +00:00
if (__get_reference (run, p, &ref) == -1)
2006-09-30 17:03:11 +00:00
{
UNWIND_RUN_STACK (run, nargs);
2006-10-24 04:10:12 +00:00
return ASE_NULL;
2006-09-30 17:03:11 +00:00
}
2006-10-24 04:10:12 +00:00
/* p->type-ASE_AWK_NDE_NAMED assumes that the
* derived value matches ASE_AWK_VAL_REF_XXX */
v = ase_awk_makerefval (
run, p->type-ASE_AWK_NDE_NAMED, ref);
2006-09-30 17:03:11 +00:00
}
2006-10-24 04:10:12 +00:00
else if (bfn_arg_spec != ASE_NULL &&
bfn_arg_spec[nargs] == ASE_T('x'))
2006-09-13 14:16:35 +00:00
{
2006-10-22 12:39:30 +00:00
/* a regular expression is passed to
2006-09-13 14:16:35 +00:00
* the function as it is */
2006-10-22 12:39:30 +00:00
v = __eval_expression0 (run, p);
2006-09-13 14:16:35 +00:00
}
else
{
2006-10-22 12:39:30 +00:00
v = __eval_expression (run, p);
2006-09-13 14:16:35 +00:00
}
2006-10-24 04:10:12 +00:00
if (v == ASE_NULL)
2006-03-25 17:04:36 +00:00
{
2006-08-18 07:52:47 +00:00
UNWIND_RUN_STACK (run, nargs);
2006-10-24 04:10:12 +00:00
return ASE_NULL;
2006-03-25 17:04:36 +00:00
}
2006-09-30 17:03:11 +00:00
#if 0
2006-10-24 04:10:12 +00:00
if (bfn_arg_spec != ASE_NULL &&
bfn_arg_spec[nargs] == ASE_T('r'))
2006-08-18 07:52:47 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t** ref;
ase_awk_val_t* tmp;
2006-09-13 14:16:35 +00:00
ref = __get_reference (run, p);
2006-10-24 04:10:12 +00:00
if (ref == ASE_NULL)
2006-08-18 07:52:47 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
ase_awk_refdownval (run, v);
2006-08-18 17:46:07 +00:00
2006-09-13 14:16:35 +00:00
UNWIND_RUN_STACK (run, nargs);
2006-10-24 04:10:12 +00:00
return ASE_NULL;
2006-09-13 14:16:35 +00:00
}
2006-08-19 16:34:24 +00:00
2006-10-24 04:10:12 +00:00
/* p->type-ASE_AWK_NDE_NAMED assumes that the
* derived value matches ASE_AWK_VAL_REF_XXX */
tmp = ase_awk_makerefval (
run, p->type-ASE_AWK_NDE_NAMED, ref);
if (tmp == ASE_NULL)
2006-09-13 14:16:35 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
ase_awk_refdownval (run, v);
2006-08-19 16:34:24 +00:00
2006-09-13 14:16:35 +00:00
UNWIND_RUN_STACK (run, nargs);
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ENOMEM);
2006-08-18 17:46:07 +00:00
}
2006-09-13 14:16:35 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
ase_awk_refdownval (run, v);
2006-09-13 14:16:35 +00:00
v = tmp;
2006-08-18 07:52:47 +00:00
}
2006-09-30 17:03:11 +00:00
#endif
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-10-24 04:10:12 +00:00
ase_awk_refupval (v);
ase_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-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ENOMEM);
2006-03-25 17:04:36 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
2006-03-25 17:04:36 +00:00
nargs++;
p = p->next;
}
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nargs == call->nargs);
2006-03-24 06:33:36 +00:00
2006-10-24 04:10:12 +00:00
if (afn != ASE_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 */
2006-10-24 04:10:12 +00:00
if (__raw_push(run,ase_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-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ENOMEM);
2006-06-20 15:27:50 +00:00
}
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-10-24 04:10:12 +00:00
/*xp_printf (ASE_T("running function body\n")); */
2006-03-24 06:33:36 +00:00
2006-10-24 04:10:12 +00:00
if (afn != ASE_NULL)
2006-06-20 15:27:50 +00:00
{
/* normal awk function */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, afn->body->type == ASE_AWK_NDE_BLK);
n = __run_block(run,(ase_awk_nde_blk_t*)afn->body);
2006-06-20 15:27:50 +00:00
}
else
{
n = 0;
/* built-in function */
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, call->nargs >= call->what.bfn.min_args &&
2006-07-14 04:19:22 +00:00
call->nargs <= call->what.bfn.max_args);
2006-06-21 15:37:51 +00:00
2006-10-24 04:10:12 +00:00
if (call->what.bfn.handler != ASE_NULL)
2006-10-10 07:02:38 +00:00
n = call->what.bfn.handler (run);
2006-06-20 15:27:50 +00:00
}
2006-03-26 16:36:30 +00:00
2006-10-24 04:10:12 +00:00
/*xp_printf (ASE_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-10-24 04:10:12 +00:00
nargs = (ase_size_t)STACK_NARGS(run);
/*xp_printf (ASE_T("block run complete nargs = %d\n"), (int)nargs); */
2006-03-25 17:04:36 +00:00
for (i = 0; i < nargs; i++)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, STACK_ARG(run,i));
2006-03-25 17:04:36 +00:00
}
2006-10-24 04:10:12 +00:00
/*xp_printf (ASE_T("got return value\n")); */
2006-03-26 16:36:30 +00:00
2006-10-02 14:54:32 +00:00
/* this trick has been 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);
2006-10-24 04:10:12 +00:00
ase_awk_refdownval_nofree (run, v);
2006-03-25 17:04:36 +00:00
2006-10-24 04:10:12 +00:00
run->stack_top = (ase_size_t)run->stack[run->stack_base+1];
run->stack_base = (ase_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-10-24 04:10:12 +00:00
/*xp_printf (ASE_T("returning from function stack_top=%ld, stack_base=%ld\n"), run->stack_top, run->stack_base); */
return (n == -1)? ASE_NULL: v;
2006-03-24 06:33:36 +00:00
}
2006-10-01 14:48:48 +00:00
static int __get_reference (
2006-10-24 04:10:12 +00:00
ase_awk_run_t* run, ase_awk_nde_t* nde, ase_awk_val_t*** ref)
2006-08-19 16:34:24 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_var_t* tgt = (ase_awk_nde_var_t*)nde;
ase_awk_val_t** tmp;
2006-08-19 16:34:24 +00:00
2006-10-02 14:54:32 +00:00
/* refer to __eval_indexed for application of a similar concept */
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_NAMED)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_pair_t* pair;
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_get (
2006-08-20 15:49:48 +00:00
&run->named, tgt->id.name, tgt->id.name_len);
2006-10-24 04:10:12 +00:00
if (pair == ASE_NULL)
2006-08-20 15:49:48 +00:00
{
/* 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? */
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_put (
2006-08-20 15:49:48 +00:00
&run->named, tgt->id.name,
2006-10-24 04:10:12 +00:00
tgt->id.name_len, ase_awk_val_nil);
if (pair == ASE_NULL) PANIC_I (run, ASE_AWK_ENOMEM);
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
*ref = (ase_awk_val_t**)&pair->val;
2006-10-01 14:48:48 +00:00
return 0;
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_GLOBAL)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
*ref = (ase_awk_val_t**)&STACK_GLOBAL(run,tgt->id.idxa);
2006-10-01 14:48:48 +00:00
return 0;
2006-08-20 15:49:48 +00:00
}
2006-08-19 16:34:24 +00:00
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_LOCAL)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
*ref = (ase_awk_val_t**)&STACK_LOCAL(run,tgt->id.idxa);
2006-10-01 14:48:48 +00:00
return 0;
2006-08-20 15:49:48 +00:00
}
2006-08-19 16:34:24 +00:00
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_ARG)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
*ref = (ase_awk_val_t**)&STACK_ARG(run,tgt->id.idxa);
2006-10-01 14:48:48 +00:00
return 0;
2006-08-20 15:49:48 +00:00
}
2006-08-19 16:34:24 +00:00
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_NAMEDIDX)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_pair_t* pair;
2006-08-19 16:34:24 +00:00
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_get (
2006-08-20 15:49:48 +00:00
&run->named, tgt->id.name, tgt->id.name_len);
2006-10-24 04:10:12 +00:00
if (pair == ASE_NULL)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_put (
2006-08-20 15:49:48 +00:00
&run->named, tgt->id.name,
2006-10-24 04:10:12 +00:00
tgt->id.name_len, ase_awk_val_nil);
if (pair == ASE_NULL) PANIC_I (run, ASE_AWK_ENOMEM);
2006-08-20 15:49:48 +00:00
}
2006-10-01 14:48:48 +00:00
tmp = __get_reference_indexed (
2006-10-24 04:10:12 +00:00
run, tgt, (ase_awk_val_t**)&pair->val);
if (tmp == ASE_NULL) return -1;
2006-10-01 14:48:48 +00:00
*ref = tmp;
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_GLOBALIDX)
2006-08-20 15:49:48 +00:00
{
2006-10-01 14:48:48 +00:00
tmp = __get_reference_indexed (run, tgt,
2006-10-24 04:10:12 +00:00
(ase_awk_val_t**)&STACK_GLOBAL(run,tgt->id.idxa));
if (tmp == ASE_NULL) return -1;
2006-10-01 14:48:48 +00:00
*ref = tmp;
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_LOCALIDX)
2006-08-20 15:49:48 +00:00
{
2006-10-01 14:48:48 +00:00
tmp = __get_reference_indexed (run, tgt,
2006-10-24 04:10:12 +00:00
(ase_awk_val_t**)&STACK_LOCAL(run,tgt->id.idxa));
if (tmp == ASE_NULL) return -1;
2006-10-01 14:48:48 +00:00
*ref = tmp;
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_ARGIDX)
2006-08-20 15:49:48 +00:00
{
2006-10-01 14:48:48 +00:00
tmp = __get_reference_indexed (run, tgt,
2006-10-24 04:10:12 +00:00
(ase_awk_val_t**)&STACK_ARG(run,tgt->id.idxa));
if (tmp == ASE_NULL) return -1;
2006-10-01 14:48:48 +00:00
*ref = tmp;
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
if (nde->type == ASE_AWK_NDE_POS)
2006-09-30 17:03:11 +00:00
{
int n;
2006-10-24 04:10:12 +00:00
ase_long_t lv;
ase_real_t rv;
ase_awk_val_t* v;
2006-09-30 17:03:11 +00:00
/* the position number is returned for the positional
* variable unlike other reference types. */
2006-10-24 04:10:12 +00:00
v = __eval_expression (run, ((ase_awk_nde_pos_t*)nde)->val);
if (v == ASE_NULL) return -1;
2006-09-30 17:03:11 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
n = ase_awk_valtonum (run, v, &lv, &rv);
ase_awk_refdownval (run, v);
2006-09-30 17:03:11 +00:00
2006-10-24 04:10:12 +00:00
if (n == -1) PANIC_I (run, ASE_AWK_EPOSIDX);
if (n == 1) lv = (ase_long_t)rv;
if (!IS_VALID_POSIDX(lv)) PANIC_I (run, ASE_AWK_EPOSIDX);
2006-09-30 17:03:11 +00:00
2006-10-24 04:10:12 +00:00
*ref = (ase_awk_val_t**)((ase_size_t)lv);
2006-10-01 14:48:48 +00:00
return 0;
2006-09-30 17:03:11 +00:00
}
2006-10-24 04:10:12 +00:00
PANIC_I (run, ASE_AWK_ENOTREFERENCEABLE);
2006-08-19 16:34:24 +00:00
}
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
static ase_awk_val_t** __get_reference_indexed (
ase_awk_run_t* run, ase_awk_nde_var_t* nde, ase_awk_val_t** val)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_pair_t* pair;
ase_char_t* str;
ase_size_t len;
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, val != ASE_NULL);
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
if ((*val)->type == ASE_AWK_VAL_NIL)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tmp;
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
tmp = ase_awk_makemapval (run);
if (tmp == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, *val);
2006-08-20 15:49:48 +00:00
*val = tmp;
2006-10-24 04:10:12 +00:00
ase_awk_refupval ((ase_awk_val_t*)*val);
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
else if ((*val)->type != ASE_AWK_VAL_MAP)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ENOTINDEXABLE);
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->idx != ASE_NULL);
2006-08-20 15:49:48 +00:00
str = __idxnde_to_str (run, nde->idx, &len);
2006-10-24 04:10:12 +00:00
if (str == ASE_NULL) return ASE_NULL;
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_get ((*(ase_awk_val_map_t**)val)->map, str, len);
if (pair == ASE_NULL)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_put (
(*(ase_awk_val_map_t**)val)->map,
str, len, ase_awk_val_nil);
if (pair == ASE_NULL)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
PANIC (run, ASE_AWK_ENOMEM);
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (pair->val);
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, str);
return (ase_awk_val_t**)&pair->val;
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_int (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val;
2006-09-27 14:14:47 +00:00
2006-10-24 04:10:12 +00:00
val = ase_awk_makeintval (run, ((ase_awk_nde_int_t*)nde)->val);
if (val == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
((ase_awk_val_int_t*)val)->nde = (ase_awk_nde_int_t*)nde;
2006-09-27 14:14:47 +00:00
2006-04-07 16:52:42 +00:00
return val;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_real (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val;
2006-09-27 14:14:47 +00:00
2006-10-24 04:10:12 +00:00
val = ase_awk_makerealval (run, ((ase_awk_nde_real_t*)nde)->val);
if (val == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
((ase_awk_val_real_t*)val)->nde = (ase_awk_nde_real_t*)nde;
2006-09-27 14:14:47 +00:00
2006-04-07 16:52:42 +00:00
return val;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_str (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val;
2006-07-25 16:41:40 +00:00
2006-10-24 04:10:12 +00:00
val = ase_awk_makestrval (run,
((ase_awk_nde_str_t*)nde)->buf,
((ase_awk_nde_str_t*)nde)->len);
if (val == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-07-25 16:41:40 +00:00
2006-04-07 16:52:42 +00:00
return val;
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_rex (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-24 11:22:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* val;
2006-07-25 16:41:40 +00:00
2006-10-24 04:10:12 +00:00
val = ase_awk_makerexval (run,
((ase_awk_nde_rex_t*)nde)->buf,
((ase_awk_nde_rex_t*)nde)->len,
((ase_awk_nde_rex_t*)nde)->code);
if (val == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-07-25 16:41:40 +00:00
return val;
2006-04-24 11:22:42 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_named (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_pair_t* pair;
2006-04-07 16:52:42 +00:00
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_get (&run->named,
((ase_awk_nde_var_t*)nde)->id.name,
((ase_awk_nde_var_t*)nde)->id.name_len);
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
return (pair == ASE_NULL)? ase_awk_val_nil: pair->val;
2006-04-07 16:52:42 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_global (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
return STACK_GLOBAL(run,((ase_awk_nde_var_t*)nde)->id.idxa);
2006-04-07 16:52:42 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_local (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
return STACK_LOCAL(run,((ase_awk_nde_var_t*)nde)->id.idxa);
2006-04-07 16:52:42 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_arg (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
return STACK_ARG(run,((ase_awk_nde_var_t*)nde)->id.idxa);
2006-04-07 16:52:42 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_indexed (
ase_awk_run_t* run, ase_awk_nde_var_t* nde, ase_awk_val_t** val)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_pair_t* pair;
ase_char_t* str;
ase_size_t len;
2006-04-16 13:30:19 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, val != ASE_NULL);
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
if ((*val)->type == ASE_AWK_VAL_NIL)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* tmp;
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
tmp = ase_awk_makemapval (run);
if (tmp == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, *val);
2006-08-20 15:49:48 +00:00
*val = tmp;
2006-10-24 04:10:12 +00:00
ase_awk_refupval ((ase_awk_val_t*)*val);
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
else if ((*val)->type != ASE_AWK_VAL_MAP)
2006-04-18 11:26:48 +00:00
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ENOTINDEXABLE);
2006-04-18 11:26:48 +00:00
}
2006-04-16 16:30:59 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde->idx != ASE_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-10-24 04:10:12 +00:00
if (str == ASE_NULL) return ASE_NULL;
2006-04-16 13:30:19 +00:00
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_get ((*(ase_awk_val_map_t**)val)->map, str, len);
ASE_AWK_FREE (run->awk, str);
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
return (pair == ASE_NULL)? ase_awk_val_nil: (ase_awk_val_t*)pair->val;
2006-04-07 16:52:42 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_namedidx (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-19 16:09:51 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_var_t* tgt = (ase_awk_nde_var_t*)nde;
ase_awk_pair_t* pair;
2006-04-19 16:09:51 +00:00
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_get (&run->named, tgt->id.name, tgt->id.name_len);
if (pair == ASE_NULL)
2006-08-20 15:49:48 +00:00
{
2006-10-24 04:10:12 +00:00
pair = ase_awk_map_put (&run->named,
tgt->id.name, tgt->id.name_len, ase_awk_val_nil);
if (pair == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-08-20 15:49:48 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (pair->val);
2006-08-20 15:49:48 +00:00
}
2006-10-24 04:10:12 +00:00
return __eval_indexed (run, tgt, (ase_awk_val_t**)&pair->val);
2006-04-19 16:09:51 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_globalidx (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-18 11:26:48 +00:00
{
2006-10-24 04:10:12 +00:00
return __eval_indexed (run, (ase_awk_nde_var_t*)nde,
(ase_awk_val_t**)&STACK_GLOBAL(run,((ase_awk_nde_var_t*)nde)->id.idxa));
2006-04-18 11:26:48 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_localidx (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
return __eval_indexed (run, (ase_awk_nde_var_t*)nde,
(ase_awk_val_t**)&STACK_LOCAL(run,((ase_awk_nde_var_t*)nde)->id.idxa));
2006-04-07 16:52:42 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_argidx (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
return __eval_indexed (run, (ase_awk_nde_var_t*)nde,
(ase_awk_val_t**)&STACK_ARG(run,((ase_awk_nde_var_t*)nde)->id.idxa));
2006-04-07 16:52:42 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_pos (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-04-07 16:52:42 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_pos_t* pos = (ase_awk_nde_pos_t*)nde;
ase_awk_val_t* v;
ase_long_t lv;
ase_real_t rv;
2006-07-05 16:20:23 +00:00
int n;
2006-10-22 12:39:30 +00:00
v = __eval_expression (run, pos->val);
2006-10-24 04:10:12 +00:00
if (v == ASE_NULL) return ASE_NULL;
2006-07-05 16:20:23 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
n = ase_awk_valtonum (run, v, &lv, &rv);
ase_awk_refdownval (run, v);
2006-07-05 16:20:23 +00:00
2006-10-24 04:10:12 +00:00
if (n == -1) PANIC (run, ASE_AWK_EPOSIDX);
if (n == 1) lv = (ase_long_t)rv;
2006-07-05 16:20:23 +00:00
2006-10-24 04:10:12 +00:00
if (lv < 0) PANIC (run, ASE_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-10-24 04:10:12 +00:00
else v = ase_awk_val_zls; /*ase_awk_val_nil;*/
2006-07-05 16:20:23 +00:00
return v;
2006-04-07 16:52:42 +00:00
}
2006-10-24 04:10:12 +00:00
static ase_awk_val_t* __eval_getline (ase_awk_run_t* run, ase_awk_nde_t* nde)
2006-06-12 15:11:02 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_nde_getline_t* p;
ase_awk_val_t* v, * res;
ase_char_t* in = ASE_NULL;
const ase_char_t* dst;
ase_awk_str_t buf;
2006-08-31 15:39:14 +00:00
int n;
2006-06-16 07:37:27 +00:00
2006-10-24 04:10:12 +00:00
p = (ase_awk_nde_getline_t*)nde;
2006-06-28 14:19:01 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, (p->in_type == ASE_AWK_IN_PIPE && p->in != ASE_NULL) ||
(p->in_type == ASE_AWK_IN_COPROC && p->in != ASE_NULL) ||
(p->in_type == ASE_AWK_IN_FILE && p->in != ASE_NULL) ||
(p->in_type == ASE_AWK_IN_CONSOLE && p->in == ASE_NULL));
2006-06-16 07:37:27 +00:00
2006-10-24 04:10:12 +00:00
if (p->in != ASE_NULL)
2006-06-16 07:37:27 +00:00
{
2006-10-24 04:10:12 +00:00
ase_size_t len;
2006-08-02 03:22:51 +00:00
2006-10-22 12:39:30 +00:00
v = __eval_expression (run, p->in);
2006-10-24 04:10:12 +00:00
if (v == ASE_NULL) return ASE_NULL;
2006-06-29 14:38:01 +00:00
2006-10-24 04:10:12 +00:00
/* TODO: distinction between v->type == ASE_AWK_VAL_STR
* and v->type != ASE_AWK_VAL_STR
2006-08-02 03:22:51 +00:00
* if you use the buffer the v directly when
2006-10-24 04:10:12 +00:00
* v->type == ASE_AWK_VAL_STR, ase_awk_refdownval(v)
2006-08-02 03:22:51 +00:00
* should not be called immediately below */
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
in = ase_awk_valtostr (
run, v, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &len);
if (in == ASE_NULL)
2006-06-29 14:38:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v);
return ASE_NULL;
2006-06-29 14:38:01 +00:00
}
2006-10-24 04:10:12 +00:00
ase_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 */
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, in);
2006-08-02 03:34:34 +00:00
n = -1;
goto skip_read;
}
2006-08-02 03:22:51 +00:00
while (len > 0)
{
2006-10-24 04:10:12 +00:00
if (in[--len] == ASE_T('\0'))
2006-08-02 03:22:51 +00:00
{
/* the input source name contains a null
* character. make getline return -1 */
2006-08-22 15:11:13 +00:00
/* TODO: set ERRNO */
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, in);
2006-08-02 03:22:51 +00:00
n = -1;
goto skip_read;
}
}
2006-06-28 10:40:24 +00:00
}
2006-06-29 14:38:01 +00:00
2006-10-24 04:10:12 +00:00
dst = (in == ASE_NULL)? ASE_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-10-24 04:10:12 +00:00
if (ase_awk_str_open (&buf, DEF_BUF_CAPA, run->awk) == ASE_NULL)
2006-06-28 14:19:01 +00:00
{
2006-10-24 04:10:12 +00:00
if (in != ASE_NULL) ASE_AWK_FREE (run->awk, in);
PANIC (run, ASE_AWK_ENOMEM);
2006-06-28 14:19:01 +00:00
}
2006-10-24 04:10:12 +00:00
n = ase_awk_readextio (run, p->in_type, dst, &buf);
if (in != ASE_NULL) ASE_AWK_FREE (run->awk, 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-10-24 04:10:12 +00:00
if (run->errnum != ASE_AWK_EIOHANDLER)
2006-07-28 10:34:22 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_str_close (&buf);
return ASE_NULL;
2006-07-28 10:34:22 +00:00
}
2006-10-24 04:10:12 +00:00
/* if run->errnum == ASE_AWK_EIOHANDLER,
2006-08-27 15:29:21 +00:00
* 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)
{
2006-10-24 04:10:12 +00:00
if (p->var == ASE_NULL)
2006-06-28 14:19:01 +00:00
{
2006-07-27 16:50:29 +00:00
/* set $0 with the input value */
2006-10-24 04:10:12 +00:00
if (ase_awk_setrec (run, 0,
ASE_AWK_STR_BUF(&buf),
ASE_AWK_STR_LEN(&buf)) == -1)
2006-07-27 16:50:29 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_str_close (&buf);
return ASE_NULL;
2006-07-27 16:50:29 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_str_close (&buf);
2006-06-28 14:19:01 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* v;
2006-06-28 14:19:01 +00:00
2006-10-24 04:10:12 +00:00
v = ase_awk_makestrval (
run, ASE_AWK_STR_BUF(&buf), ASE_AWK_STR_LEN(&buf));
ase_awk_str_close (&buf);
if (v == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-10-03 14:38:26 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (v);
if (__do_assignment(run, p->var, v) == ASE_NULL)
2006-06-28 14:19:01 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v);
return ASE_NULL;
2006-06-28 14:19:01 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, v);
2006-06-28 14:19:01 +00:00
}
}
2006-07-27 16:50:29 +00:00
else
{
2006-10-24 04:10:12 +00:00
ase_awk_str_close (&buf);
2006-07-27 16:50:29 +00:00
}
2006-06-28 14:19:01 +00:00
2006-08-02 03:22:51 +00:00
skip_read:
2006-10-24 04:10:12 +00:00
res = ase_awk_makeintval (run, n);
if (res == ASE_NULL) PANIC (run, ASE_AWK_ENOMEM);
2006-06-28 14:19:01 +00:00
return res;
2006-06-12 15:11:02 +00:00
}
2006-10-24 04:10:12 +00:00
static int __raw_push (ase_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-10-24 04:10:12 +00:00
ase_size_t n;
2006-03-24 06:33:36 +00:00
2006-04-21 17:24:31 +00:00
n = run->stack_limit + STACK_INCREMENT;
2006-03-24 06:33:36 +00:00
2006-10-24 04:10:12 +00:00
if (run->awk->syscas.realloc != ASE_NULL)
2006-08-16 11:35:54 +00:00
{
2006-10-24 04:10:12 +00:00
tmp = (void**) ASE_AWK_REALLOC (
run->awk, run->stack, n * ase_sizeof(void*));
if (tmp == ASE_NULL) return -1;
2006-08-31 14:52:12 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
tmp = (void**) ASE_AWK_MALLOC (
run->awk, n * ase_sizeof(void*));
if (tmp == ASE_NULL) return -1;
if (run->stack != ASE_NULL)
2006-08-31 14:52:12 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_MEMCPY (run->awk, tmp, run->stack,
run->stack_limit * ase_sizeof(void*));
ASE_AWK_FREE (run->awk, run->stack);
2006-08-31 14:52:12 +00:00
}
2006-08-16 11:35:54 +00:00
}
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-10-24 04:10:12 +00:00
static void __raw_pop_times (ase_awk_run_t* run, ase_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-10-24 04:10:12 +00:00
static int __read_record (ase_awk_run_t* run)
2006-04-22 13:54:53 +00:00
{
2006-10-24 04:10:12 +00:00
ase_ssize_t n;
2006-04-22 13:54:53 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_clrrec (run, ase_false) == -1) return -1;
2006-04-22 13:54:53 +00:00
2006-10-24 04:10:12 +00:00
n = ase_awk_readextio (
run, ASE_AWK_IN_CONSOLE, ASE_T(""), &run->inrec.line);
2006-07-27 16:50:29 +00:00
if (n < 0)
2006-04-22 13:54:53 +00:00
{
2006-09-12 15:20:18 +00:00
int errnum = run->errnum;
2006-10-24 04:10:12 +00:00
ase_awk_clrrec (run, ase_false);
2006-09-12 15:20:18 +00:00
run->errnum =
2006-10-24 04:10:12 +00:00
(errnum == ASE_AWK_EIOHANDLER)?
ASE_AWK_ECONINDATA: errnum;
2006-09-12 15:20:18 +00:00
return -1;
2006-07-27 16:50:29 +00:00
}
2006-10-03 15:04:59 +00:00
/*
2006-10-24 04:10:12 +00:00
xp_printf (ASE_T("len = %d str=[%s]\n"),
(int)ASE_AWK_STR_LEN(&run->inrec.line),
ASE_AWK_STR_BUF(&run->inrec.line));
2006-10-03 15:04:59 +00:00
*/
2006-07-27 16:50:29 +00:00
if (n == 0)
{
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, ASE_AWK_STR_LEN(&run->inrec.line) == 0);
2006-07-27 16:50:29 +00:00
return 0;
}
2006-04-22 13:54:53 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_setrec (run, 0,
ASE_AWK_STR_BUF(&run->inrec.line),
ASE_AWK_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-10-24 04:10:12 +00:00
static int __shorten_record (ase_awk_run_t* run, ase_size_t nflds)
2006-09-10 15:50:34 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* v;
ase_char_t* ofs_free = ASE_NULL, * ofs;
ase_size_t ofs_len, i;
ase_awk_str_t tmp;
2006-09-10 15:50:34 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nflds <= run->inrec.nflds);
2006-09-10 15:50:34 +00:00
if (nflds > 1)
{
2006-10-24 04:10:12 +00:00
v = STACK_GLOBAL(run, ASE_AWK_GLOBAL_OFS);
ase_awk_refupval (v);
2006-09-10 15:50:34 +00:00
2006-10-24 04:10:12 +00:00
if (v->type == ASE_AWK_VAL_NIL)
2006-09-10 15:50:34 +00:00
{
/* OFS not set */
2006-10-24 04:10:12 +00:00
ofs = ASE_T(" ");
2006-09-10 15:50:34 +00:00
ofs_len = 1;
}
2006-10-24 04:10:12 +00:00
else if (v->type == ASE_AWK_VAL_STR)
2006-09-10 15:50:34 +00:00
{
2006-10-24 04:10:12 +00:00
ofs = ((ase_awk_val_str_t*)v)->buf;
ofs_len = ((ase_awk_val_str_t*)v)->len;
2006-09-10 15:50:34 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
ofs = ase_awk_valtostr (
run, v, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &ofs_len);
if (ofs == ASE_NULL) return -1;
2006-09-10 15:50:34 +00:00
ofs_free = ofs;
}
}
2006-10-24 04:10:12 +00:00
if (ase_awk_str_open (&tmp,
ASE_AWK_STR_LEN(&run->inrec.line), run->awk) == ASE_NULL)
2006-09-10 15:50:34 +00:00
{
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_ENOMEM;
2006-09-10 15:50:34 +00:00
return -1;
}
for (i = 0; i < nflds; i++)
{
2006-10-24 04:10:12 +00:00
if (i > 0 && ase_awk_str_ncat (&tmp, ofs, ofs_len) == (ase_size_t)-1)
2006-09-10 15:50:34 +00:00
{
2006-10-24 04:10:12 +00:00
if (ofs_free != ASE_NULL)
ASE_AWK_FREE (run->awk, ofs_free);
if (nflds > 1) ase_awk_refdownval (run, v);
run->errnum = ASE_AWK_ENOMEM;
2006-09-10 15:50:34 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
if (ase_awk_str_ncat (&tmp,
2006-09-10 15:50:34 +00:00
run->inrec.flds[i].ptr,
2006-10-24 04:10:12 +00:00
run->inrec.flds[i].len) == (ase_size_t)-1)
2006-09-10 15:50:34 +00:00
{
2006-10-24 04:10:12 +00:00
if (ofs_free != ASE_NULL)
ASE_AWK_FREE (run->awk, ofs_free);
if (nflds > 1) ase_awk_refdownval (run, v);
run->errnum = ASE_AWK_ENOMEM;
2006-09-10 15:50:34 +00:00
return -1;
}
}
2006-10-24 04:10:12 +00:00
if (ofs_free != ASE_NULL) ASE_AWK_FREE (run->awk, ofs_free);
if (nflds > 1) ase_awk_refdownval (run, v);
2006-09-10 15:50:34 +00:00
2006-10-24 04:10:12 +00:00
v = (ase_awk_val_t*) ase_awk_makestrval (
run, ASE_AWK_STR_BUF(&tmp), ASE_AWK_STR_LEN(&tmp));
if (v == ASE_NULL)
2006-09-10 15:50:34 +00:00
{
2006-10-24 04:10:12 +00:00
run->errnum = ASE_AWK_ENOMEM;
2006-09-10 15:50:34 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, run->inrec.d0);
2006-09-10 15:50:34 +00:00
run->inrec.d0 = v;
2006-10-24 04:10:12 +00:00
ase_awk_refupval (run->inrec.d0);
2006-09-10 15:50:34 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_str_swap (&tmp, &run->inrec.line);
ase_awk_str_close (&tmp);
2006-09-10 15:50:34 +00:00
for (i = nflds; i < run->inrec.nflds; i++)
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, run->inrec.flds[i].val);
2006-09-10 15:50:34 +00:00
}
run->inrec.nflds = nflds;
return 0;
}
2006-10-24 04:10:12 +00:00
static ase_char_t* __idxnde_to_str (
ase_awk_run_t* run, ase_awk_nde_t* nde, ase_size_t* len)
2006-05-03 15:40:19 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* str;
ase_awk_val_t* idx;
2006-05-03 15:40:19 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_assert (run->awk, nde != ASE_NULL);
2006-05-03 15:40:19 +00:00
2006-10-24 04:10:12 +00:00
if (nde->next == ASE_NULL)
2006-05-03 15:40:19 +00:00
{
/* single node index */
2006-10-22 12:39:30 +00:00
idx = __eval_expression (run, nde);
2006-10-24 04:10:12 +00:00
if (idx == ASE_NULL) return ASE_NULL;
2006-05-03 15:40:19 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_refupval (idx);
2006-05-03 15:40:19 +00:00
2006-10-24 04:10:12 +00:00
str = ase_awk_valtostr (
run, idx, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, len);
if (str == ASE_NULL)
2006-05-03 15:40:19 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, idx);
return ASE_NULL;
2006-05-03 15:40:19 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, idx);
2006-05-03 15:40:19 +00:00
}
else
{
/* multidimensional index */
2006-10-24 04:10:12 +00:00
ase_awk_str_t idxstr;
2006-05-03 15:40:19 +00:00
2006-10-24 04:10:12 +00:00
if (ase_awk_str_open (
&idxstr, DEF_BUF_CAPA, run->awk) == ASE_NULL)
2006-05-03 15:40:19 +00:00
{
2006-10-24 04:10:12 +00:00
PANIC (run, ASE_AWK_ENOMEM);
2006-05-03 15:40:19 +00:00
}
2006-10-24 04:10:12 +00:00
while (nde != ASE_NULL)
2006-05-03 15:40:19 +00:00
{
2006-10-22 12:39:30 +00:00
idx = __eval_expression (run, nde);
2006-10-24 04:10:12 +00:00
if (idx == ASE_NULL)
2006-05-03 15:40:19 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_str_close (&idxstr);
return ASE_NULL;
2006-05-03 15:40:19 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refupval (idx);
2006-05-03 15:40:19 +00:00
2006-10-24 04:10:12 +00:00
if (ASE_AWK_STR_LEN(&idxstr) > 0 &&
ase_awk_str_ncat (&idxstr,
2006-09-22 14:05:30 +00:00
run->global.subsep.ptr,
2006-10-24 04:10:12 +00:00
run->global.subsep.len) == (ase_size_t)-1)
2006-05-03 15:40:19 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, idx);
ase_awk_str_close (&idxstr);
PANIC (run, ASE_AWK_ENOMEM);
2006-05-03 15:40:19 +00:00
}
2006-10-24 04:10:12 +00:00
if (ase_awk_valtostr (
run, idx, 0, &idxstr, ASE_NULL) == ASE_NULL)
2006-05-03 15:40:19 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, idx);
ase_awk_str_close (&idxstr);
return ASE_NULL;
2006-05-03 15:40:19 +00:00
}
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, idx);
2006-05-03 15:40:19 +00:00
nde = nde->next;
}
2006-10-24 04:10:12 +00:00
str = ASE_AWK_STR_BUF(&idxstr);
*len = ASE_AWK_STR_LEN(&idxstr);
ase_awk_str_forfeit (&idxstr);
2006-05-03 15:40:19 +00:00
}
return str;
}