sandbox/v1/kforth.cc

89 lines
1.6 KiB
C++
Raw Normal View History

2018-02-25 06:35:58 +00:00
#include "dict.h"
2018-02-22 19:38:27 +00:00
#include "io.h"
#include "parser.h"
2018-02-25 06:35:58 +00:00
#include "system.h"
2018-02-22 19:38:27 +00:00
2018-02-23 22:01:52 +00:00
#include <stdlib.h>
#include <string.h>
2018-02-23 22:01:52 +00:00
2018-02-22 19:38:27 +00:00
#ifdef __linux__
#include "linux.h"
#endif // __linux__
2018-02-25 06:35:58 +00:00
static System sys;
#ifndef TRACE_STACK
#define TRACE_STACK false
#endif
2018-02-23 22:01:52 +00:00
static bool
2018-02-25 06:35:58 +00:00
parser(const char *buf, const size_t buflen)
2018-02-23 22:01:52 +00:00
{
static size_t offset = 0;
static struct Token token;
static PARSE_RESULT result = PARSE_FAIL;
offset = 0;
// reset token
token.token = nullptr;
token.length = 0;
while ((result = parse_next(buf, buflen, &offset, &token)) == PARSE_OK) {
if (!lookup(&token, &sys)) {
2018-02-25 06:35:58 +00:00
break;
}
2018-02-23 22:01:52 +00:00
}
system_write_status(&sys);
sys.interface->newline();
2018-02-23 22:01:52 +00:00
switch (result) {
2018-02-25 06:35:58 +00:00
case PARSE_OK:
return false;
2018-02-23 22:01:52 +00:00
case PARSE_EOB:
return true;
case PARSE_LEN:
2018-02-25 06:35:58 +00:00
sys.interface->wrln((char *)"parse error: token too long", 27);
2018-02-23 22:01:52 +00:00
return false;
case PARSE_FAIL:
2018-02-25 06:35:58 +00:00
sys.interface->wrln((char *)"parser failure", 14);
2018-02-23 22:01:52 +00:00
return false;
default:
2018-02-25 06:35:58 +00:00
sys.interface->wrln((char *)"*** the world is broken ***", 27);
2018-02-23 22:01:52 +00:00
exit(1);
}
}
2018-02-22 19:38:27 +00:00
static void
2018-02-25 06:35:58 +00:00
interpreter()
2018-02-22 19:38:27 +00:00
{
static size_t buflen = 0;
static char linebuf[81];
while (true) {
if (TRACE_STACK) {
write_dstack(sys.interface, sys.dstack);
sys.interface->newline();
}
2018-02-25 06:35:58 +00:00
sys.interface->wrch('?');
sys.interface->wrch(' ');
buflen = sys.interface->rdbuf(linebuf, 80, true, '\n');
parser(linebuf, buflen);
2018-02-22 19:38:27 +00:00
}
}
static char banner[] = "kforth interpreter";
const size_t bannerlen = 18;
2018-02-22 19:38:27 +00:00
int
main(void)
{
reset_system(&sys);
init_dict(&sys);
2018-02-22 19:38:27 +00:00
#ifdef __linux__
Console interface;
2018-02-25 06:35:58 +00:00
sys.interface = &interface;
2018-02-22 19:38:27 +00:00
#endif
sys.interface->wrln(banner, bannerlen);
2018-02-25 06:35:58 +00:00
interpreter();
2018-02-22 19:38:27 +00:00
return 0;
}