diff options
| author | Paul Buetow <paul@buetow.org> | 2008-10-18 22:47:31 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-10-18 22:47:31 +0000 |
| commit | cb1450b796eff3c8830616e2e9a3d83d4dfb4900 (patch) | |
| tree | 1ed2b992f9b082cf82913abeaff8c208ecab67dd /src | |
| parent | a35ace22b374005c65bda8302761d24f75280170 (diff) | |
backdowngrade
Diffstat (limited to 'src')
| -rw-r--r-- | src/build.h | 2 | ||||
| -rw-r--r-- | src/core/convert.c | 23 | ||||
| -rw-r--r-- | src/core/function.c | 97 | ||||
| -rw-r--r-- | src/core/garbage.c | 15 | ||||
| -rw-r--r-- | src/core/interpret.c | 84 | ||||
| -rw-r--r-- | src/core/scanner.c | 12 | ||||
| -rw-r--r-- | src/core/scanner.h | 1 | ||||
| -rw-r--r-- | src/core/scope.c | 33 | ||||
| -rw-r--r-- | src/core/symbol.c | 14 | ||||
| -rw-r--r-- | src/core/token.c | 133 | ||||
| -rw-r--r-- | src/core/token.h | 13 | ||||
| -rw-r--r-- | src/data/array.c | 50 | ||||
| -rw-r--r-- | src/data/array.h | 2 | ||||
| -rw-r--r-- | src/data/hash.c | 4 | ||||
| -rw-r--r-- | src/data/stack.c | 17 | ||||
| -rw-r--r-- | src/data/stack.h | 3 | ||||
| -rw-r--r-- | src/defines.h | 5 | ||||
| -rw-r--r-- | src/fype.c | 4 |
18 files changed, 130 insertions, 382 deletions
diff --git a/src/build.h b/src/build.h index cb7e720..ced3b51 100644 --- a/src/build.h +++ b/src/build.h @@ -35,7 +35,7 @@ #ifndef BUILD_H #define BUILD_H -#define BUILDNR 9654 +#define BUILDNR 9214 #define OS_FREEBSD #endif diff --git a/src/core/convert.c b/src/core/convert.c index fa1282d..73fb8a2 100644 --- a/src/core/convert.c +++ b/src/core/convert.c @@ -33,7 +33,6 @@ *:*/ #include "convert.h" -#include "../data/array.h" void convert_to_integer(Token *p_token) { @@ -48,10 +47,6 @@ convert_to_integer(Token *p_token) { token_set_tt(p_token, TT_INTEGER); token_set_ival(p_token, atoi(token_get_val(p_token))); break; - case TT_ARRAY: - token_set_tt(p_token, TT_INTEGER); - token_set_ival(p_token, array_get_size(p_token->p_array)-1); - break; default: ERROR("Ouups(%s)", tt_get_name(token_get_tt(p_token))); break; @@ -67,9 +62,6 @@ convert_to_integer_get(Token *p_token) { return ((int) token_get_dval(p_token)); case TT_STRING: return (atoi(token_get_val(p_token))); - case TT_ARRAY: - return (array_get_size(p_token->p_array)-1); - break; default: ERROR("Ouups(%s)", tt_get_name(token_get_tt(p_token))); } @@ -90,10 +82,6 @@ convert_to_double(Token *p_token) { token_set_tt(p_token, TT_DOUBLE); token_set_dval(p_token, atof(token_get_val(p_token))); break; - case TT_ARRAY: - token_set_tt(p_token, TT_DOUBLE); - token_set_dval(p_token, array_get_size(p_token->p_array)-1); - break; default: token_print_val(p_token); ERROR("Datatype conversion error '%s'", token_get_val(p_token)); @@ -128,17 +116,6 @@ convert_to_string(Token *p_token) { break; case TT_STRING: break; - case TT_ARRAY: - token_set_tt(p_token, TT_STRING); - char c_tmp[1024]; - sprintf(c_tmp, "%d", array_get_size(p_token->p_array)-1); - int i_len = strlen(c_tmp); - p_token->c_val = realloc(p_token->c_val, sizeof(char) * (i_len + 1)); - strcpy(p_token->c_val, c_tmp); - p_token->c_val[i_len] = 0; - //array_iterate(p_token->p_array, token_delete_cb); - - break; default: ERROR("Datatype conversion error"); break; diff --git a/src/core/function.c b/src/core/function.c index fdbf308..fda40b8 100644 --- a/src/core/function.c +++ b/src/core/function.c @@ -52,34 +52,6 @@ ) void -_print_val(Token *p_token) { - switch (token_get_tt(p_token)) { - case TT_INTEGER: - printf("%d", token_get_ival(p_token)); - break; - case TT_DOUBLE: - printf("%f", token_get_dval(p_token)); - break; - case TT_STRING: - printf("%s", token_get_val(p_token)); - break; - case TT_ARRAY: - { - Array *p_array = p_token->p_array; - ArrayIterator *p_iter = arrayiterator_new(p_array); - while (arrayiterator_has_next(p_iter)) { - Token *p_next = arrayiterator_next(p_iter); - _print_val(p_next); - printf(" "); - } - arrayiterator_delete(p_iter); - } - break; - NO_DEFAULT; - } -} - -void _process(Interpret *p_interpret, Token *p_token_store, Token *p_token_op, Token *p_token_op2, Token *p_token_next) { @@ -95,8 +67,10 @@ _process(Interpret *p_interpret, Token *p_token_store, Token *p_token_op, printf("PROCESS OPERATOR %s %s\n", tt_get_name(tt_op), tt_get_name(tt_op2)); - token_print_ln(p_token_next); - token_print_ln(p_token_store); + token_print(p_token_next); + printf("\n"); + token_print(p_token_store); + printf("\n"); #endif /* DEBUG_FUNCTION_PROCESS */ if (p_token_op2 != NULL) { @@ -525,10 +499,11 @@ _process(Interpret *p_interpret, Token *p_token_store, Token *p_token_op, } #ifdef DEBUG_FUNCTION_PROCESS - token_print_ln(p_token_store); + token_print(p_token_store); + printf("\n\n"); #endif /* DEBUG_FUNCTION_PROCESS */ - //token_delete(p_token_next); + token_delete(p_token_next); } void @@ -600,9 +575,6 @@ function_is_buildin(Token *p_token_ident) { if (strcmp("not", token_get_val(p_token_ident)) == 0) return (true); - if (strcmp("type", token_get_val(p_token_ident)) == 0) - return (true); - return (false); } @@ -762,7 +734,18 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, StackIterator *p_iter = stackiterator_new(p_stack_args); while (stackiterator_has_next(p_iter)) { Token *p_token = stackiterator_next(p_iter); - _print_val(p_token); + switch (token_get_tt(p_token)) { + case TT_INTEGER: + printf("%d", token_get_ival(p_token)); + break; + case TT_DOUBLE: + printf("%f", token_get_dval(p_token)); + break; + case TT_STRING: + printf("%s", token_get_val(p_token)); + break; + NO_DEFAULT; + } } stackiterator_delete(p_iter); @@ -770,7 +753,18 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, StackIterator *p_iter = stackiterator_new(p_stack_args); while (stackiterator_has_next(p_iter)) { Token *p_token = stackiterator_next(p_iter); - _print_val(p_token); + switch (token_get_tt(p_token)) { + case TT_INTEGER: + printf("%d", token_get_ival(p_token)); + break; + case TT_DOUBLE: + printf("%f", token_get_dval(p_token)); + break; + case TT_STRING: + printf("%s", token_get_val(p_token)); + break; + NO_DEFAULT; + } } stackiterator_delete(p_iter); printf("\n"); @@ -818,35 +812,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, break; NO_DEFAULT; } - } else if (strcmp("not", token_get_val(p_token_ident)) == 0) { - if (0 == stack_size(p_stack_args)) - _FUNCTION_ERROR("No argument given", p_token_ident); - - Token *p_token = token_new_copy(stack_pop(p_stack_args)); - stack_push(p_stack_args, p_token); - - switch (token_get_tt(p_token)) { - case TT_INTEGER: - token_set_ival(p_token, !token_get_ival(p_token)); - break; - case TT_DOUBLE: - token_set_dval(p_token, !token_get_dval(p_token)); - break; - case TT_STRING: - token_set_ival(p_token, !atoi(token_get_val(p_token))); - token_set_tt(p_token, TT_INTEGER); - break; - NO_DEFAULT; - } - } else if (strcmp("type", token_get_val(p_token_ident)) == 0) { - if (0 == stack_size(p_stack_args)) - _FUNCTION_ERROR("No argument given", p_token_ident); - - Token *p_token = stack_pop(p_stack_args); - TokenType tt = token_get_tt(p_token); - - Token *p_token_type = token_new_string(tt_get_name(tt)); - stack_push(p_stack_args, p_token_type); } } diff --git a/src/core/garbage.c b/src/core/garbage.c index 3820144..aba19c4 100644 --- a/src/core/garbage.c +++ b/src/core/garbage.c @@ -65,12 +65,6 @@ _garbage_print(_Garbage *p_garbage) { } void -_garbage_free(_Garbage *p_garbage) { - (*p_garbage->p_func) (p_garbage->p_2free); - free(p_garbage); -} - -void garbage_destroy() { garbage_collect(); @@ -93,18 +87,17 @@ garbage_destroy() { int garbage_collect() { - //printf("GARBAGE_COLLECT\n"); ListIterator *p_iter = listiterator_new(LIST_GARBAGE); List *p_list_garbage_new = list_new(); int i_count = 0; - //printf("size %d\n", list_size(LIST_GARBAGE)); while (listiterator_has_next(p_iter)) { _Garbage *p_garbage = listiterator_next(p_iter); if (p_garbage->p_ref_count == NULL || *p_garbage->p_ref_count <= 0) { - //_garbage_print(p_garbage); - _garbage_free(p_garbage); + // _garbage_print(p_garbage); + (*p_garbage->p_func) (p_garbage->p_2free); + free(p_garbage); ++i_count; } else { @@ -113,9 +106,9 @@ garbage_collect() { } listiterator_delete(p_iter); + list_delete(LIST_GARBAGE); LIST_GARBAGE = p_list_garbage_new; - //printf("GARBAGE_COLLECT_END\n"); return (i_count); } diff --git a/src/core/interpret.c b/src/core/interpret.c index 86d5a36..5aa07ef 100644 --- a/src/core/interpret.c +++ b/src/core/interpret.c @@ -54,16 +54,15 @@ #define _NEXT_TT _next_tt(p_interpret) #define _SKIP _next(p_interpret); -int __expression_get(Interpret *p_interpret, List *p_list_block); -int __array_get(Interpret *p_interpret, Token *p_token_arr); -void __print_lookahead(Interpret *p_interpret); +void _print_lookahead(Interpret *p_interpret); int _next(Interpret *p_interpret); int _program(Interpret *p_interpret); int _var_decl(Interpret *p_interpret); int _var_assign(Interpret *p_interpret); int _var_list(Interpret *p_interpret); +int _expression_get(Interpret *p_interpret, List *p_list_block); int _block_get(Interpret *p_interpret, List *p_list_block); -int __block_skip(Interpret *p_interpret); +int _block_skip(Interpret *p_interpret); int _proc_decl(Interpret *p_interpret); int _func_decl(Interpret *p_interpret); int _statement(Interpret *p_interpret); @@ -104,7 +103,7 @@ interpret_new(List *p_list_token, Hash *p_hash_syms) { void interpret_delete(Interpret *p_interpret) { - if (p_interpret == NULL) + if (!p_interpret) return; if (p_interpret->b_scope_delete) @@ -115,7 +114,7 @@ interpret_delete(Interpret *p_interpret) { } void -__print_lookahead(Interpret *p_interpret) { +_print_lookahead(Interpret *p_interpret) { ListIterator *p_iter = p_interpret->p_iter; ListIteratorState *p_state = listiterator_get_state(p_iter); @@ -165,13 +164,8 @@ int _program(Interpret *p_interpret) { _CHECK TRACK - int i_count = 0; - while (_statement(p_interpret) == 1) { - if (++i_count == 50) { - garbage_collect(); - i_count = 0; - } - } + while (_statement(p_interpret) == 1) + garbage_collect(); return (1); } @@ -181,6 +175,7 @@ _var_decl(Interpret *p_interpret) { _CHECK TRACK switch (p_interpret->tt) { + //case TT_ARR: //TODO cleanup TT_ARR case TT_MY: { if (_NEXT_TT != TT_IDENT) @@ -205,7 +200,7 @@ _var_decl(Interpret *p_interpret) { } } default: - break; + break; } return (0); @@ -231,8 +226,8 @@ _var_assign(Interpret *p_interpret) { p_interpret->p_stack = stack_new(); if (_expression_(p_interpret)) { - function_process_buildin(p_interpret, p_token, - p_interpret->p_stack); + function_process_buildin(p_interpret, p_token, + p_interpret->p_stack); stack_merge(p_stack, p_interpret->p_stack); stack_delete(p_interpret->p_stack); @@ -241,6 +236,7 @@ _var_assign(Interpret *p_interpret) { p_token = stack_top(p_interpret->p_stack); Symbol *p_symbol = symbol_new(SYM_VARIABLE, p_token); scope_newset(p_interpret->p_scope, c_name, p_symbol); + } else { return (0); } @@ -302,10 +298,11 @@ _block_get(Interpret *p_interpret, List *p_list_block) { } int -__expression_get(Interpret *p_interpret, List *p_list_expression) { +_expression_get(Interpret *p_interpret, List *p_list_expression) { for (;;) { - if (p_interpret->tt == TT_PARANT_CL) + if (p_interpret->tt == TT_PARANT_CL) { break; /* for */ + } list_add_back(p_list_expression, p_interpret->p_token); @@ -322,36 +319,7 @@ __expression_get(Interpret *p_interpret, List *p_list_expression) { } int -__array_get(Interpret *p_interpret, Token *p_token_arr) { -#ifdef DEBUG_ARRAY_GET - printf("====> ARRAY\n"); -#endif /* DEBUG_ARRAY_GET */ - Array *p_array = p_token_arr->p_array; - - do { - Token *p_token = p_interpret->p_token; - -#ifdef DEBUG_ARRAY_GET - printf("Insert: "); - token_print_ln(p_token); -#endif /* DEBUG_ARRAY_GET */ - - array_unshift(p_array, p_token); - token_ref_up(p_token); - _NEXT - } while (p_interpret->tt != TT_PARANT_AR); - -#ifdef DEBUG_ARRAY_GET - printf("<==== ARRAY\n"); -#endif /* DEBUG_ARRAY_GET */ - /* Ignore TT_PARANT_AR */ - _NEXT - - return (0); -} - -int -__block_skip(Interpret *p_interpret) { +_block_skip(Interpret *p_interpret) { if (p_interpret->tt != TT_PARANT_CL) _INTERPRET_ERROR("Expected '{'", p_interpret->p_token); _NEXT @@ -523,16 +491,16 @@ _control(Interpret *p_interpret) { switch (tt) { case TT_IF: if (convert_to_integer_get(p_token_top)) { - //scope_up(p_interpret->p_scope); + scope_up(p_interpret->p_scope); ret = interpret_subprocess(p_interpret, p_list_block); - //scope_down(p_interpret->p_scope); + scope_down(p_interpret->p_scope); } break; case TT_IFNOT: if (!convert_to_integer_get(p_token_top)) { - //scope_up(p_interpret->p_scope); + scope_up(p_interpret->p_scope); ret = interpret_subprocess(p_interpret, p_list_block); - //scope_down(p_interpret->p_scope); + scope_down(p_interpret->p_scope); } break; NO_DEFAULT; @@ -555,7 +523,7 @@ _control(Interpret *p_interpret) { _NEXT - __expression_get(p_interpret, p_list_expr); + _expression_get(p_interpret, p_list_expr); _block_get(p_interpret, p_list_block); Token *p_token_backup = p_interpret->p_token; @@ -573,6 +541,7 @@ _control(Interpret *p_interpret) { Token *p_token_top = stack_pop(p_interpret->p_stack); if (p_token_top == NULL) { + printf("FOO\n"); exit(0); } if (tt == TT_WHILE) { @@ -817,7 +786,6 @@ _term(Interpret *p_interpret) { case TT_INTEGER: case TT_DOUBLE: case TT_STRING: - case TT_ARRAY: stack_push(p_interpret->p_stack, p_interpret->p_token); _NEXT return (1); @@ -949,13 +917,9 @@ _term(Interpret *p_interpret) { Token *p_token = p_interpret->p_token; _NEXT - Token *p_token_arr = token_new_array(ARRAY_SIZE); + Token *p_token_arr = token_new_array(ARRAY_SIZE); stack_push(p_interpret->p_stack, p_token_arr); - - if (__array_get(p_interpret, p_token_arr) != 0) - _INTERPRET_ERROR("Array syntax error", p_token); - - return (1); + _INTERPRET_ERROR("arrays not yet fully implemented", p_token_arr); } break; diff --git a/src/core/scanner.c b/src/core/scanner.c index 5190175..49026c2 100644 --- a/src/core/scanner.c +++ b/src/core/scanner.c @@ -108,8 +108,8 @@ scanner_post_task(Scanner *p_scanner) { if (tt_cur == TT_INTEGER && tt_last[1] == TT_DOT && tt_last[0] == TT_INTEGER) { - //token_ref_down(pt_last[0]); - //token_ref_down(pt_last[1]); + token_ref_down(pt_last[0]); + token_ref_down(pt_last[1]); char *c_2 = token_get_val(p_token); char *c_0 = token_get_val(pt_last[0]); @@ -338,9 +338,9 @@ scanner_add_token(Scanner *p_scanner, char **cc_token, int *p_token_len, List *p_list_token = scanner_get_list_token(p_scanner); Token *p_token = token_new(*cc_token, tt_cur, p_scanner->i_current_line_nr, p_scanner->i_current_pos_nr, p_scanner->c_filename); - p_token->b_source_token = true; list_add_back(p_list_token, p_token); + token_ref_up(p_token); *cc_token = malloc(sizeof(char)); (*cc_token)[0] = 0; @@ -361,3 +361,9 @@ scanner_get_tt_cur(char *c_token) { return tt_cur == TT_NONE ? TT_IDENT : tt_cur; } + +void +scanner_cleanup_list_token_cb(void *p_void) { + Token *p_token = p_void; + token_delete(p_token); +} diff --git a/src/core/scanner.h b/src/core/scanner.h index c4e15fa..4626595 100644 --- a/src/core/scanner.h +++ b/src/core/scanner.h @@ -65,5 +65,6 @@ void scanner_run(Fype *p_fype); void scanner_add_token(Scanner *p_scanner, char **cc_token, int *p_token_len, TokenType tt_cur); TokenType scanner_get_tt_cur(char *c_token); +void scanner_cleanup_list_token_cb(void *p_void); #endif diff --git a/src/core/scope.c b/src/core/scope.c index ecf5933..1bf8dd2 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -36,9 +36,6 @@ #include "scope.h" #include "symbol.h" -#define _SCOPE_ERROR(m) \ - ERROR("%s: Scope error", m) - Scope* scope_new(Hash *p_hash_syms) { Scope *p_scope = malloc(sizeof(Scope)); @@ -61,42 +58,14 @@ scope_delete(Scope *p_scope) { void scope_up(Scope *p_scope) { -#ifdef EXTRA_CHECKS - int i_before = (int) stack_size(p_scope->p_stack_scopes); -#elif DEBUG_SCOPE_UPDOWN - int i_before = (int) stack_size(p_scope->p_stack_scopes); -#endif - stack_push(p_scope->p_stack_scopes, hash_new(24)); -#ifdef DEBUG_SCOPE_UPDOWN - printf("SCOPE UPPED %d => %d\n", - i_before, - (int) stack_size(p_scope->p_stack_scopes)); -#endif /* DEBUG_SCOPE_UPDOWN */ -#ifdef EXTRA_CHECKS - if (i_before >= (int) stack_size(p_scope->p_stack_scopes)) - _SCOPE_ERROR("Scope should be higher"); -#endif /* EXTRA_CHECKS */ + stack_push(p_scope->p_stack_scopes, hash_new(1024)); } void scope_down(Scope *p_scope) { -#ifdef EXTRA_CHECKS - int i_before = (int) stack_size(p_scope->p_stack_scopes); -#elif DEBUG_SCOPE_UPDOWN - int i_before = (int) stack_size(p_scope->p_stack_scopes); -#endif Hash *p_hash_syms = stack_pop(p_scope->p_stack_scopes); hash_iterate(p_hash_syms, symbol_cleanup_hash_syms_cb); hash_delete(p_hash_syms); -#ifdef DEBUG_SCOPE_UPDOWN - printf("SCOPE DOWNED %d => %d\n", - i_before, - (int) stack_size(p_scope->p_stack_scopes)); -#endif /* DEBUG_SCOPE_UPDOWN */ -#ifdef EXTRA_CHECKS - if (i_before <= (int) stack_size(p_scope->p_stack_scopes)) - _SCOPE_ERROR("Scope should be lower"); -#endif /* EXTRA_CHECKS */ } static Hash* diff --git a/src/core/symbol.c b/src/core/symbol.c index 7c8a8f7..2343cdb 100644 --- a/src/core/symbol.c +++ b/src/core/symbol.c @@ -33,9 +33,7 @@ *:*/ #include "symbol.h" -#include "token.h" -#include "../data/array.h" #include "../data/list.h" Symbol* @@ -57,20 +55,8 @@ symbol_delete(Symbol *p_symbol) { list_delete(p_list_token); } break; - case SYM_VARIABLE: - { - Token *p_token = symbol_get_val(p_symbol); - switch (token_get_tt(p_token)) { - case TT_ARRAY: - //array_iterate(p_token->p_array, token_delete_cb); - break; - NO_DEFAULT; - } - } - break; NO_DEFAULT; } - free(p_symbol); } diff --git a/src/core/token.c b/src/core/token.c index 4bed0e0..1a2aec9 100644 --- a/src/core/token.c +++ b/src/core/token.c @@ -57,6 +57,7 @@ get_tt(char *c_token) { CHECK("proc") TT_PROC; CHECK("func") TT_FUNC; CHECK("my") TT_MY; + CHECK("arr") TT_ARR; CHECK("!") TT_NOT; CHECK("!=") TT_NEQ; CHECK("=~") TT_RE; @@ -122,6 +123,7 @@ tt_get_name(TokenType tt_cur) { CASE(TT_PROC,"TT_PROC") CASE(TT_FUNC,"TT_FUNC") CASE(TT_MY,"TT_MY") + CASE(TT_ARR,"TT_ARR") CASE(TT_WHILE,"TT_WHILE") CASE(TT_UNTIL,"TT_UNTIL") CASE(TT_NEXT,"TT_NEXT") @@ -181,8 +183,7 @@ tt_get_name(TokenType tt_cur) { } Token* -token_new(char *c_val, TokenType tt_cur, int i_line_nr, int i_pos_nr, - char *c_filename) { +token_new(char *c_val, TokenType tt_cur, int i_line_nr, int i_pos_nr, char *c_filename) { Token *p_token = token_new_dummy(); p_token->c_val = c_val; @@ -193,7 +194,6 @@ token_new(char *c_val, TokenType tt_cur, int i_line_nr, int i_pos_nr, p_token->i_pos_nr = i_pos_nr; p_token->c_filename = c_filename; p_token->p_array = NULL; - p_token->b_source_token = false; switch (tt_cur) { case TT_INTEGER: @@ -212,7 +212,7 @@ token_new(char *c_val, TokenType tt_cur, int i_line_nr, int i_pos_nr, NO_DEFAULT; } - return (p_token); + return p_token; } Token* @@ -248,11 +248,7 @@ Token* token_new_array(int i_size) { Token *p_token = token_new_dummy(); token_set_tt(p_token, TT_ARRAY); - - if (p_token->p_array == NULL) - p_token->p_array = array_new(); - - //array_resize(p_token->p_array, i_size); + array_resize(p_token->p_array, i_size); return (p_token); } @@ -273,7 +269,7 @@ token_new_dummy() { p_token->i_pos_nr = -1; p_token->c_filename = NULL; p_token->u_token_id = TOKEN_ID_COUNTER++; - p_token->i_ref_count = 1; + p_token->i_ref_count = 0; /* Register the token in the garbage collector */ garbage_add_token(p_token); @@ -284,7 +280,7 @@ token_new_dummy() { Token* token_new_copy(Token *p_token) { Token *p_token_copy = malloc(sizeof(Token)); - p_token_copy->u_token_id = TOKEN_ID_COUNTER++; + p_token->u_token_id = TOKEN_ID_COUNTER++; if (p_token_copy == NULL) ERROR("Memory alloc error"); @@ -298,36 +294,21 @@ token_new_copy(Token *p_token) { return (p_token_copy); } -void -token_copy_vals(Token *p_token_to, Token *p_token_from) { +void token_copy_vals(Token *p_token_to, Token *p_token_from) { int i_len; // TODO: Check against mem leak - if (p_token_to->c_val) - free(p_token_to->c_val); + // if (p_token_to->c_val) + // free(p_token_to->c_val); if (p_token_from->c_val) { i_len = strlen(p_token_from->c_val); p_token_to->c_val = calloc(i_len+1, sizeof(char)); strcpy(p_token_to->c_val, p_token_from->c_val); - } else { p_token_to->c_val = NULL; } - if (p_token_from->tt_cur == TT_ARRAY) { - p_token_to->p_array = array_new(); - ArrayIterator *p_iter = arrayiterator_new(p_token_from->p_array); - while (arrayiterator_has_next(p_iter)) { - Token *p_token = arrayiterator_next(p_iter); - token_ref_up(p_token); - array_unshift(p_token_to->p_array, p_token); - } - arrayiterator_delete(p_iter); - } else { - p_token_to->p_array = NULL; - } - p_token_to->tt_cur = p_token_from->tt_cur; p_token_to->i_val = p_token_from->i_val; p_token_to->d_val = p_token_from->d_val; @@ -342,11 +323,6 @@ token_delete_cb(void *p_void) { } void -token_delete_force_cb(void *p_void) { - token_delete_force(p_void); -} - -void token_ref_down_cb(void *p_void) { Token *p_token = p_void; token_ref_down(p_token); @@ -357,92 +333,49 @@ token_copy_cb(void *p_void) { return (token_new_copy(p_void)); } -int -token_ref_down(Token *p_token) { -// if (p_token->i_ref_count > 0) - p_token->i_ref_count--; - - return (p_token->i_ref_count); -} - -void -_token_free(Token *p_token) { - if (p_token->c_val) - free(p_token->c_val); - p_token->c_val = NULL; - - if (p_token->p_array) { - array_iterate(p_token->p_array, token_delete_cb); - array_delete(p_token->p_array); - } - p_token->p_array = NULL; - - free(p_token); -} - void token_delete(Token *p_token) { - if (IS_SOURCE_TOKEN(p_token)) - return; - - token_ref_down(p_token); - - if (p_token->i_ref_count == 0) { + if (token_ref_down(p_token) <= 0) { + if (p_token->i_ref_count == 0) { #ifdef DEBUG_TOKEN_REFCOUNT - printf("Token refcount debug: Token ref count is 0 == %d\n", - p_token->i_ref_count); - token_print_ln(p_token); + printf("Token refcount debug: Token ref count is 0 == %d\n", + p_token->i_ref_count); #endif /* DEBUG_TOKEN_REFCOUNT */ + if (p_token->c_val) + free(p_token->c_val); - _token_free(p_token); + if (p_token->p_array) + array_delete(p_token->p_array); - } + free(p_token); + } #ifdef DEBUG_TOKEN_REFCOUNT - else if (p_token->i_ref_count < 0) { - token_print_ln(p_token); - printf("Token ref count is 0 > %d\n", - p_token->i_ref_count); + else { + printf("Token refcount debug: Token ref count is 0 > %d\n", + p_token->i_ref_count); + } +#endif /* DEBUG_TOKEN_REFCOUNT */ } +#ifdef DEBUG_TOKEN_REFCOUNT else { printf("Token refcount debug: Token ref count is 0 < %d\n", p_token->i_ref_count); - token_print_ln(p_token); } #endif /* DEBUG_TOKEN_REFCOUNT */ } void -token_delete_force(Token *p_token) { - token_ref_down(p_token); - //printf("FORCE DEL "); - //token_print_ln(p_token); - _token_free(p_token); -} - -void token_print(Token *p_token) { - printf("(org=%d, id=%05u, line=%05d, pos=%04d, type=%s", - (int) p_token->b_source_token, + printf("(id=%05u, line=%05d, pos=%04d, type=%s, val=%s, ival=%d, dval=%f," + " refs=%d)", p_token->u_token_id, p_token->i_line_nr, p_token->i_pos_nr, - tt_get_name(p_token->tt_cur)); - - if (IS_ARRAY(p_token)) { - } else { - printf(", val=%s, ival=%d, dval=%f", - p_token->c_val, - p_token->i_val, - p_token->d_val); - } - - printf(", refs=%d)", p_token->i_ref_count); -} - -void -token_print_ln(Token *p_token) { - token_print(p_token); - printf("\n"); + tt_get_name(p_token->tt_cur), + p_token->c_val, + p_token->i_val, + p_token->d_val, + p_token->i_ref_count); } void diff --git a/src/core/token.h b/src/core/token.h index e683e01..caf4854 100644 --- a/src/core/token.h +++ b/src/core/token.h @@ -53,10 +53,6 @@ #define IS_ASSIGNABLE(t) (START_ASSIGNABLES < t && t < END_ASSIGNABLES) #define IS_NUMERICAL(t) (START_NUMERICAL < t && t < END_NUMERICAL) #define IS_NOT_NUMERICAL(t) !(IS_NUMERICAL(t)) -#define IS_ARRAY(t) (t->tt_cur == TT_ARRAY) -#define IS_NOT_ARRAY(t) !(IS_ARRAY(t)) -#define IS_SOURCE_TOKEN(t) (t->b_source_token == true) -#define IS_NOT_SOURCE_TOKEN(t) (t->b_source_token == false) #define token_get_filename(t) \ (t->c_filename != NULL ? t->c_filename : "Code string") @@ -74,6 +70,7 @@ #define token_get_posnr(t) t->i_pos_nr #define token_get_linenr(t) t->i_line_nr #define token_ref_up(t) ++t->i_ref_count +#define token_ref_down(t) --t->i_ref_count typedef enum { // Diverse @@ -89,8 +86,8 @@ typedef enum { TT_INTEGER, TT_DOUBLE, END_NUMERICAL, - TT_ARRAY, TT_STRING, + TT_ARRAY, END_ASSIGNABLES, TT_IDENT, END_TYPES, @@ -106,6 +103,7 @@ typedef enum { TT_PROC, TT_FUNC, TT_MY, + TT_ARR, TT_WHILE, TT_UNTIL, TT_NEXT, @@ -167,7 +165,6 @@ typedef struct { char *c_filename; unsigned int u_token_id; int i_ref_count; - _Bool b_source_token; Array *p_array; } Token; @@ -181,17 +178,13 @@ Token* token_new_(char *c_val, TokenType tt_cur, char *c_filename); Token* token_new_dummy(); void token_copy_vals(Token *p_token_to, Token *p_token_from); void token_delete(Token *p_token); -void token_delete_force(Token *p_token); -void token_delete_force_cb(void *p_token); void token_delete_cb(void *p_token); void token_ref_down_cb(void *p_token); void* token_copy_cb(void *p_token); char* tt_get_name(TokenType tt_cur); void token_print_cb(void *p_void); void token_print(Token *p_token); -void token_print_ln(Token *p_token); void token_print_val(Token *p_token); -int token_ref_down(Token *p_token); TokenType get_tt(char *c_token); #endif diff --git a/src/data/array.c b/src/data/array.c index da5bee8..2ae4ece 100644 --- a/src/data/array.c +++ b/src/data/array.c @@ -41,7 +41,7 @@ array_new() { p_array->i_size = 0; p_array->pp_ae = NULL; - return (p_array); + return p_array; } @@ -92,7 +92,7 @@ array_insert(Array *p_array, int i_index, void *p_val) { void* array_remove(Array *p_array, int i_index) { if (p_array->i_size <= i_index) - return (NULL); + return NULL; ArrayElement *p_ae = p_array->pp_ae[i_index]; void *p_ret = p_ae->p_val; @@ -105,7 +105,7 @@ array_remove(Array *p_array, int i_index) { array_resize(p_array, p_array->i_size - 1); - return (p_ret); + return p_ret; } void @@ -147,17 +147,17 @@ array_resize(Array *p_array, int i_size) { void* array_get(Array *p_array, int i_index) { if (p_array->i_size > i_index) - return (p_array->pp_ae[i_index]->p_val); + return p_array->pp_ae[i_index]->p_val; - return (NULL); + return NULL; } _Bool array_defined(Array *p_array, int i_index) { if (i_index >= p_array->i_size) - return (false); + return false; - return (p_array->pp_ae[i_index]->p_val != NULL); + return p_array->pp_ae[i_index]->p_val != NULL; } void @@ -222,7 +222,7 @@ arrayelement_new(void *p_val) { p_ae->p_val = p_val; - return (p_ae); + return p_ae; } void @@ -235,27 +235,14 @@ arrayelement_delete(ArrayElement *p_ae) { ArrayIterator* arrayiterator_new(Array *p_array) { - if (p_array == NULL) - return (NULL); + if (!p_array) + return NULL; ArrayIterator *p_arrayiterator = malloc(sizeof(ArrayIterator)); p_arrayiterator->p_array = p_array; p_arrayiterator->i_cur_pos = 0; - p_arrayiterator->b_is_reverse = false; - - return (p_arrayiterator); -} - -ArrayIterator* -arrayiterator_new_reverse(Array *p_array) { - ArrayIterator *p_arrayiterator = arrayiterator_new(p_array); - if (p_arrayiterator == NULL) - return (NULL); - - p_arrayiterator->b_is_reverse = true; - p_arrayiterator->i_cur_pos = p_array->i_size; - return (p_arrayiterator); + return p_arrayiterator; } void @@ -266,21 +253,14 @@ arrayiterator_delete(ArrayIterator *p_arrayiterator) { _Bool arrayiterator_has_next(ArrayIterator *p_arrayiterator) { - if (p_arrayiterator->b_is_reverse) - return (p_arrayiterator->i_cur_pos >= 0); - - return (p_arrayiterator->i_cur_pos < - array_get_size(p_arrayiterator->p_array)); + return p_arrayiterator->i_cur_pos < + array_get_size(p_arrayiterator->p_array); } void* arrayiterator_next(ArrayIterator *p_arrayiterator) { if (!arrayiterator_has_next(p_arrayiterator)) - return (NULL); - - if (p_arrayiterator->b_is_reverse) - return (array_get(p_arrayiterator->p_array, - p_arrayiterator->i_cur_pos--)); + return NULL; - return (array_get(p_arrayiterator->p_array, p_arrayiterator->i_cur_pos++)); + return array_get(p_arrayiterator->p_array, p_arrayiterator->i_cur_pos++); } diff --git a/src/data/array.h b/src/data/array.h index 6170c29..b343521 100644 --- a/src/data/array.h +++ b/src/data/array.h @@ -58,7 +58,6 @@ typedef struct { typedef struct { Array *p_array; int i_cur_pos; - _Bool b_is_reverse; } ArrayIterator; Array *array_new(); @@ -81,7 +80,6 @@ ArrayElement *arrayelement_new(void *p_val); void arrayelement_delete(ArrayElement *p_ae); ArrayIterator *arrayiterator_new(Array *p_array); -ArrayIterator *arrayiterator_new_reverse(Array *p_array); void arrayiterator_delete(ArrayIterator *p_arrayiterator); _Bool arrayiterator_has_next(ArrayIterator *p_arrayiterator); void *arrayiterator_next(ArrayIterator *p_arrayiterator); diff --git a/src/data/hash.c b/src/data/hash.c index d3f7634..5555eb1 100644 --- a/src/data/hash.c +++ b/src/data/hash.c @@ -44,13 +44,13 @@ hash_new(unsigned i_size) { p_hash->i_size = i_size; p_hash->i_cur_size = 0; - p_hash->p_elems = (HashElem *) calloc((int)i_size, sizeof(HashElem)); + p_hash->p_elems = (HashElem *) calloc(i_size, sizeof(HashElem)); /*Set all positions as "free" */ for (int i = 0; i < i_size; ++i) p_hash->p_elems[i].flag = 'f'; - return (p_hash); + return p_hash; } void diff --git a/src/data/stack.c b/src/data/stack.c index 0f5f58e..9afb9b4 100644 --- a/src/data/stack.c +++ b/src/data/stack.c @@ -62,8 +62,7 @@ stack_empty(Stack *p_stack) { } void -stack_debug(Stack *p_stack, void *p_val) { - printf("bPUSH %d %d\n", stack_size(p_stack), (int)p_stack); +stack_push(Stack *p_stack, void *p_val) { StackElem *p_elem = stackelem_new(); p_elem->p_val = p_val; @@ -74,20 +73,6 @@ stack_debug(Stack *p_stack, void *p_val) { p_stack->p_last = p_stack->p_first; ++p_stack->i_size; - printf("aPUSH %d %d\n", stack_size(p_stack), (int)p_stack); -} - -unsigned -stack_push(Stack *p_stack, void *p_val) { - StackElem *p_elem = stackelem_new(); - p_elem->p_val = p_val; - p_elem->p_next = p_stack->p_first; - p_stack->p_first = p_elem; - - if (p_stack->p_last == NULL) - p_stack->p_last = p_stack->p_first; - - return (++p_stack->i_size); } void* diff --git a/src/data/stack.h b/src/data/stack.h index e9fafea..4774cef 100644 --- a/src/data/stack.h +++ b/src/data/stack.h @@ -59,8 +59,7 @@ Stack *stack_new(); StackElem *stackelem_new(); _Bool stack_empty(Stack *p_stack); void stack_iterate(Stack *p_stack, void (*func)(void *p_void)); -unsigned stack_push(Stack *p_stack, void *p_val); -void stack_debug(Stack *p_stack, void *p_val); +void stack_push(Stack *p_stack, void *p_val); void *stack_pop(Stack *p_stack); void stack_clear(Stack *p_stack); void stack_delete(Stack *p_stack); diff --git a/src/defines.h b/src/defines.h index 29df2c2..569d20a 100644 --- a/src/defines.h +++ b/src/defines.h @@ -75,14 +75,11 @@ exit(1); } #define DPRINTF(...) printf("DEBUG("); printf(__VA_ARGS__); printf(")\n"); -#define EXTRA_CHECKS -#define DEBUG_SCOPE_UPDOWN //#define DEBUG_TOKEN_REFCOUNT //#define DEBUG_FUNCTION_PROCESS -#define DEBUG_TRACK +//#define DEBUG_TRACK //#define DEBUG_BLOCK_GET //#define DEBUG_EXPRESSION_GET -//#define DEBUG_ARRAY_GET #ifdef DEBUG_TRACK #define TRACK \ @@ -61,7 +61,9 @@ fype_delete(Fype *p_fype) { hash_iterate(p_fype->p_hash_syms, symbol_cleanup_hash_syms_cb); hash_delete(p_fype->p_hash_syms); - list_iterate(p_fype->p_list_token, token_delete_force_cb); + //list_iterate(p_fype->p_list_token, token_print_cb); + list_iterate(p_fype->p_list_token, token_ref_down_cb); + //list_iterate(p_fype->p_list_token, token_print_cb); list_delete(p_fype->p_list_token); if (p_fype->c_basename) |
