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.c152
1 files changed, 112 insertions, 40 deletions
diff --git a/src/core/scanner.c b/src/core/scanner.c
index a2b2b10..49026c2 100644
--- a/src/core/scanner.c
+++ b/src/core/scanner.c
@@ -1,12 +1,12 @@
/*:*
*: File: ./src/core/scanner.c
- *: A simple Fype interpreter
+ *: A simple interpreter
*:
- *: WWW: http://fype.buetow.org
- *: AUTHOR: http://paul.buetow.org
- *: E-Mail: fype at dev.buetow.org
+ *: WWW : http://fype.buetow.org
+ *: E-Mail : fype@dev.buetow.org
*:
- *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow
+ *: Copyright (c) 2005 2006 2007 2008, Dipl.-Inf. (FH) Paul C. Buetow
+ *: 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:
@@ -15,14 +15,14 @@
*: * 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
+ *: * 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 C. BUETOW AS IS'' AND ANY EXPRESS OR
+ *: 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 C. BUETOW BE LIABLE FOR ANY DIRECT,
+ *: 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)
@@ -37,7 +37,8 @@
#include <ctype.h>
#include <string.h>
-const char _TOKEN_ENDS[] = "()";
+const char _TOKENENDS[] = "}])+-*/={([<>;:,.!";
+#define _ADD_SEMICOLON_INDEX 2
int _CODESTR_INDEX = 0;
Scanner*
@@ -68,8 +69,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(_TOKEN_ENDS);
- p_scanner->tt_last = TT_IDENT;
+ p_scanner->i_num_tokenends = strlen(_TOKENENDS);
+ p_scanner->tt_last = TT_NONE;
return p_scanner;
}
@@ -81,6 +82,66 @@ 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)
@@ -98,9 +159,8 @@ _scanner_get_next_char(Scanner *p_scanner) {
}
void
-scanner_run(PBSc *p_fype) {
- Scanner *p_scanner = scanner_new(p_fype->p_list_token,
- p_fype->p_tupel_argv);
+scanner_run(Fype *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));
@@ -149,6 +209,7 @@ scanner_run(PBSc *p_fype) {
}
{
int i_num_nl = 0;
+ //_Bool flag = false;
do {
c = _scanner_get_next_char(p_scanner);
if ( c == '\n' ) {
@@ -166,6 +227,7 @@ scanner_run(PBSc *p_fype) {
c_token[i_token_len-1] = '"';
} else {
+ //flag = true;
break;
}
@@ -182,6 +244,9 @@ scanner_run(PBSc *p_fype) {
if (i_num_nl)
p_scanner->i_current_line_nr += i_num_nl;
+
+ //if (flag)
+ // _add_semicolon_to_list(p_scanner);
}
break;
@@ -201,31 +266,26 @@ scanner_run(PBSc *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);
- 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;
+ 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;
+ }
}
}
}
@@ -242,6 +302,14 @@ scanner_run(PBSc *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);
@@ -268,12 +336,11 @@ 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);
+ 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);
+ token_ref_up(p_token);
*cc_token = malloc(sizeof(char));
(*cc_token)[0] = 0;
@@ -287,7 +354,12 @@ scanner_get_tt_cur(char *c_token) {
if (isdigit(c_token[0]))
return TT_INTEGER;
- return (get_tt(c_token));
+ 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;
}
void