*** empty log message ***
This commit is contained in:
parent
a83e38a2e0
commit
3eda39390d
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: awk.h,v 1.49 2006-04-16 04:31:38 bacon Exp $
|
* $Id: awk.h,v 1.50 2006-04-16 13:30:18 bacon Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _XP_AWK_AWK_H_
|
#ifndef _XP_AWK_AWK_H_
|
||||||
@ -41,44 +41,46 @@ enum
|
|||||||
/* error code */
|
/* error code */
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
XP_AWK_ENOERR, /* no error */
|
XP_AWK_ENOERR, /* no error */
|
||||||
XP_AWK_ENOMEM, /* out of memory */
|
XP_AWK_ENOMEM, /* out of memory */
|
||||||
|
|
||||||
XP_AWK_ESRCOP,
|
XP_AWK_ESRCOP,
|
||||||
XP_AWK_ESRCCL,
|
XP_AWK_ESRCCL,
|
||||||
XP_AWK_ESRCDT, /* error in reading source */
|
XP_AWK_ESRCDT, /* error in reading source */
|
||||||
|
|
||||||
XP_AWK_ELXCHR, /* lexer came accross an wrong character */
|
XP_AWK_ELXCHR, /* lexer came accross an wrong character */
|
||||||
XP_AWK_ELXUNG, /* lexer failed to unget a character */
|
XP_AWK_ELXUNG, /* lexer failed to unget a character */
|
||||||
|
|
||||||
XP_AWK_EENDSRC, /* unexpected end of source */
|
XP_AWK_EENDSRC, /* unexpected end of source */
|
||||||
XP_AWK_EENDSTR, /* unexpected end of a string */
|
XP_AWK_EENDSTR, /* unexpected end of a string */
|
||||||
XP_AWK_ELBRACE, /* left brace expected */
|
XP_AWK_ELBRACE, /* left brace expected */
|
||||||
XP_AWK_ELPAREN, /* left parenthesis expected */
|
XP_AWK_ELPAREN, /* left parenthesis expected */
|
||||||
XP_AWK_ERPAREN, /* right parenthesis expected */
|
XP_AWK_ERPAREN, /* right parenthesis expected */
|
||||||
XP_AWK_ERBRACK, /* right bracket expected */
|
XP_AWK_ERBRACK, /* right bracket expected */
|
||||||
XP_AWK_ECOMMA, /* comma expected */
|
XP_AWK_ECOMMA, /* comma expected */
|
||||||
XP_AWK_ESEMICOLON, /* semicolon expected */
|
XP_AWK_ESEMICOLON, /* semicolon expected */
|
||||||
XP_AWK_ECOLON, /* colon expected */
|
XP_AWK_ECOLON, /* colon expected */
|
||||||
XP_AWK_EEXPRESSION, /* expression expected */
|
XP_AWK_EEXPRESSION, /* expression expected */
|
||||||
|
|
||||||
XP_AWK_EWHILE, /* keyword 'while' is expected */
|
XP_AWK_EWHILE, /* keyword 'while' is expected */
|
||||||
XP_AWK_EASSIGNMENT, /* assignment statement expected */
|
XP_AWK_EASSIGNMENT, /* assignment statement expected */
|
||||||
XP_AWK_EIDENT, /* identifier expected */
|
XP_AWK_EIDENT, /* identifier expected */
|
||||||
XP_AWK_EDUPBEGIN, /* duplicate BEGIN */
|
XP_AWK_EDUPBEGIN, /* duplicate BEGIN */
|
||||||
XP_AWK_EDUPEND, /* duplicate END */
|
XP_AWK_EDUPEND, /* duplicate END */
|
||||||
XP_AWK_EDUPFUNC, /* duplicate function name */
|
XP_AWK_EDUPFUNC, /* duplicate function name */
|
||||||
XP_AWK_EDUPPARAM, /* duplicate parameter name */
|
XP_AWK_EDUPPARAM, /* duplicate parameter name */
|
||||||
XP_AWK_EDUPVAR, /* duplicate variable name */
|
XP_AWK_EDUPVAR, /* duplicate variable name */
|
||||||
XP_AWK_EDUPNAME, /* duplicate name - function, variable, etc */
|
XP_AWK_EDUPNAME, /* duplicate name - function, variable, etc */
|
||||||
XP_AWK_EUNDEF, /* undefined identifier */
|
XP_AWK_EUNDEF, /* undefined identifier */
|
||||||
XP_AWK_ELVALUE, /* l-value required */
|
XP_AWK_ELVALUE, /* l-value required */
|
||||||
|
|
||||||
/* run time error */
|
/* run time error */
|
||||||
XP_AWK_EDIVBYZERO, /* divide by zero */
|
XP_AWK_EDIVBYZERO, /* divide by zero */
|
||||||
XP_AWK_EOPERAND, /* invalid operand */
|
XP_AWK_EOPERAND, /* invalid operand */
|
||||||
XP_AWK_ENOSUCHFUNC, /* no such function */
|
XP_AWK_ENOSUCHFUNC, /* no such function */
|
||||||
XP_AWK_EINTERNAL /* internal error */
|
XP_AWK_ENOTINDEXABLE, /* not indexable value */
|
||||||
|
XP_AWK_EINDEX, /* wrong index */
|
||||||
|
XP_AWK_EINTERNAL /* internal error */
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: err.c,v 1.9 2006-04-10 15:52:07 bacon Exp $
|
* $Id: err.c,v 1.10 2006-04-16 13:30:19 bacon Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <xp/awk/awk_i.h>
|
#include <xp/awk/awk_i.h>
|
||||||
@ -47,6 +47,8 @@ const xp_char_t* xp_awk_geterrstr (xp_awk_t* awk)
|
|||||||
XP_TEXT("divide by zero"),
|
XP_TEXT("divide by zero"),
|
||||||
XP_TEXT("invalid operand"),
|
XP_TEXT("invalid operand"),
|
||||||
XP_TEXT("no such function"),
|
XP_TEXT("no such function"),
|
||||||
|
XP_TEXT("value not indexable"),
|
||||||
|
XP_TEXT("wrong index value"),
|
||||||
|
|
||||||
XP_TEXT("internal error that should never have happened")
|
XP_TEXT("internal error that should never have happened")
|
||||||
};
|
};
|
||||||
|
161
ase/awk/run.c
161
ase/awk/run.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: run.c,v 1.51 2006-04-16 06:16:42 bacon Exp $
|
* $Id: run.c,v 1.52 2006-04-16 13:30:19 bacon Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <xp/awk/awk_i.h>
|
#include <xp/awk/awk_i.h>
|
||||||
@ -116,7 +116,7 @@ static void __raw_pop (xp_awk_t* awk);
|
|||||||
static void __raw_pop_times (xp_awk_t* awk, xp_size_t times);
|
static void __raw_pop_times (xp_awk_t* awk, xp_size_t times);
|
||||||
|
|
||||||
static int __val_to_num (xp_awk_val_t* v, xp_long_t* l, xp_real_t* r);
|
static int __val_to_num (xp_awk_val_t* v, xp_long_t* l, xp_real_t* r);
|
||||||
static int __str_to_num (xp_awk_val_str_t* str, xp_long_t* l, xp_real_t* r);
|
static xp_char_t* __val_to_str (xp_awk_val_t* v);
|
||||||
|
|
||||||
typedef xp_awk_val_t* (*binop_func_t) (
|
typedef xp_awk_val_t* (*binop_func_t) (
|
||||||
xp_awk_t* awk, xp_awk_val_t* left, xp_awk_val_t* right);
|
xp_awk_t* awk, xp_awk_val_t* left, xp_awk_val_t* right);
|
||||||
@ -1280,20 +1280,23 @@ static xp_awk_val_t* __eval_binop_le (
|
|||||||
static xp_awk_val_t* __eval_binop_lshift (
|
static xp_awk_val_t* __eval_binop_lshift (
|
||||||
xp_awk_t* awk, xp_awk_val_t* left, xp_awk_val_t* right)
|
xp_awk_t* awk, xp_awk_val_t* left, xp_awk_val_t* right)
|
||||||
{
|
{
|
||||||
xp_awk_val_t* res = XP_NULL;
|
int n1, n2, n3;
|
||||||
|
xp_long_t l1, l2;
|
||||||
|
xp_real_t r1, r2;
|
||||||
|
xp_awk_val_t* res;
|
||||||
|
|
||||||
if (left->type == XP_AWK_VAL_INT &&
|
n1 = __val_to_num (left, &l1, &r1);
|
||||||
right->type == XP_AWK_VAL_INT)
|
n2 = __val_to_num (right, &l2, &r2);
|
||||||
|
|
||||||
|
if (n1 == -1 || n2 == -1) PANIC (awk, XP_AWK_EOPERAND);
|
||||||
|
|
||||||
|
n3 = n1 + (n2 << 1);
|
||||||
|
if (n3 == 0)
|
||||||
{
|
{
|
||||||
xp_long_t r =
|
if (l2 == 0) PANIC (awk, XP_AWK_EDIVBYZERO);
|
||||||
((xp_awk_val_int_t*)left)->val <<
|
res = xp_awk_makeintval (awk, (xp_long_t)l1 << (xp_long_t)l2);
|
||||||
((xp_awk_val_int_t*)right)->val;
|
|
||||||
res = xp_awk_makeintval (awk, r);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
PANIC (awk, XP_AWK_EOPERAND);
|
|
||||||
}
|
}
|
||||||
|
else PANIC (awk, XP_AWK_EOPERAND);
|
||||||
|
|
||||||
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
||||||
return res;
|
return res;
|
||||||
@ -1302,20 +1305,23 @@ static xp_awk_val_t* __eval_binop_lshift (
|
|||||||
static xp_awk_val_t* __eval_binop_rshift (
|
static xp_awk_val_t* __eval_binop_rshift (
|
||||||
xp_awk_t* awk, xp_awk_val_t* left, xp_awk_val_t* right)
|
xp_awk_t* awk, xp_awk_val_t* left, xp_awk_val_t* right)
|
||||||
{
|
{
|
||||||
xp_awk_val_t* res = XP_NULL;
|
int n1, n2, n3;
|
||||||
|
xp_long_t l1, l2;
|
||||||
|
xp_real_t r1, r2;
|
||||||
|
xp_awk_val_t* res;
|
||||||
|
|
||||||
if (left->type == XP_AWK_VAL_INT &&
|
n1 = __val_to_num (left, &l1, &r1);
|
||||||
right->type == XP_AWK_VAL_INT)
|
n2 = __val_to_num (right, &l2, &r2);
|
||||||
|
|
||||||
|
if (n1 == -1 || n2 == -1) PANIC (awk, XP_AWK_EOPERAND);
|
||||||
|
|
||||||
|
n3 = n1 + (n2 << 1);
|
||||||
|
if (n3 == 0)
|
||||||
{
|
{
|
||||||
xp_long_t r =
|
if (l2 == 0) PANIC (awk, XP_AWK_EDIVBYZERO);
|
||||||
((xp_awk_val_int_t*)left)->val >>
|
res = xp_awk_makeintval (awk, (xp_long_t)l1 >> (xp_long_t)l2);
|
||||||
((xp_awk_val_int_t*)right)->val;
|
|
||||||
res = xp_awk_makeintval (awk, r);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
PANIC (awk, XP_AWK_EOPERAND);
|
|
||||||
}
|
}
|
||||||
|
else PANIC (awk, XP_AWK_EOPERAND);
|
||||||
|
|
||||||
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
||||||
return res;
|
return res;
|
||||||
@ -1341,23 +1347,11 @@ static xp_awk_val_t* __eval_binop_plus (
|
|||||||
1 1 = 3
|
1 1 = 3
|
||||||
*/
|
*/
|
||||||
n3 = n1 + (n2 << 1);
|
n3 = n1 + (n2 << 1);
|
||||||
if (n3 == 0)
|
xp_assert (n3 >= 0 && n3 <= 3);
|
||||||
{
|
res = (n3 == 0)? xp_awk_makeintval(awk,(xp_long_t)l1+(xp_long_t)l2):
|
||||||
res = xp_awk_makeintval (awk, (xp_long_t)l1 + (xp_long_t)l2);
|
(n3 == 1)? xp_awk_makerealval(awk,(xp_real_t)r1+(xp_real_t)l2):
|
||||||
}
|
(n3 == 2)? xp_awk_makerealval(awk,(xp_real_t)l1+(xp_real_t)r2):
|
||||||
else if (n3 == 1)
|
xp_awk_makerealval(awk,(xp_real_t)r1+(xp_real_t)r2);
|
||||||
{
|
|
||||||
res = xp_awk_makerealval (awk, (xp_real_t)r1 + (xp_real_t)l2);
|
|
||||||
}
|
|
||||||
else if (n3 == 2)
|
|
||||||
{
|
|
||||||
res = xp_awk_makerealval (awk, (xp_real_t)l1 + (xp_real_t)r2);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
xp_assert (n3 == 3);
|
|
||||||
res = xp_awk_makerealval (awk, (xp_real_t)r1 + (xp_real_t)r2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
||||||
return res;
|
return res;
|
||||||
@ -1377,23 +1371,11 @@ static xp_awk_val_t* __eval_binop_minus (
|
|||||||
if (n1 == -1 || n2 == -1) PANIC (awk, XP_AWK_EOPERAND);
|
if (n1 == -1 || n2 == -1) PANIC (awk, XP_AWK_EOPERAND);
|
||||||
|
|
||||||
n3 = n1 + (n2 << 1);
|
n3 = n1 + (n2 << 1);
|
||||||
if (n3 == 0)
|
xp_assert (n3 >= 0 && n3 <= 3);
|
||||||
{
|
res = (n3 == 0)? xp_awk_makeintval(awk,(xp_long_t)l1-(xp_long_t)l2):
|
||||||
res = xp_awk_makeintval (awk, (xp_long_t)l1 - (xp_long_t)l2);
|
(n3 == 1)? xp_awk_makerealval(awk,(xp_real_t)r1-(xp_real_t)l2):
|
||||||
}
|
(n3 == 2)? xp_awk_makerealval(awk,(xp_real_t)l1-(xp_real_t)r2):
|
||||||
else if (n3 == 1)
|
xp_awk_makerealval(awk,(xp_real_t)r1-(xp_real_t)r2);
|
||||||
{
|
|
||||||
res = xp_awk_makerealval (awk, (xp_real_t)r1 - (xp_real_t)l2);
|
|
||||||
}
|
|
||||||
else if (n3 == 2)
|
|
||||||
{
|
|
||||||
res = xp_awk_makerealval (awk, (xp_real_t)l1 - (xp_real_t)r2);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
xp_assert (n3 == 3);
|
|
||||||
res = xp_awk_makerealval (awk, (xp_real_t)r1 - (xp_real_t)r2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
||||||
return res;
|
return res;
|
||||||
@ -1413,23 +1395,11 @@ static xp_awk_val_t* __eval_binop_mul (
|
|||||||
if (n1 == -1 || n2 == -1) PANIC (awk, XP_AWK_EOPERAND);
|
if (n1 == -1 || n2 == -1) PANIC (awk, XP_AWK_EOPERAND);
|
||||||
|
|
||||||
n3 = n1 + (n2 << 1);
|
n3 = n1 + (n2 << 1);
|
||||||
if (n3 == 0)
|
xp_assert (n3 >= 0 && n3 <= 3);
|
||||||
{
|
res = (n3 == 0)? xp_awk_makeintval(awk,(xp_long_t)l1*(xp_long_t)l2):
|
||||||
res = xp_awk_makeintval (awk, (xp_long_t)l1 * (xp_long_t)l2);
|
(n3 == 1)? xp_awk_makerealval(awk,(xp_real_t)r1*(xp_real_t)l2):
|
||||||
}
|
(n3 == 2)? xp_awk_makerealval(awk,(xp_real_t)l1*(xp_real_t)r2):
|
||||||
else if (n3 == 1)
|
xp_awk_makerealval(awk,(xp_real_t)r1*(xp_real_t)r2);
|
||||||
{
|
|
||||||
res = xp_awk_makerealval (awk, (xp_real_t)r1 * (xp_real_t)l2);
|
|
||||||
}
|
|
||||||
else if (n3 == 2)
|
|
||||||
{
|
|
||||||
res = xp_awk_makerealval (awk, (xp_real_t)l1 * (xp_real_t)r2);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
xp_assert (n3 == 3);
|
|
||||||
res = xp_awk_makerealval (awk, (xp_real_t)r1 * (xp_real_t)r2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
if (res == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
|
||||||
return res;
|
return res;
|
||||||
@ -1661,7 +1631,7 @@ static xp_awk_val_t* __eval_incpre (xp_awk_t* awk, xp_awk_nde_t* nde)
|
|||||||
exp->left->type != XP_AWK_NDE_NAMEDIDX &&
|
exp->left->type != XP_AWK_NDE_NAMEDIDX &&
|
||||||
exp->left->type != XP_AWK_NDE_GLOBALIDX &&
|
exp->left->type != XP_AWK_NDE_GLOBALIDX &&
|
||||||
exp->left->type != XP_AWK_NDE_LOCALIDX &&
|
exp->left->type != XP_AWK_NDE_LOCALIDX &&
|
||||||
exp->left->type != XP_AWK_NDE_ARGIDX)*/
|
exp->left->type != XP_AWK_NDE_ARGIDX) */
|
||||||
if (exp->left->type < XP_AWK_NDE_NAMED ||
|
if (exp->left->type < XP_AWK_NDE_NAMED ||
|
||||||
exp->left->type > XP_AWK_NDE_ARGIDX)
|
exp->left->type > XP_AWK_NDE_ARGIDX)
|
||||||
{
|
{
|
||||||
@ -2104,7 +2074,23 @@ static xp_awk_val_t* __eval_namedidx (xp_awk_t* awk, xp_awk_nde_t* nde)
|
|||||||
|
|
||||||
static xp_awk_val_t* __eval_globalidx (xp_awk_t* awk, xp_awk_nde_t* nde)
|
static xp_awk_val_t* __eval_globalidx (xp_awk_t* awk, xp_awk_nde_t* nde)
|
||||||
{
|
{
|
||||||
/* TODO: */
|
xp_awk_nde_idx_t* tgt = (xp_awk_nde_idx_t*)nde;
|
||||||
|
xp_awk_val_t* v = STACK_GLOBAL(awk,tgt->id.idxa);
|
||||||
|
xp_awk_val_t* idx;
|
||||||
|
xp_char_t* str;
|
||||||
|
|
||||||
|
if (v->type != XP_AWK_VAL_MAP) PANIC (awk, XP_AWK_ENOTINDEXABLE);
|
||||||
|
|
||||||
|
idx = __eval_expression (awk, tgt->idx);
|
||||||
|
str = __val_to_str (idx);
|
||||||
|
if (str == XP_NULL)
|
||||||
|
{
|
||||||
|
/* TODO: how to tell memory error from conversion error? */
|
||||||
|
/*PANIC (awk, XP_AWK_ENOMEM);*/
|
||||||
|
PANIC (awk, XP_AWK_EINDEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
xp_free (str);
|
||||||
return XP_NULL;
|
return XP_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2198,24 +2184,17 @@ static int __val_to_num (xp_awk_val_t* v, xp_long_t* l, xp_real_t* r)
|
|||||||
return 0; /* long */
|
return 0; /* long */
|
||||||
}
|
}
|
||||||
|
|
||||||
xp_assert (!"should never happen - invalid value type");
|
|
||||||
return -1; /* error */
|
return -1; /* error */
|
||||||
}
|
}
|
||||||
|
|
||||||
static int __str_to_num (xp_awk_val_str_t* str, xp_long_t* l, xp_real_t* r)
|
static xp_char_t* __val_to_str (xp_awk_val_t* v)
|
||||||
{
|
{
|
||||||
const xp_char_t* endptr;
|
/*TODO:*/
|
||||||
|
if (v->type == XP_AWK_VAL_STR)
|
||||||
/* don't care about str->len */
|
|
||||||
*l = xp_awk_strtolong (str->buf, 0, &endptr);
|
|
||||||
|
|
||||||
if (*endptr == XP_CHAR('.') ||
|
|
||||||
*endptr == XP_CHAR('E') ||
|
|
||||||
*endptr == XP_CHAR('e'))
|
|
||||||
{
|
{
|
||||||
*r = xp_awk_strtoreal (((xp_awk_val_str_t*)str)->buf);
|
return xp_strxdup (
|
||||||
return 1; /* real */
|
((xp_awk_val_str_t*)v)->buf,
|
||||||
|
((xp_awk_val_str_t*)v)->len);
|
||||||
}
|
}
|
||||||
|
return XP_NULL;
|
||||||
return 0; /* long */
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user