int lookup(char *sym) { // int symbol = symhash(sym)%NHASH; // return symbol; //this probabliy ain't good struct symbol *sp = &symtab[symhash(sym) % NHASH]; int h = symhash(sym) % NHASH; int scount = NHASH; /* how many have we looked at */ while (--scount >= 0) { if (sp->name && !strcmp(sp->name, sym)) { /*return sp;*/ if (sp->value == -1) sp->value = 0; return h; } if (!sp->name) { /* new entry */ sp->name = strdup(sym); sp->value = -1; // sp->func = NULL; // sp->syms = NULL; return h; //return sp; } if (++sp >= symtab + NHASH) sp = symtab; /* try the next entry */ h = (h + 1) % NHASH; } yyerror("symbol table overflow\n"); abort(); /* tried them all, table is full */ }
cell_t *lookup_env(secd_t *secd, const char *symbol, cell_t **symc) { cell_t *env = secd->env; assert(cell_type(env) == CELL_CONS, "lookup_env: environment is not a list"); cell_t *res = lookup_fake_variables(secd, symbol); if (not_nil(res)) return res; hash_t symh = secd_strhash(symbol); while (not_nil(env)) { // walk through frames cell_t *frame = get_car(env); if (is_nil(frame)) { /* skip omega-frame */ env = list_next(secd, env); continue; } cell_t *symlist = get_car(frame); cell_t *vallist = get_cdr(frame); while (not_nil(symlist)) { // walk through symbols if (is_symbol(symlist)) { if (symh == symhash(symlist) && str_eq(symbol, symname(symlist))) { if (symc != NULL) *symc = symlist; return vallist; } break; } cell_t *curc = get_car(symlist); assert(is_symbol(curc), "lookup_env: variable at [%ld] is not a symbol\n", cell_index(secd, curc)); if (symh == symhash(curc) && str_eq(symbol, symname(curc))) { if (symc != NULL) *symc = curc; return get_car(vallist); } symlist = list_next(secd, symlist); vallist = list_next(secd, vallist); } env = list_next(secd, env); } //errorf(";; error in lookup_env(): %s not found\n", symbol); return new_error(secd, SECD_NIL, "Lookup failed for: '%s'", symbol); }
struct symbol *lookup(char *sym) { struct symbol *sp = &symtab[symhash(sym)%NHASH]; int scount = NHASH; /* how many have we looked at */ while(--scount >= 0) { if(sp->name && !strcmp(sp->name, sym)) return sp; if(!sp->name) { sp->name = strdup(sym); sp->value = 0; sp->func = NULL; sp->syms = NULL; return sp; } if(++sp >= symtab+NHASH) sp = symtab; } yyerror("symbol table overflow\n"); abort(); }
MRB_API mrb_value mrb_check_intern(mrb_state *mrb, const char *name, size_t len) { mrb_sym sym; sym_validate_len(mrb, len); sym = find_symbol(mrb, name, len, symhash(name, len)); if (sym > 0) return mrb_symbol_value(sym); return mrb_nil_value(); }
void dbg_print_cell(secd_t *secd, const cell_t *c) { if (is_nil(c)) { secd_printf(secd, "NIL\n"); return; } char buf[128]; if (c->nref > DONT_FREE_THIS - 100000) strncpy(buf, "-", 64); else snprintf(buf, 128, "%ld", (long)c->nref); printf("[%ld]^%s: ", cell_index(secd, c), buf); switch (cell_type(c)) { case CELL_CONS: printf("CONS([%ld], [%ld])\n", cell_index(secd, get_car(c)), cell_index(secd, get_cdr(c))); break; case CELL_FRAME: printf("FRAME(syms: [%ld], vals: [%ld])\n", cell_index(secd, get_car(c)), cell_index(secd, get_cdr(c))); break; case CELL_INT: printf("%d\n", c->as.num); break; case CELL_CHAR: if (isprint(c->as.num)) printf("#\\%c\n", (char)c->as.num); else printf("#x%x\n", c->as.num); break; case CELL_OP: sexp_print_opcode(secd, secd->output_port, c->as.op); printf("\n"); break; case CELL_FUNC: printf("*%p()\n", c->as.ptr); break; case CELL_KONT: printf("KONT[%ld, %ld, %ld]\n", cell_index(secd, c->as.kont.stack), cell_index(secd, c->as.kont.env), cell_index(secd, c->as.kont.ctrl)); break; case CELL_ARRAY: printf("ARR[%ld]\n", cell_index(secd, arr_val(c, 0))); break; case CELL_STR: printf("STR[%ld\n", cell_index(secd, (cell_t*)strval(c))); break; case CELL_SYM: printf("SYM[%08x]='%s'\n", symhash(c), symname(c)); break; case CELL_BYTES: printf("BVECT[%ld]\n", cell_index(secd, (cell_t*)strval(c))); break; case CELL_REF: printf("REF[%ld]\n", cell_index(secd, c->as.ref)); break; case CELL_ERROR: printf("ERR[%s]\n", errmsg(c)); break; case CELL_ARRMETA: printf("META[%ld, %ld]\n", cell_index(secd, mcons_prev((cell_t*)c)), cell_index(secd, mcons_next((cell_t*)c))); break; case CELL_UNDEF: printf("#?\n"); break; case CELL_FREE: printf("FREE\n"); break; default: printf("unknown type: %d\n", cell_type(c)); } }
static mrb_sym sym_intern(mrb_state *mrb, const char *name, size_t len, mrb_bool lit) { mrb_sym sym; symbol_name *sname; uint8_t hash; sym_validate_len(mrb, len); hash = symhash(name, len); sym = find_symbol(mrb, name, len, hash); if (sym > 0) return sym; /* registering a new symbol */ sym = ++mrb->symidx; if (mrb->symcapa < sym) { if (mrb->symcapa == 0) mrb->symcapa = 100; else mrb->symcapa = (size_t)(mrb->symcapa * 6 / 5); mrb->symtbl = (symbol_name*)mrb_realloc(mrb, mrb->symtbl, sizeof(symbol_name)*(mrb->symcapa+1)); } sname = &mrb->symtbl[sym]; sname->len = (uint16_t)len; if (lit || mrb_ro_data_p(name)) { sname->name = name; sname->lit = TRUE; } else { char *p = (char *)mrb_malloc(mrb, len+1); memcpy(p, name, len); p[len] = 0; sname->name = (const char*)p; sname->lit = FALSE; } if (mrb->symhash[hash]) { mrb_sym i = sym - mrb->symhash[hash]; if (i > 0xff) sname->prev = 0xff; else sname->prev = i; } else { sname->prev = 0; } mrb->symhash[hash] = sym; return sym<<1; }
struct symbol* lookup(char* sym) { struct symbol *sp = &symtab[symhash(sym)%NHASH]; int scount = NHASH; while(--scount >= 0) { if(sp->name && !strcmp(sp->name, sym)) { return sp; } if(!sp->name) { sp->name = strdup(sym); sp->value = 0; sp->func = NULL; sp->syms = NULL; return sp; } if(++sp >= symtab+NHASH) sp = symtab; // try the next entry } yyerror("symbol table overflow\n"); return NULL; }
struct symbol* lookup(char* sym) { struct symbol *sp = &symtab[symhash(sym)%NHASH]; int scount = NHASH; /* how many have we looked at */ while(--scount >= 0) { if(sp->name && !strcmp(sp->name, sym)) { return sp; } if(!sp->name) { /* new entry */ sp->name = strdup(sym); sp->value = 0; sp->func = NULL; sp->syms = NULL; return sp; } if(++sp >= symtab+NHASH) sp = symtab; /* try the next entry */ } yyerror("symbol table overflow\n"); abort(); /* tried them all, table is full */ }
static cell_t * check_io_args(secd_t *secd, cell_t *sym, cell_t *val, cell_t **args_io) { /* check for overriden *stdin* or *stdout* */ hash_t symh = symhash(sym); if ((symh == stdinhash) && str_eq(symname(sym), SECD_FAKEVAR_STDIN)) { assert(cell_type(val) == CELL_PORT, "*stdin* must bind a port"); if (is_nil(*args_io)) *args_io = new_cons(secd, val, SECD_NIL); else (*args_io)->as.cons.car = share_cell(secd, val); } else if ((symh == stdouthash) && str_eq(symname(sym), SECD_FAKEVAR_STDOUT)) { assert(cell_type(val) == CELL_PORT, "*stdout* must bind a port"); if (is_nil(*args_io)) *args_io = new_cons(secd, SECD_NIL, val); else (*args_io)->as.cons.cdr = share_cell(secd, val); } return SECD_NIL; }
void flexDeleteFunc(char* sym) { struct symbol *sp = &symtab[symhash(sym)%NHASH]; sp->func = 0; }
/*----------------------------------------------- | remref - removes a reference from the symtab +---------------------------------------------*/ void remref(struct declarator *sym, struct symtabl *curr_symtab) { int i; int hash; struct declarator *sp; /* used to keep current place in symbol table */ struct declarator *spname= sym; /* used to investigate name of symbol in table */ struct declarator *symorig= sym; /* used to keep location of original param */ /* fastforward to the id of the declarator parameter +----------------------------------------------------*/ while(sym->next != NULL) { sym= sym->next; } if(sym->nodetype == ARRAY_DECLARATOR || sym->nodetype == FUNCTION_DECLARATOR) { sym= sym->adeclarator; } /* hash the symbol name */ hash= symhash(sym->id) % NHASH; /* set symbol pointer 'sp' to the address of the cell that | this symbol is supposed to be in. +---------------------------------------------------------------*/ sp= (struct declarator *)&curr_symtab->symtab[hash]; int scount= NHASH; while(--scount >= 0) { /* fastforward to the the name of the declarator that we're currently | pointing to in the symbol table. +---------------------------------------------------------------------*/ spname= curr_symtab->symtab[hash]; /* fastforward to declarator name */ while(spname && spname->next != NULL) { spname= spname->next; } if(spname && (spname->nodetype == ARRAY_DECLARATOR || spname->nodetype == FUNCTION_DECLARATOR ) ) { spname= spname->adeclarator; } /* if the symbol is in this cell already... +------------------------------------------*/ if(curr_symtab->symtab[hash] != 0) { /* and if cell contains the same id as the declarator param, then it's a dup. */ if(spname->id && !strcmp(spname->id,sym->id)) { curr_symtab->symtab[hash]=0; return; } } /* if this cell is empty, then there's nothing to do +--------------------------------------------------*/ if(curr_symtab->symtab[hash] == 0) { ; } /* if the cell is not empty and DOES NOT store the current symbol, | then a collision has occured and we need to move ahead to the | next cell. Be sure to wrap around sure to the front of the symtab | if you happen to reach the back. +------------------------------------------------------------------*/ if( curr_symtab->symtab[hash] != 0 && strcmp(spname->id,sym->id) ) { ++hash; if(hash > NHASH-1) { hash=0; sp= (struct declarator *) &curr_symtab->symtab[hash]; } } } printf("Error trying to remove symbol '%s'\n", spname->id); exit(-1); }
/*--------------------------------------------------- | resolve - Just like lookup (returns the address of | the declarator in the symtab cell) but it | also searches all parent symtabs. +-------------------------------------------------*/ struct declarator *resolve(struct declarator *sym, struct symtabl *curr_symtab) { int i; int hash; struct declarator *sp; /* used to keep current place in symbol table */ struct declarator *spname= sym; /* used to investigate name of symbol in table */ struct declarator *symorig= sym; /* used to keep location of original param */ /* fastforward to the id of the declarator parameter +----------------------------------------------------*/ if(sym->nodetype == ARRAY_DECLARATOR || sym->nodetype == FUNCTION_DECLARATOR) { sym= sym->adeclarator; } if(sym->nodetype == FUNCTION_DECLARATOR) { sym= sym->adeclarator; } if(sym->nodetype == POINTER_DECLARATOR) { while(sym->next) { sym= sym->next; } if(!sym->id) { sym= sym->adeclarator; } } /* hash the symbol name */ hash= symhash(sym->id) % NHASH; /* set symbol pointer 'sp' to the address of the cell that | this symbol hashed to. +---------------------------------------------------------------*/ sp= (struct declarator *)&curr_symtab->symtab[hash]; search_table: ; int scount= NHASH; while(--scount >= 0) { /* fastforward to the the name of the symbol that we're currently | pointing to in the symbol table. +---------------------------------------------------------------------*/ spname= curr_symtab->symtab[hash]; /* fastforward to declarator name */ while(spname && spname->next != NULL) { spname= spname->next; } if(spname && (spname->nodetype == ARRAY_DECLARATOR || spname->nodetype == FUNCTION_DECLARATOR ) ) { spname= spname->adeclarator; } /* if a symbol is in this cell already, check the name of | the symbol. +-----------------------------------------------------------*/ if(curr_symtab->symtab[hash] != 0) /* if cell is not empty... */ { /* if the cell contains the same symbol as the declarator param, * then we've found what we were looking for; return the address. */ if(spname->id && !strcmp(spname->id,sym->id)) { return (struct declarator *) curr_symtab->symtab[hash]; } } /* if this cell is empty, switch to the parent symtab | if possible and continue the search. +----------------------------------------------------*/ if(curr_symtab->symtab[hash] == 0) { if(curr_symtab->parent != 0) { curr_symtab= curr_symtab->parent; goto search_table; } } /* if the cell is not empty and DOES NOT store the current symbol, | then a collision has occured and we need to move ahead to the | next cell. Be sure to wrap around sure to the front of the symtab | if you happen to reach the back. +------------------------------------------------------------------*/ if( curr_symtab->symtab[hash] != 0 && strcmp(spname->id,sym->id) ) { ++hash; if(hash > NHASH-1) { hash=0; sp= (struct declarator *) &curr_symtab->symtab[hash]; } } } if(curr_symtab->parent != NULL) { curr_symtab= curr_symtab->parent; goto search_table; } printf("Error: Symbol '%s' not found in symbol table\n", sym->id); exit(-1); }