summaryrefslogtreecommitdiff
path: root/ecomp-c/ec.c
diff options
context:
space:
mode:
Diffstat (limited to 'ecomp-c/ec.c')
-rw-r--r--ecomp-c/ec.c46
1 files changed, 28 insertions, 18 deletions
diff --git a/ecomp-c/ec.c b/ecomp-c/ec.c
index 2b88b0e..dd95fe7 100644
--- a/ecomp-c/ec.c
+++ b/ecomp-c/ec.c
@@ -672,6 +672,7 @@ typedef struct Symbol {
int dim;
/* procedure type */
char *label;
+ struct Scope *scope;
} Symbol;
static Symbol *integer_type;
@@ -718,23 +719,15 @@ static void free_symbol( Symbol *sym )
static void free_scope( Scope *scope )
{
Symbol *sym, *next;
- Scope *next_scope;
- while( scope != NULL ) {
- sym = scope->symbol;
- while( sym != NULL ) {
- next = sym->next;
- free_symbol( sym );
- sym = next;
- }
- next_scope = scope->parent;
- if( scope->parent != NULL ) {
- free_scope( scope->parent );
- }
- free( scope->name );
- free( scope );
- scope = next_scope;
+ sym = scope->symbol;
+ while( sym != NULL ) {
+ next = sym->next;
+ free_symbol( sym );
+ sym = next;
}
+ free( scope->name );
+ free( scope );
}
char *get_local_label( Scope *scope )
@@ -780,7 +773,13 @@ static Symbol *insert_symbol( Scope *scope, char *name, SymbolClass class )
{
Symbol *sym;
- sym = get_symbol( scope, name );
+ sym = scope->symbol;
+ while( sym != NULL ) {
+ if( strcmp( sym->name, name ) == 0 ) {
+ return sym;
+ }
+ sym = sym->next;
+ }
if( sym != NULL ) {
Abort( "'%s' is already defined.", name );
}
@@ -1952,12 +1951,23 @@ static void parseProcedureBlock( Scope *scope )
Expect( S_semicolon );
if( sym == S_const || sym == S_var || sym == S_begin ) {
- parseProcedureDeclarationBlock( scope );
Emit( "; PROC %s\n", symbol->name );
+
+ symbol->scope = create_scope( scope, symbol->name );
+
+ parseProcedureDeclarationBlock( symbol->scope );
+
+ /* TODO: allocate space for local variables on stack, set
+ * stack pointer */
+ /* TODO: assign symbols to relative addresses (base pointer) */
Emit( "%s:\n", symbol->label );
- parseStatementBlock( scope );
+ parseStatementBlock( symbol->scope );
+
+ free_scope( symbol->scope );
+
+ /* TODO: discard locals here, reset base pointer */
Emit( "ret\n" );
}