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/core | |
added the scheme branch
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/frame.c | 152 | ||||
| -rw-r--r-- | src/core/frame.h | 68 | ||||
| -rw-r--r-- | src/core/interpret.c | 379 | ||||
| -rw-r--r-- | src/core/interpret.h | 58 | ||||
| -rw-r--r-- | src/core/lambda.c | 98 | ||||
| -rw-r--r-- | src/core/lambda.h | 55 | ||||
| -rw-r--r-- | src/core/promise.c | 60 | ||||
| -rw-r--r-- | src/core/promise.h | 49 | ||||
| -rw-r--r-- | src/core/scanner.c | 297 | ||||
| -rw-r--r-- | src/core/scanner.h | 69 | ||||
| -rw-r--r-- | src/core/token.c | 135 | ||||
| -rw-r--r-- | src/core/token.h | 75 | ||||
| -rw-r--r-- | src/core/tools.c | 64 | ||||
| -rw-r--r-- | src/core/tools.h | 42 | ||||
| -rw-r--r-- | src/core/variable.c | 59 | ||||
| -rw-r--r-- | src/core/variable.h | 52 |
16 files changed, 1712 insertions, 0 deletions
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"); + tool_skip_block(p_iter, 1 ); + ListElem *p_listelem_end = listiterator_current_elem(p_iter); + Lambda *p_lambda_ = lambda_new( + c_name, + NULL, + p_listelem, + p_listelem_end, + p_frame_local); + if (!frame_add_symbol(p_frame_local, + c_name, + ST_LAMBDA, + p_lambda_)) { + printf("Illegal reuse of parameter '%s' @ function '%s'", + c_name, p_lambda->c_name); + ERROR_INTERPRET(". Error binding local lambda", p_token); + } + } + break; + case TT_PARANT_R: + ERROR_INTERPRET("Didn't expect ) here", p_token); + break; + default: { + Variable *p_variable = variable_new(c_name, + p_token, + p_frame_local); + if (!frame_add_symbol(p_frame_local, + c_name, + ST_VARIABLE, + p_variable)) { + printf("Illegal reuse of parameter '%s' @ function '%s'\n", + c_name, p_lambda->c_name); + frame_print(p_frame_local); + ERROR_INTERPRET("Fatal", p_token); + } + } + break; + } + } + + listiterator_delete(p_iter_args); + } + + if (!listiterator_has_next(p_iter)) + ERROR_EOB; + + _eval(p_inter_local); + interpret_delete(p_inter_local); +} + +void +_eval_symbol(Interpret *p_inter, Symbol *p_symbol, ListIterator *p_iter) { + switch (p_symbol->st) { + case ST_LAMBDA: { + _eval_lambda(p_inter, p_symbol->p_val, p_iter); + //printf("::EVAL_LAMBDA_END\n"); + } + break; + case ST_VARIABLE: + break; + } +} + +Token* +_parant(Interpret *p_inter, Token *p_token) { + //printf("::PARANT<%d>: %s\n", p_inter->i_pcount, p_token->c_val); + if (p_token->tt_cur == TT_PARANT_L) { + ++p_inter->i_pcount; + + } else if (p_token->tt_cur == TT_PARANT_R) { + if (--p_inter->i_pcount == 0) + return (NULL); + + else if (p_inter->i_pcount < 0) + ERROR_INTERPRET("Too many closing )'s", + p_token); + } + + return (p_token); +} + +void +_run_func(Interpret *p_inter, Token *p_token, ListIterator *p_iter) { + Frame *p_frame = p_inter->p_frame; + Symbol *p_symbol = NULL; + + if (listiterator_has_next(p_iter)) { + if (! (p_token = _parant(p_inter, listiterator_next(p_iter))) ) + return; + + //printf("::Interpreting: %s\n", p_token->c_val); + + if (token_is(p_token, "def")) { + + _def(p_inter, p_token, p_iter); + + } else if (token_is(p_token, "say")) { + _say(p_inter, p_token, p_iter); + + } else if (token_is(p_token, "noop")) { + + } else if ( (p_symbol = frame_get_symbol(p_frame, p_token->c_val)) != NULL ) { + _eval_symbol(p_inter, p_symbol, p_iter); + + } else if (token_is(p_token, "show-frames")) { + frame_print(p_inter->p_frame); + + } else if (token_is(p_token, "beep")) { + printf("BEEP\n"); + + } else { + printf("No symbol '%s' defined @ any frame:\n", p_token->c_val); + frame_print(p_frame); + ERROR_INTERPRET("Error.", p_token); + } + } +} + +void +_eval(Interpret *p_inter) { + ListIterator *p_iter = NULL; + ListElem *p_listelem_end = NULL; + + if (!p_inter->b_is_lambda_interpretation) { + p_iter = listiterator_new(p_inter->p_list_token); + } else { + p_iter = listiterator_new_from_elem(p_inter->p_lambda->p_listelem); + p_listelem_end = p_inter->p_lambda->p_listelem_end; + } + + while (listiterator_has_next(p_iter)) { + Token *p_token = _parant(p_inter, listiterator_next(p_iter)); + if (!p_token) + break; + + if (p_listelem_end && listiterator_current_elem_equals( + p_iter, + p_listelem_end)) + break; + + switch (p_token->tt_cur) { + case TT_PARANT_L: + _run_func(p_inter, p_token, p_iter); + break; + case TT_PARANT_R: + break; + NO_DEFAULT; + } + } + listiterator_delete(p_iter); +} diff --git a/src/core/interpret.h b/src/core/interpret.h new file mode 100644 index 0000000..6d3a4a5 --- /dev/null +++ b/src/core/interpret.h @@ -0,0 +1,58 @@ +/*:* + *: File: ./src/core/interpret.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 INTERPRET_H +#define INTERPRET_H + +#include "frame.h" +#include "token.h" +#include "lambda.h" +#include "../data/list.h" +#include "../fype.h" + +typedef struct _Interpret { + List *p_list_token; + Frame *p_frame; + Token *p_token; + int i_pcount; + _Bool b_is_lambda_interpretation; + Lambda *p_lambda; +} Interpret; + +Interpret* interpret_new(List *p_list_token); +Interpret* interpret_new_lambda(Interpret *p_inter, Lambda *p_lambda); +void interpret_delete(Interpret *p_inter); +void interpret_run(PBSc *p_fype); + +#endif /* INTERPRET_H */ diff --git a/src/core/lambda.c b/src/core/lambda.c new file mode 100644 index 0000000..c6ad9a2 --- /dev/null +++ b/src/core/lambda.c @@ -0,0 +1,98 @@ +/*:* + *: File: ./src/core/lambda.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 "lambda.h" +#include "token.h" + +Lambda* +lambda_new(char *c_name, List *p_list_args, ListElem *p_listelem, ListElem *p_listelem_end, Frame *p_frame) { + Lambda *p_lambda = malloc(sizeof(Lambda)); + + p_lambda->c_name = c_name; + p_lambda->p_list_args = p_list_args; + p_lambda->p_listelem = p_listelem; + p_lambda->p_listelem_end = p_listelem_end;; + p_lambda->p_frame = p_frame; + + return (p_lambda); +} + +void +lambda_delete(Lambda *p_lambda) { + if (p_lambda->p_list_args) + list_delete(p_lambda->p_list_args); + free(p_lambda); +} + +void +lambda_print(Lambda *p_lambda) { + printf("+ST_LAMBDA(name=%s;args=", p_lambda->c_name); + + if (p_lambda->p_list_args) { + unsigned i_count = p_lambda->p_list_args->i_size; + ListIterator *p_iter = listiterator_new(p_lambda->p_list_args); + + while (listiterator_has_next(p_iter)) { + char *c_name = listiterator_next(p_iter); + if (--i_count == 0) + printf("%s", c_name); + else + printf("%s ", c_name); + } + + listiterator_delete(p_iter); + } + printf(")\n( "); + + ListIterator *p_iter = listiterator_new_from_elem(p_lambda->p_listelem); + Token *p_token = listiterator_current(p_iter); + ListElem *p_listelem_end = p_lambda->p_listelem_end; + + + while (listiterator_has_next(p_iter)) { + p_token = listiterator_next(p_iter); + printf("%s ", p_token->c_val); + + if (listiterator_current_elem_equals(p_iter, p_listelem_end)) + goto LAMBDA_PRINT_END; + } + + ERROR_EOB; + +LAMBDA_PRINT_END: + + printf("\n"); + listiterator_delete(p_iter); +} + diff --git a/src/core/lambda.h b/src/core/lambda.h new file mode 100644 index 0000000..8377c30 --- /dev/null +++ b/src/core/lambda.h @@ -0,0 +1,55 @@ +/*:* + *: File: ./src/core/lambda.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 LAMBDA_H +#define LAMBDA_H + +#include "../defines.h" +#include "../data/list.h" +#include "frame.h" + +typedef struct _Lambda { + char *c_name; + List *p_list_args; + ListElem *p_listelem; // Body code starts here + ListElem *p_listelem_end; // Body code ends here + Frame *p_frame; +} Lambda; + +Lambda* lambda_new(char *c_name, List *p_list_args, ListElem *p_listelem, ListElem *p_listelem_end, Frame *p_frame); +void lambda_delete(Lambda *p_lambda); +void lambda_print(Lambda *p_lambda); +//void lambda_eval(Lambda *p_pambda, ListIterator *p_iter); + +#endif diff --git a/src/core/promise.c b/src/core/promise.c new file mode 100644 index 0000000..fa50e78 --- /dev/null +++ b/src/core/promise.c @@ -0,0 +1,60 @@ +/*:* + *: File: ./src/core/promise.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 "promise.h" + +#define _ERROR(m,t) \ + ERROR(\ + "%s: Promise error in %s line %d pos %d near '%s'", m, \ + t->c_filename, \ + t->i_line_nr, \ + t->i_pos_nr, \ + t->c_val \ + ) + +Promise* +promise_new(Token *p_token_lambda, ListElem *p_elem_start) { + Promise *p_promise = malloc(sizeof(Promise)); + + p_promise->p_token_lambda = p_token_lambda; + p_promise->p_elem_start = p_elem_start; + + return (p_promise); +} + +void +promise_delete(Promise *p_promise) { + free(p_promise); +} + diff --git a/src/core/promise.h b/src/core/promise.h new file mode 100644 index 0000000..76cd162 --- /dev/null +++ b/src/core/promise.h @@ -0,0 +1,49 @@ +/*:* + *: File: ./src/core/promise.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 L |
