2005-08-07 19:45:09 +02:00
|
|
|
// The symbol table is a simple array of struct U.
|
2004-04-04 10:21:10 +02:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "defs.h"
|
2005-08-07 19:45:09 +02:00
|
|
|
#define NSYM 1000
|
|
|
|
U symtab[NSYM];
|
|
|
|
char *printname[NSYM];
|
2005-08-06 22:57:37 +02:00
|
|
|
int nsym; // number of symbols in table;
|
2004-04-04 10:21:10 +02:00
|
|
|
|
2005-08-07 19:45:09 +02:00
|
|
|
// put symbol at index n
|
|
|
|
|
2005-08-07 16:42:42 +02:00
|
|
|
void
|
|
|
|
std_symbol(char *s, int n)
|
|
|
|
{
|
|
|
|
U *p;
|
|
|
|
p = symtab + n;
|
|
|
|
p->k = SYM;
|
|
|
|
p->u.sym.binding = p;
|
|
|
|
p->u.sym.binding2 = nil;
|
2005-08-07 19:45:09 +02:00
|
|
|
printname[n] = s;
|
2005-08-07 16:42:42 +02:00
|
|
|
}
|
|
|
|
|
2005-08-07 19:45:09 +02:00
|
|
|
// symbol lookup, create symbol if need be
|
|
|
|
|
2004-04-04 10:21:10 +02:00
|
|
|
U *
|
2005-08-07 16:42:42 +02:00
|
|
|
usr_symbol(char *s)
|
2004-04-04 10:21:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
U *p;
|
|
|
|
for (i = 0; i < nsym; i++)
|
|
|
|
if (strcmp(s, printname[i]) == 0)
|
|
|
|
return symtab + i;
|
2005-08-07 19:45:09 +02:00
|
|
|
if (nsym == NSYM)
|
2004-04-04 10:21:10 +02:00
|
|
|
stop("symbol table overflow");
|
|
|
|
p = symtab + nsym;
|
|
|
|
printname[nsym] = strdup(s);
|
|
|
|
nsym++;
|
|
|
|
p->k = SYM;
|
|
|
|
p->u.sym.binding = p;
|
|
|
|
p->u.sym.binding2 = nil;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2005-08-07 19:45:09 +02:00
|
|
|
// get the symbol's printname
|
|
|
|
|
2004-04-04 10:21:10 +02:00
|
|
|
char *
|
|
|
|
get_printname(U *p)
|
|
|
|
{
|
|
|
|
int n;
|
2004-05-07 21:11:08 +02:00
|
|
|
n = (int) (p - symtab);
|
2004-04-04 10:21:10 +02:00
|
|
|
return printname[n];
|
|
|
|
}
|
|
|
|
|
2005-08-07 19:45:09 +02:00
|
|
|
// get symbol's index from ptr
|
|
|
|
|
|
|
|
int
|
|
|
|
symbol_index(U *p)
|
2004-06-19 03:21:00 +02:00
|
|
|
{
|
2005-08-07 19:45:09 +02:00
|
|
|
return (int) (p - symtab);
|
2004-06-19 03:21:00 +02:00
|
|
|
}
|
|
|
|
|
2005-08-07 19:45:09 +02:00
|
|
|
// get symbol's ptr from index
|
|
|
|
|
2004-04-04 10:21:10 +02:00
|
|
|
U *
|
|
|
|
symbol(int k)
|
|
|
|
{
|
2005-08-06 22:57:37 +02:00
|
|
|
return symtab + k;
|
2004-04-04 10:21:10 +02:00
|
|
|
}
|
|
|
|
|
2005-08-07 19:45:09 +02:00
|
|
|
// push indexed symbol
|
|
|
|
|
2004-04-04 10:21:10 +02:00
|
|
|
void
|
|
|
|
push_symbol(int k)
|
|
|
|
{
|
2005-08-06 22:57:37 +02:00
|
|
|
push(symtab + k);
|
2004-04-04 10:21:10 +02:00
|
|
|
}
|
|
|
|
|
2005-08-07 19:45:09 +02:00
|
|
|
void
|
|
|
|
clear_symbols(void)
|
2004-04-04 10:21:10 +02:00
|
|
|
{
|
2005-08-07 19:45:09 +02:00
|
|
|
int i;
|
|
|
|
U *p;
|
|
|
|
for (i = 0; i < nsym; i++) {
|
|
|
|
p = symtab + i;
|
|
|
|
p->u.sym.binding = p;
|
|
|
|
p->u.sym.binding2 = nil;
|
|
|
|
}
|
2004-04-04 10:21:10 +02:00
|
|
|
}
|