Source

dcpu-16-forth / forth-core.c

Full commit
// I'm going to implement the core in C, then convert to asm manually.
// I just don't know enough asm to do it the hard way first.

// byte is 8 bits long
// int is 16 bits long
typedef struct {
  char length;
  char[] string;
} string;

typedef struct {
  char word_length;
  string word;
  string *defn;
} 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.
} 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 (j = 0; j < entry.word_length; j++) {
      char *n = &dict + offset + j;
      if (*n != word->string[j]) {
        break;
      }
    } 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
                                     // length + pointer.
  }
  crash("Could not lookup word " + word)
}