From c925f5620584e62e5af553bb96978d2c474196e0 Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <clyne@bitgloo.com>
Date: Tue, 28 Jan 2025 08:09:09 -0500
Subject: add bye to x86

---
 foci.c     | 16 ++++++++--------
 foci.h     | 11 +++++++----
 x86/main.c |  4 ++++
 3 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/foci.c b/foci.c
index fdf7d76..e752946 100644
--- a/foci.c
+++ b/foci.c
@@ -115,8 +115,8 @@ N(mod,    "mod",    &w_ndiv)  { sp[1] %= sp[0]; ++sp; NEXT; }
 N(and,    "and",    &w_mod)   { sp[1] &= sp[0]; ++sp; NEXT; }
 N(or,     "or",     &w_and)   { sp[1] |= sp[0]; ++sp; NEXT; }
 N(xor,    "xor",    &w_or)    { sp[1] ^= sp[0]; ++sp; NEXT; }
-N(eq,     "=",      &w_xor)   { sp[1] = sp[0] == sp[1]; ++sp; NEXT; }
-N(lt,     "<",      &w_eq)    { sp[1] = sp[0] > sp[1]; ++sp; NEXT; }
+N(eq,     "=",      &w_xor)   { sp[1] = sp[0] == sp[1] ? -1 : 0; ++sp; NEXT; }
+N(lt,     "<",      &w_eq)    { sp[1] = sp[0] > sp[1] ? -1 : 0; ++sp; NEXT; }
 N(compname, "_c",   &w_lt)    ;
 C(intr,   "[",      &w_compname) { STATE = 0; NEXT; }
 N(comp,   "]",      &w_intr)  { STATE = -1; NEXT; }
@@ -165,7 +165,7 @@ N(pushr2, "2>r",    &w_popr)    { *--rp = (intptr_t **)sp[1];
 N(popr2,  "2r>",    &w_pushr2)  { *--sp = (intptr_t)rp[1];
                                   *--sp = (intptr_t)rp[0]; rp += 2; NEXT; }
 N(rpeek,  "r@",     &w_popr2)   { *--sp = (intptr_t)*rp; NEXT; }
-N(i,      "i",      &w_rpeek)   { *--sp = (intptr_t)*rp; NEXT; }
+N(i,      "i",      &w_rpeek)   { goto *&rpeek; }
 I(fdo,    "do",     &w_i)       LIT(pushr2), comma, FTH(begin), END
 I(loop,   "loop",   &w_fdo)     LIT(popr), comma,
                                 LIT(enter), comma,
@@ -180,9 +180,9 @@ I(loop,   "loop",   &w_fdo)     LIT(popr), comma,
                                 LIT(drop), comma, LIT(drop), comma, END
 W(immediate, "immediate", &w_loop) FTH(latest), LIT(2 * sizeof(intptr_t)), add,
                                    LIT(ATTR_IMMEDIATE), over, cpeek, or, swap, cpoke, END
-#define LATEST_INIT &w_immediate
+// Be sure to update LATEST_INIT in foci.h!
 
-void enter_forth(const void *ptr)
+static void enter_forth(void *ptr)
 {
     STASH;
     sp = saved_sp;
@@ -281,7 +281,7 @@ static void parse_word(const char *start, const char *end)
                 *here++ = (intptr_t)l->body[0];
             }
         } else {
-            enter_forth(l->body);
+            enter_forth((void *)l->body);
         }
     }
 }
@@ -333,9 +333,9 @@ int compiling(void)
     return STATE;
 }
 
-void define(struct word_t *w)
+void define(const struct word_t *w)
 {
-    w->prev = (struct word_t *)LATEST;
+    //w->prev = (struct word_t *)LATEST;
     LATEST = (intptr_t)w;
 }
 
diff --git a/foci.h b/foci.h
index aae4946..b2b8e1c 100644
--- a/foci.h
+++ b/foci.h
@@ -35,13 +35,13 @@
 #define FTH(w)  enter, (w##_body)
 #define END    fexit, };
 #define NATIVE(name, tname, prev, attr) \
-    static const void * const name##_body[2]; \
+    const void * const name##_body[2]; \
     NAKED void name(void); \
-    static const struct word_t w_##name = { \
+    const struct word_t w_##name = { \
         name##_body, prev, \
         (attr | ATTR_NATIVE) + sizeof(tname) - 1, tname \
     }; \
-    static const void * const name##_body[2] = { name, fexit }; \
+    const void * const name##_body[2] = { name, fexit }; \
     NAKED void name(void)
 
 #define W(name, tname, prev) WORD(name, tname, prev, 0)
@@ -87,6 +87,9 @@ register intptr_t     tmp asm("r7");
 #define NEXT              asm("incd r6\nbr @r6")
 #endif // FOCI_MSP430
 
+extern const struct word_t w_immediate;
+#define LATEST_INIT &w_immediate
+
 extern void foci_putchar(int);
 extern int  foci_getchar(void);
 
@@ -95,7 +98,7 @@ int depth(void);
 int rdepth(void);
 int compiling(void);
 void interpret(void);
-void define(struct word_t *w);
+void define(const struct word_t *w);
 
 void fexit(void); // internal use only
 
diff --git a/x86/main.c b/x86/main.c
index 1415cc2..fe4692a 100644
--- a/x86/main.c
+++ b/x86/main.c
@@ -17,12 +17,16 @@
 #include "foci.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 
 static intptr_t dict[8192];
 
+N(bye, "bye", LATEST_INIT) { exit(0); }
+
 int main()
 {
     init(dict);
+    define(&w_bye);
 
     for (;;) {
         interpret();
-- 
cgit v1.2.3