2018-02-25 06:35:58 +00:00
|
|
|
#include "defs.h"
|
|
|
|
#include "dict.h"
|
|
|
|
#include "stack.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "word.h"
|
|
|
|
|
2018-02-27 16:04:17 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-02-25 06:35:58 +00:00
|
|
|
static bool
|
|
|
|
add(System *sys)
|
|
|
|
{
|
|
|
|
KF_INT a = 0;
|
|
|
|
KF_INT b = 0;
|
|
|
|
if (!sys->dstack.pop(&a)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.pop(&b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
a += b;
|
|
|
|
return sys->dstack.push(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sub(System *sys)
|
|
|
|
{
|
|
|
|
KF_INT a = 0;
|
|
|
|
KF_INT b = 0;
|
|
|
|
if (!sys->dstack.pop(&a)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.pop(&b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
b -= a;
|
|
|
|
return sys->dstack.push(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
mul(System *sys)
|
|
|
|
{
|
|
|
|
KF_INT a = 0;
|
|
|
|
KF_INT b = 0;
|
|
|
|
if (!sys->dstack.pop(&a)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.pop(&b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
b *= a;
|
|
|
|
return sys->dstack.push(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
div(System *sys)
|
|
|
|
{
|
|
|
|
KF_INT a = 0;
|
|
|
|
KF_INT b = 0;
|
|
|
|
if (!sys->dstack.pop(&a)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.pop(&b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-27 16:04:17 +00:00
|
|
|
b /= a;
|
2018-02-25 06:35:58 +00:00
|
|
|
return sys->dstack.push(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
swap(System *sys)
|
|
|
|
{
|
|
|
|
KF_INT a = 0;
|
|
|
|
KF_INT b = 0;
|
|
|
|
if (!sys->dstack.pop(&a)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.pop(&b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.push(a)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sys->dstack.push(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
rot(System *sys)
|
|
|
|
{
|
|
|
|
KF_INT a = 0;
|
|
|
|
KF_INT b = 0;
|
|
|
|
KF_INT c = 0;
|
|
|
|
if (!sys->dstack.pop(&a)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.pop(&b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.pop(&c)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.push(b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sys->dstack.push(a)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sys->dstack.push(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: print multiple per line
|
|
|
|
static bool
|
|
|
|
definitions(System *sys)
|
|
|
|
{
|
2018-02-27 16:04:17 +00:00
|
|
|
Word *cursor = sys->dict;
|
2018-02-25 06:35:58 +00:00
|
|
|
char buf[MAX_TOKEN_LENGTH];
|
2018-02-27 16:04:17 +00:00
|
|
|
char line[72];
|
|
|
|
size_t buflen = 0, linelen = 0;
|
|
|
|
bool ready = false;
|
|
|
|
|
2018-02-25 06:35:58 +00:00
|
|
|
while (cursor != nullptr) {
|
2018-02-27 16:04:17 +00:00
|
|
|
if (ready) {
|
|
|
|
ready = false;
|
|
|
|
sys->interface->wrln(line, linelen);
|
|
|
|
linelen = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-25 06:35:58 +00:00
|
|
|
cursor->getname(buf, &buflen);
|
2018-02-27 16:04:17 +00:00
|
|
|
|
|
|
|
// TODO: get rid of magic numbers
|
|
|
|
if ((buflen + linelen) > 72) {
|
|
|
|
ready = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy(line + linelen, buf, buflen);
|
|
|
|
linelen += buflen;
|
|
|
|
|
|
|
|
if (linelen < 71) {
|
|
|
|
line[linelen++] = ' ';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ready = true;
|
|
|
|
}
|
2018-02-25 06:35:58 +00:00
|
|
|
cursor = cursor->next();
|
|
|
|
}
|
2018-02-27 16:04:17 +00:00
|
|
|
|
|
|
|
sys->interface->wrln(line, linelen);
|
2018-02-25 06:35:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-27 16:04:17 +00:00
|
|
|
init_dict(System *sys)
|
2018-02-25 06:35:58 +00:00
|
|
|
{
|
2018-02-27 16:04:17 +00:00
|
|
|
sys->dict = nullptr;
|
|
|
|
sys->dict = new Builtin((const char *)"DEFINITIONS", 11, sys->dict, definitions);
|
|
|
|
sys->dict = new Builtin((const char *)"+", 1, sys->dict, add);
|
|
|
|
sys->dict = new Builtin((const char *)"-", 1, sys->dict, sub);
|
|
|
|
sys->dict = new Builtin((const char *)"*", 1, sys->dict, mul);
|
|
|
|
sys->dict = new Builtin((const char *)"/", 1, sys->dict, div);
|
|
|
|
sys->dict = new Builtin((const char *)"SWAP", 4, sys->dict, swap);
|
|
|
|
sys->dict = new Builtin((const char *)"ROT", 3, sys->dict, rot);
|
2018-02-25 06:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LOOKUP
|
|
|
|
lookup(struct Token *token, System *sys)
|
|
|
|
{
|
2018-02-27 16:04:17 +00:00
|
|
|
Word *cursor = sys->dict;
|
2018-02-25 06:35:58 +00:00
|
|
|
KF_INT n;
|
|
|
|
|
|
|
|
if (parse_num(token, &n)) {
|
|
|
|
if (sys->dstack.push(n)) {
|
|
|
|
return LOOKUP_OK;
|
|
|
|
}
|
|
|
|
return LOOKUP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cursor != nullptr) {
|
|
|
|
if (cursor->match(token)) {
|
|
|
|
if (cursor->eval(sys)) {
|
|
|
|
return LOOKUP_OK;
|
|
|
|
}
|
|
|
|
return LOOKUP_FAILED;
|
|
|
|
}
|
|
|
|
cursor = cursor->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return LOOKUP_NOTFOUND;
|
|
|
|
}
|