2015-05-08 14:29:35 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2018-02-21 10:11:39 +00:00
|
|
|
Copyright (c) 2014-2018 Chung, Hyung-Hwan. All rights reserved.
|
2015-05-08 14:29:35 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-11-11 16:53:21 +00:00
|
|
|
#include <moo-std.h>
|
|
|
|
#include <moo-utl.h>
|
|
|
|
#include <moo-opt.h>
|
2015-05-07 15:58:04 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2018-11-11 16:53:21 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <locale.h>
|
2017-02-22 09:48:58 +00:00
|
|
|
|
2018-12-04 03:34:54 +00:00
|
|
|
#if (defined(__unix) || defined(__linux) || defined(__ultrix) || defined(_AIX) || defined(__hpux) || defined(__sgi)) && defined(HAVE_SIGNAL_H)
|
|
|
|
# include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
2018-11-26 09:49:03 +00:00
|
|
|
static void print_syntax_error (moo_t* moo, const char* main_src_file)
|
2018-11-15 08:40:22 +00:00
|
|
|
{
|
2018-11-26 09:49:03 +00:00
|
|
|
moo_synerr_t synerr;
|
2018-11-15 08:40:22 +00:00
|
|
|
|
2018-11-26 09:49:03 +00:00
|
|
|
moo_getsynerr (moo, &synerr);
|
2016-05-27 15:01:54 +00:00
|
|
|
|
2018-11-26 09:49:03 +00:00
|
|
|
moo_logbfmt (moo, MOO_LOG_STDERR, "ERROR: ");
|
|
|
|
if (synerr.loc.file)
|
2018-05-18 16:41:45 +00:00
|
|
|
{
|
2018-11-26 09:49:03 +00:00
|
|
|
moo_logbfmt (moo, MOO_LOG_STDERR, "%js", synerr.loc.file);
|
2018-05-18 16:41:45 +00:00
|
|
|
}
|
2018-11-26 09:49:03 +00:00
|
|
|
else
|
2018-05-18 16:41:45 +00:00
|
|
|
{
|
2018-11-26 09:49:03 +00:00
|
|
|
moo_logbfmt (moo, MOO_LOG_STDERR, "%s", main_src_file);
|
2018-05-18 16:41:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 09:49:03 +00:00
|
|
|
moo_logbfmt (moo, MOO_LOG_STDERR, "[%zu,%zu] %js",
|
|
|
|
synerr.loc.line, synerr.loc.colm,
|
|
|
|
(moo_geterrmsg(moo) != moo_geterrstr(moo)? moo_geterrmsg(moo): moo_geterrstr(moo))
|
|
|
|
);
|
2018-05-18 16:41:45 +00:00
|
|
|
|
2018-11-26 09:49:03 +00:00
|
|
|
if (synerr.tgt.len > 0)
|
|
|
|
{
|
|
|
|
moo_logbfmt (moo, MOO_LOG_STDERR, " - %.*js", synerr.tgt.len, synerr.tgt.ptr);
|
|
|
|
}
|
2018-05-18 16:41:45 +00:00
|
|
|
|
2018-11-26 09:49:03 +00:00
|
|
|
moo_logbfmt (moo, MOO_LOG_STDERR, "\n");
|
2017-02-14 14:00:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 15:01:54 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2017-12-16 16:14:23 +00:00
|
|
|
#define MIN_MEMSIZE 2048000ul
|
|
|
|
|
2015-05-07 15:58:04 +00:00
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
2017-02-10 09:06:53 +00:00
|
|
|
static moo_ooch_t str_my_object[] = { 'M', 'y', 'O', 'b','j','e','c','t' }; /*TODO: make this an argument */
|
|
|
|
static moo_ooch_t str_main[] = { 'm', 'a', 'i', 'n' };
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_t* moo;
|
2018-11-13 09:53:48 +00:00
|
|
|
moo_cfgstd_t cfg;
|
2018-11-11 16:53:21 +00:00
|
|
|
moo_errinf_t errinf;
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oocs_t objname;
|
|
|
|
moo_oocs_t mthname;
|
2018-11-30 10:33:17 +00:00
|
|
|
moo_oow_t memsize;
|
2016-05-17 15:12:27 +00:00
|
|
|
int i, xret;
|
2015-05-16 07:31:16 +00:00
|
|
|
|
2017-11-22 04:52:45 +00:00
|
|
|
moo_bci_t c;
|
2017-12-03 17:08:04 +00:00
|
|
|
static moo_bopt_lng_t lopt[] =
|
|
|
|
{
|
2017-12-24 17:36:20 +00:00
|
|
|
{ ":log", 'l' },
|
|
|
|
{ ":memsize", 'm' },
|
2018-03-07 08:09:06 +00:00
|
|
|
{ "large-pages", '\0' },
|
2018-11-14 08:47:50 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
{ "utf8", '\0' },
|
|
|
|
{ "utf16", '\0' },
|
|
|
|
#endif
|
2018-11-11 16:53:21 +00:00
|
|
|
#if defined(MOO_BUILD_DEBUG)
|
2017-12-24 17:36:20 +00:00
|
|
|
{ ":debug", '\0' }, /* NOTE: there is no short option for --debug */
|
2018-11-11 16:53:21 +00:00
|
|
|
#endif
|
2017-12-24 17:36:20 +00:00
|
|
|
{ MOO_NULL, '\0' }
|
2017-12-03 17:08:04 +00:00
|
|
|
};
|
2017-11-22 04:52:45 +00:00
|
|
|
static moo_bopt_t opt =
|
|
|
|
{
|
2017-12-16 16:14:23 +00:00
|
|
|
"l:m:",
|
2017-12-03 17:08:04 +00:00
|
|
|
lopt
|
2017-11-22 04:52:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
2015-05-28 16:51:37 +00:00
|
|
|
#if !defined(macintosh)
|
2015-06-11 12:09:10 +00:00
|
|
|
if (argc < 2)
|
2015-05-16 07:31:16 +00:00
|
|
|
{
|
2017-11-22 04:52:45 +00:00
|
|
|
print_usage:
|
2018-10-10 04:23:44 +00:00
|
|
|
fprintf (stderr, "Usage: %s [options] filename ...\n", argv[0]);
|
|
|
|
fprintf (stderr, " --log filename[,logopts]\n");
|
2018-11-14 08:47:50 +00:00
|
|
|
fprintf (stderr, " --memsize number\n");
|
|
|
|
fprintf (stderr, " --large-pages\n");
|
|
|
|
#if defined(_WIN32)
|
|
|
|
fprintf (stderr, " --utf16\n");
|
|
|
|
fprintf (stderr, " --utf8\n");
|
|
|
|
#endif
|
2018-10-10 04:23:44 +00:00
|
|
|
#if defined(MOO_BUILD_DEBUG)
|
|
|
|
fprintf (stderr, " --debug dbgopts\n");
|
|
|
|
#endif
|
2015-05-16 07:31:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-11 16:53:21 +00:00
|
|
|
memset (&cfg, 0, MOO_SIZEOF(cfg));
|
2018-11-13 09:53:48 +00:00
|
|
|
cfg.type = MOO_CFGSTD_OPTB;
|
2018-11-14 07:59:39 +00:00
|
|
|
cfg.cmgr = moo_get_utf8_cmgr();
|
2018-11-11 16:53:21 +00:00
|
|
|
|
2018-11-30 10:33:17 +00:00
|
|
|
memsize = MIN_MEMSIZE;
|
|
|
|
|
2018-11-11 16:53:21 +00:00
|
|
|
while ((c = moo_getbopt(argc, argv, &opt)) != MOO_BCI_EOF)
|
2017-11-22 04:52:45 +00:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'l':
|
2018-11-13 09:53:48 +00:00
|
|
|
cfg.u.optb.log = opt.arg;
|
2017-11-22 04:52:45 +00:00
|
|
|
break;
|
|
|
|
|
2017-12-16 16:14:23 +00:00
|
|
|
case 'm':
|
2018-11-30 10:33:17 +00:00
|
|
|
memsize = strtoul(opt.arg, MOO_NULL, 0);
|
|
|
|
if (memsize <= MIN_MEMSIZE) memsize = MIN_MEMSIZE;
|
2017-12-16 16:14:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\0':
|
2018-04-07 15:54:09 +00:00
|
|
|
if (moo_comp_bcstr(opt.lngopt, "large-pages") == 0)
|
2018-03-07 08:09:06 +00:00
|
|
|
{
|
2018-11-11 16:53:21 +00:00
|
|
|
cfg.large_pages = 1;
|
2018-03-07 08:09:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-11-14 08:47:50 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
else if (moo_comp_bcstr(opt.lngopt, "utf8") == 0)
|
|
|
|
{
|
|
|
|
cfg.cmgr = moo_get_utf8_cmgr();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (moo_comp_bcstr(opt.lngopt, "utf16") == 0)
|
|
|
|
{
|
|
|
|
cfg.cmgr = moo_get_utf16_cmgr();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-21 09:35:59 +00:00
|
|
|
#if defined(MOO_BUILD_DEBUG)
|
2018-04-07 15:54:09 +00:00
|
|
|
else if (moo_comp_bcstr(opt.lngopt, "debug") == 0)
|
2017-12-16 16:14:23 +00:00
|
|
|
{
|
2018-11-13 09:53:48 +00:00
|
|
|
cfg.u.optb.dbg = opt.arg;
|
2017-12-16 16:14:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-16 16:27:27 +00:00
|
|
|
#endif
|
2017-12-16 16:14:23 +00:00
|
|
|
goto print_usage;
|
|
|
|
|
2017-11-22 04:52:45 +00:00
|
|
|
case ':':
|
|
|
|
if (opt.lngopt)
|
|
|
|
fprintf (stderr, "bad argument for '%s'\n", opt.lngopt);
|
|
|
|
else
|
|
|
|
fprintf (stderr, "bad argument for '%c'\n", opt.opt);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto print_usage;
|
|
|
|
}
|
|
|
|
}
|
2017-12-16 05:21:38 +00:00
|
|
|
|
|
|
|
if (opt.ind >= argc) goto print_usage;
|
2017-11-22 04:52:45 +00:00
|
|
|
#endif
|
2017-07-27 08:32:16 +00:00
|
|
|
|
2018-11-11 16:53:21 +00:00
|
|
|
moo = moo_openstd(0, &cfg, &errinf);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!moo)
|
2015-05-07 15:58:04 +00:00
|
|
|
{
|
2018-11-13 09:53:48 +00:00
|
|
|
#if defined(MOO_OOCH_IS_BCH)
|
|
|
|
fprintf (stderr, "ERROR: cannot open moo - [%d] %s\n", (int)errinf.num, errinf.msg);
|
|
|
|
#elif (MOO_SIZEOF_UCH_T == MOO_SIZEOF_WCHAR_T)
|
|
|
|
fprintf (stderr, "ERROR: cannot open moo - [%d] %ls\n", (int)errinf.num, errinf.msg);
|
|
|
|
#else
|
|
|
|
moo_bch_t bcsmsg[MOO_COUNTOF(errinf.msg) * 2]; /* error messages may get truncated */
|
|
|
|
moo_oow_t wcslen, bcslen;
|
|
|
|
bcslen = MOO_COUNTOF(bcsmsg);
|
|
|
|
moo_conv_ucstr_to_utf8 (errinf.msg, &wcslen, bcsmsg, &bcslen);
|
|
|
|
fprintf (stderr, "ERROR: cannot open moo - [%d] %s\n", (int)errinf.num, bcsmsg);
|
|
|
|
#endif
|
2015-05-07 15:58:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t tab_size;
|
2015-05-28 16:51:37 +00:00
|
|
|
|
2018-12-20 16:08:56 +00:00
|
|
|
tab_size = 10;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_setoption (moo, MOO_SYMTAB_SIZE, &tab_size);
|
2018-12-20 16:08:56 +00:00
|
|
|
tab_size = 10;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_setoption (moo, MOO_SYSDIC_SIZE, &tab_size);
|
2016-02-13 17:39:11 +00:00
|
|
|
tab_size = 600;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_setoption (moo, MOO_PROCSTK_SIZE, &tab_size);
|
2015-05-07 15:58:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 14:20:37 +00:00
|
|
|
{
|
2018-11-11 16:53:21 +00:00
|
|
|
moo_bitmask_t bm = 0;
|
2015-06-28 14:20:37 +00:00
|
|
|
|
2018-11-11 16:53:21 +00:00
|
|
|
/*bm |= MOO_NOGC;*/
|
|
|
|
bm |= MOO_AWAIT_PROCS;
|
|
|
|
moo_setoption (moo, MOO_TRAIT, &bm);
|
2017-05-21 16:57:21 +00:00
|
|
|
|
|
|
|
/* disable GC logs */
|
2018-11-11 16:53:21 +00:00
|
|
|
bm = ~MOO_LOG_GC;
|
|
|
|
moo_setoption (moo, MOO_LOG_MASK, &bm);
|
2017-11-22 04:52:45 +00:00
|
|
|
}
|
2017-12-16 16:14:23 +00:00
|
|
|
|
2018-11-30 10:33:17 +00:00
|
|
|
if (moo_ignite(moo, memsize) <= -1)
|
2015-05-07 15:58:04 +00:00
|
|
|
{
|
2017-12-31 16:46:52 +00:00
|
|
|
moo_logbfmt (moo, MOO_LOG_STDERR, "ERROR: cannot ignite moo - [%d] %js\n", moo_geterrnum(moo), moo_geterrstr(moo));
|
2018-02-03 16:42:13 +00:00
|
|
|
moo_close (moo);
|
2015-05-07 15:58:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-12 09:19:35 +00:00
|
|
|
/*
|
2015-06-11 12:09:10 +00:00
|
|
|
#if defined(macintosh)
|
|
|
|
i = 20;
|
2017-12-16 05:21:38 +00:00
|
|
|
xtn->source_path = "test.moo";
|
2015-06-11 12:09:10 +00:00
|
|
|
goto compile;
|
2015-05-28 16:51:37 +00:00
|
|
|
#endif
|
2018-11-12 09:19:35 +00:00
|
|
|
*/
|
2015-06-11 12:09:10 +00:00
|
|
|
|
2017-11-22 04:52:45 +00:00
|
|
|
for (i = opt.ind; i < argc; i++)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2018-11-13 06:54:30 +00:00
|
|
|
moo_iostd_t in;
|
|
|
|
|
2018-11-26 09:49:03 +00:00
|
|
|
#if 1
|
2018-11-13 06:54:30 +00:00
|
|
|
in.type = MOO_IOSTD_FILEB;
|
|
|
|
in.u.fileb.path = argv[i];
|
|
|
|
#else
|
|
|
|
moo_uch_t tmp[1000];
|
|
|
|
moo_oow_t bcslen, ucslen;
|
|
|
|
ucslen = MOO_COUNTOF(tmp);
|
|
|
|
moo_conv_utf8_to_ucstr(argv[i], &bcslen, tmp, &ucslen);
|
|
|
|
in.type = MOO_IOSTD_FILEU;
|
|
|
|
in.u.fileu.path = tmp;
|
|
|
|
#endif
|
2015-05-20 15:52:45 +00:00
|
|
|
|
2018-11-12 09:19:35 +00:00
|
|
|
/*compile:*/
|
2018-11-13 06:54:30 +00:00
|
|
|
if (moo_compilestd(moo, &in, 1) <= -1)
|
2015-06-11 12:09:10 +00:00
|
|
|
{
|
2017-12-17 06:38:08 +00:00
|
|
|
if (moo->errnum == MOO_ESYNERR)
|
2015-05-20 15:52:45 +00:00
|
|
|
{
|
2018-11-26 09:49:03 +00:00
|
|
|
print_syntax_error (moo, argv[i]);
|
2015-05-19 15:16:18 +00:00
|
|
|
}
|
2015-06-11 12:09:10 +00:00
|
|
|
else
|
|
|
|
{
|
2017-12-31 16:46:52 +00:00
|
|
|
moo_logbfmt (moo, MOO_LOG_STDERR, "ERROR: cannot compile code - [%d] %js\n", moo_geterrnum(moo), moo_geterrmsg(moo));
|
2015-06-11 12:09:10 +00:00
|
|
|
}
|
2017-11-22 04:52:45 +00:00
|
|
|
|
2018-02-03 16:42:13 +00:00
|
|
|
moo_close (moo);
|
2015-06-11 12:09:10 +00:00
|
|
|
return -1;
|
2015-05-19 15:16:18 +00:00
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 13:56:19 +00:00
|
|
|
MOO_DEBUG0 (moo, "COMPILE OK. STARTING EXECUTION...\n");
|
2016-05-17 15:12:27 +00:00
|
|
|
xret = 0;
|
2018-11-26 09:49:03 +00:00
|
|
|
|
2018-12-04 03:34:54 +00:00
|
|
|
#if defined(SIGINT) && defined(HAVE_SIGNAL)
|
|
|
|
/* i'd like the program to ignore the interrupt signal
|
|
|
|
* before moo_catch_termreq() and after moo_uncatch_termreq() */
|
|
|
|
signal (SIGINT, SIG_IGN);
|
|
|
|
signal (SIGTERM, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
|
2018-11-26 09:49:03 +00:00
|
|
|
moo_catch_termreq ();
|
2018-11-19 15:52:26 +00:00
|
|
|
moo_start_ticker ();
|
2016-05-17 15:12:27 +00:00
|
|
|
|
2018-11-19 15:52:26 +00:00
|
|
|
moo_rcvtickstd (moo, 1);
|
|
|
|
|
2015-06-11 09:11:18 +00:00
|
|
|
objname.ptr = str_my_object;
|
|
|
|
objname.len = 8;
|
2015-06-06 07:24:35 +00:00
|
|
|
mthname.ptr = str_main;
|
|
|
|
mthname.len = 4;
|
2018-02-05 09:56:05 +00:00
|
|
|
if (moo_invoke(moo, &objname, &mthname) <= -1)
|
2015-06-04 18:34:37 +00:00
|
|
|
{
|
2017-12-31 16:46:52 +00:00
|
|
|
moo_logbfmt (moo, MOO_LOG_STDERR, "ERROR: cannot execute code - [%d] %js\n", moo_geterrnum(moo), moo_geterrmsg(moo));
|
2016-05-17 15:12:27 +00:00
|
|
|
xret = -1;
|
2015-06-04 18:34:37 +00:00
|
|
|
}
|
2015-06-06 07:24:35 +00:00
|
|
|
|
2018-11-19 15:52:26 +00:00
|
|
|
moo_stop_ticker ();
|
2018-11-26 09:49:03 +00:00
|
|
|
moo_uncatch_termreq ();
|
2015-06-06 07:24:35 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
/*moo_dumpsymtab(moo);
|
2018-12-19 16:57:16 +00:00
|
|
|
moo_dumpdic(moo, moo->sysdic, "System dictionary");*/
|
2016-06-05 18:37:28 +00:00
|
|
|
|
2018-02-03 16:42:13 +00:00
|
|
|
moo_close (moo);
|
2016-05-17 15:12:27 +00:00
|
|
|
return xret;
|
2015-05-07 15:58:04 +00:00
|
|
|
}
|