added a new directory - samples

more code in Hawk and HawkStd
This commit is contained in:
2020-01-07 09:02:56 +00:00
parent 053dec0f99
commit 05c58df30f
21 changed files with 3285 additions and 2230 deletions

View File

@ -24,11 +24,7 @@
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>
@ -1083,325 +1079,6 @@ 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;
@ -1574,10 +1251,6 @@ oops:
return ret;
}
/* -------------------------------------------------------------------------
* END OF execute_hawk() in C
* ------------------------------------------------------------------------- */
#endif
/* ---------------------------------------------------------------------- */