enhanced Awk::Awk to utilize Awk::Value

This commit is contained in:
hyung-hwan 2009-07-13 07:06:01 +00:00
parent 356e4c0792
commit 814ed89e53
8 changed files with 467 additions and 1031 deletions

20
qse/configure vendored
View File

@ -1,6 +1,6 @@
#! /bin/sh #! /bin/sh
# Guess values for system-dependent variables and create Makefiles. # Guess values for system-dependent variables and create Makefiles.
# Generated by GNU Autoconf 2.63 for qse 0.5.1. # Generated by GNU Autoconf 2.63 for qse 0.5.2.
# #
# Report bugs to <bacon@abiyo.net>. # Report bugs to <bacon@abiyo.net>.
# #
@ -745,8 +745,8 @@ SHELL=${CONFIG_SHELL-/bin/sh}
# Identity of this package. # Identity of this package.
PACKAGE_NAME='qse' PACKAGE_NAME='qse'
PACKAGE_TARNAME='qse' PACKAGE_TARNAME='qse'
PACKAGE_VERSION='0.5.1' PACKAGE_VERSION='0.5.2'
PACKAGE_STRING='qse 0.5.1' PACKAGE_STRING='qse 0.5.2'
PACKAGE_BUGREPORT='bacon@abiyo.net' PACKAGE_BUGREPORT='bacon@abiyo.net'
# Factoring default headers for most tests. # Factoring default headers for most tests.
@ -1511,7 +1511,7 @@ if test "$ac_init_help" = "long"; then
# Omit some internal or obsolete options to make the list less imposing. # Omit some internal or obsolete options to make the list less imposing.
# This message is too long to be a string in the A/UX 3.1 sh. # This message is too long to be a string in the A/UX 3.1 sh.
cat <<_ACEOF cat <<_ACEOF
\`configure' configures qse 0.5.1 to adapt to many kinds of systems. \`configure' configures qse 0.5.2 to adapt to many kinds of systems.
Usage: $0 [OPTION]... [VAR=VALUE]... Usage: $0 [OPTION]... [VAR=VALUE]...
@ -1581,7 +1581,7 @@ fi
if test -n "$ac_init_help"; then if test -n "$ac_init_help"; then
case $ac_init_help in case $ac_init_help in
short | recursive ) echo "Configuration of qse 0.5.1:";; short | recursive ) echo "Configuration of qse 0.5.2:";;
esac esac
cat <<\_ACEOF cat <<\_ACEOF
@ -1695,7 +1695,7 @@ fi
test -n "$ac_init_help" && exit $ac_status test -n "$ac_init_help" && exit $ac_status
if $ac_init_version; then if $ac_init_version; then
cat <<\_ACEOF cat <<\_ACEOF
qse configure 0.5.1 qse configure 0.5.2
generated by GNU Autoconf 2.63 generated by GNU Autoconf 2.63
Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
@ -1709,7 +1709,7 @@ cat >config.log <<_ACEOF
This file contains any messages produced by compilers while This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake. running configure, to aid debugging if configure makes a mistake.
It was created by qse $as_me 0.5.1, which was It was created by qse $as_me 0.5.2, which was
generated by GNU Autoconf 2.63. Invocation command line was generated by GNU Autoconf 2.63. Invocation command line was
$ $0 $@ $ $0 $@
@ -2436,7 +2436,7 @@ fi
# Define the identity of the package. # Define the identity of the package.
PACKAGE='qse' PACKAGE='qse'
VERSION='0.5.1' VERSION='0.5.2'
cat >>confdefs.h <<_ACEOF cat >>confdefs.h <<_ACEOF
@ -26126,7 +26126,7 @@ exec 6>&1
# report actual input values of CONFIG_FILES etc. instead of their # report actual input values of CONFIG_FILES etc. instead of their
# values after options handling. # values after options handling.
ac_log=" ac_log="
This file was extended by qse $as_me 0.5.1, which was This file was extended by qse $as_me 0.5.2, which was
generated by GNU Autoconf 2.63. Invocation command line was generated by GNU Autoconf 2.63. Invocation command line was
CONFIG_FILES = $CONFIG_FILES CONFIG_FILES = $CONFIG_FILES
@ -26189,7 +26189,7 @@ Report bugs to <bug-autoconf@gnu.org>."
_ACEOF _ACEOF
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
ac_cs_version="\\ ac_cs_version="\\
qse config.status 0.5.1 qse config.status 0.5.2
configured by $0, generated by GNU Autoconf 2.63, configured by $0, generated by GNU Autoconf 2.63,
with options \\"`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\" with options \\"`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\"

View File

@ -1,5 +1,5 @@
dnl AC_PREREQ(2.59) dnl AC_PREREQ(2.59)
AC_INIT([qse],[0.5.1],[bacon@abiyo.net]) AC_INIT([qse],[0.5.2],[bacon@abiyo.net])
AC_CONFIG_HEADER([include/qse/config.h]) AC_CONFIG_HEADER([include/qse/config.h])
AC_CONFIG_AUX_DIR([ac/au]) AC_CONFIG_AUX_DIR([ac/au])
AC_CONFIG_MACRO_DIR([ac/m4]) AC_CONFIG_MACRO_DIR([ac/m4])

View File

@ -1,5 +1,5 @@
/* /*
* $Id: Awk.hpp 228 2009-07-11 03:01:36Z hyunghwan.chung $ * $Id: Awk.hpp 229 2009-07-12 13:06:01Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -55,6 +55,9 @@ public:
typedef qse_awk_rio_cmd_t rio_cmd_t; typedef qse_awk_rio_cmd_t rio_cmd_t;
class Run;
friend class Run;
class Source class Source
{ {
public: public:
@ -118,10 +121,10 @@ public:
Source& operator= (const Source&); Source& operator= (const Source&);
}; };
class Run;
/** /**
* RIO class * The RIOBase class is a base class to represent runtime I/O context.
* The Console, File, Pipe classes inherit this class to implement
* an actual I/O context.
*/ */
class RIOBase class RIOBase
{ {
@ -219,74 +222,116 @@ public:
char_t* filename; char_t* filename;
}; };
class Argument;
class Return;
friend class Run;
friend class Argument;
friend class Return;
class Value class Value
{ {
public: public:
Value (): run (QSE_NULL), val (qse_awk_val_nil) {} // initialization
Value (Run& run): run (&run), val (qse_awk_val_nil) {} void* operator new (size_t n, Run* run) throw ();
Value (Run* run): run (run), val (qse_awk_val_nil) {} void* operator new[] (size_t n, Run* run) throw ();
#if !defined(__BORLANDC__)
// deletion when initialization fails
void operator delete (void* p, Run* run) throw ();
void operator delete[] (void* p, Run* run) throw ();
#endif
// normal deletion
void operator delete (void* p) throw ();
void operator delete[] (void* p) throw ();
Value ();
Value (Run& run);
Value (Run* run);
Value (const Value& v); Value (const Value& v);
~Value (); ~Value ();
Value& operator= (const Value& v); Value& operator= (const Value& v);
void clear ();
operator val_t* () const { return val; } operator val_t* () const { return val; }
operator long_t () const; operator long_t () const;
operator real_t () const; operator real_t () const;
operator const char_t* () const; operator const char_t* () const;
void clear (); val_t* toVal () const
{
return operator val_t* ();
}
int get (long_t* v) const; long_t toInt () const
int get (real_t* v) const; {
int get (const char_t** str, size_t* len) const; return operator long_t ();
}
int set (val_t* v); real_t toReal () const
int set (Run* r, val_t* v); {
return operator real_t ();
}
int set (long_t v); const char_t* toStr (size_t* len) const
int set (Run* r, long_t v); {
int set (real_t v); const char_t* p;
int set (Run* r, real_t v); size_t l;
int set (const char_t* str, size_t len);
int set (Run* r, const char_t* str, size_t len);
int set (const char_t* str);
int set (Run* r, const char_t* str);
int setIndexed (const char_t* idx, size_t isz, val_t* v); if (getStr (&p, &l) == -1)
int setIndexed (Run* r, const char_t* idx, size_t isz, val_t* v); {
p = EMPTY_STRING;
l = 0;
}
if (len != QSE_NULL) *len = l;
return p;
}
int setIndexed (const char_t* idx, size_t isz, long_t v); int getInt (long_t* v) const;
int setIndexed (Run* r, const char_t* idx, size_t isz, long_t v); int getReal (real_t* v) const;
int setIndexed ( int getStr (const char_t** str, size_t* len) const;
int setVal (val_t* v);
int setVal (Run* r, val_t* v);
int setInt (long_t v);
int setInt (Run* r, long_t v);
int setReal (real_t v);
int setReal (Run* r, real_t v);
int setStr (const char_t* str, size_t len);
int setStr (Run* r, const char_t* str, size_t len);
int setStr (const char_t* str);
int setStr (Run* r, const char_t* str);
int setIndexedVal (
const char_t* idx, size_t isz, val_t* v);
int setIndexedVal (
Run* r, const char_t* idx, size_t isz, val_t* v);
int setIndexedInt (
const char_t* idx, size_t isz, long_t v);
int setIndexedInt (
Run* r, const char_t* idx, size_t isz, long_t v);
int setIndexedReal (
const char_t* idx, const char_t* idx,
size_t isz, size_t isz,
real_t v real_t v
); );
int setIndexed ( int setIndexedReal (
Run* r, Run* r,
const char_t* idx, const char_t* idx,
size_t isz, size_t isz,
real_t v real_t v
); );
int setIndexed ( int setIndexedStr (
const char_t* idx, const char_t* idx,
size_t isz, size_t isz,
const char_t* str, const char_t* str,
size_t len size_t len
); );
int setIndexed ( int setIndexedStr (
Run* r, Run* r,
const char_t* idx, const char_t* idx,
size_t isz, size_t isz,
@ -294,6 +339,19 @@ public:
size_t len size_t len
); );
int setIndexedStr (
const char_t* idx,
size_t isz,
const char_t* str
);
int setIndexedStr (
Run* r,
const char_t* idx,
size_t isz,
const char_t* str
);
bool isIndexed () const; bool isIndexed () const;
int getIndexed ( int getIndexed (
@ -305,111 +363,13 @@ public:
protected: protected:
Run* run; Run* run;
val_t* val; val_t* val;
};
/** mutable struct
* Represents an argument to an intrinsic function {
*/ qse_xstr_t str;
class Argument } cached;
{
public:
friend class Awk;
friend class Run;
Argument (Run& run); static const char_t* EMPTY_STRING;
Argument (Run* run);
~Argument ();
protected:
Argument ();
void clear ();
public:
// initialization
void* operator new (size_t n, awk_t* awk) throw ();
void* operator new[] (size_t n, awk_t* awk) throw ();
#if !defined(__BORLANDC__)
// deletion when initialization fails
void operator delete (void* p, awk_t* awk);
void operator delete[] (void* p, awk_t* awk);
#endif
// normal deletion
void operator delete (void* p);
void operator delete[] (void* p);
private:
Argument (const Argument&);
Argument& operator= (const Argument&);
protected:
int init (val_t* v);
int init (const char_t* str, size_t len);
public:
long_t toInt () const;
real_t toReal () const;
const char_t* toStr (size_t* len) const;
bool isIndexed () const;
int getIndexed (const char_t* idxptr, Argument& val) const;
int getIndexed (const char_t* idxptr, size_t idxlen, Argument& val) const;
int getIndexed (long_t idx, Argument& val) const;
int getFirstIndex (Argument& val) const;
int getNextIndex (Argument& val) const;
protected:
Run* run;
val_t* val;
qse_long_t inum;
qse_real_t rnum;
mutable qse_str_t str;
};
/**
* Represents a return value of an intrinsic function
*/
class Return
{
public:
friend class Awk;
friend class Run;
Return (Run& run);
Return (Run* run);
~Return ();
private:
Return (const Return&);
Return& operator= (const Return&);
protected:
val_t* toVal () const;
operator val_t* () const;
public:
int set (long_t v);
int set (real_t v);
int set (const char_t* ptr, size_t len);
bool isIndexed () const;
int setIndexed (const char_t* idx, size_t iln, long_t v);
int setIndexed (const char_t* idx, size_t iln, real_t v);
int setIndexed (const char_t* idx, size_t iln, const char_t* str, size_t sln);
int setIndexed (long_t idx, long_t v);
int setIndexed (long_t idx, real_t v);
int setIndexed (long_t idx, const char_t* str, size_t sln);
void clear ();
protected:
Run* run;
val_t* val;
}; };
// generated by generrcode.awk // generated by generrcode.awk
@ -608,9 +568,7 @@ public:
{ {
protected: protected:
friend class Awk; friend class Awk;
friend class Argument; friend class Value;
friend class Return;
friend class RIO;
Run (Awk* awk); Run (Awk* awk);
Run (Awk* awk, rtx_t* run); Run (Awk* awk, rtx_t* run);
@ -702,7 +660,7 @@ public:
* On success, 0 is returned. * On success, 0 is returned.
* On failure, -1 is returned. * On failure, -1 is returned.
*/ */
int setGlobal (int id, const Return& global); int setGlobal (int id, const Value& global);
/** /**
* Gets the value of a global variable. * Gets the value of a global variable.
@ -720,7 +678,7 @@ public:
* On success, 0 is returned. * On success, 0 is returned.
* On failure, -1 is returned. * On failure, -1 is returned.
*/ */
int getGlobal (int id, Argument& global) const; int getGlobal (int id, Value& global) const;
void* alloc (size_t size); void* alloc (size_t size);
void free (void* ptr); void free (void* ptr);
@ -847,8 +805,9 @@ public:
*/ */
virtual int call ( virtual int call (
const char_t* name, const char_t* name,
const Return* args, Value* ret,
size_t nargs const Value* args,
size_t nargs
); );
/** /**
@ -887,7 +846,7 @@ public:
* Represents a user-defined intrinsic function. * Represents a user-defined intrinsic function.
*/ */
typedef int (Awk::*FunctionHandler) ( typedef int (Awk::*FunctionHandler) (
Run& run, Return& ret, const Argument* args, size_t nargs, Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len); const char_t* name, size_t len);
/** /**
@ -953,7 +912,7 @@ protected:
// run-time callbacks // run-time callbacks
virtual bool onLoopEnter (Run& run); virtual bool onLoopEnter (Run& run);
virtual void onLoopExit (Run& run, const Argument& ret); virtual void onLoopExit (Run& run, const Value& ret);
virtual void onStatement (Run& run, size_t line); virtual void onStatement (Run& run, size_t line);
// primitive handlers // primitive handlers

View File

@ -1,5 +1,5 @@
/* /*
* $Id: StdAwk.hpp 226 2009-07-09 12:46:14Z hyunghwan.chung $ * $Id: StdAwk.hpp 229 2009-07-12 13:06:01Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -78,29 +78,29 @@ public:
protected: protected:
// intrinsic functions // intrinsic functions
int sin (Run& run, Return& ret, const Argument* args, size_t nargs, int sin (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 cos (Run& run, Return& ret, const Argument* args, size_t nargs, int cos (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 tan (Run& run, Return& ret, const Argument* args, size_t nargs, int tan (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 atan (Run& run, Return& ret, const Argument* args, size_t nargs, int atan (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 atan2 (Run& run, Return& ret, const Argument* args, size_t nargs, int atan2 (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 log (Run& run, Return& ret, const Argument* args, size_t nargs, int log (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 exp (Run& run, Return& ret, const Argument* args, size_t nargs, int exp (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 sqrt (Run& run, Return& ret, const Argument* args, size_t nargs, int sqrt (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 fnint (Run& run, Return& ret, const Argument* args, size_t nargs, int fnint (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 rand (Run& run, Return& ret, const Argument* 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, Return& ret, const Argument* args, size_t nargs, int srand (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 system (Run& run, Return& ret, const Argument* args, size_t nargs, int system (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len); const char_t* name, size_t len);
// pipe io handlers // pipe io handlers

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
/* /*
* $Id: StdAwk.cpp 226 2009-07-09 12:46:14Z hyunghwan.chung $ * $Id: StdAwk.cpp 229 2009-07-12 13:06:01Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -81,10 +81,10 @@ void StdAwk::close ()
Awk::close (); Awk::close ();
} }
int StdAwk::sin (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::sin (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
return ret.set ( return ret.setReal (
#if defined(HAVE_SINL) #if defined(HAVE_SINL)
(real_t)::sinl(args[0].toReal()) (real_t)::sinl(args[0].toReal())
#elif defined(HAVE_SIN) #elif defined(HAVE_SIN)
@ -97,10 +97,10 @@ int StdAwk::sin (Run& run, Return& ret, const Argument* args, size_t nargs,
); );
} }
int StdAwk::cos (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::cos (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
return ret.set ( return ret.setReal (
#if defined(HAVE_COSL) #if defined(HAVE_COSL)
(real_t)::cosl(args[0].toReal()) (real_t)::cosl(args[0].toReal())
#elif defined(HAVE_COS) #elif defined(HAVE_COS)
@ -113,10 +113,10 @@ int StdAwk::cos (Run& run, Return& ret, const Argument* args, size_t nargs,
); );
} }
int StdAwk::tan (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::tan (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
return ret.set ( return ret.setReal (
#if defined(HAVE_TANL) #if defined(HAVE_TANL)
(real_t)::tanl(args[0].toReal()) (real_t)::tanl(args[0].toReal())
#elif defined(HAVE_TAN) #elif defined(HAVE_TAN)
@ -129,10 +129,10 @@ int StdAwk::tan (Run& run, Return& ret, const Argument* args, size_t nargs,
); );
} }
int StdAwk::atan (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::atan (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
return ret.set ( return ret.setReal (
#if defined(HAVE_ATANL) #if defined(HAVE_ATANL)
(real_t)::atanl(args[0].toReal()) (real_t)::atanl(args[0].toReal())
#elif defined(HAVE_ATAN) #elif defined(HAVE_ATAN)
@ -145,10 +145,10 @@ int StdAwk::atan (Run& run, Return& ret, const Argument* args, size_t nargs,
); );
} }
int StdAwk::atan2 (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::atan2 (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
return ret.set ( return ret.setReal (
#if defined(HAVE_ATAN2L) #if defined(HAVE_ATAN2L)
(real_t)::atan2l(args[0].toReal(), args[1].toReal()) (real_t)::atan2l(args[0].toReal(), args[1].toReal())
#elif defined(HAVE_ATAN2) #elif defined(HAVE_ATAN2)
@ -161,10 +161,10 @@ int StdAwk::atan2 (Run& run, Return& ret, const Argument* args, size_t nargs,
); );
} }
int StdAwk::log (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::log (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
return ret.set ( return ret.setReal (
#if defined(HAVE_LOGL) #if defined(HAVE_LOGL)
(real_t)::logl(args[0].toReal()) (real_t)::logl(args[0].toReal())
#elif defined(HAVE_LOG) #elif defined(HAVE_LOG)
@ -177,10 +177,10 @@ int StdAwk::log (Run& run, Return& ret, const Argument* args, size_t nargs,
); );
} }
int StdAwk::exp (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::exp (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
return ret.set ( return ret.setReal (
#if defined(HAVE_EXPL) #if defined(HAVE_EXPL)
(real_t)::expl(args[0].toReal()) (real_t)::expl(args[0].toReal())
#elif defined(HAVE_EXP) #elif defined(HAVE_EXP)
@ -193,10 +193,10 @@ int StdAwk::exp (Run& run, Return& ret, const Argument* args, size_t nargs,
); );
} }
int StdAwk::sqrt (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::sqrt (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
return ret.set ( return ret.setReal (
#if defined(HAVE_SQRTL) #if defined(HAVE_SQRTL)
(real_t)::sqrtl(args[0].toReal()) (real_t)::sqrtl(args[0].toReal())
#elif defined(HAVE_SQRT) #elif defined(HAVE_SQRT)
@ -209,19 +209,19 @@ int StdAwk::sqrt (Run& run, Return& ret, const Argument* args, size_t nargs,
); );
} }
int StdAwk::fnint (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::fnint (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
return ret.set (args[0].toInt()); return ret.setInt (args[0].toInt());
} }
int StdAwk::rand (Run& run, Return& ret, const Argument* 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)
{ {
return ret.set ((real_t)(::rand() % RAND_MAX) / RAND_MAX); return ret.setReal ((real_t)(::rand() % RAND_MAX) / RAND_MAX);
} }
int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::srand (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
unsigned int prevSeed = this->seed; unsigned int prevSeed = this->seed;
@ -240,19 +240,19 @@ int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs,
} }
::srand (this->seed); ::srand (this->seed);
return ret.set ((long_t)prevSeed); return ret.setInt ((long_t)prevSeed);
} }
int StdAwk::system (Run& run, Return& ret, const Argument* args, size_t nargs, int StdAwk::system (Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
size_t l; size_t l;
const char_t* ptr = args[0].toStr(&l); const char_t* ptr = args[0].toStr(&l);
#ifdef _WIN32 #ifdef _WIN32
return ret.set ((long_t)::_tsystem(ptr)); return ret.setInt ((long_t)::_tsystem(ptr));
#elif defined(QSE_CHAR_IS_MCHAR) #elif defined(QSE_CHAR_IS_MCHAR)
return ret.set ((long_t)::system(ptr)); return ret.setInt ((long_t)::system(ptr));
#else #else
char* mbs = (char*) qse_awk_alloc ((awk_t*)(Awk*)run, l*5+1); char* mbs = (char*) qse_awk_alloc ((awk_t*)(Awk*)run, l*5+1);
if (mbs == QSE_NULL) return -1; if (mbs == QSE_NULL) return -1;
@ -271,7 +271,7 @@ int StdAwk::system (Run& run, Return& ret, const Argument* args, size_t nargs,
} }
mbs[mbl] = '\0'; mbs[mbl] = '\0';
int n = ret.set ((long_t)::system(mbs)); int n = ret.setInt ((long_t)::system(mbs));
qse_awk_free ((awk_t*)(Awk*)run, mbs); qse_awk_free ((awk_t*)(Awk*)run, mbs);
return n; return n;

View File

@ -34,33 +34,13 @@ static void print_error (const qse_char_t* msg)
print_error (0, msg); print_error (0, msg);
} }
static int awk_main (int argc, qse_char_t* argv[]) static int run_awk (QSE::StdAwk& awk)
{ {
QSE::StdAwk awk;
QSE::StdAwk::Run* run;
// initialize awk
if (awk.open() <= -1)
{
print_error (awk.getErrorMessage());
return -1;
}
// ARGV[0] // ARGV[0]
if (awk.addArgument (QSE_T("awk05")) <= -1) if (awk.addArgument (QSE_T("awk05")) <= -1) return -1;
{
print_error (awk.getErrorMessage());
awk.close ();
return -1;
}
// ARGV[1] and/or the first console input file // ARGV[1] and/or the first console input file
if (awk.addArgument (QSE_T("awk05.cpp")) <= -1) if (awk.addArgument (QSE_T("awk05.cpp")) <= -1) return -1;
{
print_error (awk.getErrorMessage());
awk.close ();
return -1;
}
const qse_char_t* script = QSE_T( const qse_char_t* script = QSE_T(
"BEGIN { print \">> PRINT ALL LINES WHOSE LENGTH IS GREATER THAN 0\"; }\n" "BEGIN { print \">> PRINT ALL LINES WHOSE LENGTH IS GREATER THAN 0\"; }\n"
@ -72,24 +52,23 @@ static int awk_main (int argc, qse_char_t* argv[])
QSE::StdAwk::SourceFile out (QSE_T("awk05.out")); QSE::StdAwk::SourceFile out (QSE_T("awk05.out"));
// parse the script string and deparse it to awk05.out. // parse the script string and deparse it to awk05.out.
run = awk.parse (&in, &out); if (awk.parse (&in, &out) == QSE_NULL) return -1;
if (run == QSE_NULL)
{
print_error (awk.getErrorLine(), awk.getErrorMessage());
awk.close ();
return -1;
}
// execute the BEGIN, pattern-action, END blocks. // execute the BEGIN, pattern-action, END blocks.
if (awk.loop () <= -1) return awk.loop ();
{ }
print_error (awk.getErrorLine(), awk.getErrorMessage());
awk.close (); static int awk_main (int argc, qse_char_t* argv[])
return -1; {
} QSE::StdAwk awk;
int ret = awk.open ();
if (ret >= 0) ret = run_awk (awk);
if (ret <= -1) print_error (awk.getErrorLine(), awk.getErrorMessage());
awk.close (); awk.close ();
return 0; return ret;
} }
int qse_main (int argc, qse_achar_t* argv[]) int qse_main (int argc, qse_achar_t* argv[])

View File

@ -29,67 +29,59 @@ static void print_error (unsigned long line, const qse_char_t* msg)
} }
static void print_error (const qse_char_t* msg) static int run_awk (QSE::StdAwk& awk)
{ {
print_error (0, msg); QSE::StdAwk::Run* run;
const qse_char_t* script = QSE_T(
"function add (a, b) { return a + b }\n"
"function mul (a, b) { return a * b }\n"
"function div (a, b) { return a / b }\n"
);
QSE::StdAwk::SourceString in (script);
QSE::StdAwk::SourceFile out (QSE_T("awk06.out"));
// parse the script and deparse it to awk06.out
run = awk.parse (&in, &out);
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;
if (awk.call (QSE_T("add"), &ret, arg, 2) <= -1) return -1;
// ret = mul (ret, 51);
arg[0] = ret;
if (awk.call (QSE_T("mul"), &ret, arg, 2) <= -1) return -1;
// ret = div (ret, 2);
arg[0] = ret;
if (arg[1].setReal (run, 2) <= -1) return -1;
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));
return 0;
} }
static int awk_main (int argc, qse_char_t* argv[]) static int awk_main (int argc, qse_char_t* argv[])
{ {
QSE::StdAwk awk; QSE::StdAwk awk;
QSE::StdAwk::Run* run;
// initialize awk int ret = awk.open();
if (awk.open() <= -1)
{
print_error (awk.getErrorMessage());
return -1;
}
// ARGV[0] if (ret >= 0) ret = run_awk (awk);
if (awk.addArgument (QSE_T("awk05")) <= -1) if (ret <= -1) print_error (awk.getErrorLine(), awk.getErrorMessage());
{
print_error (awk.getErrorMessage());
awk.close ();
return -1;
}
// ARGV[1] and/or the first console input file
if (awk.addArgument (QSE_T("awk05.cpp")) <= -1)
{
print_error (awk.getErrorMessage());
awk.close ();
return -1;
}
const qse_char_t* script = QSE_T(
"BEGIN { print \">> PRINT ALL LINES WHOSE LENGTH IS GREATER THAN 0\"; }\n"
"length($0) > 0 { print $0; count++; }\n"
"END { print \">> TOTAL\", count, \"LINES\"; }\n"
);
QSE::StdAwk::SourceString in (script);
QSE::StdAwk::SourceFile out (QSE_T("awk05.out"));
// parse the script string and deparse it to awk05.out.
run = awk.parse (&in, &out);
if (run == QSE_NULL)
{
print_error (awk.getErrorLine(), awk.getErrorMessage());
awk.close ();
return -1;
}
// execute the BEGIN, pattern-action, END blocks.
if (awk.loop () <= -1)
{
print_error (awk.getErrorLine(), awk.getErrorMessage());
awk.close ();
return -1;
}
awk.close (); awk.close ();
return 0; return -1;
} }
int qse_main (int argc, qse_achar_t* argv[]) int qse_main (int argc, qse_achar_t* argv[])