aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-01-14 19:22:48 +0100
committerkdx <kikoodx@paranoici.org>2023-01-14 19:22:48 +0100
commit13802c4d560e8e6de4a06fb8f7e707e41436dffe (patch)
treedd6af82efeece61b01725609c742788be5052611
parenteaca074e5fee695fcf08a9b21687e3f01df107ae (diff)
downloadsloth-13802c4d560e8e6de4a06fb8f7e707e41436dffe.tar.gz
sloth_exec_line
-rw-r--r--main.c5
-rw-r--r--sloth.c18
-rw-r--r--sloth.h2
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 <stdio.h>
#include <stdlib.h>
+#include <string.h>
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);