commit 9db2a4ea56a9d940b9ab41b7d762b11aee0997db
parent 0fb8efc3df32a6bb87d73b2326e5261b7cbf5cbe
Author: prenev <an2qzavok@gmail.com>
Date: Tue, 12 Jul 2022 22:44:27 +0300
start a simple repl
Diffstat:
M | Makefile | | | 18 | +++++++++++++----- |
A | repl.c | | | 85 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | util.c | | | 20 | ++++++++++++++++++++ |
A | util.h | | | 2 | ++ |
4 files changed, 120 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,26 +1,34 @@
.POSIX:
+TARG =\
+ main\
+ repl
+
OBJ =\
bus\
cell\
mem\
- main
+ util\
HEAD =\
bus\
cell\
+ util\
mem
OBJS=${OBJ:=.o}
-all: main
+all: repl main
-main: ${OBJS}
- cc -o $@ ${OBJS}
+main: ${OBJS} main.o
+ cc -o $@ ${OBJS} main.o
+
+repl: ${OBJS} repl.o
+ cc -o repl repl.o util.o
.c.o:
cc -o $@ -c $<
clean:
- rm -rf ${OBJS} main
+ rm -rf ${OBJS} main repl main.o repl.o
diff --git a/repl.c b/repl.c
@@ -0,0 +1,85 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "util.h"
+
+char buf[1024];
+int n;
+
+#define CMD(x) void x(void)
+CMD(str);
+CMD(hex);
+CMD(stk);
+CMD(hlp);
+
+struct {
+ const char *cmd;
+ void (*fp)(void);
+} dict[]= {
+ {"str", str},
+ {"hex", hex},
+ {"stk", stk},
+ {"hlp", hlp},
+};
+
+void
+repl(void)
+{
+ int i;
+ printf("> ");
+ fflush(stdout);
+ while((n = read(0, buf, 1024)) > 0){
+ void (*fp)(void) = NULL;
+ for (i = 0; i < sizeof(dict)/sizeof(dict[0]); i++){
+ if (strncmp(dict[i].cmd, buf, 3) == 0)
+ fp = dict[i].fp;
+ }
+ if (fp == NULL) printf("?\n");
+ else fp();
+ printf("> ");
+ fflush(stdout);
+ }
+}
+
+int
+main(void)
+{
+ repl();
+ return 0;
+}
+
+void
+str(void)
+{
+ printf("` ");
+ fflush(stdout);
+ n = read(0, buf, 1024);
+}
+
+void
+hex(void)
+{
+ long long x;
+ printf("0x");
+ fflush(stdout);
+ n = read(0, buf, 1024);
+ x = strtoll(buf, 0, 16);
+}
+
+void
+hlp(void)
+{
+ int i;
+ for (i = 0; i < sizeof(dict) / sizeof(dict[0]); i++) {
+ printf("%s ", dict[i].cmd);
+ }
+ printf("\n");
+}
+
+void
+stk(void)
+{
+}
+
diff --git a/util.c b/util.c
@@ -0,0 +1,20 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+int
+hexdump(int n, char *p)
+{
+ if (n > 16) n = 16;
+ int i;
+ for (i = 0; i < 16; i++) {
+ if (i < n) printf("%02hhx ", p[i]);
+ else printf(" ");
+ }
+ for (i = 0; i < n; i++) {
+ char c = p[i];
+ if ((c < 0x20) && (c >= 0x7f)) c = '.';
+ printf("%c", c);
+ }
+ return n;
+}
+
diff --git a/util.h b/util.h
@@ -0,0 +1,2 @@
+int hexdump(int, char *);
+