From ae9496e29f58d81c01a73319ca4450c35246933d Mon Sep 17 00:00:00 2001 From: Nathan R Date: Mon, 2 Aug 2021 15:29:19 +0200 Subject: [PATCH] updated scopes to be easier to manipulate --- scope.c | 111 +++++++++++++++++--------------------------------------- 1 file changed, 33 insertions(+), 78 deletions(-) diff --git a/scope.c b/scope.c index 936d529..6420cda 100644 --- a/scope.c +++ b/scope.c @@ -1,16 +1,18 @@ #include #include #include +#include -// Scoope definition +// Scope definition /** A scope is a set of identifiers */ -typedef struct { +typedef struct scope { size_t scopeSize; - char** identifiers; + char **identifiers; + struct scope *parent; } scope_t ; -scope_t* actualScope; +scope_t *actualScope = 0; /** Add a new specifier to a scope */ int addId(char *name) { @@ -40,97 +42,50 @@ int addId(char *name) { return 1; } -/** A new scopeList is pushed on an other scope : we have a stack of scopeList */ -typedef struct { - size_t stackSize; - scope_t** scopeList; -} stackS_t; - -stackS_t *scopeStack; -int zebi; - -/** Functions */ -void initScope() { - scopeStack = malloc(sizeof(scopeStack)); - scopeStack->stackSize = 0; - actualScope = malloc(sizeof(actualScope)); - actualScope->scopeSize = 0; - scopeStack->scopeList = malloc(sizeof(scopeStack->scopeList)); - scopeStack->scopeList[0] = actualScope; -} - -int pushScope() { +int pushScope(void) { /* A new scope is initialized */ scope_t* newScope = malloc(sizeof(scope_t)); newScope->scopeSize = 0; - newScope->identifiers = malloc(sizeof(newScope->identifiers)); - - /* A memory area is given in the stack */ - scopeStack->scopeList = realloc(scopeStack->scopeList, sizeof(**scopeStack->scopeList)*(scopeStack->stackSize + 1)); - - /* And finally, actualScope is the stack that was just pushed */ - scopeStack->stackSize++; - scopeStack->scopeList[scopeStack->stackSize] = newScope; + newScope->identifiers = 0; + newScope->parent = actualScope; actualScope = newScope; /* Everything was done well*/ return 1; } -int popScope() { - - /* Verifying that we don't pop the last scope */ - if (scopeStack->stackSize > 0) { - - /* Removing the last scope */ - scope_t* lastScope = malloc(sizeof(actualScope)); - lastScope = scopeStack->scopeList[scopeStack->stackSize]; - free(lastScope->identifiers); - free(lastScope); - scopeStack->stackSize--; - - /* Setting the actual scope */ - actualScope = malloc(sizeof(actualScope)); - actualScope = scopeStack->scopeList[scopeStack->stackSize]; - if (actualScope == NULL) printf("LE C C NULL\n"); - return 1; - - } else { - - /* Error */ - fprintf(stderr, "\nCan't pop the last scope\n"); - exit(0); - return -1; +int popScope(void) { - } + assert(actualScope); + scope_t *parent = actualScope->parent; + free(actualScope->identifiers); + free(actualScope); + actualScope = parent; + return 1; } + int isTypedefed(char *s) { - /** Latest scopes prevail */ - for (int i = scopeStack->stackSize ; i != 0 ; i--) { - scope_t* scope = malloc(sizeof(scopeStack->scopeList[i])); - scope = scopeStack->scopeList[i]; - - if (scope->scopeSize) { - ssize_t l = 0; - ssize_t r = scope->scopeSize - 1; - while ( l <= r ) { - ssize_t c = (l+r)/2; - int result = strcmp(s, scope->identifiers[c]); - if (result > 0) { - l = c + 1; - } else if (result < 0) { - r = c - 1; - } else { - /* Symbol found */ - return 1; - } + for (scope_t *scope = actualScope ; scope ; scope = scope->parent) { + + ssize_t l = 0; + ssize_t r = scope->scopeSize - 1; + while ( l <= r ) { + ssize_t c = (l+r)/2; + int result = strcmp(s, scope->identifiers[c]); + if (result > 0) { + l = c + 1; + } else if (result < 0) { + r = c - 1; + } else { + /* Symbol found */ + return 1; } - } - } + } + } /* Symbol not found */ return 0;