From a90467d4be3bcf91cab299b4521bf5f762abb1d5 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 9 May 2010 09:30:29 +0000 Subject: added the scheme branch --- src/core/frame.c | 152 +++++++++++++++++++++ src/core/frame.h | 68 +++++++++ src/core/interpret.c | 379 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/interpret.h | 58 ++++++++ src/core/lambda.c | 98 +++++++++++++ src/core/lambda.h | 55 ++++++++ src/core/promise.c | 60 ++++++++ src/core/promise.h | 49 +++++++ src/core/scanner.c | 297 ++++++++++++++++++++++++++++++++++++++++ src/core/scanner.h | 69 ++++++++++ src/core/token.c | 135 ++++++++++++++++++ src/core/token.h | 75 ++++++++++ src/core/tools.c | 64 +++++++++ src/core/tools.h | 42 ++++++ src/core/variable.c | 59 ++++++++ src/core/variable.h | 52 +++++++ 16 files changed, 1712 insertions(+) create mode 100644 src/core/frame.c create mode 100644 src/core/frame.h create mode 100644 src/core/interpret.c create mode 100644 src/core/interpret.h create mode 100644 src/core/lambda.c create mode 100644 src/core/lambda.h create mode 100644 src/core/promise.c create mode 100644 src/core/promise.h create mode 100644 src/core/scanner.c create mode 100644 src/core/scanner.h create mode 100644 src/core/token.c create mode 100644 src/core/token.h create mode 100644 src/core/tools.c create mode 100644 src/core/tools.h create mode 100644 src/core/variable.c create mode 100644 src/core/variable.h (limited to 'src/core') 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 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 PROMISE_H +#define PROMISE_H + +#include "../data/list.h" +#include "token.h" + +typedef struct { + ListElem *p_elem_start; + Token *p_token_lambda; +} Promise; + +Promise* promise_new(Token *p_token_lambda, ListElem *p_elem_start); +void promise_delete(Promise *p_promise); + +#endif /* PROMISE_H */ diff --git a/src/core/scanner.c b/src/core/scanner.c new file mode 100644 index 0000000..a2b2b10 --- /dev/null +++ b/src/core/scanner.c @@ -0,0 +1,297 @@ +/*:* + *: File: ./src/core/scanner.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 "scanner.h" + +#include +#include + +const char _TOKEN_ENDS[] = "()"; +int _CODESTR_INDEX = 0; + +Scanner* +scanner_new(List *p_list_token, Tupel *p_tupel_argv) { + Scanner *p_scanner = malloc(sizeof(Scanner)); + + Dat *p_dat_string = p_tupel_argv->a; + + if (dat_empty(p_dat_string)) + ERROR("No source given"); + + if (argv_checkopts("e", p_tupel_argv)) { + p_scanner->c_codestring = dat_pop(p_dat_string); + p_scanner->c_filename = NULL; + p_scanner->fp = NULL; + + } else { + p_scanner->c_codestring = NULL; + p_scanner->c_filename = dat_pop(p_dat_string); + p_scanner->fp = fopen(p_scanner->c_filename, "r"); + + if (!p_scanner->fp) + ERROR("Could not open '%s' for reading!", p_scanner->c_filename); + } + + p_scanner->p_list_token = p_list_token; + + p_scanner->i_current_line_nr = 1; + p_scanner->i_current_pos_nr = 0; + + p_scanner->i_num_tokenends = strlen(_TOKEN_ENDS); + p_scanner->tt_last = TT_IDENT; + + return p_scanner; +} + +void +scanner_delete(Scanner *p_scanner) { + if (p_scanner->fp) + fclose(p_scanner->fp); + free(p_scanner); +} + +_Bool +_scanner_has_next_char(Scanner *p_scanner) { + if (p_scanner->fp) + return !feof(p_scanner->fp); + + return p_scanner->c_codestring[_CODESTR_INDEX] != 0; +} + +char +_scanner_get_next_char(Scanner *p_scanner) { + if (p_scanner->fp) + return fgetc(p_scanner->fp); + + return (p_scanner->c_codestring[_CODESTR_INDEX++]); +} + +void +scanner_run(PBSc *p_fype) { + Scanner *p_scanner = scanner_new(p_fype->p_list_token, + p_fype->p_tupel_argv); + + int i_token_len = 0; + char *c_token = malloc(sizeof(char)); + + c_token[0] = 0; + + while ( _scanner_has_next_char(p_scanner) ) { + char c = _scanner_get_next_char(p_scanner); + ++p_scanner->i_current_pos_nr; + + switch (c) { + case '#': + { + c = _scanner_get_next_char(p_scanner); + ++p_scanner->i_current_pos_nr; + _Bool b_multi_comment = c == '*'; + + do { + c = _scanner_get_next_char(p_scanner); + ++p_scanner->i_current_pos_nr; + if (c == '\n') { + ++p_scanner->i_current_line_nr; + p_scanner->i_current_pos_nr = 0; + + if (!b_multi_comment) + break; + + } else if (b_multi_comment && c == '*' && + _scanner_has_next_char(p_scanner)) { + if ( (c = _scanner_get_next_char(p_scanner)) == '#') + break; + + else if (c == '\n') + ++p_scanner->i_current_line_nr; + } + + } while ( _scanner_has_next_char(p_scanner) ); + } + + break; + + case '"': + if (i_token_len) { + TokenType tt_cur = scanner_get_tt_cur(c_token); + scanner_add_token(p_scanner, &c_token, &i_token_len, tt_cur); + } + { + int i_num_nl = 0; + do { + c = _scanner_get_next_char(p_scanner); + if ( c == '\n' ) { + ++i_num_nl; + p_scanner->i_current_pos_nr = 0; + + i_token_len += 2; + c_token = realloc(c_token, sizeof(char) * (i_token_len + 1)); + c_token[i_token_len-2] = '\\'; + c_token[i_token_len-1] = 'n'; + c_token[i_token_len] = 0; + + } else if (c == '"') { + if (i_token_len && c_token[i_token_len-1] == '\\') { + c_token[i_token_len-1] = '"'; + + } else { + break; + } + + } else { + ++i_token_len; + c_token = realloc(c_token, sizeof(char) * (i_token_len + 1)); + c_token[i_token_len-1] = c; + c_token[i_token_len] = 0; + } + + } while ( _scanner_has_next_char(p_scanner) ); + + scanner_add_token(p_scanner, &c_token, &i_token_len, TT_STRING); + + if (i_num_nl) + p_scanner->i_current_line_nr += i_num_nl; + } + + break; + + case '\n': + case '\t': + case ' ': + if (i_token_len) { + TokenType tt_cur = scanner_get_tt_cur(c_token); + scanner_add_token(p_scanner, &c_token, &i_token_len, tt_cur); + } + + if (c == '\n') { + ++p_scanner->i_current_line_nr; + p_scanner->i_current_pos_nr = 0; + } + + break; + + case '(': + case '.': + { + if (i_token_len) { + TokenType tt_cur = scanner_get_tt_cur(c_token); + scanner_add_token(p_scanner, &c_token, &i_token_len, tt_cur); + } + ++i_token_len; + c_token = realloc(c_token, sizeof(char) * i_token_len + 1); + c_token[i_token_len-1] = c; + c_token[i_token_len] = 0; + TokenType tt_cur = scanner_get_tt_cur(c_token); + scanner_add_token(p_scanner, &c_token, &i_token_len, tt_cur); + } + break; + + default: + if (i_token_len) { + TokenType tt_cur = scanner_get_tt_cur(c_token); + + for (int i = 0; i < p_scanner->i_num_tokenends; ++i) { + if (_TOKEN_ENDS[i] == c) { + scanner_add_token(p_scanner, &c_token, + &i_token_len, tt_cur); + break; + } + } + } + + ++i_token_len; + c_token = realloc(c_token, sizeof(char) * i_token_len + 1); + c_token[i_token_len-1] = c; + c_token[i_token_len] = 0; + } + } + + if (argv_checkopts("e", p_fype->p_tupel_argv) && i_token_len) { + TokenType tt_cur = scanner_get_tt_cur(c_token); + scanner_add_token(p_scanner, &c_token, &i_token_len, tt_cur); + } + + char *c_filename = scanner_get_filename(p_scanner); + scanner_delete(p_scanner); + + if (argv_checkopts("TV", p_fype->p_tupel_argv)) + list_iterate(p_fype->p_list_token, token_print_cb); + + char *c_basename = NULL; + if (c_filename) { + int i_len = strlen(c_filename) - 3; + c_basename = calloc(i_len+1, sizeof(char)); + strncpy(c_basename, c_filename, i_len); + c_basename[i_len] = 0; + + } else { + char *c_basename = calloc(1, sizeof(char)); + c_basename[0] = 0; + } + + p_fype->c_basename = c_basename; +} + +void +scanner_add_token(Scanner *p_scanner, char **cc_token, int *p_token_len, + TokenType tt_cur) { + + 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); + + list_add_back(p_list_token, p_token); + + *cc_token = malloc(sizeof(char)); + (*cc_token)[0] = 0; + *p_token_len = 0; + + p_scanner->tt_last = tt_cur; +} + +TokenType +scanner_get_tt_cur(char *c_token) { + if (isdigit(c_token[0])) + return TT_INTEGER; + + return (get_tt(c_token)); +} + +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 new file mode 100644 index 0000000..b15707b --- /dev/null +++ b/src/core/scanner.h @@ -0,0 +1,69 @@ +/*:* + *: File: ./src/core/scanner.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 SCANNER_H +#define SCANNER_H + +#include +#include + +#include "token.h" +#include "../fype.h" +#include "../data/dat.h" + +#define scanner_get_list_token(s) s->p_list_token +#define scanner_get_fp(s) s->fp +#define scanner_get_filename(s) s->c_filename +#define scanner_get_codestring(s) s->c_codestring + +typedef struct { + int i_current_line_nr; + int i_current_pos_nr; + int i_num_tokenends; + char *c_filename; + char *c_codestring; + FILE *fp; + List *p_list_token; + TokenType tt_last; +} Scanner; + +void scanner_run(PBSc *p_fype); +Scanner *scanner_new(List *p_list_token, Tupel *p_tupel_argv); +void scanner_delete(Scanner *p_scanner); +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/token.c b/src/core/token.c new file mode 100644 index 0000000..a0a6ad0 --- /dev/null +++ b/src/core/token.c @@ -0,0 +1,135 @@ +/*:* + *: File: ./src/core/token.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 "token.h" + +#define CHECK(...) if (!strcmp(c_token, __VA_ARGS__)) return +#define CASE(t,r) case t: return r; + +long TOKEN_ID_COUNTER = 0; + +TokenType +get_tt(char *c_token) { + CHECK("(") TT_PARANT_L; + CHECK(")") TT_PARANT_R; + CHECK("'") TT_SQUOTE; + CHECK(".") TT_DOT; + + return TT_IDENT; +} + +char* +tt_get_name(TokenType tt_cur) { + switch (tt_cur) { + CASE(TT_PARANT_L,"TT_PARANT_L") + CASE(TT_PARANT_R,"TT_PARANT_R") + CASE(TT_INTEGER,"TT_INTEGER") + CASE(TT_DOUBLE,"TT_DOUBLE") + CASE(TT_STRING,"TT_STRING") + CASE(TT_DOT,"TT_DOT") + CASE(TT_SQUOTE,"TT_SQUOTE") + CASE(TT_IDENT,"TT_IDENT") + } + + return "TT_IDENT"; +} + +Token* +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; + p_token->tt_cur = tt_cur; + p_token->i_line_nr = i_line_nr; + p_token->i_pos_nr = i_pos_nr; + p_token->c_filename = c_filename; + + return (p_token); +} + +Token* +token_new_dummy() { + Token *p_token = malloc(sizeof(Token)); + + p_token->c_val = NULL; + p_token->tt_cur = TT_IDENT; + p_token->i_line_nr = -1; + p_token->i_pos_nr = -1; + p_token->c_filename = NULL; + p_token->u_token_id = TOKEN_ID_COUNTER++; + + return (p_token); +} + +void +token_delete_cb(void *p_void) { + token_delete(p_void); +} + +void +token_delete(Token *p_token) { +} + +_Bool +token_is(Token *p_token, char *c_str) { + if (p_token->tt_cur == TT_IDENT && strcmp(p_token->c_val, c_str) == 0) + return true; + + return false; +} + +void +token_print(Token *p_token) { + printf("(id=%05u, line=%05d, pos=%04d, type=%s, val=%s)", + p_token->u_token_id, + p_token->i_line_nr, + p_token->i_pos_nr, + tt_get_name(p_token->tt_cur), + p_token->c_val); +} + +void +token_print_ln(Token *p_token) { + token_print(p_token); + printf("\n"); +} + +void +token_print_cb(void *p_void) { + Token *p_token = p_void; + printf("Token "); + token_print(p_token); + printf("\n"); +} diff --git a/src/core/token.h b/src/core/token.h new file mode 100644 index 0000000..8f1ab53 --- /dev/null +++ b/src/core/token.h @@ -0,0 +1,75 @@ +/*:* + *: File: ./src/core/token.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 TOKEN_H +#define TOKEN_H + +#include "../defines.h" +#include "../data/array.h" + +typedef enum { + TT_IDENT, + TT_PARANT_L, + TT_PARANT_R, + TT_INTEGER, + TT_DOUBLE, + TT_STRING, + TT_SQUOTE, + TT_DOT +} TokenType; + +typedef struct { + TokenType tt_cur; + char *c_val; + int i_line_nr; + int i_pos_nr; + char *c_filename; + unsigned int u_token_id; +} Token; + +Token* token_new(char *c_val, TokenType tt_cur, int i_line_nr, + int i_pos_nr, char *c_filename); +Token* token_new_dummy(); +void token_delete(Token *p_token); +void token_delete_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); +_Bool token_is(Token *p_token, char *c_str); +TokenType get_tt(char *c_token); + +#endif diff --git a/src/core/tools.c b/src/core/tools.c new file mode 100644 index 0000000..6b6dbab --- /dev/null +++ b/src/core/tools.c @@ -0,0 +1,64 @@ +/*:* + *: File: ./src/core/tools.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 "tools.h" +#include "token.h" +#include "../defines.h" + +void +tool_skip_block(ListIterator *p_iter, int i_offset) { + Token *p_token = listiterator_current(p_iter); + + do { + if (p_token->tt_cur == TT_PARANT_R && --i_offset == 0) { + //printf("::DoneSkip<%d>: %s\n", i_offset, p_token->c_val); + return; + + } else if (p_token->tt_cur == TT_PARANT_L) { + ++i_offset; + + } else if (i_offset < 0) { + ERROR_INTERPRET("Fatal Error", p_token); + } + + //printf("::Skipping<%d>: %s\n", i_offset, p_token->c_val); + p_token = listiterator_next(p_iter); + } while (listiterator_has_next(p_iter)); + + if (p_token->tt_cur == TT_PARANT_R) + return; + + ERROR_EOB; +} + diff --git a/src/core/tools.h b/src/core/tools.h new file mode 100644 index 0000000..19d9fa6 --- /dev/null +++ b/src/core/tools.h @@ -0,0 +1,42 @@ +/*:* + *: File: ./src/core/tools.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 TOOLS_H +#define TOOLS_H + +#include "../fype.h" + +void tool_skip_block(ListIterator *p_iter, int i_offset); + +#endif /* TOOLS_H */ diff --git a/src/core/variable.c b/src/core/variable.c new file mode 100644 index 0000000..2b924a5 --- /dev/null +++ b/src/core/variable.c @@ -0,0 +1,59 @@ +/*:* + *: File: ./src/core/variable.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 "variable.h" +#include "token.h" + +Variable* +variable_new(char *c_name, Token *p_token,Frame *p_frame) { + Variable *p_variable = malloc(sizeof(Variable)); + + p_variable->c_name = c_name; + p_variable->p_token = p_token; + p_variable->p_frame = p_frame; + + return (p_variable); +} + +void +variable_delete(Variable *p_variable) { + free(p_variable); +} + +void +variable_print(Variable *p_variable) { + printf("+ST_VARIABLE(name=%s,value=%s)\n", + p_variable->c_name, + p_variable->p_token->c_val); +} diff --git a/src/core/variable.h b/src/core/variable.h new file mode 100644 index 0000000..7a7d27c --- /dev/null +++ b/src/core/variable.h @@ -0,0 +1,52 @@ +/*:* + *: File: ./src/core/variable.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 VARIABLE_H +#define VARIABLE_H + +#include "../defines.h" +#include "frame.h" +#include "token.h" + +typedef struct _Variable { + char *c_name; + Token *p_token; + Frame *p_frame; +} Variable; + +Variable* variable_new(char *c_name, Token *p_token,Frame *p_frame); +void variable_delete(Variable *p_variable); +void variable_print(Variable *p_variable); + +#endif -- cgit v1.2.3