diff options
| author | Paul Buetow <paul@buetow.org> | 2010-05-09 09:30:29 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2010-05-09 09:30:29 +0000 |
| commit | a90467d4be3bcf91cab299b4521bf5f762abb1d5 (patch) | |
| tree | 5171d406e6be467807a914ce42923ac997d74858 /src | |
added the scheme branch
Diffstat (limited to 'src')
44 files changed, 5740 insertions, 0 deletions
diff --git a/src/argv.c b/src/argv.c new file mode 100644 index 0000000..dc938d9 --- /dev/null +++ b/src/argv.c @@ -0,0 +1,181 @@ +/*:* + *: File: ./src/argv.c + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#include "argv.h" + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include "defines.h" +#include "data/dat.h" + +#define ARGV_OSLEN 128 + +char *BINARY; + +void +argv_run(PBSc *p_fype, int i_argc, char **pc_argv) { + Dat *p_dat_string = dat_new(); + + BINARY = pc_argv[0]; + char *c_tmp = STR_NEW(ARGV_OSLEN); + c_tmp[0] = '\0'; + + p_fype->p_tupel_argv->a = p_dat_string; + p_fype->p_tupel_argv->b = c_tmp; + + if (i_argc <= 1) { + argv_synopsis(p_fype->p_tupel_argv); + exit(1); + } + + for (int i = 1; i < i_argc; ++i) { + if (pc_argv[i][0] != '-' && pc_argv[i][0] != '/') { + dat_push(p_dat_string, pc_argv[i]); + continue; + } + + int i_len = strlen(pc_argv[i]); + unsigned i_argc_left = i_argc - i + dat_size(p_dat_string); + + for (int j = 1; j < i_len; ++j) + argv_switch(pc_argv[i][j], p_fype->p_tupel_argv, i_argc_left); + } +} + +void +argv_switch(char c_arg, Tupel *p_tupel_argv, unsigned i_argc_left) { + switch (c_arg) { + case 'e': + case 'T': + case 'V': + argv_addopt(c_arg, p_tupel_argv); + return; + case 'h': + argv_help(); + exit(0); + case 's': + argv_synopsis(p_tupel_argv); + exit(0); + case 'v': + printf("%s %s %d\n", NAME, VERSION, BUILDNR); + exit(0); + } + + char buf[2]; + buf[0] = c_arg; + buf[1] = '\0'; + + EPRINTF("Error: No such option '%s'!\n", buf); + exit(1); +} + +void +argv_synopsis(Tupel *p_tupel_argv) { + printf("Synopsis:\n"); + printf("%s ", BINARY); + printf("[-[hsvTV]] file.fy\n"); + printf("%s ", BINARY); + printf("-e \"code string;\"\n"); + + if (!argv_checkopt('h', p_tupel_argv)) + printf("\t(Hint: Try -h for details)\n"); +} + +void +argv_help() { + printf("%s %s %d\n", NAME, VERSION, BUILDNR); + printf("%s\n", COPYRIGHT); + printf("\t-e\tExecutes given code string (see synopses)\n"); + printf("\t-h\tPrints this help\n"); + printf("\t-s\tPrints the synopsis\n"); + printf("\t-v\tPrints the current version\n"); + printf("\t-T\tPrints token list after scanning\n"); + printf("\t-V\tVerbose mode: Print all possible output\n"); + exit(0); +} + +void +argv_addopt(char c_opt, Tupel *p_tupel_argv) { + char *c_opts = (char *) p_tupel_argv->b; + int i_len = strlen(c_opts), i; + + for (i = 0; i < i_len; ++i) + if (c_opts[i] == c_opt) + return; + + c_opts[i] = c_opt; + c_opts[i+1] = '\0'; +} + +_Bool +argv_checkopt(char c_opt, Tupel *p_tupel_argv) { + char *c_opts = (char *) p_tupel_argv->b; + int i_len = strlen(c_opts); + + for (int i = 0; i < i_len; ++i) + if (c_opts[i] == c_opt) + return 1; + + return 0; +} + +_Bool +argv_checkopts(char *c_opts, Tupel *p_tupel_argv) { + int i_len = strlen(c_opts); + + for (int i = 0; i < i_len; ++i) + if (argv_checkopt(c_opts[i], p_tupel_argv)) + return 1; + + return 0; +} + +void +argv_check_argc(int i_required, unsigned i_argc_left, Tupel *p_tupel_argv) { + if ((unsigned) ++i_required > i_argc_left) { + argv_synopsis(p_tupel_argv); + EPRINTF("%d argument(s) missing!\n", i_required-i_argc_left); + exit(1); + } +} + +void +argv_tupel_delete(Tupel *p_tupel_argv) { + dat_delete(p_tupel_argv->a); + free(p_tupel_argv->b); + tupel_delete(p_tupel_argv); +} + diff --git a/src/argv.h b/src/argv.h new file mode 100644 index 0000000..a7575d2 --- /dev/null +++ b/src/argv.h @@ -0,0 +1,51 @@ +/*:* + *: File: ./src/argv.h + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#ifndef ARGV_H +#define ARGV_H + +#include "data/tupel.h" +#include "fype.h" + +void argv_run(PBSc *p_fype, int i_argc, char **pc_argv); +void argv_switch(char c_arg, Tupel *p_tupel_argv, unsigned i_argc_left); +void argv_synopsis(Tupel *p_tupel_argv); +void argv_help(); +void argv_addopt(char c_opt, Tupel *p_tupel_argv); +_Bool argv_checkopt(char c_opt, Tupel *p_tupel_argv); +_Bool argv_checkopts(char *c_opts, Tupel *p_tupel_argv); +void argv_check_argc(int i_required, unsigned i_argc_left, Tupel *p_tupel_argv); +void argv_tupel_delete(Tupel *p_tupel_argv); + +#endif diff --git a/src/build.h b/src/build.h new file mode 100644 index 0000000..1e6006e --- /dev/null +++ b/src/build.h @@ -0,0 +1,41 @@ +/*:* + *: File: ./src/build.h + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#ifndef BUILD_H +#define BUILD_H + +#define BUILDNR 10388 +#define OS_LINUX + +#endif diff --git a/src/core/frame.c b/src/core/frame.c new file mode 100644 index 0000000..9143a0d --- /dev/null +++ b/src/core/frame.c @@ -0,0 +1,152 @@ +/*:* + *: File: ./src/core/frame.c + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#include "frame.h" +#include "lambda.h" +#include "variable.h" + +unsigned _I_FRAME_ID_COUNT = 0; + +Symbol* +symbol_new(SymbolType st, void *p_val) { + Symbol *p_symbol = malloc(sizeof(Symbol)); + + p_symbol->st = st; + p_symbol->p_val = p_val; + + return (p_symbol); +} + +void +symbol_delete(Symbol *p_symbol) { + switch (p_symbol->st) { + case ST_LAMBDA: + lambda_delete(p_symbol->p_val); + break; + case ST_VARIABLE: + variable_delete(p_symbol->p_val); + break; + } + + free(p_symbol); +} + +void +symbol_delete_cb(void *p_symbol) { + symbol_delete(p_symbol); +} + +char* +symbol_get_type_name(Symbol *p_symbol) { + switch (p_symbol->st) { + case ST_LAMBDA: + return ("ST_LAMBDA"); + case ST_VARIABLE: + return ("ST_VARIABLE"); + } + + return ("ST_UNKNOWN"); +} + +Frame* +frame_new(Frame *p_parent_frame) { + Frame *p_frame = malloc(sizeof(Frame)); + + p_frame->p_parent_frame = p_parent_frame; + p_frame->p_hash_symbols = hash_new(8); + p_frame->i_frame_id = _I_FRAME_ID_COUNT++; + + return (p_frame); +} + +void +frame_delete(Frame *p_frame) { + Hash *p_hash_symbols = p_frame->p_hash_symbols; + + hash_iterate(p_hash_symbols, symbol_delete_cb); + hash_delete(p_hash_symbols); +} + +_Bool +frame_add_symbol(Frame *p_frame, char *c_name, SymbolType st, void *p_val) { + Hash *p_hash_symbols = p_frame->p_hash_symbols; + + if (hash_key_exists(p_hash_symbols, c_name)) + return (false); + + + Symbol *p_symbol = symbol_new(st, p_val); + hash_insert(p_hash_symbols, c_name, p_symbol); + + return (true); +} + +Symbol* +frame_get_symbol(Frame *p_frame, char *c_name) { + void *p_val = hash_get(p_frame->p_hash_symbols, c_name); + + if (!p_val && p_frame->p_parent_frame) + return (frame_get_symbol(p_frame->p_parent_frame, c_name)); + + return (p_val); +} + +void +_symbol_print_cb(void *p_val, char *c_name) { + Symbol *p_symbol = p_val; + switch (p_symbol->st) { + case ST_LAMBDA: + lambda_print(p_symbol->p_val); + break; + case ST_VARIABLE: + variable_print(p_symbol->p_val); + break; + } +} + +void +_frame_print(Frame *p_frame, int i_frame_nr) { + printf("FRAME(id=%u) %d:\n", p_frame->i_frame_id, i_frame_nr); + + Hash *p_hash_symbols = p_frame->p_hash_symbols; + hash_iterate_key(p_hash_symbols, _symbol_print_cb); + + if (p_frame->p_parent_frame) + _frame_print(p_frame->p_parent_frame, i_frame_nr + 1); +} + +void +frame_print(Frame *p_frame) { + _frame_print(p_frame, 0); +} diff --git a/src/core/frame.h b/src/core/frame.h new file mode 100644 index 0000000..d1e01f5 --- /dev/null +++ b/src/core/frame.h @@ -0,0 +1,68 @@ +/*:* + *: File: ./src/core/frame.h + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#ifndef FRAME_H +#define FRAME_H + +#include "../defines.h" +#include "../data/hash.h" + +typedef enum { + ST_LAMBDA, + ST_VARIABLE, +} SymbolType; + +typedef struct { + SymbolType st; + void *p_val; +} Symbol; + +typedef struct _Frame { + struct _Frame *p_parent_frame; + Hash *p_hash_symbols; + unsigned i_frame_id; +} Frame; + +Symbol* symbol_new(SymbolType st, void *p_val); +void symbol_delete(Symbol *p_symbol); +void symbol_delete_cb(void *p_symbol); +char* symbol_get_type_name(Symbol *p_symbol); + +Frame* frame_new(Frame *p_parent_frame); +void frame_delete(Frame *p_frame); +_Bool frame_add_symbol(Frame *p_frame, char *c_name, SymbolType st, void *p_val); +Symbol *frame_get_symbol(Frame *p_frame, char *c_name); +void frame_print(Frame *p_frame); + +#endif diff --git a/src/core/interpret.c b/src/core/interpret.c new file mode 100644 index 0000000..28239ef --- /dev/null +++ b/src/core/interpret.c @@ -0,0 +1,379 @@ +/*:* + *: File: ./src/core/interpret.c + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#include "interpret.h" +#include "promise.h" +#include "variable.h" +#include "tools.h" + +void _eval(Interpret *p_inter); + +Interpret* +interpret_new(List *p_list_token) { + Interpret *p_inter = malloc(sizeof(Interpret)); + + p_inter->p_frame = frame_new(NULL); + + p_inter->p_list_token = p_list_token; + p_inter->p_token = NULL; + + p_inter->i_pcount = 0; + + p_inter->b_is_lambda_interpretation = false; + p_inter->p_lambda = NULL; + + return (p_inter); +} + +Interpret* +interpret_new_lambda(Interpret *p_inter, Lambda *p_lambda) { + Interpret *p_inter_up = malloc(sizeof(Interpret)); + p_inter_up->p_frame = frame_new(p_lambda->p_frame); + p_inter_up->p_list_token = NULL; + p_inter_up->p_token = NULL; + p_inter_up->i_pcount = 0; + + p_inter_up->b_is_lambda_interpretation = true; + p_inter_up->p_lambda = p_lambda; + + return (p_inter_up); +} + +void +interpret_delete(Interpret *p_inter) { + frame_delete(p_inter->p_frame); + free(p_inter); +} + +void +interpret_run(PBSc *p_fype) { + Interpret *p_inter = + interpret_new(p_fype->p_list_token); + + _eval(p_inter); + + interpret_delete(p_inter); +} + +void +_def(Interpret *p_inter, Token *p_token, ListIterator *p_iter) { + Frame *p_frame = p_inter->p_frame; + _Bool b_success; + + if (!listiterator_has_next(p_iter)) + ERROR_EOB; + + p_token = listiterator_next(p_iter); + char *c_name; + + if (p_token->tt_cur == TT_IDENT) { + c_name = p_token->c_val; + + ListElem *p_listelem = listiterator_current_elem(p_iter); + + if (!listiterator_has_next(p_iter)) + ERROR_EOB; + + p_token = listiterator_next(p_iter); + + switch (p_token->tt_cur) { + case TT_PARANT_L: { + tool_skip_block(p_iter, 0); + ListElem *p_listelem_end = listiterator_current_elem(p_iter); + Lambda *p_lambda = lambda_new( + c_name, + NULL, + p_listelem, + p_listelem_end, + p_inter->p_frame); + //printf("::1\n"); + b_success = frame_add_symbol(p_frame, c_name, ST_LAMBDA, p_lambda); + } + break; + case TT_IDENT: { + Variable *p_variable = variable_new( + c_name, + p_token, + p_inter->p_frame); + b_success = frame_add_symbol(p_frame, + c_name, ST_VARIABLE, p_variable); + if (!listiterator_has_next(p_iter)) + ERROR_EOB; + Token *p_token2 = listiterator_next(p_iter); + if (p_token2->tt_cur != TT_PARANT_R) + ERROR_INTERPRET("Expected ) or (", p_token); + } + break; + default: + ERROR_INTERPRET("Expected ( or identifier", p_token); + } + + } else if (p_token->tt_cur == TT_PARANT_L) { + List *p_list_args = list_new(); + + if (!listiterator_has_next(p_iter)) + ERROR_INTERPRET("Expected identifier", p_token); + + p_token = listiterator_next(p_iter); + if (p_token->tt_cur != TT_IDENT) + ERROR_INTERPRET("Expected identifier", p_token); + + c_name = p_token->c_val; + while (listiterator_has_next(p_iter)) { + p_token = listiterator_next(p_iter); + + if (p_token->tt_cur != TT_IDENT) { + if (p_token->tt_cur != TT_PARANT_R) + ERROR_INTERPRET("Expected identifier or )", p_token); + break; + + } else { + list_add_back(p_list_args, p_token->c_val); + } + } + + ListElem *p_listelem = listiterator_next_elem(p_iter); + //printf("::2\n"); + tool_skip_block(p_iter, 2); + ListElem *p_listelem_end = listiterator_current_elem(p_iter); + + Lambda *p_lambda = lambda_new( + c_name, + p_list_args, + p_listelem, + p_listelem_end, + p_inter->p_frame); + b_success = frame_add_symbol(p_frame, c_name, ST_LAMBDA, p_lambda); + } else { + ERROR_INTERPRET("Expected identifier or (", p_token); + } + + if (!b_success) + ERROR("Forbidden to redef symbol \"%s\" @ current frame", c_name); +} + +void +_say(Interpret *p_inter, Token *p_token, ListIterator *p_iter) { + while (listiterator_has_next(p_iter)) { + Token *p_token = listiterator_next(p_iter); + switch (p_token->tt_cur) { + case TT_IDENT: + case TT_INTEGER: + case TT_STRING: + printf("%s\n", p_token->c_val); + break; + case TT_PARANT_L: + ERROR("Not yet implemented"); + break; + case TT_PARANT_R: + return; + NO_DEFAULT; + } + } +} + +void +_eval_lambda(Interpret *p_inter, Lambda *p_lambda, ListIterator *p_iter) { + Interpret *p_inter_local = interpret_new_lambda(p_inter, p_lambda); + Frame *p_frame_local = p_inter_local->p_frame; + ListIterator *p_iter_args = NULL; + + + if (p_lambda->p_list_args) + p_iter_args = listiterator_new(p_lambda->p_list_args); + + Token *p_token = listiterator_current(p_iter); + + if (p_iter_args) { + while (listiterator_has_next(p_iter_args)) { + char *c_name = listiterator_next(p_iter_args); + + if (!listiterator_has_next(p_iter)) + ERROR_EOB; + ListElem *p_listelem = listiterator_current_elem(p_iter); + p_token = listiterator_next(p_iter); + + switch (p_token->tt_cur) { + case TT_PARANT_L: { + //printf("::3\n"); |
