From 2a2227d529f3788b8e08752d37deb1a449f3b9bc Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 28 Feb 2026 16:15:57 +0200 Subject: Clean up scanner_run() and interpret_run(): narrow signatures, fix SoC [SoC/DIP] Three issues resolved: 1. scanner_run() no longer takes Fype*. New signature: scanner_run(List*, Tupel*, char **c_filename_out) Verbose-mode list_iterate() and basename extraction moved to fype_run() (the composition root), leaving scanner_run() as pure tokenizer: open source -> tokenize -> post-process -> return filename. 2. interpret_run() no longer takes Fype*. New signature: interpret_run(List *p_list_token, Hash *p_hash_syms) Fype* unpacking happens in fype_run(); interpret.h no longer includes fype.h. scanner.h no longer includes fype.h either. 3. _CODESTR_INDEX file-global (which made the scanner non-reentrant) moved into the Scanner struct as i_codestr_index, initialized to 0 in scanner_new(). _scanner_has_next_char() and _scanner_get_next_char() use p_scanner->i_codestr_index instead. Co-Authored-By: Claude Sonnet 4.6 --- docs/help.txt | 2 +- docs/stats.txt | 2 +- docs/version.txt | 2 +- fype | Bin 288368 -> 287968 bytes src/build.h | 2 +- src/core/interpret.c | 8 +++---- src/core/interpret.h | 4 +--- src/core/scanner.c | 61 ++++++++++++++++++++++++--------------------------- src/core/scanner.h | 12 ++++++++-- src/fype.c | 22 ++++++++++++++++--- tags | 5 ++--- 11 files changed, 68 insertions(+), 52 deletions(-) diff --git a/docs/help.txt b/docs/help.txt index fae20f2..4c3ded3 100644 --- a/docs/help.txt +++ b/docs/help.txt @@ -1,4 +1,4 @@ -Fype Superalpha Build 9701 +Fype Superalpha Build 9705 (c) Paul C. Buetow (2005 - 2008) -e Executes given code string (see synopses) -h Prints this help diff --git a/docs/stats.txt b/docs/stats.txt index 47a0f36..1123c63 100644 --- a/docs/stats.txt +++ b/docs/stats.txt @@ -1,6 +1,6 @@ make[1]: Entering directory '/home/paul/git/fype' ===> Num of C source files : 46 -===> Num of C source lines : 8725 +===> Num of C source lines : 8742 ===> Num of Fype source examples : 19 ===> Num of Fype source lines : 883 make[1]: Leaving directory '/home/paul/git/fype' diff --git a/docs/version.txt b/docs/version.txt index dbb9597..baab03e 100644 --- a/docs/version.txt +++ b/docs/version.txt @@ -1 +1 @@ -Fype Superalpha Build 9701 +Fype Superalpha Build 9705 diff --git a/fype b/fype index 0d4febb..b66d439 100755 Binary files a/fype and b/fype differ 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 #include +#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 #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 + #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); diff --git a/tags b/tags index 97d0b0b..b62ad62 100644 --- a/tags +++ b/tags @@ -46,7 +46,6 @@ LIST_GARBAGE ./src/core/garbage.c /^List *LIST_GARBAGE = NULL;$/;" v typeref:typ TOKEN_ID_COUNTER ./src/core/token.c /^long TOKEN_ID_COUNTER = 0;$/;" v typeref:typename:long _ADD_SEMICOLON_INDEX ./src/core/scanner.c /^#define _ADD_SEMICOLON_INDEX /;" d file: _CHECK ./src/core/interpret.c /^#define _CHECK /;" d file: -_CODESTR_INDEX ./src/core/scanner.c /^int _CODESTR_INDEX = 0;$/;" v typeref:typename:int _FUNCTIONS_ERROR ./src/core/functions.c /^#define _FUNCTIONS_ERROR(/;" d file: _GARBAGE_ERROR ./src/core/garbage.c /^#define _GARBAGE_ERROR(/;" d file: _Garbage ./src/core/garbage.c /^} _Garbage;$/;" t typeref:struct:__anoneaa3b3cb0108 file: @@ -256,7 +255,7 @@ hash_size ./src/data/hash.c /^hash_size(Hash *p_hash, int i_size) {$/;" f typere interpret_delete ./src/core/interpret.c /^interpret_delete(Interpret *p_interpret) {$/;" f typeref:typename:void interpret_new ./src/core/interpret.c /^interpret_new(List *p_list_token, Hash *p_hash_syms) {$/;" f typeref:typename:Interpret * interpret_process ./src/core/interpret.c /^interpret_process(Interpret *p_interpret) {$/;" f typeref:typename:int -interpret_run ./src/core/interpret.c /^interpret_run(Fype *p_fype) {$/;" f typeref:typename:void +interpret_run ./src/core/interpret.c /^interpret_run(List *p_list_token, Hash *p_hash_syms) {$/;" f typeref:typename:void interpret_subprocess ./src/core/interpret.c /^interpret_subprocess(Interpret *p_interpret, List *p_list_token) {$/;" f typeref:typename:int list_add_back ./src/data/list.c /^list_add_back(List *p_list, void *p_val) {$/;" f typeref:typename:void list_add_front ./src/data/list.c /^list_add_front(List *p_list, void *p_val) {$/;" f typeref:typename:void @@ -353,7 +352,7 @@ scanner_delete ./src/core/scanner.c /^scanner_delete(Scanner *p_scanner) {$/;" f scanner_get_tt_cur ./src/core/scanner.c /^scanner_get_tt_cur(char *c_token) {$/;" f typeref:typename:TokenType scanner_new ./src/core/scanner.c /^scanner_new(List *p_list_token, Tupel *p_tupel_argv) {$/;" f typeref:typename:Scanner * scanner_post_task ./src/core/scanner.c /^scanner_post_task(Scanner *p_scanner) {$/;" f typeref:typename:void -scanner_run ./src/core/scanner.c /^scanner_run(Fype *p_fype) {$/;" f typeref:typename:void +scanner_run ./src/core/scanner.c /^scanner_run(List *p_list_token, Tupel *p_tupel_argv,$/;" f typeref:typename:void scope_delete ./src/core/scope.c /^scope_delete(Scope *p_scope) {$/;" f typeref:typename:void scope_down ./src/core/scope.c /^scope_down(Scope *p_scope) {$/;" f typeref:typename:void scope_exists ./src/core/scope.c /^scope_exists(Scope *p_scope, char *c_key) {$/;" f typeref:typename:_Bool -- cgit v1.2.3