aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-01-15 02:09:24 +0100
committerkdx <kikoodx@paranoici.org>2023-01-15 02:09:24 +0100
commite44e9fbdb4fcdb71bdeba28deeb3dfca8526bbd3 (patch)
tree5143619a25c06a7278b9109cb7ac553f5bc7c6e0
parent49aeb9687849941d73e63669ff1313295e22af76 (diff)
downloadsloth-e44e9fbdb4fcdb71bdeba28deeb3dfca8526bbd3.tar.gz
make ; immediateHEADmain
-rw-r--r--sloth.c13
-rw-r--r--sloth.h5
2 files changed, 15 insertions, 3 deletions
diff --git a/sloth.c b/sloth.c
index 500c327..3ef6334 100644
--- a/sloth.c
+++ b/sloth.c
@@ -43,7 +43,7 @@ SlothError sloth_init(Sloth *ctx)
sloth_deinit(ctx);
return err;
}
- err = sloth_dict_append(ctx, SLOTH_DICT_C(";", sloth_compile_end));
+ err = sloth_dict_append(ctx, SLOTH_DICT_I(";", sloth_compile_end));
if (err != NULL) {
sloth_deinit(ctx);
return err;
@@ -179,12 +179,21 @@ SlothError sloth_compile(Sloth *ctx, const char *s)
return NULL;
}
+static int sloth_is_immediate(Sloth *ctx, const char *s)
+{
+ const SlothHash hash = XXH32(s, strlen(s), 0);
+ for (SlothDict *dict = ctx->dict; dict != NULL; dict = dict->next)
+ if (dict->hash == hash)
+ return dict->immediate;
+ return 0;
+}
+
SlothError sloth_exec_line(Sloth *ctx, char *s)
{
SlothError err = NULL;
static const char *sep = "\t\n\v\f\r ";
for (char *tok = strtok(s, sep); tok != NULL; tok = strtok(NULL, sep)) {
- if (ctx->compile && strcmp(tok, ";") != 0)
+ if (ctx->compile && sloth_is_immediate(ctx, tok))
err = sloth_compile(ctx, tok);
else
err = sloth_exec(ctx, tok);
diff --git a/sloth.h b/sloth.h
index 53d7c24..d3bb00c 100644
--- a/sloth.h
+++ b/sloth.h
@@ -15,12 +15,15 @@ typedef struct SlothInstr SlothInstr;
struct SlothDict {
SlothHash hash;
int c_func;
+ int immediate;
SlothError (*func)(Sloth *ctx);
SlothInstr *instrs;
SlothDict *next;
};
#define SLOTH_DICT_C(word, func) \
- (SlothDict){XXH32(word, strlen(word), 0), 1, func, NULL, NULL}
+ (SlothDict){XXH32(word, strlen(word), 0), 1, 0, func, NULL, NULL}
+#define SLOTH_DICT_I(word, func) \
+ (SlothDict){XXH32(word, strlen(word), 0), 1, 1, func, NULL, NULL}
struct Sloth {
SlothByte *stack;