summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2008-05-15 23:28:07 +0000
committerPaul Buetow <paul@buetow.org>2008-05-15 23:28:07 +0000
commitbe839900419c7a74c4a46efd279d0ca16b35dc1f (patch)
tree1355c8f238d1c58ffd5cb8803bcc2adf987e79aa /src
parent33c945e58f86267b0d3bdca4c3421155e11eb0d9 (diff)
Moved stuff into trunk.
Diffstat (limited to 'src')
-rw-r--r--src/argv.c181
-rw-r--r--src/argv.h51
-rw-r--r--src/build.h41
-rw-r--r--src/core/convert.c185
-rw-r--r--src/core/convert.h53
-rw-r--r--src/core/function.c661
-rw-r--r--src/core/function.h50
-rw-r--r--src/core/garbage.c136
-rw-r--r--src/core/garbage.h54
-rw-r--r--src/core/interpret.c946
-rw-r--r--src/core/interpret.h75
-rw-r--r--src/core/scanner.c391
-rw-r--r--src/core/scanner.h71
-rw-r--r--src/core/scope.c150
-rw-r--r--src/core/scope.h59
-rw-r--r--src/core/symbol.c66
-rw-r--r--src/core/symbol.h64
-rw-r--r--src/core/token.c380
-rw-r--r--src/core/token.h183
-rw-r--r--src/data/array.c266
-rw-r--r--src/data/array.h86
-rw-r--r--src/data/dat.c267
-rw-r--r--src/data/dat.h88
-rw-r--r--src/data/hash.c290
-rw-r--r--src/data/hash.h80
-rw-r--r--src/data/list.c458
-rw-r--r--src/data/list.h105
-rw-r--r--src/data/map.c283
-rw-r--r--src/data/map.h82
-rw-r--r--src/data/queue.c210
-rw-r--r--src/data/queue.h81
-rw-r--r--src/data/stack.c234
-rw-r--r--src/data/stack.h76
-rw-r--r--src/data/tree.c250
-rw-r--r--src/data/tree.h106
-rw-r--r--src/data/tupel.c53
-rw-r--r--src/data/tupel.h47
-rw-r--r--src/data/types.h64
-rw-r--r--src/defines.h99
-rw-r--r--src/fype.c91
-rw-r--r--src/fype.h57
-rw-r--r--src/main.c45
42 files changed, 7215 insertions, 0 deletions
diff --git a/src/argv.c b/src/argv.c
new file mode 100644
index 0000000..07a3ea0
--- /dev/null
+++ b/src/argv.c
@@ -0,0 +1,181 @@
+/*:*
+ *: File: ./src/argv.c
+ *: A simple interpreter
+ *:
+ *: WWW : http://fype.buetow.org
+ *: E-Mail : fype@dev.buetow.org
+ *:
+ *: Copyright (c) 2005 2006 2007 2008, Paul Buetow (http://www.pblabs.net)
+ *: All rights reserved.
+ *:
+ *: 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 P. B. Labs 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 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 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(Fype *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..bcf7eb4
--- /dev/null
+++ b/src/argv.h
@@ -0,0 +1,51 @@
+/*:*
+ *: File: ./src/argv.h
+ *: A simple interpreter
+ *:
+ *: WWW : http://fype.buetow.org
+ *: E-Mail : fype@dev.buetow.org
+ *:
+ *: Copyright (c) 2005 2006 2007 2008, Paul Buetow (http://www.pblabs.net)
+ *: All rights reserved.
+ *:
+ *: 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 P. B. Labs 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 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 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(Fype *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..a4066a3
--- /dev/null
+++ b/src/build.h
@@ -0,0 +1,41 @@
+/*:*
+ *: File: ./src/build.h
+ *: A simple interpreter
+ *:
+ *: WWW : http://fype.buetow.org
+ *: E-Mail : fype@dev.buetow.org
+ *:
+ *: Copyright (c) 2005 2006 2007 2008, Paul Buetow (http://www.pblabs.net)
+ *: All rights reserved.
+ *:
+ *: 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 P. B. Labs 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 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 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 8867
+#define OS_FREEBSD
+
+#endif
diff --git a/src/core/convert.c b/src/core/convert.c
new file mode 100644
index 0000000..0d63619
--- /dev/null
+++ b/src/core/convert.c
@@ -0,0 +1,185 @@
+/*:*
+ *: File: ./src/core/convert.c
+ *: A simple interpreter
+ *:
+ *: WWW : http://fype.buetow.org
+ *: E-Mail : fype@dev.buetow.org
+ *:
+ *: Copyright (c) 2005 2006 2007 2008, Paul Buetow (http://www.pblabs.net)
+ *: All rights reserved.
+ *:
+ *: 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 P. B. Labs 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 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 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 "convert.h"
+
+void
+convert_to_integer(Token *p_token) {
+ switch (token_get_tt(p_token)) {
+ case TT_INTEGER:
+ break;
+ case TT_DOUBLE:
+ token_set_tt(p_token, TT_INTEGER);
+ token_set_ival(p_token, (int)token_get_dval(p_token));
+ break;
+ case TT_STRING:
+ token_set_tt(p_token, TT_INTEGER);
+ token_set_ival(p_token, atoi(token_get_val(p_token)));
+ break;
+ default:
+ ERROR("Ouups(%s)", tt_get_name(token_get_tt(p_token)));
+ break;
+ }
+}
+
+int
+convert_to_integer_get(Token *p_token) {
+ switch (token_get_tt(p_token)) {
+ case TT_INTEGER:
+ return (token_get_ival(p_token));
+ case TT_DOUBLE:
+ return ((int) token_get_dval(p_token));
+ case TT_STRING:
+ return (atoi(token_get_val(p_token)));
+ default:
+ ERROR("Ouups(%s)", tt_get_name(token_get_tt(p_token)));
+ }
+
+ return (0); /* Never reach this point */
+}
+
+void
+convert_to_double(Token *p_token) {
+ switch (token_get_tt(p_token)) {
+ case TT_INTEGER:
+ token_set_tt(p_token, TT_DOUBLE);
+ token_set_dval(p_token, token_get_ival(p_token));
+ break;
+ case TT_DOUBLE:
+ break;
+ case TT_STRING:
+ token_set_tt(p_token, TT_DOUBLE);
+ token_set_dval(p_token, atof(token_get_val(p_token)));
+ break;
+ default:
+ token_print_val(p_token);
+ ERROR("Datatype conversion error '%s'", token_get_val(p_token));
+ break;
+ }
+}
+
+void
+convert_to_string(Token *p_token) {
+ switch (token_get_tt(p_token)) {
+ case TT_INTEGER:
+ {
+ token_set_tt(p_token, TT_STRING);
+ char c_tmp[1024];
+ sprintf(c_tmp, "%d", token_get_ival(p_token));
+ int i_len = strlen(c_tmp);
+ p_token->c_val = realloc(p_token->c_val, sizeof(char) * (i_len + 1));
+ strcpy(p_token->c_val, c_tmp);
+ p_token->c_val[i_len] = 0;
+ }
+ break;
+ case TT_DOUBLE:
+ {
+ token_set_tt(p_token, TT_STRING);
+ char c_tmp[1024];
+ sprintf(c_tmp, "%f", token_get_dval(p_token));
+ int i_len = strlen(c_tmp);
+ p_token->c_val = realloc(p_token->c_val, sizeof(char) * (i_len + 1));
+ strcpy(p_token->c_val, c_tmp);
+ p_token->c_val[i_len] = 0;
+ }
+ break;
+ case TT_STRING:
+ break;
+ default:
+ ERROR("Datatype conversion error");
+ break;
+ }
+}
+
+void
+convert_to_tt(Token *p_token, TokenType tt) {
+ switch (tt) {
+ case TT_INTEGER:
+ convert_to_integer(p_token);
+ break;
+ case TT_DOUBLE:
+ convert_to_double(p_token);
+ break;
+ case TT_STRING:
+ convert_to_string(p_token);
+ break;
+ default:
+ ERROR("Ouups!");
+ }
+}
+
+TokenType
+convert_to_highest(Token *p_token1, Token *p_token2) {
+ TokenType tt_highest = token_get_tt(p_token1);
+
+ if (tt_highest < token_get_tt(p_token2)) {
+ tt_highest = token_get_tt(p_token2);
+
+ convert_to_tt(p_token1, tt_highest);
+
+ } else {
+ convert_to_tt(p_token2, tt_highest);
+ }
+
+ return (tt_highest);
+}
+
+TokenType
+convert_function_arg_types_to_highest(Stack *p_stack_args, int i_args) {
+
+ if (i_args <= 0)
+ i_args = stack_size(p_stack_args);
+
+ StackIterator *p_iter = stackiterator_new(p_stack_args);
+ TokenType tt_highest = TT_INTEGER;
+
+ for (int i = 0; i < i_args && stackiterator_has_next(p_iter); ++i) {
+ Token *p_token = stackiterator_next(p_iter);
+ if (token_get_tt(p_token) > tt_highest)
+ tt_highest = token_get_tt(p_token);
+ }
+
+ stackiterator_delete(p_iter);
+ p_iter = stackiterator_new(p_stack_args);
+
+ for (int i = 0; i < i_args && stackiterator_has_next(p_iter); ++i) {
+ Token *p_token = stackiterator_next(p_iter);
+ convert_to_tt(p_token, tt_highest);
+ }
+
+ stackiterator_delete(p_iter);
+
+ return (tt_highest);
+}
+
diff --git a/src/core/convert.h b/src/core/convert.h
new file mode 100644
index 0000000..c4e0b1d
--- /dev/null
+++ b/src/core/convert.h
@@ -0,0 +1,53 @@
+/*:*
+ *: File: ./src/core/convert.h
+ *: A simple interpreter
+ *:
+ *: WWW : http://fype.buetow.org
+ *: E-Mail : fype@dev.buetow.org
+ *:
+ *: Copyright (c) 2005 2006 2007 2008, Paul Buetow (http://www.pblabs.net)
+ *: All rights reserved.
+ *:
+ *: 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 P. B. Labs 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 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 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 CONVERT_H
+#define CONVERT_H
+
+#include "../defines.h"
+
+#include "../data/stack.h"
+
+#include "token.h"
+
+int convert_to_integer_get(Token *p_token);
+void convert_to_integer(Token *p_token);
+void convert_to_double(Token *p_token);
+void convert_to_string(Token *p_token);
+void convert_to_tt(Token *p_token, TokenType tt);
+TokenType convert_to_highest(Token *p_token1, Token *p_token2);
+TokenType convert_function_arg_types_to_highest(Stack *p_stack_args, int
+ i_args);
+
+#endif
diff --git a/src/core/function.c b/src/core/function.c
new file mode 100644
index 0000000..fd18ba0
--- /dev/null
+++ b/src/core/function.c
@@ -0,0 +1,661 @@
+/*:*
+ *: File: ./src/core/function.c
+ *: A simple interpreter
+ *:
+ *: WWW : http://fype.buetow.org
+ *: E-Mail : fype@dev.buetow.org
+ *:
+ *: Copyright (c) 2005 2006 2007 2008, Paul Buetow (http://www.pblabs.net)
+ *: All rights reserved.
+ *:
+ *: 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 P. B. Labs 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 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 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 <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "function.h"
+
+#include "convert.h"
+#include "scope.h"
+#include "symbol.h"
+
+#define _FUNCTION_ERROR(m,t) \
+ ERROR(\
+ "%s: Function error in %s line %d pos %d near '%s'", m, \
+ token_get_filename(t), \
+ token_get_line_nr(t), \
+ token_get_pos_nr(t), \
+ token_get_val(t) \
+ )
+
+void
+_process(Interpret *p_interpret, Token *p_token_store, Token *p_token_op,
+ Token *p_token_next) {
+
+ TokenType tt_op = token_get_tt(p_token_op);
+
+#ifdef DEBUG_FUNCTION_PROCESS
+ printf("PROCESS OPERATOR %s\n", tt_get_name(tt_op));
+
+ token_print(p_token_next);
+ printf("\n");
+ token_print(p_token_store);
+ printf("\n");
+#endif /* DEBUG_FUNCTION_PROCESS */
+
+
+ switch (tt_op) {
+ case TT_ASSIGN:
+ {
+ Token *p_token_assign = p_interpret->p_token_temp;
+ TokenType tt_assign = token_get_tt(p_token_assign);
+
+ if (tt_assign != TT_IDENT) {
+ _FUNCTION_ERROR("Can only assign to symbols",
+ p_token_store);
+ }
+
+ Symbol *p_symbol = scope_get(p_interpret->p_scope,
+ token_get_val(p_token_assign));
+
+ if (p_symbol == NULL) {
+ _FUNCTION_ERROR("No such symbol",
+ p_token_assign);
+ }
+
+ symbol_set_val(p_symbol, p_token_store);
+ symbol_set_sym(p_symbol, SYM_VARIABLE);
+
+ return;
+ }
+
+ break;
+ NO_DEFAULT;
+ }
+
+ p_token_next = token_new_copy(p_token_next);
+ TokenType tt_highest = convert_to_highest(p_token_store, p_token_next);
+
+#ifdef DEBUG_FUNCTION_PROCESS
+ printf("===> %s %s %s\n",
+ tt_get_name(tt_highest),
+ tt_get_name(tt_op),
+ tt_get_name(tt_highest));
+#endif /* DEBUG_FUNCTION_PROCESS */
+
+ switch (tt_op) {
+ case TT_ADD:
+ switch (tt_highest) {
+ case TT_INTEGER:
+ token_set_ival(p_token_store,
+ token_get_ival(p_token_next) +
+ token_get_ival(p_token_store));
+ break;
+ case TT_DOUBLE:
+ token_set_dval(p_token_store,
+ token_get_dval(p_token_next) +
+ token_get_dval(p_token_store));
+ case TT_STRING:
+ token_set_ival(p_token_store,
+ atoi(token_get_val(p_token_next)) +
+ atoi(token_get_val(p_token_store)));
+ token_set_tt(p_token_store, TT_INTEGER);
+ break;
+ NO_DEFAULT;
+ }
+ break;
+ case TT_SUB:
+ switch (tt_highest) {
+ case TT_INTEGER:
+ token_set_ival(p_token_store,
+ token_get_ival(p_token_next) -
+ token_get_ival(p_token_store));
+ break;
+ case TT_DOUBLE:
+ token_set_dval(p_token_store,
+ token_get_dval(p_token_next) -
+ token_get_dval(p_token_store));
+ break;
+ case TT_STRING:
+ token_set_ival(p_token_store,
+ atoi(token_get_val(p_token_next)) -
+ atoi(token_get_val(p_token_store)));
+ token_set_tt(p_token_store, TT_INTEGER);
+ break;
+ NO_DEFAULT;
+ }
+ break;
+ case TT_MULT:
+ switch (tt_highest) {
+ case TT_INTEGER:
+ token_set_ival(p_token_store,
+ token_get_ival(p_token_next) *
+ token_get_ival(p_token_store));
+ break;
+ case TT_DOUBLE:
+ token_set_dval(p_token_store,
+ token_get_dval(p_token_next) *
+ token_get_dval(p_token_store));
+ break;
+ case TT_STRING:
+ token_set_ival(p_token_store,
+ atoi(token_get_val(p_token_next)) *
+ atoi(token_get_val(p_token_store)));
+ token_set_tt(p_token_store, TT_INTEGER);
+ break;
+ NO_DEFAULT;
+ }
+ break;
+ case TT_DIV:
+ switch (tt_highest) {
+ case TT_INTEGER:
+ token_set_ival(p_token_store,
+ (int) token_get_ival(p_token_next) /
+ token_get_ival(p_token_store));
+ break;
+ case TT_DOUBLE:
+ token_set_dval(p_token_store,
+ token_get_dval(p_token_next) /
+ token_get_dval(p_token_store));
+ break;
+ case TT_STRING:
+ token_set_dval(p_token_store,
+ atof(token_get_val(p_token_next)) /
+ atof(token_get_val(p_token_store)));
+ token_set_tt(p_token_store, TT_DOUBLE);
+ break;
+ NO_DEFAULT;
+ }
+ break;
+ case TT_EQ:
+ switch (tt_highest) {
+ case TT_INTEGER:
+ token_set_ival(p_token_store,
+ (int) token_get_ival(p_token_next) ==
+ token_get_ival(p_token_store));
+ break;
+ case TT_DOUBLE:
+ token_set_ival(p_token_store,
+ token_get_dval(p_token_next) ==
+ token_get_dval(p_token_store));
+ token_set_tt(p_token_store, TT_INTEGER);
+ break;
+ case TT_STRING:
+ token_set_ival(p_token_store,
+ strcmp(token_get_val(p_token_next),
+ token_get_val(p_token_store)) == 0);
+ token_set_tt(p_token_store, TT_INTEGER);
+ break;
+ NO_DEFAULT;
+ }
+ break;
+ case TT_NEQ:
+ switch (tt_highest) {
+ case TT_INTEGER:
+ token_set_ival(p_token_store,
+ (int) token_get_ival(p_token_next) !=
+ token_get_ival(p_token_store));
+ break;
+ case TT_DOUBLE:
+ token_set_ival(p_token_store,
+ token_get_dval(p_token_next) !=
+ token_get_dval(p_token_store));
+ token_set_tt(p_token_store, TT_INTEGER);
+ break;
+ case TT_STRING:
+ token_set_ival(p_token_store,
+ strcmp(token_get_val(p_token_next),
+ token_get_val(p_token_store)) != 0);
+ token_set_tt(p_token_store, TT_INTEGER);
+ break;
+ NO_DEFAULT;
+ }
+ break;
+ case TT_LT:
+ switch (tt_highest) {
+ case TT_INTEGER:
+ token_set_ival(p_token_store,