summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/build.h2
-rw-r--r--src/core/interpret.c8
-rw-r--r--src/core/interpret.h4
-rw-r--r--src/core/scanner.c61
-rw-r--r--src/core/scanner.h12
-rw-r--r--src/fype.c22
6 files changed, 63 insertions, 46 deletions
diff --git a/src/build.h b/src/build.h
index b96d7c4..8c250dc 100644
--- a/src/build.h
+++ b/src/build.h
@@ -36,7 +36,7 @@
#ifndef BUILD_H
#define BUILD_H
-#define BUILDNR 9702
+#define BUILDNR 9707
#define OS_LINUX
#endif
diff --git a/src/core/interpret.c b/src/core/interpret.c
index b6c7c33..ba0eade 100644
--- a/src/core/interpret.c
+++ b/src/core/interpret.c
@@ -1352,13 +1352,11 @@ interpret_subprocess(Interpret *p_interpret, List *p_list_token) {
return (i_ret);
}
+/* Create an interpreter, run it over the token list, then tear it down. */
void
-interpret_run(Fype *p_fype) {
- Interpret *p_interpret =
- interpret_new(p_fype->p_list_token, p_fype->p_hash_syms);
-
+interpret_run(List *p_list_token, Hash *p_hash_syms) {
+ Interpret *p_interpret = interpret_new(p_list_token, p_hash_syms);
interpret_process(p_interpret);
-
interpret_delete(p_interpret);
}
diff --git a/src/core/interpret.h b/src/core/interpret.h
index 7c2eba8..9ab3016 100644
--- a/src/core/interpret.h
+++ b/src/core/interpret.h
@@ -40,8 +40,6 @@
#include "../data/stack.h"
#include "../data/hash.h"
-#include "../fype.h"
-
#include "garbage.h"
#include "scope.h"
#include "token.h"
@@ -74,7 +72,7 @@ typedef struct {
Interpret* interpret_new(List *p_list_token, Hash *p_hash_syms);
void interpret_delete(Interpret *p_interpret);
-void interpret_run(Fype *p_type);
+void interpret_run(List *p_list_token, Hash *p_hash_syms);
int interpret_process(Interpret *p_interpret);
int interpret_subprocess(Interpret *p_interpret, List *p_list_token);
diff --git a/src/core/scanner.c b/src/core/scanner.c
index ff7baca..87fc624 100644
--- a/src/core/scanner.c
+++ b/src/core/scanner.c
@@ -38,9 +38,11 @@
#include <ctype.h>
#include <string.h>
+#include "../argv.h"
+
const char _TOKENENDS[] = "}])+-*/={([<>;:,.!";
#define _ADD_SEMICOLON_INDEX 2
-int _CODESTR_INDEX = 0;
+/* _CODESTR_INDEX was removed; use p_scanner->i_codestr_index instead. */
Scanner*
scanner_new(List *p_list_token, Tupel *p_tupel_argv) {
@@ -69,6 +71,7 @@ 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_codestr_index = 0; /* start of inline code string */
p_scanner->i_num_tokenends = strlen(_TOKENENDS);
p_scanner->tt_last = TT_NONE;
@@ -148,7 +151,7 @@ _scanner_has_next_char(Scanner *p_scanner) {
if (p_scanner->fp)
return !feof(p_scanner->fp);
- return p_scanner->c_codestring[_CODESTR_INDEX] != 0;
+ return p_scanner->c_codestring[p_scanner->i_codestr_index] != 0;
}
char
@@ -156,13 +159,19 @@ _scanner_get_next_char(Scanner *p_scanner) {
if (p_scanner->fp)
return fgetc(p_scanner->fp);
- return (p_scanner->c_codestring[_CODESTR_INDEX++]);
+ return (p_scanner->c_codestring[p_scanner->i_codestr_index++]);
}
+/* Tokenise p_list_token from source given in p_tupel_argv, then
+ * post-process the token list. Sets *c_filename_out to the source
+ * filename (NULL for inline -e mode); the pointer is owned by the
+ * argv Dat — do not free it. Verbose printing and basename extraction
+ * are the caller's responsibility. */
void
-scanner_run(Fype *p_fype) {
- Scanner *p_scanner = scanner_new(p_fype->p_list_token,
- p_fype->p_tupel_argv);
+scanner_run(List *p_list_token, Tupel *p_tupel_argv,
+ char **c_filename_out) {
+
+ Scanner *p_scanner = scanner_new(p_list_token, p_tupel_argv);
int i_token_len = 0;
char *c_token = malloc(sizeof(char));
@@ -219,7 +228,8 @@ scanner_run(Fype *p_fype) {
p_scanner->i_current_pos_nr = 0;
i_token_len += 2;
- c_token = realloc(c_token, sizeof(char) * (i_token_len + 1));
+ 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;
@@ -235,14 +245,16 @@ scanner_run(Fype *p_fype) {
} else {
++i_token_len;
- c_token = realloc(c_token, sizeof(char) * (i_token_len + 1));
+ 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);
+ 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;
@@ -278,7 +290,8 @@ scanner_run(Fype *p_fype) {
if ((!isalpha(d) && !isdigit(d) /*&& d != '-'*/) &&
(isalpha(c) || isdigit(c))) {
- scanner_add_token(p_scanner, &c_token, &i_token_len, tt_cur);
+ scanner_add_token(p_scanner, &c_token, &i_token_len,
+ tt_cur);
} else {
for (int i = 0; i < p_scanner->i_num_tokenends; ++i) {
@@ -300,38 +313,22 @@ scanner_run(Fype *p_fype) {
}
}
- if (argv_checkopts("e", p_fype->p_tupel_argv) && i_token_len) {
+ if (argv_checkopts("e", 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);
}
- /* 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);
+ /* Ensure the token list is properly terminated with a semicolon */
+ List *p_list = scanner_get_list_token(p_scanner);
+ Token *p_last_token = list_last(p_list);
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);
+ /* Return the filename to the caller; NULL for inline (-e) mode */
+ *c_filename_out = 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
diff --git a/src/core/scanner.h b/src/core/scanner.h
index 5156430..fb2208e 100644
--- a/src/core/scanner.h
+++ b/src/core/scanner.h
@@ -40,8 +40,9 @@
#include <ctype.h>
#include "token.h"
-#include "../fype.h"
#include "../data/dat.h"
+#include "../data/list.h"
+#include "../data/tupel.h"
#define scanner_get_list_token(s) s->p_list_token
#define scanner_get_fp(s) s->fp
@@ -52,6 +53,9 @@ typedef struct {
int i_current_line_nr;
int i_current_pos_nr;
int i_num_tokenends;
+ /* Index into c_codestring for inline (-e) mode; replaces the old
+ * file-global _CODESTR_INDEX so the scanner is reentrant. */
+ int i_codestr_index;
char *c_filename;
char *c_codestring;
FILE *fp;
@@ -62,7 +66,11 @@ typedef struct {
Scanner *scanner_new(List *p_list_token, Tupel *p_tupel_argv);
void scanner_post_task(Scanner *p_scanner);
void scanner_delete(Scanner *p_scanner);
-void scanner_run(Fype *p_fype);
+/* Tokenise source and post-process into p_list_token.
+ * Sets *c_filename_out to the source filename (NULL for inline -e mode);
+ * the caller owns that pointer (do not free it). */
+void scanner_run(List *p_list_token, Tupel *p_tupel_argv,
+ char **c_filename_out);
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);
diff --git a/src/fype.c b/src/fype.c
index ebd906d..681a48c 100644
--- a/src/fype.c
+++ b/src/fype.c
@@ -35,7 +35,10 @@
#include "fype.h"
+#include <string.h>
+
#include "argv.h"
+#include "core/token.h"
#include "core/garbage.h"
#include "core/interpret.h"
#include "core/scanner.h"
@@ -80,11 +83,24 @@ fype_run(int i_argc, char **pc_argv) {
// argv: Maintains command line options
argv_run(p_fype, i_argc, pc_argv);
- // scanner: Creates a list of token
- scanner_run(p_fype);
+ // scanner: Tokenise source into the token list
+ char *c_filename = NULL;
+ scanner_run(p_fype->p_list_token, p_fype->p_tupel_argv, &c_filename);
+
+ // Verbose mode: print the token list after scanning
+ if (argv_checkopts("TV", p_fype->p_tupel_argv))
+ list_iterate(p_fype->p_list_token, token_print_cb);
+
+ // Derive the script basename (filename minus the .fy extension)
+ if (c_filename) {
+ int i_len = strlen(c_filename) - 3;
+ p_fype->c_basename = calloc(i_len + 1, sizeof(char));
+ strncpy(p_fype->c_basename, c_filename, i_len);
+ p_fype->c_basename[i_len] = 0;
+ }
// interpret: Interpret the list of token
- interpret_run(p_fype);
+ interpret_run(p_fype->p_list_token, p_fype->p_hash_syms);
fype_delete(p_fype);