Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef SCOPE_H | ||
#define SCOPE_H | ||
|
||
#include "lispy/global.h" | ||
|
||
#include "jazlib/gen_hash_reset.h" | ||
#include "jazlib/gen_hash.h" | ||
GEN_HASH_DECLARE_STORAGE(__scope_table, const char *, VALUE); | ||
|
||
typedef struct scope scope_t; | ||
struct scope { | ||
scope_t *parent; | ||
__scope_table_t table; | ||
}; | ||
|
||
void scope_init(scope_t *scope, scope_t *parent); | ||
void scope_dealloc(scope_t *scope); | ||
VALUE scope_find(scope_t *scope, const char *key); | ||
VALUE scope_get(scope_t *scope, const char *key); | ||
void scope_set(scope_t *scope, const char *key, VALUE value); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "lispy/scope.h" | ||
|
||
#include <stdlib.h> | ||
#include "jazlib/common.h" | ||
|
||
#define FREE_STRING(hsh, k) free((void*)k) | ||
|
||
#include "jazlib/gen_hash_reset.h" | ||
#define GEN_HASH_HASH_FUNC hash_djb2 | ||
#define GEN_HASH_KEY_CMP strcmp | ||
#define GEN_HASH_KEY_COPY gen_strcpy | ||
#define GEN_HASH_KEY_FREE FREE_STRING | ||
#include "jazlib/gen_hash.h" | ||
GEN_HASH_DECLARE_STATIC_INTERFACE(__scope_table, const char *, VALUE); | ||
GEN_HASH_INIT(__scope_table, const char *, VALUE); | ||
|
||
void scope_init(scope_t *scope, scope_t *parent) { | ||
__scope_table_init(&scope->table); | ||
scope->parent = parent; | ||
} | ||
|
||
void scope_dealloc(scope_t *scope) { | ||
__scope_table_dealloc(&scope->table); | ||
} | ||
|
||
VALUE scope_find(scope_t *scope, const char *key) { | ||
while (scope) { | ||
VALUE out; | ||
if (__scope_table_read(&scope->table, key, &out)) { | ||
return out; | ||
} else { | ||
scope = scope->parent; | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
VALUE scope_get(scope_t *scope, const char *key) { | ||
VALUE out; | ||
if (__scope_table_read(&scope->table, key, &out)) { | ||
return out; | ||
} else { | ||
return NULL; | ||
} | ||
} | ||
|
||
void scope_set(scope_t *scope, const char *key, VALUE value) { | ||
__scope_table_put(&scope->table, key, value); | ||
} | ||
|