included the int() function into the main awk

This commit is contained in:
hyung-hwan 2011-05-18 20:32:39 +00:00
parent 4da9a4d010
commit 845b4ae4f0
4 changed files with 35 additions and 42 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: StdAwk.hpp 460 2011-05-17 14:56:54Z hyunghwan.chung $ * $Id: StdAwk.hpp 461 2011-05-18 02:32:39Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -103,8 +103,6 @@ public:
protected: protected:
// intrinsic functions // intrinsic functions
int fnint (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len);
int rand (Run& run, Value& ret, const Value* args, size_t nargs, int rand (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len); const char_t* name, size_t len);
int srand (Run& run, Value& ret, const Value* args, size_t nargs, int srand (Run& run, Value& ret, const Value* args, size_t nargs,
@ -139,6 +137,7 @@ protected:
void* reallocMem (void* ptr, size_t n); void* reallocMem (void* ptr, size_t n);
void freeMem (void* ptr); void freeMem (void* ptr);
int vsprintf (char_t* buf, size_t size, int vsprintf (char_t* buf, size_t size,
const char_t* fmt, va_list arg); const char_t* fmt, va_list arg);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: StdAwk.cpp 460 2011-05-17 14:56:54Z hyunghwan.chung $ * $Id: StdAwk.cpp 461 2011-05-18 02:32:39Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -56,7 +56,6 @@ int StdAwk::open ()
int n = Awk::open (); int n = Awk::open ();
if (n == -1) return n; if (n == -1) return n;
ADDFNC (QSE_T("int"), 1, 1, &StdAwk::fnint);
ADDFNC (QSE_T("rand"), 0, 0, &StdAwk::rand); ADDFNC (QSE_T("rand"), 0, 0, &StdAwk::rand);
ADDFNC (QSE_T("srand"), 0, 1, &StdAwk::srand); ADDFNC (QSE_T("srand"), 0, 1, &StdAwk::srand);
ADDFNC (QSE_T("system"), 1, 1, &StdAwk::system); ADDFNC (QSE_T("system"), 1, 1, &StdAwk::system);
@ -76,12 +75,6 @@ void StdAwk::close ()
Awk::close (); Awk::close ();
} }
int StdAwk::fnint (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len)
{
return ret.setInt (args[0].toInt());
}
int StdAwk::rand (Run& run, Value& ret, const Value* args, size_t nargs, int StdAwk::rand (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {

View File

@ -1,5 +1,5 @@
/* /*
* $Id: fnc.c 459 2011-05-17 14:37:51Z hyunghwan.chung $ * $Id: fnc.c 461 2011-05-18 02:32:39Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -41,6 +41,7 @@ static int fnc_atan2 (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
static int fnc_log (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm); static int fnc_log (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
static int fnc_exp (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm); static int fnc_exp (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
static int fnc_sqrt (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm); static int fnc_sqrt (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
static int fnc_int (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
#undef MAX #undef MAX
#define MAX QSE_TYPE_UNSIGNED_MAX(qse_size_t) #define MAX QSE_TYPE_UNSIGNED_MAX(qse_size_t)
@ -85,6 +86,7 @@ static qse_awk_fnc_t sys_fnc[] =
{ {QSE_T("log"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_log}, { {QSE_T("log"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_log},
{ {QSE_T("exp"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_exp}, { {QSE_T("exp"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_exp},
{ {QSE_T("sqrt"), 4}, 0, 0, {1, 1, QSE_NULL}, fnc_sqrt}, { {QSE_T("sqrt"), 4}, 0, 0, {1, 1, QSE_NULL}, fnc_sqrt},
{ {QSE_T("int"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_int},
{ {QSE_NULL, 0}, 0, 0, {0, 0, QSE_NULL}, QSE_NULL} { {QSE_NULL, 0}, 0, 0, {0, 0, QSE_NULL}, QSE_NULL}
}; };
@ -1637,3 +1639,28 @@ static int fnc_sqrt (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
{ {
return fnc_math_1 (rtx, fnm, rtx->awk->prm.math.sqrt); return fnc_math_1 (rtx, fnm, rtx->awk->prm.math.sqrt);
} }
static int fnc_int (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
{
qse_size_t nargs;
qse_awk_val_t* a0;
qse_long_t lv;
qse_real_t rv;
qse_awk_val_t* r;
int n;
nargs = qse_awk_rtx_getnargs (run);
QSE_ASSERT (nargs == 1);
a0 = qse_awk_rtx_getarg (run, 0);
n = qse_awk_rtx_valtonum (run, a0, &lv, &rv);
if (n == -1) return -1;
if (n == 1) lv = (qse_long_t)rv;
r = qse_awk_rtx_makeintval (run, lv);
if (r == QSE_NULL) return -1;
qse_awk_rtx_setretval (run, r);
return 0;
}

View File

@ -1,5 +1,5 @@
/* /*
* $Id: std.c 459 2011-05-17 14:37:51Z hyunghwan.chung $ * $Id: std.c 461 2011-05-18 02:32:39Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -1338,31 +1338,6 @@ void* qse_awk_rtx_getxtnstd (qse_awk_rtx_t* rtx)
return (void*)((rxtn_t*)QSE_XTN(rtx) + 1); return (void*)((rxtn_t*)QSE_XTN(rtx) + 1);
} }
static int fnc_int (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
{
qse_size_t nargs;
qse_awk_val_t* a0;
qse_long_t lv;
qse_real_t rv;
qse_awk_val_t* r;
int n;
nargs = qse_awk_rtx_getnargs (run);
QSE_ASSERT (nargs == 1);
a0 = qse_awk_rtx_getarg (run, 0);
n = qse_awk_rtx_valtonum (run, a0, &lv, &rv);
if (n == -1) return -1;
if (n == 1) lv = (qse_long_t)rv;
r = qse_awk_rtx_makeintval (run, lv);
if (r == QSE_NULL) return -1;
qse_awk_rtx_setretval (run, r);
return 0;
}
static int fnc_rand (qse_awk_rtx_t* run, const qse_cstr_t* fnm) static int fnc_rand (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
{ {
qse_awk_val_t* r; qse_awk_val_t* r;
@ -1516,9 +1491,8 @@ skip_system:
static int add_functions (qse_awk_t* awk) static int add_functions (qse_awk_t* awk)
{ {
ADDFNC (awk, QSE_T("int"), 1, 1, fnc_int); ADDFNC (awk, QSE_T("rand"), 0, 0, fnc_rand);
ADDFNC (awk, QSE_T("rand"), 0, 0, fnc_rand); ADDFNC (awk, QSE_T("srand"), 0, 1, fnc_srand);
ADDFNC (awk, QSE_T("srand"), 0, 1, fnc_srand); ADDFNC (awk, QSE_T("system"), 1, 1, fnc_system);
ADDFNC (awk, QSE_T("system"), 1, 1, fnc_system);
return 0; return 0;
} }