2009-07-10 06:46:14 +00:00
|
|
|
/*
|
2009-09-16 04:01:02 +00:00
|
|
|
* $Id$
|
2009-07-10 06:46:14 +00:00
|
|
|
*
|
2009-09-16 04:01:02 +00:00
|
|
|
Copyright 2006-2009 Chung, Hyung-Hwan.
|
|
|
|
This file is part of QSE.
|
2009-07-10 06:46:14 +00:00
|
|
|
|
2009-09-16 04:01:02 +00:00
|
|
|
QSE is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of
|
|
|
|
the License, or (at your option) any later version.
|
2009-07-10 06:46:14 +00:00
|
|
|
|
2009-09-16 04:01:02 +00:00
|
|
|
QSE is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
2009-07-10 06:46:14 +00:00
|
|
|
|
2009-09-16 04:01:02 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
|
2009-07-10 06:46:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <qse/awk/StdAwk.hpp>
|
|
|
|
#include <qse/cmn/stdio.h>
|
|
|
|
#include <qse/cmn/main.h>
|
|
|
|
|
2009-08-26 07:07:54 +00:00
|
|
|
static void print_error (
|
|
|
|
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
|
2009-07-10 06:46:14 +00:00
|
|
|
{
|
2009-08-26 03:50:07 +00:00
|
|
|
if (loc.lin > 0 || loc.col > 0)
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s at LINE %lu COLUMN %lu\n"), msg, loc.lin, loc.col);
|
2009-07-10 06:46:14 +00:00
|
|
|
else
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-07-13 07:06:01 +00:00
|
|
|
static int run_awk (QSE::StdAwk& awk)
|
2009-07-10 06:46:14 +00:00
|
|
|
{
|
|
|
|
QSE::StdAwk::Run* run;
|
|
|
|
|
|
|
|
const qse_char_t* script = QSE_T(
|
2009-07-13 07:06:01 +00:00
|
|
|
"function add (a, b) { return a + b }\n"
|
|
|
|
"function mul (a, b) { return a * b }\n"
|
|
|
|
"function div (a, b) { return a / b }\n"
|
2009-07-17 02:27:53 +00:00
|
|
|
"function sine (a) { return sin(a) }\n"
|
2009-07-10 06:46:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
QSE::StdAwk::SourceString in (script);
|
2009-07-13 07:06:01 +00:00
|
|
|
QSE::StdAwk::SourceFile out (QSE_T("awk06.out"));
|
2009-07-10 06:46:14 +00:00
|
|
|
|
2009-07-13 07:06:01 +00:00
|
|
|
// parse the script and deparse it to awk06.out
|
2009-07-14 04:03:53 +00:00
|
|
|
run = awk.parse (in, out);
|
2009-07-13 07:06:01 +00:00
|
|
|
if (run == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
QSE::StdAwk::Value arg[2];
|
|
|
|
if (arg[0].setInt (run, -20) <= -1) return -1;
|
|
|
|
if (arg[1].setStr (run, QSE_T("51")) <= -1) return -1;
|
|
|
|
|
|
|
|
// ret = add (-20, 51)
|
|
|
|
QSE::StdAwk::Value ret;
|
2009-07-17 02:27:53 +00:00
|
|
|
if (awk.call (QSE_T("add"), &ret, arg, 2) <= -1) return -1;
|
2009-07-13 07:06:01 +00:00
|
|
|
|
|
|
|
// ret = mul (ret, 51);
|
|
|
|
arg[0] = ret;
|
2009-07-17 02:27:53 +00:00
|
|
|
if (awk.call (QSE_T("mul"), &ret, arg, 2) <= -1) return -1;
|
2009-07-13 07:06:01 +00:00
|
|
|
|
|
|
|
// ret = div (ret, 2);
|
|
|
|
arg[0] = ret;
|
|
|
|
if (arg[1].setReal (run, 2) <= -1) return -1;
|
2009-07-17 02:27:53 +00:00
|
|
|
if (awk.call (QSE_T("div"), &ret, arg, 2) <= -1) return -1;
|
|
|
|
|
|
|
|
// output the result in various types
|
|
|
|
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)ret.toInt());
|
|
|
|
qse_printf (QSE_T(" (real) [%Lf]\n"), (long double)ret.toReal());
|
|
|
|
qse_printf (QSE_T(" (str) [%s]\n"), ret.toStr(QSE_NULL));
|
|
|
|
|
|
|
|
// ret = sine (ret);
|
|
|
|
arg[0] = ret;
|
|
|
|
if (awk.call (QSE_T("sine"), &ret, arg, 1) <= -1) return -1;
|
2009-07-13 07:06:01 +00:00
|
|
|
|
|
|
|
// output the result in various types
|
|
|
|
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)ret.toInt());
|
|
|
|
qse_printf (QSE_T(" (real) [%Lf]\n"), (long double)ret.toReal());
|
|
|
|
qse_printf (QSE_T(" (str) [%s]\n"), ret.toStr(QSE_NULL));
|
2009-07-10 06:46:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-13 07:06:01 +00:00
|
|
|
static int awk_main (int argc, qse_char_t* argv[])
|
|
|
|
{
|
|
|
|
QSE::StdAwk awk;
|
|
|
|
|
|
|
|
int ret = awk.open();
|
|
|
|
|
|
|
|
if (ret >= 0) ret = run_awk (awk);
|
2009-08-26 03:50:07 +00:00
|
|
|
if (ret <= -1)
|
|
|
|
{
|
2009-08-26 07:07:54 +00:00
|
|
|
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
|
2009-08-26 03:50:07 +00:00
|
|
|
print_error (loc, awk.getErrorMessage());
|
|
|
|
}
|
2009-07-13 07:06:01 +00:00
|
|
|
|
|
|
|
awk.close ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-07-10 06:46:14 +00:00
|
|
|
int qse_main (int argc, qse_achar_t* argv[])
|
|
|
|
{
|
|
|
|
return qse_runmain (argc,argv,awk_main);
|
|
|
|
}
|