From 13802c4d560e8e6de4a06fb8f7e707e41436dffe Mon Sep 17 00:00:00 2001 From: kdx Date: Sat, 14 Jan 2023 19:22:48 +0100 Subject: sloth_exec_line --- main.c | 5 ++--- sloth.c | 18 ++++++++++++++++++ sloth.h | 2 ++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index ad9036c..d6d8b1b 100644 --- a/main.c +++ b/main.c @@ -14,13 +14,12 @@ int main(int argc, char **argv) /* Read stdin line by line until exhaustion. */ char *line = NULL; while ((line = getln(stdin)) != NULL) { - err = sloth_push(sloth, line[0]); + err = sloth_exec_line(sloth, line); + free(line); if (err != NULL) { fprintf(stderr, "%s\n", err); break; } - sloth_inspect_stack(sloth); - free(line); } /* Free everything. */ getln_cleanup(); diff --git a/sloth.c b/sloth.c index 8314b79..5db7df0 100644 --- a/sloth.c +++ b/sloth.c @@ -1,6 +1,7 @@ #include "sloth.h" #include #include +#include void sloth_deinit(Sloth *ctx) { @@ -10,6 +11,23 @@ void sloth_deinit(Sloth *ctx) } } +SlothError sloth_exec(Sloth *ctx, const char *s) +{ + printf("exec %s\n", s); + return NULL; +} + +SlothError sloth_exec_line(Sloth *ctx, char *s) +{ + static const char *sep = "\t\n\v\f\r "; + for (char *tok = strtok(s, sep); tok != NULL; tok = strtok(NULL, sep)) { + const SlothError err = sloth_exec(ctx, tok); + if (err != NULL) + return err; + } + return NULL; +} + SlothError sloth_pop(Sloth *ctx, SlothByte *v) { if (ctx->stack_size == 0) diff --git a/sloth.h b/sloth.h index af6a7c1..809b99d 100644 --- a/sloth.h +++ b/sloth.h @@ -15,6 +15,8 @@ typedef struct Sloth { } Sloth; void sloth_deinit(Sloth *ctx); +SlothError sloth_exec(Sloth *ctx, const char *s); +SlothError sloth_exec_line(Sloth *ctx, char *s); SlothError sloth_pop(Sloth *ctx, SlothByte *v); SlothError sloth_push(Sloth *ctx, SlothByte v); void sloth_inspect_stack(const Sloth *ctx); -- cgit v1.2.3