summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2008-10-26 12:51:57 +0000
committerPaul Buetow <paul@buetow.org>2008-10-26 12:51:57 +0000
commita1c3f47491b98cd9026f8e853cc9e72630805c12 (patch)
treeec2ea29640b43f2c1f820fd3fee317beec27d130 /src/core
parent380eacd0f4037ec00f37ad5b5e4baa18301cf3dd (diff)
added the "scope" function
Diffstat (limited to 'src/core')
-rw-r--r--src/core/function.c7
-rw-r--r--src/core/scope.c17
-rw-r--r--src/core/scope.h1
-rw-r--r--src/core/symbol.c46
-rw-r--r--src/core/symbol.h3
5 files changed, 70 insertions, 4 deletions
diff --git a/src/core/function.c b/src/core/function.c
index a528aa1..8541960 100644
--- a/src/core/function.c
+++ b/src/core/function.c
@@ -524,6 +524,7 @@ function_process(Interpret *p_interpret, Token *p_token_op,
_Bool
function_is_buildin(Token *p_token_ident) {
+ /* TODO: optimize this function */
if (strcmp("assert", token_get_val(p_token_ident)) == 0)
return (true);
@@ -563,6 +564,9 @@ function_is_buildin(Token *p_token_ident) {
if (strcmp("put", token_get_val(p_token_ident)) == 0)
return (true);
+ if (strcmp("scope", token_get_val(p_token_ident)) == 0)
+ return (true);
+
if (strcmp("say", token_get_val(p_token_ident)) == 0)
return (true);
@@ -751,6 +755,9 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident,
}
stackiterator_delete(p_iter);
+ } else if (strcmp("scope", token_get_val(p_token_ident)) == 0) {
+ scope_print(p_interpret->p_scope);
+
} else if (strcmp("say", token_get_val(p_token_ident)) == 0) {
StackIterator *p_iter = stackiterator_new(p_stack_args);
while (stackiterator_has_next(p_iter)) {
diff --git a/src/core/scope.c b/src/core/scope.c
index 2a19390..8c3d4bf 100644
--- a/src/core/scope.c
+++ b/src/core/scope.c
@@ -150,8 +150,19 @@ scope_newset(Scope *p_scope, char *c_key, Symbol *p_symbol) {
}
void
-scopes_print(Scope *p_scope) {
+_scope_print_cb(void *p_void, int i_level) {
+ Hash *p_hash_syms = p_void;
+ if (i_level > 0)
+ printf("%d level(s) up:\n", i_level);
+ hash_iterate_key(p_hash_syms, symbol_print_cb);
+}
+
+void
+scope_print(Scope *p_scope) {
printf("Scopes:\n");
- printf("Scope stack size: %d\n", p_scope->p_stack_scopes);
-
+ printf("Scope stack size: %d\n", stack_size(p_scope->p_stack_scopes));
+ printf("Global symbols:\n");
+ hash_iterate_key(p_scope->p_hash_global, symbol_print_cb);
+ printf("Local symbols:\n");
+ stack_iterate_level(p_scope->p_stack_scopes, _scope_print_cb);
}
diff --git a/src/core/scope.h b/src/core/scope.h
index be29e67..4954344 100644
--- a/src/core/scope.h
+++ b/src/core/scope.h
@@ -40,7 +40,6 @@
#include "../defines.h"
#include "symbol.h"
-
typedef struct {
Hash *p_hash_global;
Stack *p_stack_scopes;
diff --git a/src/core/symbol.c b/src/core/symbol.c
index 8c39f5c..ef5957b 100644
--- a/src/core/symbol.c
+++ b/src/core/symbol.c
@@ -35,6 +35,7 @@
#include "symbol.h"
#include "../data/list.h"
+#include "token.h"
Symbol*
symbol_new(SymbolType sym, void *p_val) {
@@ -67,3 +68,48 @@ void
symbol_cleanup_hash_syms_cb(void *p_void) {
symbol_delete(p_void);
}
+
+void
+symbol_print(Symbol *p_symbol, char *c_key) {
+ printf("%s: %s", sym_get_name(p_symbol->sym), c_key);
+
+ switch (p_symbol->sym) {
+ case SYM_BUILDIN:
+ case SYM_CONSTANT:
+ break;
+ case SYM_PROCEDURE:
+ case SYM_FUNCTION:
+ //list_iterate(symbol_get_val(p_symbol), token_print_cb);
+ break;
+ case SYM_VARIABLE:
+ printf(" ");
+ token_print(symbol_get_val(p_symbol));
+ break;
+ }
+
+ printf("\n");
+}
+
+void
+symbol_print_cb(void *p_void, char *c_key) {
+ symbol_print(p_void, c_key);
+}
+
+char*
+sym_get_name(SymbolType sym) {
+ switch (sym) {
+ case SYM_CONSTANT:
+ return ("SYM_CONSTANT");
+ case SYM_VARIABLE:
+ return ("SYM_VARIABLE");
+ case SYM_BUILDIN:
+ return ("SYM_BUILDIN");
+ case SYM_PROCEDURE:
+ return ("SYM_PROCEDURE");
+ case SYM_FUNCTION:
+ return ("SYM_FUNCTION");
+ }
+
+ // Never reach this point
+ return ("NONE");
+}
diff --git a/src/core/symbol.h b/src/core/symbol.h
index 2abc67e..e0f8337 100644
--- a/src/core/symbol.h
+++ b/src/core/symbol.h
@@ -62,5 +62,8 @@ typedef struct {
Symbol* symbol_new(SymbolType sym, void *p_val);
void symbol_delete(Symbol *p_symbol);
void symbol_cleanup_hash_syms_cb(void *p_void);
+void symbol_print(Symbol *p_symbol, char *c_key);
+void symbol_print_cb(void *p_void, char *c_key);
+char* sym_get_name(SymbolType sym);
#endif