Skip to content

Commit

Permalink
scope
Browse files Browse the repository at this point in the history
  • Loading branch information
jaz303 committed Nov 5, 2011
1 parent f4b80a8 commit 53c0ac6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -15,7 +15,8 @@ OBJS = src/lispy.o \
src/lexer.o \
src/object.o \
src/parser.o \
src/intern.o
src/intern.o \
src/scope.o

# Products

Expand Down
22 changes: 22 additions & 0 deletions include/lispy/scope.h
@@ -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
50 changes: 50 additions & 0 deletions src/scope.c
@@ -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);
}

0 comments on commit 53c0ac6

Please sign in to comment.