added some c++ code

This commit is contained in:
hyung-hwan 2020-01-06 16:18:33 +00:00
parent b62142dea7
commit 053dec0f99
6 changed files with 357 additions and 73 deletions

View File

@ -24,7 +24,11 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(__cplusplus)
#include <HawkStd.hpp>
#else
#include <hawk-std.h>
#endif
#include <hawk-utl.h>
#include <hawk-fmt.h>
#include <hawk-cli.h>
@ -1079,6 +1083,325 @@ static hawk_mmgr_t debug_mmgr =
#endif
#if defined(__cplusplus)
/* -------------------------------------------------------------------------
* BEGINNING OF execute_hawk() in C++
* ------------------------------------------------------------------------- */
typedef HAWK::HawkStd HawkStd;
typedef HAWK::HawkStd::Run Run;
typedef HAWK::HawkStd::Value Value;
class MyHawk: public HawkStd
{
public:
MyHawk (HAWK::Mmgr* mmgr = HAWK_NULL): HawkStd(mmgr) { }
~MyHawk () { close (); }
int open ()
{
if (HawkStd::open() <= -1) return -1;
idLastSleep = this->addGlobal(HAWK_T("LAST_SLEEP"));
if (idLastSleep <= -1) goto oops;
/* this is for demonstration only.
* you can use sys::sleep() instead */
if (this->addFunction(HAWK_T("sleep"), 1, 1, HAWK_NULL, (FunctionHandler)&MyHawk::sleep) <= -1 ||
this->addFunction(HAWK_T("sumintarray"), 1, 1, HAWK_NULL, (FunctionHandler)&MyHawk::sumintarray) <= -1 ||
this->addFunction(HAWK_T("arrayindices"), 1, 1, HAWK_NULL, (FunctionHandler)&MyHawk::arrayindices) <= -1) goto oops;
return 0;
oops:
HawkStd::close ();
return -1;
}
int sleep (
Run& run, Value& ret, Value* args, hawk_oow_t nargs,
const hawk_ooch_t* name, hawk_oow_t len)
{
if (args[0].isIndexed())
{
run.setError (HAWK_EINVAL);
return -1;
}
Hawk::int_t x = args[0].toInt();
/*Value arg;
if (run.getGlobal(idLastSleep, arg) == 0)
qse_printf (HAWK_T("GOOD: [%d]\n"), (int)arg.toInt());
else { qse_printf (HAWK_T("BAD:\n")); }
*/
if (run.setGlobal(idLastSleep, x) <= -1) return -1;
#if defined(_WIN32)
::Sleep ((DWORD)(x * 1000));
return ret.setInt (0);
#elif defined(__OS2__)
::DosSleep ((ULONG)(x * 1000));
return ret.setInt (0);
#else
return ret.setInt (::sleep (x));
#endif
}
int sumintarray (
Run& run, Value& ret, Value* args, hawk_oow_t nargs,
const hawk_ooch_t* name, hawk_oow_t len)
{
// BEGIN {
// for(i=0;i<=10;i++) x[i]=i;
// print sumintarray(x);
// }
long_t x = 0;
if (args[0].isIndexed())
{
Value val(run);
Value::Index idx;
Value::IndexIterator ii;
ii = args[0].getFirstIndex (&idx);
while (ii != ii.END)
{
if (args[0].getIndexed(idx, &val) <= -1) return -1;
x += val.toInt ();
ii = args[0].getNextIndex (&idx, ii);
}
}
else x += args[0].toInt();
return ret.setInt (x);
}
int arrayindices (
Run& run,
Value& ret,
Value* args,
hawk_oow_t nargs,
const hawk_ooch_t* name,
hawk_oow_t len)
{
// create another array composed of array indices
// BEGIN {
// for(i=0;i<=10;i++) x[i]=i;
// y=arrayindices(x);
// for (i in y) print y[i];
// }
if (!args[0].isIndexed()) return 0;
Value::Index idx;
Value::IndexIterator ii;
long_t i;
ii = args[0].getFirstIndex (&idx);
for (i = 0; ii != ii.END ; i++)
{
Value::IntIndex iidx (i);
if (ret.setIndexedStr (iidx, idx.pointer(), idx.length()) <= -1) return -1;
ii = args[0].getNextIndex (&idx, ii);
}
return 0;
}
private:
int idLastSleep;
};
static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[])
{
hawk_t* awk = HAWK_NULL;
hawk_rtx_t* rtx = HAWK_NULL;
hawk_val_t* retv;
int i;
struct arg_t arg;
int ret = -1;
#if defined(ENABLE_CALLBACK)
static hawk_rtx_ecb_t rtx_ecb =
{
HAWK_FV(.close, HAWK_NULL),
HAWK_FV(.stmt, on_statement),
HAWK_FV(.gblset, HAWK_NULL)
};
#endif
/* TODO: change it to support multiple source files */
hawk_parsestd_t psout;
#if 0
hawk_mmgr_t* mmgr = HAWK_MMGR_GETDFL();
#endif
i = process_argv(argc, argv, &arg);
if (i <= 0)
{
print_usage (((i == 0)? stdout: stderr), argv[0]);
return i;
}
if (i == 2) return 0;
if (i == 3) return -1;
if (arg.osf)
{
psout.type = HAWK_PARSESTD_FILEB;
psout.u.fileb.path = arg.osf;
psout.u.fileb.cmgr = arg.script_cmgr;
}
#if 0
#if defined(HAWK_BUILD_DEBUG)
if (arg.failmalloc > 0)
{
debug_mmgr.ctx = &arg;
mmgr = &debug_mmgr;
}
else
#endif
if (arg.memlimit > 0)
{
xma_mmgr.ctx = hawk_xma_open(HAWK_MMGR_GETDFL(), 0, arg.memlimit);
if (xma_mmgr.ctx == HAWK_NULL)
{
print_error ("cannot open memory heap\n");
goto oops;
}
mmgr = &xma_mmgr;
}
#endif
MyHawk hawk;
if (hawk.open() <= -1);
{
//print_error ("cannot open awk - %hs\n", hawk.getErrorMessage()); // TODO: get ErrorMessage as byte string
print_error ("cannot open awk\n");
goto oops;
}
if (arg.modern) i = HAWK_MODERN;
else if (arg.classic) i = HAWK_CLASSIC;
else i = hawk.getTrait();
if (arg.opton) i |= arg.opton;
if (arg.optoff) i &= ~arg.optoff;
hawk.setTrait (i);
/* TODO: get depth from command line */
hawk.setMaxDepth (MyHawk::DEPTH_BLOCK_PARSE, 50);
hawk.setMaxDepth (MyHawk::DEPTH_EXPR_PARSE, 50);
hawk.setMaxDepth (MyHawk::DEPTH_BLOCK_RUN, 500);
hawk.setMaxDepth (MyHawk::DEPTH_EXPR_RUN, 500);
hawk.setMaxDepth (MyHawk::DEPTH_INCLUDE, 64);
if (add_gvs_to_awk(awk, &arg) <= -1)
{
print_hawk_error (awk);
goto oops;
}
MyHawk::Source* in, * out;
MyHawk::SourceString in_str (cmdline.ins);
MyHawk::SourceFile in_file (cmdline.inf);
MyHawk::SourceFile out_file (cmdline.outf);
in = (cmdline.ins)? (MyHawk::Source*)&in_str: (MyHawk::Source*)&in_file;
out = (cmdline.outf)? (MyHawk::Source*)&out_file: &MyHawk::Source::NONE;
MyHawk::Run* run = hawk.parse(*in, *out);
if (!run)
{
print_error ("parse error\n"); // TODO: include error message...
return -1;
}
if (hawk_parsestd(awk, arg.psin, ((arg.osf == HAWK_NULL)? HAWK_NULL: &psout)) <= -1)
{
print_hawk_error (awk);
goto oops;
}
rtx = hawk_rtx_openstdwithbcstr(
awk, 0, "hawk",
(arg.call? HAWK_NULL: arg.icf.ptr), /* console input */
arg.ocf.ptr, /* console output */
arg.console_cmgr
);
if (rtx == HAWK_NULL)
{
print_hawk_error (awk);
goto oops;
}
if (apply_fs_and_gvs_to_rtx(rtx, &arg) <= -1)
{
print_hawk_error (awk);
goto oops;
}
app_rtx = rtx;
#if defined(ENABLE_CALLBACK)
hawk_rtx_pushecb (rtx, &rtx_ecb);
#endif
set_intr_run ();
retv = arg.call?
hawk_rtx_callwithbcstrarr(rtx, arg.call, (const hawk_bch_t**)arg.icf.ptr, arg.icf.size):
hawk_rtx_loop(rtx);
unset_intr_run ();
if (retv)
{
hawk_int_t tmp;
hawk_rtx_refdownval (rtx, retv);
if (app_debug) dprint_return (rtx, retv);
ret = 0;
if (hawk_rtx_valtoint (rtx, retv, &tmp) >= 0) ret = tmp;
}
else
{
print_hawk_rtx_error (rtx);
goto oops;
}
oops:
if (rtx) hawk_rtx_close (rtx);
if (awk) hawk_close (awk);
#if 0
if (xma_mmgr.ctx) hawk_xma_close (xma_mmgr.ctx);
#endif
freearg (&arg);
#if defined(HAWK_BUILD_DEBUG)
/*
if (arg.failmalloc > 0)
{
hawk_fprintf (HAWK_STDERR, HAWK_T("\n"));
hawk_fprintf (HAWK_STDERR, HAWK_T("-[MALLOC COUNTS]---------------------------------------\n"));
hawk_fprintf (HAWK_STDERR, HAWK_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"),
(unsigned long)debug_mmgr_alloc_count,
(unsigned long)debug_mmgr_free_count,
(unsigned long)debug_mmgr_realloc_count);
hawk_fprintf (HAWK_STDERR, HAWK_T("-------------------------------------------------------\n"));
}*/
#endif
return ret;
}
/* -------------------------------------------------------------------------
* END OF execute_hawk() in C++
* ------------------------------------------------------------------------- */
#else
/* -------------------------------------------------------------------------
* BEGINNING OF execute_hawk() in C
* ------------------------------------------------------------------------- */
static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[])
{
hawk_t* awk = HAWK_NULL;
@ -1179,19 +1502,12 @@ static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[])
goto oops;
}
#if 1
rtx = hawk_rtx_openstdwithbcstr(
awk, 0, "hawk",
(arg.call? HAWK_NULL: arg.icf.ptr), /* console input */
arg.ocf.ptr, /* console output */
arg.console_cmgr
);
#else
/* this part is for testing only. don't use it for production */
const hawk_uch_t* icf[] = { HAWK_UT("/dev/stdin"), NULL };
const hawk_uch_t* ocf[] = { HAWK_UT("/dev/stdout"), NULL };
rtx = hawk_rtx_openstdwithucstr(awk, 0, HAWK_UT("hawk"), icf, ocf, arg.console_cmgr);
#endif
if (rtx == HAWK_NULL)
{
print_hawk_error (awk);
@ -1258,6 +1574,10 @@ oops:
return ret;
}
/* -------------------------------------------------------------------------
* END OF execute_hawk() in C
* ------------------------------------------------------------------------- */
#endif
/* ---------------------------------------------------------------------- */
@ -1269,6 +1589,7 @@ int main (int argc, hawk_bch_t* argv[])
char locale[100];
UINT codepage;
WSADATA wsadata;
int sock_inited = 0;
#elif defined(__DOS__)
extern BOOL _watt_do_exit;
int sock_inited = 0;
@ -1297,30 +1618,24 @@ int main (int argc, hawk_bch_t* argv[])
#endif
#if defined(_WIN32)
if (WSAStartup (MAKEWORD(2,0), &wsadata) != 0)
{
print_error ("Failed to start up winsock\n");
ret = -1;
goto oops;
}
if (WSAStartup(MAKEWORD(2,0), &wsadata) != 0)
print_warning ("Failed to start up winsock\n");
else sock_inited = 1;
#elif defined(__DOS__)
/* TODO: add an option to skip watt-32 */
/* TODO: add an option to skip watt-32 */
_watt_do_exit = 0; /* prevent sock_init from exiting upon failure */
if (sock_init() != 0)
print_warning ("Failed to initialize watt-32\n");
if (sock_init() != 0) print_warning ("Failed to initialize watt-32\n");
else sock_inited = 1;
#endif
ret = execute_hawk(argc, argv);
#if defined(_WIN32)
WSACleanup ();
if (sock_inited) WSACleanup ();
#elif defined(__DOS__)
if (sock_inited) sock_exit ();
#endif
oops:
return ret;
}

View File

@ -167,32 +167,6 @@ void Hawk::RIOBase::setUflags (int uflags)
this->riod->uflags = uflags;
}
Hawk::RIOBase::operator Hawk* () const
{
return this->run->awk;
}
Hawk::RIOBase::operator hawk_t* () const
{
HAWK_ASSERT (hawk_rtx_getawk(this->run->rtx) == this->run->awk->getHandle());
return this->run->awk->getHandle();
}
Hawk::RIOBase::operator hawk_rio_arg_t* () const
{
return this->riod;
}
Hawk::RIOBase::operator Hawk::Run* () const
{
return this->run;
}
Hawk::RIOBase::operator hawk_rtx_t* () const
{
return this->run->rtx;
}
//////////////////////////////////////////////////////////////////
// Hawk::Pipe
//////////////////////////////////////////////////////////////////

View File

@ -633,11 +633,16 @@ public:
int getUflags () const;
void setUflags (int uflags);
operator Hawk* () const;
operator hawk_t* () const;
operator hawk_rio_arg_t* () const;
operator Run* () const;
operator hawk_rtx_t* () const;
operator Hawk* () const { return this->run->awk; }
operator hawk_t* () const
{
HAWK_ASSERT (hawk_rtx_getawk(this->run->rtx) == this->run->awk->getHandle());
return this->run->awk->getHandle();
}
operator hawk_rio_arg_t* () const { return this->riod; }
operator Run* () const { return this->run; }
operator hawk_rtx_t* () const { return this->run->rtx; }
operator hawk_gem_t* () const { return hawk_rtx_getgem(this->run->rtx); }
protected:
Run* run;

View File

@ -578,10 +578,9 @@ int HawkStd::open_pio (Pipe& io)
}
pio = hawk_pio_open (
this->getMmgr(),
*this,
0,
io.getName(),
HAWK_NULL,
flags
);
if (pio == HAWK_NULL) return -1;
@ -751,7 +750,7 @@ int HawkStd::openFile (File& io)
break;
}
sio = hawk_sio_open(this->getMmgr(), 0, io.getName(), flags);
sio = hawk_sio_open(*this, 0, io.getName(), flags);
if (!sio) return -1;
#if defined(HAWK_OOCH_IS_UCH)
hawk_cmgr_t* cmgr = this->getiocmgr(io.getName());
@ -890,10 +889,7 @@ int HawkStd::open_console_in (Console& io)
if (hawk_count_oocstr(file) != this->runarg.ptr[this->runarg_index].len)
{
hawk_oocs_t arg;
arg.ptr = (hawk_ooch_t*)file;
arg.len = hawk_count_oocstr (arg.ptr);
((Run*)io)->setError (HAWK_EIONMNL, &arg);
((Run*)io)->formatError (HAWK_EIONMNL, HAWK_NULL, HAWK_T("invalid I/O name of length %zu containing '\\0'"), this->runarg.ptr[this->runarg_index].len);
return -1;
}
@ -921,7 +917,7 @@ int HawkStd::open_console_in (Console& io)
v = (hawk_val_t*)HAWK_HTB_VPTR(pair);
HAWK_ASSERT (v != HAWK_NULL);
as.ptr = hawk_rtx_getvaloocstr (rtx, v, &as.len);
as.ptr = hawk_rtx_getvaloocstr(rtx, v, &as.len);
if (as.ptr == HAWK_NULL) return -1;
if (as.len == 0)
@ -935,10 +931,7 @@ int HawkStd::open_console_in (Console& io)
if (hawk_count_oocstr(as.ptr) < as.len)
{
/* the name contains one or more '\0' */
hawk_oocs_t arg;
arg.ptr = as.ptr;
arg.len = hawk_count_oocstr (as.ptr);
((Run*)io)->setError (HAWK_EIONMNL, &arg);
((Run*)io)->formatError (HAWK_EIONMNL, HAWK_NULL, HAWK_T("invalid I/O name of length %zu containing '\\0'"), as.len);
hawk_rtx_freevaloocstr (rtx, v, as.ptr);
return -1;
}
@ -955,7 +948,7 @@ int HawkStd::open_console_in (Console& io)
return -1;
}
if (hawk_rtx_setfilename (rtx, file, hawk_count_oocstr(file)) <= -1)
if (hawk_rtx_setfilename(rtx, file, hawk_count_oocstr(file)) <= -1)
{
hawk_sio_close (sio);
hawk_rtx_freevaloocstr (rtx, v, as.ptr);
@ -1018,10 +1011,7 @@ int HawkStd::open_console_out (Console& io)
if (hawk_count_oocstr(file) != this->ofile.ptr[this->ofile_index].len)
{
hawk_oocs_t arg;
arg.ptr = (hawk_ooch_t*)file;
arg.len = hawk_count_oocstr(arg.ptr);
((Run*)io)->setError (HAWK_EIONMNL, &arg);
((Run*)io)->formatError (HAWK_EIONMNL, HAWK_NULL, HAWK_T("invalid I/O name of length %zu containing '\\0'"), this->ofile.ptr[this->ofile_index].len);
return -1;
}
@ -1179,7 +1169,7 @@ void HawkStd::modclose (void* handle)
void* HawkStd::modgetsym (void* handle, const hawk_ooch_t* name)
{
void* s;
s = hawk_stdmodgetsym (this->awk, handle, name);
s = hawk_stdmodgetsym(this->awk, handle, name);
if (!s) this->retrieveError ();
return s;
}
@ -1234,12 +1224,12 @@ int HawkStd::SourceFile::open (Data& io)
{
const hawk_ooch_t* outer;
outer = hawk_sio_getpath ((hawk_sio_t*)io.getPrevHandle());
outer = hawk_sio_getpath((hawk_sio_t*)io.getPrevHandle());
if (outer)
{
const hawk_ooch_t* base;
base = hawk_basename(outer);
base = hawk_get_base_name_oocstr(outer);
if (base != outer && ioname[0] != HAWK_T('/'))
{
hawk_oow_t tmplen, totlen, dirlen;
@ -1333,7 +1323,7 @@ int HawkStd::SourceString::open (Data& io)
{
const hawk_ooch_t* base;
base = hawk_basename(outer);
base = hawk_get_base_name_oocstr(outer);
if (base != outer && ioname[0] != HAWK_T('/'))
{
hawk_oow_t tmplen, totlen, dirlen;

View File

@ -178,7 +178,7 @@ protected:
void* modopen (const mod_spec_t* spec);
void modclose (void* handle);
void* modsym (void* handle, const hawk_ooch_t* name);
void* modgetsym (void* handle, const hawk_ooch_t* name);
protected:
hawk_htb_t cmgrtab;

View File

@ -254,7 +254,7 @@ HAWK_EXPORT void hawk_stdmodclose (
void* handle
);
HAWK_EXPORT void* hawk_stdmodsym (
HAWK_EXPORT void* hawk_stdmodgetsym (
hawk_t* hawk,
void* handle,
const hawk_ooch_t* name