scyther/src/symbol.c

273 lines
4.4 KiB
C
Raw Normal View History

2004-04-23 11:58:43 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
2004-04-23 11:58:43 +01:00
#include "symbol.h"
#include "debug.h"
2004-04-23 11:58:43 +01:00
#include "memory.h"
/*
Symbol processor.
Stores symbols for the lexical scanner. Can later print them.
Implementation uses a hashtable, the size of which is defined in
symbols.h.
*/
/* accessible for externals */
int globalError; //!< If >0, stdout output goes to stderr (for e.g. terms)
/* external declarations */
2004-04-23 11:58:43 +01:00
extern int yylineno;
/* global declarations */
2004-05-15 16:45:08 +01:00
//! Symbol hash table.
2004-04-23 11:58:43 +01:00
Symbol symbtab[HASHSIZE];
2004-05-15 16:45:08 +01:00
//! List of available (freed) symbol blocks.
2004-04-23 11:58:43 +01:00
Symbol symb_list;
2004-05-15 16:45:08 +01:00
//! List of all allocated symbol blocks.
2004-04-23 11:58:43 +01:00
Symbol symb_alloc;
/* main code */
2004-05-15 16:45:08 +01:00
//! Open symbols code.
2004-04-23 11:58:43 +01:00
void
symbolsInit (void)
{
int i;
for (i = 0; i < HASHSIZE; i++)
symbtab[i] = NULL;
symb_list = NULL;
symb_alloc = NULL;
globalError = 0;
2004-04-23 11:58:43 +01:00
}
2004-05-15 16:45:08 +01:00
//! Close symbols code.
2004-04-23 11:58:43 +01:00
void
symbolsDone (void)
{
Symbol s;
while (symb_alloc != NULL)
{
s = symb_alloc;
symb_alloc = s->allocnext;
memFree (s, sizeof (struct symbol));
}
}
2004-05-15 16:45:08 +01:00
//! Create a memory block for a symbol.
/**
* Internal memory management is used.
*@return A pointer to a memory block of size struct.
*/
2004-04-23 11:58:43 +01:00
Symbol
get_symb (void)
{
Symbol t;
if (symb_list != NULL)
{
t = symb_list;
symb_list = symb_list->next;
}
else
{
t = (Symbol) memAlloc (sizeof (struct symbol));
t->allocnext = symb_alloc;
symb_alloc = t;
}
t->keylevel = INT_MAX;
2004-04-23 11:58:43 +01:00
return t;
}
2004-05-15 16:45:08 +01:00
//! Declare a symbol to be freed.
2004-04-23 11:58:43 +01:00
void
free_symb (const Symbol s)
2004-04-23 11:58:43 +01:00
{
if (s == NULL)
return;
s->next = symb_list;
symb_list = s;
}
2004-05-15 16:45:08 +01:00
//! Return the index in the hash table for the string.
2004-04-23 11:58:43 +01:00
int
hash (const char *s)
2004-04-23 11:58:43 +01:00
{
int hv = 0;
int i;
for (i = 0; s[i] != EOS; i++)
{
int v = (hv >> 28) ^ (s[i] & 0xf);
hv = (hv << 4) | v;
}
hv = hv & 0x7fffffff;
return hv % HASHSIZE;
}
2004-05-15 16:45:08 +01:00
//! Insert a string into the hash table.
2004-04-23 11:58:43 +01:00
void
insert (const Symbol s)
2004-04-23 11:58:43 +01:00
{
2004-05-26 13:17:09 +01:00
int hv;
2004-04-23 11:58:43 +01:00
if (s == NULL)
return; /* illegal insertion of empty stuff */
2004-05-26 13:17:09 +01:00
hv = hash (s->text);
2004-04-23 11:58:43 +01:00
s->next = symbtab[hv];
symbtab[hv] = s;
}
2004-05-15 16:45:08 +01:00
//! Find a string in the hash table.
2004-04-23 11:58:43 +01:00
Symbol
lookup (const char *s)
2004-04-23 11:58:43 +01:00
{
2004-05-26 13:17:09 +01:00
int hv;
Symbol t;
2004-04-23 11:58:43 +01:00
if (s == NULL)
return NULL;
2004-05-26 13:17:09 +01:00
hv = hash (s);
t = symbtab[hv];
2004-04-23 11:58:43 +01:00
while (t != NULL)
{
if (strcmp (t->text, s) == 0)
break;
else
t = t->next;
}
return t;
}
2004-05-15 16:45:08 +01:00
//! Print a symbol.
2004-04-23 11:58:43 +01:00
void
symbolPrint (const Symbol s)
2004-04-23 11:58:43 +01:00
{
if (s == NULL)
return;
/* TODO maybe action depending on type? */
eprintf ("%s", s->text);
2004-04-23 11:58:43 +01:00
}
//! Print all symbols
void
symbolPrintAll (void)
{
int i, count;
eprintf ("List of all symbols\n");
count = 0;
for (i = 0; i < HASHSIZE; i++)
{
Symbol sym;
sym = symbtab[i];
if (sym != NULL)
{
eprintf ("H%i:\t", i);
while (sym != NULL)
{
count++;
eprintf ("[%s]\t", sym->text);
sym = sym->next;
}
eprintf ("\n");
}
}
eprintf ("Total:\t%i\n", count);
}
2004-05-15 16:45:08 +01:00
//! Insert a string into the symbol table, if it wasn't there yet.
/**
* Also sets line numbers and type.
*\sa T_SYSCONST
*/
2004-04-23 11:58:43 +01:00
Symbol
symbolSysConst (const char *str)
2004-04-23 11:58:43 +01:00
{
Symbol symb;
symb = lookup (str);
if (symb == NULL)
{
symb = get_symb ();
symb->lineno = yylineno;
symb->type = T_SYSCONST;
symb->text = str;
insert (symb);
2004-04-23 11:58:43 +01:00
}
return symb;
}
//! Fix all the unset keylevels
void
symbol_fix_keylevels (void)
{
int i;
for (i = 0; i < HASHSIZE; i++)
{
Symbol sym;
sym = symbtab[i];
while (sym != NULL)
{
#ifdef DEBUG
if (DEBUGL (5))
{
eprintf ("Symbol ");
symbolPrint (sym);
}
#endif
if (sym->keylevel == INT_MAX)
{
// Nothing currently, this simply does not originate on a strand.
#ifdef DEBUG
if (DEBUGL (5))
{
eprintf (" doesn't have a keylevel yet.\n");
}
#endif
}
#ifdef DEBUG
else
{
if (DEBUGL (5))
{
eprintf (" has keylevel %i\n", sym->keylevel);
}
}
#endif
sym = sym->next;
}
}
}
//! Print out according to globalError
/**
* Input is comparable to printf, only depends on globalError. This should be used by any function trying to do output.
*\sa globalError
*/
void
eprintf (char *fmt, ...)
{
va_list args;
va_start (args, fmt);
if (globalError == 0)
vfprintf (stdout, fmt, args);
else
vfprintf (stderr, fmt, args);
va_end (args);
}