head	1.1;
access;
symbols;
locks
	gtoal:1.1; strict;
comment	@ * @;


1.1
date	2022.02.15.00.59.09;	author gtoal;	state Exp;
branches;
next	;


desc
@symbol table managememt
@


1.1
log
@Initial revision
@
text
@#ifndef _SYMTAB_H_
// public interface
#include "symtab.h"
#endif

// See symtab.h for descriptionm

#ifndef _SYMTAB_PRIV_H_
// private section
#include "symtab_priv.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// #define TEST_MAIN 1   // Generate a main() to test the library - set in Makefile

#ifndef TRUE
#define TRUE (0==0)
#define FALSE (0!=0)
#endif

static int debug = FALSE;

scope_list *top_level_scope = NULL;
scope_list *base_level_scope = NULL;

static void *Malloc(int n) {
  void *p = malloc(n);
  if (debug) fprintf(stderr, "Malloc: %p\n", p);
  return p;
}

static void Free(void *p) {
  if (debug) fprintf(stderr, "Free: %p\n", p);
}

// Using 'match' rather than strcmp in the lookup procedures allows us to look up a
// variable in multiple symbol tables.  We also sneak in a wildcard here though it
// is unlikely to ever be very useful with the current interface.

static int match(char *given_name, char *symbol_name) {
  if (given_name == symbol_name) return TRUE; // fast match if using strings found in a string table.
  if (*given_name == '*' && given_name[1] == '\0') return TRUE; // wildcard match. Only "*" supported.
  return strcmp(given_name, symbol_name) == 0;
}

static void destroy_table(generic_table_entry *entry) {
  generic_table_entry *next_entry;
  while (entry != NULL) {
    next_entry = entry->next_entry;
    // destroy_data(entry->entry_data); // This would require a callback... let's leave it to the user for now.
    Free(entry);
    entry = next_entry;
  }
}

static scope_list *new_scope(scope_list *previous_scopes) {
  scope_list *scope = Malloc(sizeof(scope_list));
  scope->level = ++current_scope;
  scope->table = NULL;
  scope->next_scope = previous_scopes;
  if (debug) fprintf(stderr, "Create %s scope %d\n", (previous_scopes == NULL ? "top-level" : "nested"), current_scope);
  return scope;
}

static void destroy_tables(table_list *table) {
  table_list *next_table;
  while (table != NULL) {
    next_table = table->next_table;
    destroy_table(table->table_data);
    Free(table);
    table = next_table;
  }
}

static void end_scope(scope_list **parent) {
#define scope (*parent)
  scope_list *next_scope;
  if (parent == NULL || scope == NULL) {
    fprintf(stderr, "Warning! Asked to destroy NULL scope\n");
    return;
  }
  if (scope->level != current_scope) {
    fprintf(stderr, "Warning! Asked to destroy scope level %d, but current scope is %d\n", scope->level, current_scope);
  }
  if (scope != top_level_scope) {
    fprintf(stderr, "Warning! Scope being ended (level %d) is not the top-level scope (level %d)!\n", scope->level, top_level_scope->level);
    return;
  }
  if (debug) fprintf(stderr, "Destroying scope level %d\n", current_scope);
  next_scope = scope->next_scope;
  current_scope -= 1;
  destroy_tables(scope->table); scope->table = NULL;
  Free(scope);
  scope = next_scope;
#undef scope
}

static void add_new_table(scope_list *current_scope, char *table_name) {
  table_list *table = Malloc(sizeof(table_list));
  table->table_name = table_name;
  table->table_data = NULL;
  table->next_table = current_scope->table;
  current_scope->table = table;
}

static int add_entry_internal(char *table_name, char *entry_name, void *entry_data, int top) {
  // If table_name does not exist at this scope, create it.
  scope_list *s = top_level_scope;
  if (debug && top) fprintf(stderr, "Add %s/%s to Scope: %d\n", table_name, entry_name, s->level);
  if (s->table != NULL) {
    table_list *t = s->table;
    while (t != NULL) {
      if (debug && top) fprintf(stderr, "  Add to Table: %s\n", t->table_name);
      if (match(table_name, t->table_name)) {
        // Table exists!
        // add entry to the table in current scope

        // Check for presence:
        generic_table_entry *d = t->table_data;
        if (top) {
          while (d != NULL) {
            if (debug && top) fprintf(stderr, "    Add to Entry: %s\n", d->entry_name);
            if (match(entry_name, d->entry_name)) {
              // User can warn or error if already present...
              return FALSE;
            }
            d = d->next_entry;
          }
        }

        // Now add:
        d = Malloc(sizeof(generic_table_entry));
        d->entry_name = entry_name;
        d->entry_data = entry_data;
        d->next_entry = t->table_data;
        t->table_data = d;
        return TRUE;
      }
      t = t->next_table;
    }
  }
  // Table does not exist!  Create it!
  add_new_table(top_level_scope, table_name);
  return add_entry_internal(table_name, entry_name, entry_data, FALSE); // Safe to unconditionally add now...
}

int add_entry(char *table_name, char *entry_name, void *entry_data) {
  return add_entry_internal(table_name, entry_name, entry_data, TRUE);
}

// exported, but not included in header file.
void *lookup_with_scope(char *table_name, char *entry_name, int *scope_level) {
  scope_list *s = top_level_scope;
  while (s != NULL) {
    if (debug) fprintf(stderr, "Lookup %s/%s in Scope: %d\n", table_name, entry_name, s->level);
    if (s->table != NULL) {
      table_list *t = s->table;
      if (debug) fprintf(stderr, "  Lookup %s in Table: %s\n", table_name, t->table_name);
      while (t != NULL) {
        if (match(table_name, t->table_name)) {
          generic_table_entry *d = t->table_data;
          while (d != NULL) {
            if (debug) fprintf(stderr, "    Lookup %s in Entry: %s\n", entry_name, d->entry_name);
            if (match(entry_name, d->entry_name)) {
              if (scope_level != NULL) *scope_level = s->level;
              return d->entry_data;
            }
            d = d->next_entry;
          }
        }
        t = t->next_table;
      }
    }
    s = s->next_scope;
  }
  return NULL;
}

void *lookup(char *table_name, char *entry_name) {
  return lookup_with_scope(table_name, entry_name, NULL);
}

int scope_level(char *table_name, char *entry_name) {
  void *result;
  int scope_level;
  result = lookup_with_scope(table_name, entry_name, &scope_level);
  if (result != NULL) return scope_level;
  return -1;
}

void push_scope_level(void) {
  if (top_level_scope == NULL) {
    top_level_scope = base_level_scope = new_scope(NULL); // Move to scope level 0
    return;
  }
  top_level_scope = new_scope(top_level_scope); // Move to scope level 1
}

void pop_scope_level(void) {
  end_scope(&top_level_scope);
  if (top_level_scope == NULL) base_level_scope = NULL;
}

#ifdef TEST_MAIN
int main(int argc, char **argv) {
  double pi = 3.141592653589793;
  double pi2 = 3.14;
  double *found;

  if ((argc == 2) && (strcmp(argv[1], "-d") == 0)) debug = TRUE;

  push_scope_level(); // Move to scope level 0
  fprintf(stderr, "push scope\n");
  
  // declare any over-ridable extrinsics here...
  push_scope_level(); // Move to scope level 1 - ready for externals, top-level statics, etc.
  fprintf(stderr, "push scope\n");

  // enter a '{' group
  push_scope_level(); // Move to next scope level - inside a '{ ... }' block
  fprintf(stderr, "push scope\n");

  if (!add_entry("floats", "pi", &pi)) fprintf(stderr, "Error: %s already exists in %s in scope %d!\n", "pi", "floats", current_scope);
  else fprintf(stderr, "Added floats/pi at scope %d\n", current_scope);

  // enter a '{' group
  push_scope_level(); // Move to next scope level - inside a '{ ... }' block
  fprintf(stderr, "push scope\n");

  if (!add_entry("floats", "pi", &pi2)) fprintf(stderr, "Error: %s already exists in %s in scope %d!\n", "pi", "floats", current_scope);
  else fprintf(stderr, "Added floats/pi at scope %d\n", current_scope);

  if (!add_entry("floats", "pi", &pi2)) fprintf(stderr, "Error: %s already exists in %s in scope %d!\n", "pi", "floats", current_scope);
  else fprintf(stderr, "Added floats/pi at scope %d\n", current_scope);

  found = lookup("floats", "pi");
  if (found) fprintf(stdout, "      pi in scope %d is %f\n", current_scope, *found);
  else fprintf(stderr, "      floats/pi not found in scope %d\n", current_scope);
  // return from '}' group
  pop_scope_level();
  fprintf(stderr, "pop scope\n");

  found = lookup("floats", "pi");
  if (found) fprintf(stdout, "      pi in scope %d is %f\n", current_scope, *found);
  else fprintf(stderr, "      floats/pi not found in scope %d\n", current_scope);
  // return from '}' group
  pop_scope_level();
  fprintf(stderr, "pop scope\n");

  found = lookup("floats", "pi");
  if (found) fprintf(stdout, "      pi in scope %d is %f\n", current_scope, *found);
  else fprintf(stderr, "      floats/pi not found in scope %d\n", current_scope);
  pop_scope_level();
  fprintf(stderr, "pop scope\n");

  while (current_scope >= 0) {
    pop_scope_level(); // Clean up...
    fprintf(stderr, "pop scope\n");
  }
  
  return 0;
}
#endif
@
