2017-04-18 02:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2017-09-09 18:39:51 +00:00
|
|
|
#include <dst/dst.h>
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
#define DST_CLIENT_HELP 1
|
|
|
|
#define DST_CLIENT_VERBOSE 2
|
|
|
|
#define DST_CLIENT_VERSION 4
|
|
|
|
#define DST_CLIENT_REPL 8
|
2017-12-30 21:46:59 +00:00
|
|
|
#define DST_CLIENT_UNKNOWN 16
|
2017-07-02 18:42:30 +00:00
|
|
|
|
2018-01-06 16:09:15 +00:00
|
|
|
static Dst env;
|
2017-07-03 00:51:52 +00:00
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
static int client_strequal(const char *a, const char *b) {
|
|
|
|
while (*a) if (*a++ != *b++) return 0;
|
|
|
|
return *a == *b;
|
2017-06-25 23:47:49 +00:00
|
|
|
}
|
2017-04-19 16:56:29 +00:00
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
static int client_strequal_witharg(const char *a, const char *b) {
|
|
|
|
while (*b) if (*a++ != *b++) return 0;
|
|
|
|
return *a == '=';
|
2017-04-18 02:14:35 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
/* Load source from a file */
|
|
|
|
static const uint8_t *loadsource(const char *fpath, int32_t *len) {
|
|
|
|
FILE *f = fopen(fpath, "rb");
|
|
|
|
long fsize;
|
|
|
|
size_t fsizet;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint8_t *source = NULL;
|
2017-12-30 21:46:59 +00:00
|
|
|
if (fseek(f, 0, SEEK_END)) goto error;
|
|
|
|
fsize = ftell(f);
|
|
|
|
if (fsize > INT32_MAX || fsize < 0) goto error;
|
|
|
|
fsizet = fsize;
|
|
|
|
if (fseek(f, 0, SEEK_SET)) goto error;
|
|
|
|
if (!fsize) goto error;
|
|
|
|
source = malloc(fsize);
|
|
|
|
if (fread(source, 1, fsize, f) != fsizet) goto error;
|
|
|
|
if (fclose(f)) goto error;
|
|
|
|
*len = (int32_t) fsizet;
|
|
|
|
return source;
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
error:
|
|
|
|
free(source);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Shift bytes in buffer down */
|
|
|
|
static void bshift(DstBuffer *buf, int32_t delta) {
|
|
|
|
buf->count -= delta;
|
|
|
|
if (delta) {
|
|
|
|
memmove(buf->data, buf->data + delta, buf->count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 15:34:59 +00:00
|
|
|
/* Wrap a value to add to the env */
|
|
|
|
static void set_underscore(Dst val) {
|
|
|
|
DstTable *t = dst_table(0);
|
|
|
|
dst_table_put(t, dst_csymbolv("value"), val);
|
|
|
|
dst_put(env, dst_csymbolv("_"), dst_wrap_table(t));
|
|
|
|
}
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
/* simple repl */
|
|
|
|
static int repl() {
|
|
|
|
DstBuffer b;
|
|
|
|
dst_buffer_init(&b, 256);
|
2018-01-13 15:34:59 +00:00
|
|
|
set_underscore(dst_wrap_nil());
|
2017-05-07 20:48:35 +00:00
|
|
|
for (;;) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int c;
|
|
|
|
DstParseResult res;
|
|
|
|
DstCompileResult cres;
|
|
|
|
DstCompileOptions opts;
|
|
|
|
res = dst_parse(b.data, b.count);
|
|
|
|
switch (res.status) {
|
|
|
|
case DST_PARSE_NODATA:
|
|
|
|
b.count = 0;
|
|
|
|
case DST_PARSE_UNEXPECTED_EOS:
|
2018-01-04 02:36:10 +00:00
|
|
|
if (b.count == 0)
|
|
|
|
printf("> ");
|
|
|
|
else
|
|
|
|
printf(">> ");
|
|
|
|
for (;;) {
|
|
|
|
c = fgetc(stdin);
|
|
|
|
if (c == EOF) {
|
|
|
|
printf("\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
dst_buffer_push_u8(&b, c);
|
|
|
|
if (c == '\n') break;
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
break;
|
|
|
|
case DST_PARSE_ERROR:
|
2018-01-04 02:36:10 +00:00
|
|
|
dst_puts(dst_formatc("syntax error: %S\n", res.error));
|
2017-12-30 21:46:59 +00:00
|
|
|
b.count = 0;
|
|
|
|
break;
|
|
|
|
case DST_PARSE_OK:
|
|
|
|
{
|
|
|
|
opts.source = res.value;
|
|
|
|
opts.flags = 0;
|
|
|
|
opts.env = env;
|
|
|
|
cres = dst_compile(opts);
|
|
|
|
if (cres.status == DST_COMPILE_OK) {
|
2018-01-04 02:36:10 +00:00
|
|
|
/*dst_puts(dst_formatc("asm: %v\n", dst_disasm(cres.funcdef)));*/
|
2017-12-30 21:46:59 +00:00
|
|
|
DstFunction *f = dst_compile_func(cres);
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst ret;
|
2017-12-30 21:46:59 +00:00
|
|
|
if (dst_run(dst_wrap_function(f), &ret)) {
|
2018-01-04 02:36:10 +00:00
|
|
|
dst_puts(dst_formatc("runtime error: %S\n", dst_to_string(ret)));
|
2017-12-30 21:46:59 +00:00
|
|
|
} else {
|
|
|
|
dst_puts(dst_formatc("%v\n", ret));
|
2018-01-13 15:34:59 +00:00
|
|
|
set_underscore(ret);
|
2017-12-30 21:46:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-04 02:36:10 +00:00
|
|
|
dst_puts(dst_formatc("compile error: %S\n", cres.error));
|
2017-12-30 21:46:59 +00:00
|
|
|
}
|
2018-01-04 02:36:10 +00:00
|
|
|
bshift(&b, res.bytes_read);
|
2017-12-30 21:46:59 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-05-07 20:48:35 +00:00
|
|
|
}
|
2017-04-17 04:15:18 +00:00
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
done:
|
|
|
|
dst_buffer_deinit(&b);
|
|
|
|
return 0;
|
2017-04-18 02:14:35 +00:00
|
|
|
}
|
2017-04-17 04:15:18 +00:00
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
/* Run file */
|
|
|
|
static void runfile(const uint8_t *src, int32_t len) {
|
|
|
|
DstCompileOptions opts;
|
|
|
|
DstCompileResult cres;
|
|
|
|
DstParseResult res;
|
|
|
|
const uint8_t *s = src;
|
|
|
|
const uint8_t *end = src + len;
|
|
|
|
while (s < end) {
|
|
|
|
res = dst_parse(s, end - s);
|
|
|
|
switch (res.status) {
|
|
|
|
case DST_PARSE_NODATA:
|
|
|
|
return;
|
|
|
|
case DST_PARSE_UNEXPECTED_EOS:
|
|
|
|
case DST_PARSE_ERROR:
|
|
|
|
dst_puts(dst_formatc("syntax error at %d: %S\n",
|
|
|
|
s - src + res.bytes_read + 1, res.error));
|
2018-01-14 15:17:18 +00:00
|
|
|
if (res.bytes_read == 0) {
|
|
|
|
s++;
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
break;
|
|
|
|
case DST_PARSE_OK:
|
|
|
|
{
|
|
|
|
opts.source = res.value;
|
|
|
|
opts.flags = 0;
|
|
|
|
opts.env = env;
|
|
|
|
cres = dst_compile(opts);
|
|
|
|
if (cres.status == DST_COMPILE_OK) {
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst ret = dst_wrap_nil();
|
2017-12-30 21:46:59 +00:00
|
|
|
DstFunction *f = dst_compile_func(cres);
|
|
|
|
if (dst_run(dst_wrap_function(f), &ret)) {
|
|
|
|
dst_puts(dst_formatc("runtime error: %v\n", ret));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dst_puts(dst_formatc("compile error at %d: %S\n",
|
|
|
|
s - src + cres.error_start + 1, cres.error));
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 16:56:29 +00:00
|
|
|
break;
|
2017-04-17 04:15:18 +00:00
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
s += res.bytes_read;
|
2017-04-17 04:15:18 +00:00
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-04-17 04:15:18 +00:00
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
int main(int argc, char **argv) {
|
2018-01-12 21:25:24 +00:00
|
|
|
int status = 0;
|
2017-07-02 02:34:31 +00:00
|
|
|
int i;
|
|
|
|
int fileRead = 0;
|
2018-01-04 02:36:10 +00:00
|
|
|
uint32_t gcinterval = 0x10000;
|
2017-07-02 02:34:31 +00:00
|
|
|
uint64_t flags = 0;
|
2017-04-18 02:14:35 +00:00
|
|
|
|
2017-07-02 02:34:31 +00:00
|
|
|
/* Read the arguments. Ignore files. */
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
const char *arg = argv[i];
|
|
|
|
if (*arg == '-') {
|
|
|
|
/* Flag or option */
|
|
|
|
if (arg[1] == '-') {
|
|
|
|
/* Option */
|
|
|
|
if (client_strequal(arg + 2, "help")) {
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_HELP;
|
2017-07-02 02:34:31 +00:00
|
|
|
} else if (client_strequal(arg + 2, "version")) {
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_VERSION;
|
2017-07-02 02:34:31 +00:00
|
|
|
} else if (client_strequal(arg + 2, "verbose")) {
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_VERBOSE;
|
2017-07-02 02:34:31 +00:00
|
|
|
} else if (client_strequal(arg + 2, "repl")) {
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_REPL;
|
2017-12-30 21:46:59 +00:00
|
|
|
} else if (client_strequal_witharg(arg + 2, "gcinterval")) {
|
|
|
|
int status = 0;
|
|
|
|
int32_t m;
|
|
|
|
const uint8_t *start = (const uint8_t *)(arg + 13);
|
|
|
|
const uint8_t *end = start;
|
2017-07-03 00:51:52 +00:00
|
|
|
while (*end) ++end;
|
2017-12-30 21:46:59 +00:00
|
|
|
m = dst_scan_integer(start, end - start, &status);
|
|
|
|
if (!status)
|
|
|
|
gcinterval = m;
|
2017-07-02 02:34:31 +00:00
|
|
|
} else {
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_UNKNOWN;
|
2017-07-02 02:34:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Flag */
|
|
|
|
const char *c = arg;
|
|
|
|
while (*(++c)) {
|
|
|
|
switch (*c) {
|
|
|
|
case 'h':
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_HELP;
|
2017-07-02 02:34:31 +00:00
|
|
|
break;
|
2017-07-02 18:42:30 +00:00
|
|
|
case 'v':
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_VERSION;
|
2017-07-02 02:34:31 +00:00
|
|
|
break;
|
2017-07-02 18:42:30 +00:00
|
|
|
case 'V':
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_VERBOSE;
|
2017-07-02 02:34:31 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_REPL;
|
2017-07-02 02:34:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-09 18:39:51 +00:00
|
|
|
flags |= DST_CLIENT_UNKNOWN;
|
2017-07-02 02:34:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle flags and options */
|
2017-09-09 18:39:51 +00:00
|
|
|
if ((flags & DST_CLIENT_HELP) || (flags & DST_CLIENT_UNKNOWN)) {
|
2017-07-02 02:34:31 +00:00
|
|
|
printf( "Usage:\n"
|
|
|
|
"%s -opts --fullopt1 --fullopt2 file1 file2...\n"
|
|
|
|
"\n"
|
2017-12-30 21:46:59 +00:00
|
|
|
" -h --help Shows this information.\n"
|
|
|
|
" -V --verbose Show more output.\n"
|
|
|
|
" -r --repl Launch a repl after all files are processed.\n"
|
|
|
|
" -v --version Print the version number and exit.\n"
|
|
|
|
" --gcinterval=[int] Set the amount of memory to allocate before\n"
|
|
|
|
" forcing a collection in bytes. Max is 2^31-1,\n"
|
|
|
|
" min is 0.\n\n",
|
2017-07-02 02:34:31 +00:00
|
|
|
argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
if (flags & DST_CLIENT_VERSION) {
|
|
|
|
printf("%s\n", DST_VERSION);
|
2017-07-02 02:34:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up VM */
|
2017-12-30 21:46:59 +00:00
|
|
|
dst_init();
|
|
|
|
dst_vm_gc_interval = gcinterval;
|
2018-01-16 01:14:21 +00:00
|
|
|
env = dst_stl_env();
|
|
|
|
dst_gcroot(env);
|
2017-06-24 18:27:29 +00:00
|
|
|
|
2017-07-02 02:34:31 +00:00
|
|
|
/* Read the arguments. Only process files. */
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
const char *arg = argv[i];
|
|
|
|
if (*arg != '-') {
|
|
|
|
fileRead = 1;
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t len;
|
|
|
|
const uint8_t *s = loadsource(arg, &len);
|
|
|
|
if (NULL == s) {
|
|
|
|
printf("could not load file %s\n", arg);
|
|
|
|
} else {
|
|
|
|
runfile(s, len);
|
|
|
|
}
|
2017-07-02 02:34:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
/* Run a repl if nothing else happened, or the flag is set */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (!fileRead || (flags & DST_CLIENT_REPL)) {
|
2018-01-17 04:18:45 +00:00
|
|
|
DstContext ctxt;
|
|
|
|
dst_context_repl(&ctxt, env);
|
|
|
|
status = dst_context_run(&ctxt);
|
2017-04-18 02:14:35 +00:00
|
|
|
}
|
2017-04-17 04:15:18 +00:00
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
dst_deinit();
|
2017-04-17 04:15:18 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|