diff options
| author | Paul Buetow <paul@buetow.org> | 2008-11-07 22:11:06 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-11-07 22:11:06 +0000 |
| commit | a46655f67043af257e70715903badf9d4321c4de (patch) | |
| tree | 556997ba868ac106c6f92703e6e35875e4872dc2 | |
| parent | fbff89d91c2a13155423bd83d2bbefd5d6891e23 (diff) | |
one step further for arrays.
| -rw-r--r-- | docs/help.txt | 2 | ||||
| -rw-r--r-- | docs/stats.txt | 2 | ||||
| -rw-r--r-- | docs/version.txt | 2 | ||||
| -rwxr-xr-x | fype | bin | 455135 -> 456748 bytes | |||
| -rw-r--r-- | src/build.h | 2 | ||||
| -rw-r--r-- | src/core/function.c | 46 | ||||
| -rw-r--r-- | src/core/interpret.c | 68 | ||||
| -rw-r--r-- | src/core/token.c | 4 | ||||
| -rw-r--r-- | src/data/array.c | 34 | ||||
| -rw-r--r-- | src/data/array.h | 3 | ||||
| -rw-r--r-- | src/data/stack.c | 8 | ||||
| -rw-r--r-- | src/data/stack.h | 3 | ||||
| -rw-r--r-- | src/defines.h | 1 | ||||
| -rw-r--r-- | tags | 2 | ||||
| -rw-r--r-- | tmp/test.fy | 3 | ||||
| -rw-r--r-- | tmp/test.out | 55 |
16 files changed, 147 insertions, 88 deletions
diff --git a/docs/help.txt b/docs/help.txt index 821034c..f27684b 100644 --- a/docs/help.txt +++ b/docs/help.txt @@ -1,4 +1,4 @@ -Fype v0.1-devel Build 9363 +Fype v0.1-devel Build 9477 (c) Paul C. Buetow (2005 - 2008) <fype@dev.buetow.org> -e Executes given code string (see synopses) -h Prints this help diff --git a/docs/stats.txt b/docs/stats.txt index 4d2b190..4d4d8c6 100644 --- a/docs/stats.txt +++ b/docs/stats.txt @@ -1,4 +1,4 @@ ===> Num of C source files : 44 -===> Num of C source lines : 7993 +===> Num of C source lines : 8000 ===> Num of Fype source examples : 14 ===> Num of Fype source lines : 362 diff --git a/docs/version.txt b/docs/version.txt index 2dac819..754e1f7 100644 --- a/docs/version.txt +++ b/docs/version.txt @@ -1 +1 @@ -Fype v0.1-devel Build 9363 +Fype v0.1-devel Build 9477 Binary files differdiff --git a/src/build.h b/src/build.h index 0fecd98..01a4ce8 100644 --- a/src/build.h +++ b/src/build.h @@ -35,7 +35,7 @@ #ifndef BUILD_H #define BUILD_H -#define BUILDNR 9375 +#define BUILDNR 9608 #define OS_FREEBSD #endif diff --git a/src/core/function.c b/src/core/function.c index a102d7b..0df0b65 100644 --- a/src/core/function.c +++ b/src/core/function.c @@ -636,12 +636,27 @@ void function_process_buildin(Interpret *p_interpret, Token *p_token_ident, Stack *p_stack_args) { + Token *p_token = stack_top(p_stack_args); + + if (token_get_tt(p_token) == TT_ARRAY) { + ArrayIterator *p_iter = arrayiterator_new(p_token->p_array); + + while (arrayiterator_has_next(p_iter)) { + stack_push(p_stack_args, arrayiterator_next(p_iter)); + function_process_buildin(p_interpret, p_token_ident, + p_stack_args); + stack_pop(p_stack_args); + } + + arrayiterator_delete(p_iter); + + return; + } + if (strcmp("assert", 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_top(p_stack_args); - switch (token_get_tt(p_token)) { case TT_INTEGER: if (token_get_ival(p_token) == 0) @@ -655,9 +670,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, if (atoi(token_get_val(p_token)) == 0) _FUNCTION_ERROR("Assert failed", p_token); break; - case TT_ARRAY: - ERROR("ARRAY bla yet implemented"); - break; NO_DEFAULT; } @@ -665,7 +677,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, if (0 == stack_size(p_stack_args)) _FUNCTION_ERROR("No argument given", p_token_ident); - Token *p_token = stack_top(p_stack_args); switch (token_get_tt(p_token)) { case TT_INTEGER: token_set_ival(p_token, token_get_ival(p_token) - 1); @@ -677,9 +688,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, convert_to_integer(p_token); token_set_ival(p_token, token_get_ival(p_token) - 1); break; - case TT_ARRAY: - ERROR("ARRAY bla yet implemented"); - break; NO_DEFAULT; } @@ -707,7 +715,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, if (0 == stack_size(p_stack_args)) _FUNCTION_ERROR("No argument given", p_token_ident); - Token *p_token = stack_top(p_stack_args); p_token = token_new_copy(p_token); convert_to_integer(p_token); exit(token_get_ival(p_token)); @@ -716,7 +723,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, if (0 == stack_size(p_stack_args)) _FUNCTION_ERROR("No argument given", p_token_ident); - Token *p_token = stack_top(p_stack_args); switch (token_get_tt(p_token)) { case TT_INTEGER: token_set_ival(p_token, token_get_ival(p_token) + 1); @@ -728,9 +734,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, convert_to_integer(p_token); token_set_ival(p_token, token_get_ival(p_token) + 1); break; - case TT_ARRAY: - ERROR("ARRAY bla yet implemented"); - break; NO_DEFAULT; } @@ -763,9 +766,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, token_set_ival(p_token, -atoi(token_get_val(p_token))); token_set_tt(p_token, TT_INTEGER); break; - case TT_ARRAY: - ERROR("ARRAY bla yet implemented"); - break; NO_DEFAULT; } @@ -789,9 +789,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, token_set_ival(p_token, !atoi(token_get_val(p_token))); token_set_tt(p_token, TT_INTEGER); break; - case TT_ARRAY: - ERROR("ARRAY bla yet implemented"); - break; NO_DEFAULT; } } @@ -812,9 +809,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, case TT_STRING: printf("%s", token_get_val(p_token)); break; - case TT_ARRAY: - ERROR("ARRAY bla yet implemented"); - break; NO_DEFAULT; } } @@ -837,9 +831,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, case TT_STRING: printf("%s", token_get_val(p_token)); break; - case TT_ARRAY: - ERROR("ARRAY bla yet implemented"); - break; } } stackiterator_delete(p_iter); @@ -886,9 +877,6 @@ function_process_buildin(Interpret *p_interpret, Token *p_token_ident, token_set_ival(p_token, !atoi(token_get_val(p_token))); token_set_tt(p_token, TT_INTEGER); break; - case TT_ARRAY: - ERROR("ARRAY bla yet implemented"); - break; NO_DEFAULT; } diff --git a/src/core/interpret.c b/src/core/interpret.c index 67ebc22..cc1d87f 100644 --- a/src/core/interpret.c +++ b/src/core/interpret.c @@ -50,6 +50,7 @@ #define _CHECK if (p_interpret->p_token == NULL) return (0); #define _HAS_NEXT listiterator_has_next(p_interpret->p_iter) +#define _NEXT_ORG _next(p_interpret); #define _NEXT if (!_next(p_interpret)) { return (2); } #define _NEXT_TT _next_tt(p_interpret) #define _SKIP _next(p_interpret); @@ -146,6 +147,7 @@ _next(Interpret *p_interpret) { p_interpret->p_token = NULL; p_interpret->tt = TT_NONE; + //printf("==>\n"); return (0); } @@ -465,7 +467,8 @@ _expression(Interpret *p_interpret) { _CHECK TRACK if (_expression_(p_interpret)) { - if (p_interpret->tt == TT_SEMICOLON) { + TokenType tt = p_interpret->tt; + if (tt == TT_SEMICOLON || tt == TT_NONE) { _NEXT } else { @@ -798,13 +801,18 @@ _term(Interpret *p_interpret) { _CHECK TRACK switch (p_interpret->tt) { + case TT_STRING: 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); + // Checks if the term is the last element of an array + // say ["element"] # The "element" + // or of a function + // func foo { say 1 } # The 1 + if (_NEXT_TT != TT_PARANT_AR && _NEXT_TT != TT_PARANT_CR) + _NEXT + return (1); case TT_IDENT: { @@ -949,49 +957,29 @@ _term(Interpret *p_interpret) { } break; - /* - // Reference operator - case TT_AAND: + case TT_PARANT_AL: { - _NEXT - if (p_interpret->tt != TT_IDENT) - _INTERPRET_ERROR("Expexted identifier for '&'", - p_interpret->p_token); - - char *c_name = token_get_val(p_interpret->p_token); - Symbol *p_symbol = scope_get(p_interpret->p_scope, c_name); - - _INTERPRET_ERROR("nyi", p_interpret->p_token); - - _NEXT; - return (1); - } - break; + Token *p_token_arr = token_new_array(ARRAY_SIZE); + Array *p_array = p_token_arr->p_array; - // Dereference opeator - case TT_MULT: - { _NEXT - if (p_interpret->tt != TT_IDENT) - _INTERPRET_ERROR("Expexted identifier for '*'", - p_interpret->p_token); + // Get the array elements + while (p_interpret->tt != TT_PARANT_AR) { + TokenType tt = tt = p_interpret->tt; + if (tt != TT_COMMA && tt != TT_SEMICOLON) { + UNLESS (_expression_(p_interpret)) { + Token *p_token = p_interpret->p_token; + _INTERPRET_ERROR("Expected expression", p_token); + } + array_unshift(p_array, stack_pop(p_interpret->p_stack)); + } - _INTERPRET_ERROR("nyi", p_interpret->p_token); - _NEXT; - return (1); - } - break; - */ + _NEXT + } - case TT_PARANT_AL: - { - Token *p_token = p_interpret->p_token; + stack_push(p_interpret->p_stack, p_token_arr); _NEXT - - _INTERPRET_ERROR("arrays not yet fully implemented", p_token); - Token *p_token_arr = token_new_array(ARRAY_SIZE); - //stack_push(p_interpret->p_stack, p_token_arr); } break; diff --git a/src/core/token.c b/src/core/token.c index a60a790..8f8adb0 100644 --- a/src/core/token.c +++ b/src/core/token.c @@ -252,7 +252,10 @@ token_new_string(char *c_val) { Token* token_new_array(int i_size) { Token *p_token = token_new_dummy(); + token_set_tt(p_token, TT_ARRAY); + p_token->p_array = array_new(); + array_resize(p_token->p_array, i_size); return (p_token); @@ -415,3 +418,4 @@ token_print_cb(void *p_void) { token_print(p_token); printf("\n"); } + diff --git a/src/data/array.c b/src/data/array.c index 9bd0101..ba9cf2a 100644 --- a/src/data/array.c +++ b/src/data/array.c @@ -40,16 +40,15 @@ array_new() { p_array->i_size = 0; p_array->pp_ae = NULL; + array_set_used(p_array, 0); return (p_array); } Array* array_new_size(int i_size) { - Array *p_array = malloc(sizeof(Array)); + Array *p_array = array_new(); - p_array->i_size = 0; - p_array->pp_ae = NULL; array_resize(p_array, i_size); return (p_array); @@ -109,9 +108,19 @@ array_set(Array *p_array, int i_index, void *p_val) { array_resize(p_array, i_index + 1); p_array->pp_ae[i_index]->p_val = p_val; } + + if (p_array->i_used < i_index) + array_set_used(p_array, i_index); } void +array_set_used(Array *p_array, int i_used) { + //printf("foo %d",i_used); + p_array->i_used = i_used; +} + + +void array_insert(Array *p_array, int i_index, void *p_val) { if (p_array->i_size <= i_index) { array_set(p_array, i_index, p_val); @@ -127,6 +136,9 @@ array_insert(Array *p_array, int i_index, void *p_val) { p_array->pp_ae[i] = p_ae; p_ae->p_val = p_val; } + + if (p_array->i_used < i_index) + array_set_used(p_array, i_index); } void* @@ -144,7 +156,6 @@ array_remove(Array *p_array, int i_index) { p_array->pp_ae[i-1] = p_ae; array_resize(p_array, p_array->i_size - 1); - return (p_ret); } @@ -182,6 +193,9 @@ array_resize(Array *p_array, int i_size) { p_array->pp_ae[i] = arrayelement_new(NULL); p_array->i_size = i_size; + //printf("[%d > %d]", p_array->i_used, i_size); + if (p_array->i_used > i_size) + array_set_used(p_array, i_size); } void* @@ -223,8 +237,9 @@ array_splice(Array *p_array, int i_index, Array *p_array2) { void array_unshift(Array *p_array, void *p_void) { - int i_size = array_get_size(p_array); - array_set(p_array, i_size, p_void); + int i_used = array_get_used(p_array); + array_set(p_array, i_used, p_void); + array_set_used(p_array, 1+i_used); } void @@ -250,7 +265,7 @@ array_iterate(Array *p_array, void (*func)(void *)) { if (!p_array) return; - for (int i = 0; i < array_get_size(p_array); ++i) + for (int i = 0; i < array_get_used(p_array); ++i) (*func) (array_get(p_array, i)); } @@ -259,7 +274,7 @@ array_iterate2(Array *p_array, void (*func)(void *, void *), void *p_void) { if (!p_array) return; - for (int i = 0; i < array_get_size(p_array); ++i) + for (int i = 0; i < array_get_used(p_array); ++i) (*func) (array_get(p_array, i), p_void); } @@ -300,8 +315,9 @@ arrayiterator_delete(ArrayIterator *p_arrayiterator) { _Bool arrayiterator_has_next(ArrayIterator *p_arrayiterator) { + //printf("[%d]", p_arrayiterator->p_array->i_used); return (p_arrayiterator->i_cur_pos < - array_get_size(p_arrayiterator->p_array)); + array_get_used(p_arrayiterator->p_array)); } void* diff --git a/src/data/array.h b/src/data/array.h index a6648bf..eb935e9 100644 --- a/src/data/array.h +++ b/src/data/array.h @@ -41,6 +41,7 @@ #include "../defines.h" #define array_get_size(a) a->i_size +#define array_get_used(a) a->i_used #define array_empty(a) a->i_size == 0 #define array_clear(a) array_resize(a, 0) #define array_get_first(a) array_get(a, 0) @@ -52,6 +53,7 @@ typedef struct { typedef struct { ArrayElement **pp_ae; + int i_used; int i_size; } Array; @@ -79,6 +81,7 @@ void array_unshift(Array *p_array, void *p_void); void array_iterate(Array *p_array, void (*func)(void *)); void array_iterate2(Array *p_array, void (*func)(void *, void *), void *p_void); +void array_set_used(Array *p_array, int i_used); ArrayElement *arrayelement_new(void *p_val); void arrayelement_delete(ArrayElement *p_ae); diff --git a/src/data/stack.c b/src/data/stack.c index f1da22a..00d5d88 100644 --- a/src/data/stack.c +++ b/src/data/stack.c @@ -93,6 +93,14 @@ stack_pop(Stack *p_stack) { return (p_val); } +void* +stack_top(Stack *p_stack) { + if (stack_empty(p_stack)) + return (NULL); + + return (p_stack->p_first->p_val); +} + void stack_clear(Stack *p_stack) { for (;!stack_empty(p_stack); stack_pop(p_stack)); diff --git a/src/data/stack.h b/src/data/stack.h index 6d42d87..4468bbe 100644 --- a/src/data/stack.h +++ b/src/data/stack.h @@ -35,8 +35,6 @@ #ifndef STACK_H #define STACK_H -#define stack_top(s) s->p_first->p_val; - #include <stdlib.h> typedef struct StackElem_ { @@ -65,6 +63,7 @@ void stack_iterate_level(Stack *p_stack, void (*func)(void *p_void, int i_level)); void stack_push(Stack *p_stack, void *p_val); void *stack_pop(Stack *p_stack); +void *stack_top(Stack *p_stack); void stack_clear(Stack *p_stack); void stack_delete(Stack *p_stack); void stack_delete_and_free(Stack *p_stack); diff --git a/src/defines.h b/src/defines.h index 19c0179..fdcd701 100644 --- a/src/defines.h +++ b/src/defines.h @@ -66,6 +66,7 @@ #define true (_Bool)1 #endif +#define UNLESS(x) if (!x) // Makes the compiler always happy (end of switch statements) :) #define NO_DEFAULT default: if (0) @@ -64,6 +64,7 @@ array_push ./src/data/array.c /^array_push(Array *p_array, void *p_void) {$/ array_remove ./src/data/array.c /^array_remove(Array *p_array, int i_index) {$/ array_resize ./src/data/array.c /^array_resize(Array *p_array, int i_size) {$/ array_set ./src/data/array.c /^array_set(Array *p_array, int i_index, void *p_val/ +array_set_used ./src/data/array.c /^array_set_used(Array *p_array, int i_used) {$/ array_splice ./src/data/array.c /^array_splice(Array *p_array, int i_index, Array *p/ array_unshift ./src/data/array.c /^array_unshift(Array *p_array, void *p_void) {$/ arrayelement_delete ./src/data/array.c /^arrayelement_delete(ArrayElement *p_ae) {$/ @@ -259,6 +260,7 @@ stack_new ./src/data/stack.c /^stack_new() {$/ stack_pop ./src/data/stack.c /^stack_pop(Stack *p_stack) {$/ stack_push ./src/data/stack.c /^stack_push(Stack *p_stack, void *p_val) {$/ stack_size ./src/data/stack.c /^stack_size(Stack *p_stack) {$/ +stack_top ./src/data/stack.c /^stack_top(Stack *p_stack) {$/ stackelem_new ./src/data/stack.c /^stackelem_new() {$/ stackiterator_delete ./src/data/stack.c /^stackiterator_delete(StackIterator *p_iter) {$/ stackiterator_has_next ./src/data/stack.c /^stackiterator_has_next(StackIterator *p_iter) {$/ diff --git a/tmp/test.fy b/tmp/test.fy index e91b86d..84dda3a 100644 --- a/tmp/test.fy +++ b/tmp/test.fy @@ -2,5 +2,4 @@ * Examples of how to use arrays *# -my foo = 1, bar = 2; -my baz = [foo, bar]; +say ["string"]; diff --git a/tmp/test.out b/tmp/test.out index e91b86d..dddf01d 100644 --- a/tmp/test.out +++ b/tmp/test.out @@ -2,5 +2,56 @@ * Examples of how to use arrays *# -my foo = 1, bar = 2; -my baz = [foo, bar]; +say ["string"]; +Token (id=00000, line=00005, pos=0004, type=TT_IDENT, val=say, ival=0, dval=0.000000, refs=1) +Token (id=00001, line=00005, pos=0006, type=TT_PARANT_AL, val=[, ival=0, dval=0.000000, refs=1) +Token (id=00002, line=00005, pos=0006, type=TT_STRING, val=string, ival=0, dval=0.000000, refs=1) +Token (id=00003, line=00005, pos=0008, type=TT_PARANT_AR, val=], ival=0, dval=0.000000, refs=1) +Token (id=00004, line=00005, pos=0009, type=TT_SEMICOLON, val=;, ival=0, dval=0.000000, refs=1) +DEBUG(Track: ./src/core/interpret.c:_program:167) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_statement:432) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_proc_decl:364) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_func_decl:398) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_var_decl:177) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_control:492) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_expression:467) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_compare:627) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_sum:678) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_product:729) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_product2:767) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_term:801) +DEBUG(Token: say) +DEBUG(Track: ./src/core/interpret.c:_compare:627) +DEBUG(Token: [) +DEBUG(Track: ./src/core/interpret.c:_sum:678) +DEBUG(Token: [) +DEBUG(Track: ./src/core/interpret.c:_product:729) +DEBUG(Token: [) +DEBUG(Track: ./src/core/interpret.c:_product2:767) +DEBUG(Token: [) +DEBUG(Track: ./src/core/interpret.c:_term:801) +DEBUG(Token: [) +DEBUG(Track: ./src/core/interpret.c:_compare:627) +DEBUG(Token: string) +DEBUG(Track: ./src/core/interpret.c:_sum:678) +DEBUG(Token: string) +DEBUG(Track: ./src/core/interpret.c:_product:729) +DEBUG(Token: string) +DEBUG(Track: ./src/core/interpret.c:_product2:767) +DEBUG(Token: string) +DEBUG(Track: ./src/core/interpret.c:_term:801) +DEBUG(Token: string) +==> + +==> |
