From 845b4ae4f0aea1473f5a51bef30628e3f36806f0 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 18 May 2011 20:32:39 +0000 Subject: [PATCH] included the int() function into the main awk --- qse/include/qse/awk/StdAwk.hpp | 5 ++--- qse/lib/awk/StdAwk.cpp | 9 +-------- qse/lib/awk/fnc.c | 29 ++++++++++++++++++++++++++++- qse/lib/awk/std.c | 34 ++++------------------------------ 4 files changed, 35 insertions(+), 42 deletions(-) diff --git a/qse/include/qse/awk/StdAwk.hpp b/qse/include/qse/awk/StdAwk.hpp index 11a091b3..e61892ca 100644 --- a/qse/include/qse/awk/StdAwk.hpp +++ b/qse/include/qse/awk/StdAwk.hpp @@ -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. This file is part of QSE. @@ -103,8 +103,6 @@ public: protected: // 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, const char_t* name, size_t len); 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 freeMem (void* ptr); + int vsprintf (char_t* buf, size_t size, const char_t* fmt, va_list arg); diff --git a/qse/lib/awk/StdAwk.cpp b/qse/lib/awk/StdAwk.cpp index a7010795..d47868a7 100644 --- a/qse/lib/awk/StdAwk.cpp +++ b/qse/lib/awk/StdAwk.cpp @@ -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. This file is part of QSE. @@ -56,7 +56,6 @@ int StdAwk::open () int n = Awk::open (); if (n == -1) return n; - ADDFNC (QSE_T("int"), 1, 1, &StdAwk::fnint); ADDFNC (QSE_T("rand"), 0, 0, &StdAwk::rand); ADDFNC (QSE_T("srand"), 0, 1, &StdAwk::srand); ADDFNC (QSE_T("system"), 1, 1, &StdAwk::system); @@ -76,12 +75,6 @@ void StdAwk::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, const char_t* name, size_t len) { diff --git a/qse/lib/awk/fnc.c b/qse/lib/awk/fnc.c index 2d823f70..d8a30bc9 100644 --- a/qse/lib/awk/fnc.c +++ b/qse/lib/awk/fnc.c @@ -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. 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_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_int (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm); #undef MAX #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("exp"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_exp}, { {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} }; @@ -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); } + +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; +} diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index a1f77ff9..37174913 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -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. 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); } -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) { qse_awk_val_t* r; @@ -1516,9 +1491,8 @@ skip_system: 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("srand"), 0, 1, fnc_srand); - ADDFNC (awk, QSE_T("system"), 1, 1, fnc_system); + ADDFNC (awk, QSE_T("rand"), 0, 0, fnc_rand); + ADDFNC (awk, QSE_T("srand"), 0, 1, fnc_srand); + ADDFNC (awk, QSE_T("system"), 1, 1, fnc_system); return 0; }