1. Nick Meharry
  2. dcpu-16-forth

Commits

Nick Meharry  committed 2c60938

Need to find a better assembler. This one won't use labels defined later.

  • Participants
  • Parent commits 14bd239
  • Branches default

Comments (0)

Files changed (2)

File forth-core.c

View file
  • Ignore whitespace
 
 // byte is 8 bits long
 // int is 16 bits long
+
+// Of course I'm using inline wrong.
+// The idea is that the data structure will grow
+// upwards, freely stomping on everything else
+// because I have no idea how to implement malloc.
+
+// This means I can't use direct addressing in wrapping
+// data structures, but I'll try to work around that.
 typedef struct {
   char length;
-  char[] string;
+  inline char[] string;
 } string;
 
 typedef struct {
 } dict_entry_t;
 
 typedef struct {
-  int count;
-  inline dict_entry[] words; // Of course I'm using inline wrong.
-                             // The idea is that the data structure will grow
-                             // upwards, freely stomping on everything else
-                             // because I have no idea how to implement malloc.
+  byte count;
+  inline dict_entry[] words;
 } dict_t;
 
 dict_t dict = {0};
 
 string *wl(string *word) {
-  int i = 0;
   char *j;
   int offset = 1;
-  for (i = 0; i < dict.count; i++) {
-    dict_entry_t entry = dict.words[i];
-    byte matches = 0;
+  for (byte i = 0; i < dict.count; i++) {
+    dict_entry_t entry = (dict_entry_t)*(&dict+offset);
     for (j = 0; j < entry.word_length; j++) {
       char *n = &dict + offset + j;
       if (*n != word->string[j]) {
     } else { // If the loop falls through without a break
       return entry.defn;
     }
-    offset += entry.word_length + 2; // Move to next word. The 3 is for
+    offset += entry.word_length + 3; // Move to next word. The 3 is for
                                      // length + pointer.
   }
   crash("Could not lookup word " + word)

File forth-core.dasm

View file
  • Ignore whitespace
+; FORTH core
+; by Nick Meharry
+
+; Datatypes:
+;   Strings are length-prefixed, not null terminated.
+; Register conventions:
+;   A, B, C, I, and J are used for the currently executing function.
+;   You should stash these on the stack before calling a function.
+;   X, Y, and Z are used for passing parameters to the called function.
+
+#define DICT 0xa00
+
+:setup
+	; Initialize the master dict
+	set [0xa00], 0x0
+
+; master dict starts at DICT
+:wl
+	set i, 0
+	; a is the offset from the dict base
+	set a, 1
+:wlwordloop
+	ifg [DICT], i
+		set pc, wlwordloopend; No matching word, return
+	; entry is at DICT + a
+	; b is a pointer to the entry
+	set b, [DICT + a]
+	; c is a flag to track if the word matched
+	set c, 1
+	set j, 0
+:wlcharloop
+	ifn [b+j], [x+j+1]
+		set c, 0
+	
+:wlwordloopend
+	set x, 0
+	set pc, pop
+
+
+:wlnomatch
+:crash  set pc, crash