feat add func call and rewrite codes
This commit is contained in:
		@ -3,25 +3,25 @@
 | 
			
		||||
#include "scope.h"
 | 
			
		||||
#include "symtab.h"
 | 
			
		||||
 | 
			
		||||
typedef struct SymbolTable SymbolTable;
 | 
			
		||||
typedef symtab_t symtab_t;
 | 
			
		||||
typedef struct Scope Scope;
 | 
			
		||||
 | 
			
		||||
void init_symtab(SymbolTable* symtab) {
 | 
			
		||||
void init_symtab(symtab_t* symtab) {
 | 
			
		||||
    symtab->global_scope = scope_create(NULL);
 | 
			
		||||
    symtab->cur_scope = symtab->global_scope;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void del_symtab(SymbolTable* symtab) {
 | 
			
		||||
void del_symtab(symtab_t* symtab) {
 | 
			
		||||
    scope_destroy(symtab->global_scope);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void symtab_enter_scope(SymbolTable* symtab) {
 | 
			
		||||
void symtab_enter_scope(symtab_t* symtab) {
 | 
			
		||||
    struct Scope* scope = scope_create(symtab->cur_scope);
 | 
			
		||||
    scope->base_offset = symtab->cur_scope->base_offset + symtab->cur_scope->cur_offset;
 | 
			
		||||
    symtab->cur_scope = scope;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void symtab_leave_scope(SymbolTable* symtab) {
 | 
			
		||||
void symtab_leave_scope(symtab_t* symtab) {
 | 
			
		||||
    Scope * scope = symtab->cur_scope;
 | 
			
		||||
    if (scope == NULL) {
 | 
			
		||||
        error("cannot leave NULL scope or global scope");
 | 
			
		||||
@ -30,16 +30,20 @@ void symtab_leave_scope(SymbolTable* symtab) {
 | 
			
		||||
    scope_destroy(scope);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void symtab_add_symbol(SymbolTable* symtab, const char* name, void* ast_node) {
 | 
			
		||||
void* symtab_add_symbol(symtab_t* symtab, const char* name, void* ast_node, int can_duplicate) {
 | 
			
		||||
    struct Scope* scope = symtab->cur_scope;
 | 
			
		||||
    if (scope_lookup_current(scope, name) != NULL) {
 | 
			
		||||
        // TODO WARNING
 | 
			
		||||
        // return NULL;
 | 
			
		||||
    void* node = scope_lookup_current(scope, name);
 | 
			
		||||
    if (node != NULL) {
 | 
			
		||||
        if (!can_duplicate) {
 | 
			
		||||
            error("duplicate symbol %s", name);
 | 
			
		||||
        }
 | 
			
		||||
        return node;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    scope_insert(scope, name, ast_node);
 | 
			
		||||
    return node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* symtab_lookup_symbol(SymbolTable* symtab, const char* name) {
 | 
			
		||||
void* symtab_lookup_symbol(symtab_t* symtab, const char* name) {
 | 
			
		||||
    return scope_lookup(symtab->cur_scope, name);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -2,17 +2,17 @@
 | 
			
		||||
#ifndef __SYMTAB_H__
 | 
			
		||||
#define __SYMTAB_H__
 | 
			
		||||
 | 
			
		||||
struct SymbolTable {
 | 
			
		||||
typedef struct symtab {
 | 
			
		||||
    struct Scope* cur_scope;
 | 
			
		||||
    struct Scope* global_scope;
 | 
			
		||||
};
 | 
			
		||||
} symtab_t;
 | 
			
		||||
 | 
			
		||||
void init_symtab(struct SymbolTable* symtab);
 | 
			
		||||
void del_symtab(struct SymbolTable* symtab);
 | 
			
		||||
void init_symtab(symtab_t* symtab);
 | 
			
		||||
void del_symtab(symtab_t* symtab);
 | 
			
		||||
 | 
			
		||||
void symtab_enter_scope(struct SymbolTable* symtab);
 | 
			
		||||
void symtab_leave_scope(struct SymbolTable* symtab);
 | 
			
		||||
void symtab_add_symbol(struct SymbolTable* symtab, const char* name, void* ast_node);
 | 
			
		||||
void* symtab_lookup_symbol(struct SymbolTable* symtab, const char* name);
 | 
			
		||||
void symtab_enter_scope(symtab_t* symtab);
 | 
			
		||||
void symtab_leave_scope(symtab_t* symtab);
 | 
			
		||||
void* symtab_add_symbol(symtab_t* symtab, const char* name, void* ast_node, int can_duplicate);
 | 
			
		||||
void* symtab_lookup_symbol(symtab_t* symtab, const char* name);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user