/**
 * Find the topmost symbol table that contains an entry for a string
 *
 * @param stack a stack of symbol tables
 * @return the symbol table containing the string or null
 */
SymTable findSymtab(SymtabStack stack, char* key) {

	DNode node = dlinkHead(stack);
	while (node != NULL) {
	  if (SymQueryIndex((SymTable)dlinkNodeAtom(node),key) != SYM_INVALID_INDEX)
	    break;
	  node = dlinkNext(node);
	}

	if (node != NULL)
		return (SymTable)dlinkNodeAtom(node);
	else
		return NULL;
}
示例#2
0
文件: codegen.c 项目: bjnix/pedogogy
/**
 * Add an identifier to the symbol table and store its offset in the activation record.
 * This function is called by dlinkApply1.
 *
 * @param node a node on a linked list containing the symbol table index of a variable
 * 		  delcared in a program
 * @param data a structure containing the symbol table index of the type of the variable,
 * 		  the symbol table, and the current offset in the activation record.
 */
void addIdToSymtab(DNode node, AddIdStructPtr data) {
        int symIndex = (int)dlinkNodeAtom(node);
        int typeIndex = (int)SymGetFieldByIndex(data->idSymtab,symIndex,SYMTAB_TYPE_INDEX_FIELD);

        if (typeIndex == -1) {
                SymPutFieldByIndex(data->idSymtab,symIndex,SYMTAB_TYPE_INDEX_FIELD,(Generic)data->typeIndex);
                typeIndex = data->typeIndex;
        }

        int size = (int)SymGetFieldByIndex(data->typeSymtab,typeIndex,SYMTAB_SIZE_FIELD);

        SymPutFieldByIndex(data->idSymtab,symIndex,SYMTAB_OFFSET_FIELD,(Generic)(data->offset));
        data->offset += size;      
}
示例#3
0
文件: codegen.c 项目: bjnix/pedogogy
/**
 * Print an assembly instruction to stdout. This function is only called by dlinkApply.
 *
 * @param inst a DNode containing an assembly instruction.
 */
static void printInstruction(DNode inst) {
	fprintf(stdout,"%s\n",(char*)dlinkNodeAtom(inst));
}
示例#4
0
文件: codegen.c 项目: bjnix/pedogogy
/**
 * Print a data declaration to stdout. This function is called by dlinkApply only.
 *
 * @param decl a DNode containing a data declaration
 */
static void printDataDeclaration(DNode decl) {
	fprintf(stdout,"%s\n",(char*)dlinkNodeAtom(decl));
}
/**
 * Pop a symbol table off of the stack when leave a scope region
 *
 * @param stack a stack of symbol tables
 * @return the symbol table popped off of the stack
 */
SymTable endScope(SymtabStack stack) {
	SymTable symtab = (SymTable)dlinkNodeAtom(dlinkPop(stack));
	int *size = (int*)dlinkListAtom(stack);
	(*size)--;
	return symtab;
}
/**
 * Get the symbol table on the bottom of the stack
 *
 * @param stack a stack of symbol tables
 * @return the symbol table on the bottom of the stack
 */
SymTable lastSymtab(SymtabStack stack) {
	return (SymTable)dlinkNodeAtom(dlinkTail(stack));
}
/**
 * Get the most recently defined symbol table
 *
 * @param stack a stack of symbol tables
 * @return the symbol table on top of the stack
 */
SymTable currentSymtab(SymtabStack stack) {
	return (SymTable)dlinkNodeAtom(dlinkHead(stack));
}