symtab_t * symtab_flat_copy (symtab_t *symtab, symtab_t *parent) { symtab_t *newtab; symbol_t *newsym; symbol_t *symbol; newtab = new_symtab (parent, stab_local); do { for (symbol = symtab->symbols; symbol; symbol = symbol->next) { if (Hash_Find (newtab->tab, symbol->name)) continue; newsym = copy_symbol (symbol); symtab_addsymbol (newtab, newsym); } symtab = symtab->parent; // Set the tail pointer so symbols in ancestor tables come before // those in decendent tables. newtab->symtail = &newtab->symbols; } while (symtab); // Reset the tail pointer so any symbols added to newtab come after // those copied from the input symbol table chain. for (symbol = newtab->symbols; symbol && symbol->next; symbol = symbol->next) ; newtab->symtail = symbol ? &symbol->next : &newtab->symbols; return newtab; }
/****************************************************************************** * * P U T _ S Y M B O L (value symbval, char *name, int type, node *n) * * Stores a node under a given name, with a supplied symbol type and with a * value (n) in a symbol table (all given). A symboltable ICL value is passed * and symboltable_part() is used to get the actual symbol table involved. * * If the symbol already exists, its node value is re-used (overwritten), * so that existing pointers to the symbol table entry will point to the * new value. I don't believe anything relies on this feature currently. ****************************************************************************** */ value put_symbol(value symbval, char *name, int type, node * n) { node *found; symtab *symb; symb = symboltable_part(symbval); if ((found = get_symbol(symb, name, type)) != NODENIL) { destroy(found); symb->next->value = n; /* get_symbol has reordered table */ return (n->val); } else { symtab *sym; sym = new_symtab(name, type, n); if (sym == SYMBNIL) return (exception("SYSERR memory exhausted in put_symbol")); else { sym->next = symb->next; sym->prev = symb; symb->next->prev = sym; symb->next = sym; return (n->val); } } }
symtab_t * start_enum (symbol_t *sym) { if (sym->table == current_symtab && sym->type->t.symtab) { error (0, "%s defined as wrong kind of tag", sym->name); sym = find_enum (0); } sym->type->t.symtab = new_symtab (current_symtab, stab_local); return sym->type->t.symtab; }
symbol_t * make_structure (const char *name, int su, struct_def_t *defs, type_t *type) { symtab_t *strct; symbol_t *field; symbol_t *sym = 0; if (name) sym = new_symbol (name); if (su == 'u') strct = new_symtab (0, stab_union); else strct = new_symtab (0, stab_struct); while (defs->name) { field = new_symbol_type (defs->name, defs->type); if (!symtab_addsymbol (strct, field)) internal_error (0, "duplicate symbol: %s", defs->name); defs++; } sym = build_struct (su, sym, strct, type); return sym; }
/****************************************************************************** * * PUT_VALUE (symtab *symb, char *name, int type, value val) * * Stores a given value under the given name, with the given symbol type in * the given symbol table. * * If the symbol already exists, the same node object is used, so that * pointers to the symbol table entry will point at the new value. I don't * believe anything relies on this currently. * put_value() exists because it is common to change a value in a variable. * This avoids creating and freeing a node object on every assignment. * If a node value is changed, the interpreter function is changed so that, * if the node is interpreted, the correct thing happens. * * Returns the value assigned (or exception if failed to assign). * ****************************************************************************** */ static value put_value(symtab * symb, char *name, int type, value val) { node *found; char buf[512]; if ((found = get_symbol(symb, name, type)) != NODENIL) { found->val = val; switch (thetypeof(val)) { case TYPE_INTEGER: found->interpret = integer_interpret; return (val); case TYPE_REAL: found->interpret = real_interpret; return (val); case TYPE_LOGICAL: found->interpret = logical_interpret; return (val); case TYPE_STRING: found->interpret = string_interpret; return (val); case TYPE_FUNCTION: found->interpret = builtin_interpret; return (val); case TYPE_FILE: found->interpret = 0; return (val); default: sprintf(buf, "put_value: assigning an unknown value type %d\n", thetypeof(found->val)); systemfail(buf); return (val); /* for lint */ } } else { node *valuenode = node_value(val); symtab *sym = new_symtab(name, type, valuenode); if (valuenode == NODENIL || sym == SYMBNIL) return (exception("SYSERR memory exhausted in put_value()")); else { sym->next = symb->next; sym->prev = symb; symb->next->prev = sym; symb->next = sym; return (val); } } }
static void init_compiler(M1_compiler *comp) { memset(comp, 0, sizeof(M1_compiler)); comp->breakstack = new_intstack(); comp->regstack = new_regstack(); comp->continuestack = new_intstack(); comp->expect_usertype = 0; /* when not parsing a function's body, then identifiers are types */ comp->is_parsing_usertype = 1; /* register built-in types in type declaration module. */ type_enter_type(comp, "void", DECL_VOID, 0); type_enter_type(comp, "int", DECL_INT, 4); type_enter_type(comp, "num", DECL_NUM, 8); type_enter_type(comp, "bool", DECL_BOOL, 4); /* bools are stored in ints. */ type_enter_type(comp, "string", DECL_STRING, 4); /* strings are pointers, so size is 4. */ type_enter_type(comp, "char", DECL_CHAR, 4); /* XXX can this be 1? what about padding in structs? */ /* global symbol table for functions, as they need a return type m1_decl pointer. */ comp->globalsymtab = new_symtab(); }
//initialize estimator with c samplers and k counters (used by Misra-Gries alg) Estimator_type * Estimator_Init(int c, int k) { Estimator_type* est = (Estimator_type*) safe_malloc(sizeof(Estimator_type)); est->c=c; est->k=k; est->count = 0; est->two_distinct_tokens=0; est->prng=prng_Init(drand48(), 2); // initialize the random number generator est->freq=Freq_Init((float)1.0/k); est->samplers = (Sample_type**) safe_malloc(sizeof(Sample_type*) * c); for(int i = 0; i < c; i++) { est->samplers[i]=Sample_Init(); } est->hashtable=new_symtab(2*c); est->prim_heap = new_heap(prim_cmp, c); est->bheap = new_bheap(b_cmp, c); return est; }
/****************************************************************************** * * V A L U E _ E M P T Y S Y M T A B (void) * * Construct an empty symbol table and return as an ICL value. * ****************************************************************************** */ value value_emptysymtab(void) { return (value_symtab(new_symtab(CHARNIL, 0, NODENULL))); }