summaryrefslogtreecommitdiff
path: root/src/core/scanner.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/scanner.c')
-rw-r--r--src/core/scanner.c169
1 files changed, 46 insertions, 123 deletions
diff --git a/src/core/scanner.c b/src/core/scanner.c
index 96d3756..a2b2b10 100644
--- a/src/core/scanner.c
+++ b/src/core/scanner.c
@@ -1,14 +1,13 @@
/*:*
*: File: ./src/core/scanner.c
- *: A simple interpreter
- *:
- *: WWW : http://fype.buetow.org
- *: AUTHOR : http://paul.buetow.org
- *: E-Mail : fype at dev.buetow.org
- *:
- *: Copyright (c) 2005 - 2009, Dipl.-Inform. (FH) Paul C. Buetow
- *: All rights reserved.
- *:
+ *: 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
@@ -16,20 +15,20 @@
*: * 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
+ *: * 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
+ *:
+ *: 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)
+ *: 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
+ *: 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.
*:*/
@@ -38,8 +37,7 @@
#include <ctype.h>
#include <string.h>
-const char _TOKENENDS[] = "}])+-*/={([<>;:,.!";
-#define _ADD_SEMICOLON_INDEX 2
+const char _TOKEN_ENDS[] = "()";
int _CODESTR_INDEX = 0;
Scanner*
@@ -70,8 +68,8 @@ scanner_new(List *p_list_token, Tupel *p_tupel_argv) {
p_scanner->i_current_line_nr = 1;
p_scanner->i_current_pos_nr = 0;
- p_scanner->i_num_tokenends = strlen(_TOKENENDS);
- p_scanner->tt_last = TT_NONE;
+ p_scanner->i_num_tokenends = strlen(_TOKEN_ENDS);
+ p_scanner->tt_last = TT_IDENT;
return p_scanner;
}
@@ -83,66 +81,6 @@ scanner_delete(Scanner *p_scanner) {
free(p_scanner);
}
-void
-_add_semicolon_to_list(Scanner *p_scanner) {
- int i_token_len = 1;
- char *c_token = calloc(2, sizeof(char*));
- c_token[0] = ';';
- c_token[1] = '\0';
- scanner_add_token(p_scanner, &c_token, &i_token_len, TT_SEMICOLON);
-}
-
-void
-scanner_post_task(Scanner *p_scanner) {
- List *p_list_token = scanner_get_list_token(p_scanner);
- ListIterator *p_iter = listiterator_new(p_list_token);
-
- Token *pt_last[] = { NULL, NULL };
- TokenType tt_last[] = { TT_NONE, TT_NONE };
-
- while (listiterator_has_next(p_iter)) {
- ListElem *p_le = listiterator_next_elem(p_iter);
- Token *p_token = p_le->p_val;
- TokenType tt_cur = token_get_tt(p_token);
-
- if (pt_last[0]) {
- if (tt_cur == TT_INTEGER && tt_last[1] == TT_DOT
- && tt_last[0] == TT_INTEGER) {
-
- token_ref_down(pt_last[0]);
- token_ref_down(pt_last[1]);
-
- char *c_2 = token_get_val(p_token);
- char *c_0 = token_get_val(pt_last[0]);
- int i_len = strlen(c_2) + strlen(c_0) + 1;
- char *c_new = calloc(i_len+1, sizeof(char));
-
- sprintf(c_new, "%s.%s", c_0, c_2);
- free(c_2);
- c_new[i_len] = 0;
-
- token_set_val(p_token, c_new);
- token_set_tt(p_token, TT_DOUBLE);
- token_set_dval(p_token, atof(c_new));
-
- list_remove_elem(p_list_token, p_le->p_prev);
- list_remove_elem(p_list_token, p_le->p_prev);
-
- pt_last[0] = pt_last[1] = NULL;
- tt_last[0] = tt_last[1] = TT_NONE;
- }
- }
-
- tt_last[0] = tt_last[1];
- tt_last[1] = tt_cur;
-
- pt_last[0] = pt_last[1];
- pt_last[1] = p_token;
- }
-
- listiterator_delete(p_iter);
-}
-
_Bool
_scanner_has_next_char(Scanner *p_scanner) {
if (p_scanner->fp)
@@ -160,7 +98,7 @@ _scanner_get_next_char(Scanner *p_scanner) {
}
void
-scanner_run(Fype *p_fype) {
+scanner_run(PBSc *p_fype) {
Scanner *p_scanner = scanner_new(p_fype->p_list_token,
p_fype->p_tupel_argv);
@@ -211,7 +149,6 @@ scanner_run(Fype *p_fype) {
}
{
int i_num_nl = 0;
- //_Bool flag = false;
do {
c = _scanner_get_next_char(p_scanner);
if ( c == '\n' ) {
@@ -229,7 +166,6 @@ scanner_run(Fype *p_fype) {
c_token[i_token_len-1] = '"';
} else {
- //flag = true;
break;
}
@@ -246,9 +182,6 @@ scanner_run(Fype *p_fype) {
if (i_num_nl)
p_scanner->i_current_line_nr += i_num_nl;
-
- //if (flag)
- // _add_semicolon_to_list(p_scanner);
}
break;
@@ -268,27 +201,31 @@ scanner_run(Fype *p_fype) {
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);
- if (tt_cur == TT_PARANT_CR && p_scanner->tt_last == TT_STRING)
- _add_semicolon_to_list(p_scanner);
-
- char d = c_token[i_token_len-1];
- if ((!isalpha(d) && !isdigit(d) /*&& d != '-'*/) &&
- (isalpha(c) || isdigit(c))) {
-
- scanner_add_token(p_scanner, &c_token, &i_token_len, tt_cur);
-
- } else {
- for (int i = 0; i < p_scanner->i_num_tokenends; ++i) {
- if (_TOKENENDS[i] == c) {
- scanner_add_token(p_scanner, &c_token,
- &i_token_len, tt_cur);
- if (i < _ADD_SEMICOLON_INDEX)
- _add_semicolon_to_list(p_scanner);
- break;
- }
+
+ 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;
}
}
}
@@ -305,14 +242,6 @@ scanner_run(Fype *p_fype) {
scanner_add_token(p_scanner, &c_token, &i_token_len, tt_cur);
}
- /* Check if there is a ; missing */
- List *p_list_token = scanner_get_list_token(p_scanner);
- Token *p_last_token = list_last(p_list_token);
- if (token_get_tt(p_last_token) != TT_SEMICOLON)
- _add_semicolon_to_list(p_scanner);
-
- scanner_post_task(p_scanner);
-
char *c_filename = scanner_get_filename(p_scanner);
scanner_delete(p_scanner);
@@ -345,7 +274,6 @@ scanner_add_token(Scanner *p_scanner, char **cc_token, int *p_token_len,
p_scanner->c_filename);
list_add_back(p_list_token, p_token);
- token_ref_up(p_token);
*cc_token = malloc(sizeof(char));
(*cc_token)[0] = 0;
@@ -359,12 +287,7 @@ scanner_get_tt_cur(char *c_token) {
if (isdigit(c_token[0]))
return TT_INTEGER;
- if (c_token[0] == '-' && 1 < strlen(c_token) && isdigit(c_token[1]))
- return TT_INTEGER;
-
- TokenType tt_cur = get_tt(c_token);
-
- return tt_cur == TT_NONE ? TT_IDENT : tt_cur;
+ return (get_tt(c_token));
}
void