Skip to content

Commit

Permalink
Wrote push_env_table() and pop_env_table()
Browse files Browse the repository at this point in the history
  • Loading branch information
johngunderman committed Nov 6, 2009
1 parent d4cba6c commit 231f696
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
41 changes: 40 additions & 1 deletion intmdt_code_gen.c
Expand Up @@ -35,12 +35,18 @@ intmdt_code_t *init_code() {
/*
Initialize the environment variable for the program.
*/
env_t *init_env(hash_table_t *table) {
env_t *init_env() {
env_t *temp = malloc( sizeof(env_t ));
if (temp == NULL) {
fprintf(stderr, "Error: failed to malloc env_t in init_env()\n");
exit(1);
}

hash_table_t *table = hash_table_init(cmp_string, string_hasher);
if (table == NULL) {
fprintf(stderr, "Error: failed to malloc hash_table_t in init_env()\n");
}

temp->prev = NULL;
temp->table = table;
return temp;
Expand Down Expand Up @@ -276,3 +282,36 @@ void print_id_type (void *id) {
printf(",%p}", (void *) ((id_type_t *)id)->supersize);
}
}

/*
Generates and pushes a new symbol table onto the environment.
Returns a pointer to the new top of the stack.
*/
env_t *push_env_table(env_t *env) {
env_t *top = init_env();

top->table = hash_table_init(cmp_string ,string_hasher);
top->prev = env;

return top;
}

/*
Pops the top symbol table off the environment.
Returns a pointer to the new top of the stack.
*/
env_t *pop_env_table(env_t *env) {
if (env == NULL) {
fprintf(stderr, "Attempted to pop from a null environment\n");
exit(1);
}

hash_table_delete(env->table);

env_t *temp = env->prev;

free (env);

return temp;

}
9 changes: 7 additions & 2 deletions intmdt_code_gen.h
Expand Up @@ -48,9 +48,9 @@ typedef struct {
} intmdt_code_t;


typedef struct {
typedef struct env_t {
hash_table_t *table;
hash_table_t *prev;
struct env_t *prev;
} env_t;


Expand Down Expand Up @@ -81,3 +81,8 @@ intmdt_code_t *init_code();
env_t *init_env();

void print_intmdt_code(intmdt_code_t *code);

env_t *push_env_table(env_t *env);

env_t *pop_env_table(env_t *env);

14 changes: 5 additions & 9 deletions parser.yacc
Expand Up @@ -17,8 +17,6 @@

void *install_id (char *, struct id_type_t *);

hash_table_t *sym_table;

intmdt_code_t *intermediate_code;
env_t *env;

Expand Down Expand Up @@ -123,7 +121,7 @@ loc : loc '[' bool ']' {printf("loc-> loc [ bool ]\n");
$$ = $1;
/* TODO: figure out what the value of the bool is and pass it */ }
| ID {printf("loc->ID\n");
id_type_t *id = hash_table_search(sym_table, $1);
id_type_t *id = hash_table_search(env->table, $1);
if (id == NULL) {
fprintf(stderr, "Error: symbol '%s' was not previously defined.\n", (char*)$1);
exit(1);
Expand Down Expand Up @@ -272,7 +270,7 @@ factor : '(' bool ')' {printf("factor->( bool )\n");
but does not GC the original string. Beware.
*/
void *install_id (char *token, id_type_t *type_info) {
if (!hash_table_search(sym_table, token)) {
if (!hash_table_search(env->table, token)) {
char *tempstr = malloc (sizeof(char) + strlen(token));
strcpy (tempstr, token);
return hash_table_insert(env->table, (void *)tempstr, type_info);
Expand All @@ -297,24 +295,22 @@ void print_str (void *str) {
*/
void yyerror (char const * s) {
fprintf (stderr, "%s\n", s);
hash_pretty_print (sym_table, print_str, print_id_type);
hash_pretty_print (env->table, print_str, print_id_type);
}


int main () {
yyin = fopen("test.code", "r");

sym_table = hash_table_init(cmp_string,string_hasher);

intermediate_code = init_code();
env = init_env(sym_table);
env = init_env();



yyparse();

printf("\n\n");
hash_pretty_print (sym_table, print_str, print_id_type);
hash_pretty_print (env->table, print_str, print_id_type);

intmdt_code_print(intermediate_code);

Expand Down

0 comments on commit 231f696

Please sign in to comment.