From 691c02e23bbb0228e9986593e4294ccf5af03afc Mon Sep 17 00:00:00 2001 From: Koichi Nakamura Date: Fri, 3 Dec 2021 07:27:04 +0900 Subject: [PATCH] Add include-guard to include/included and add load/loaded --- bootstrap.fs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/bootstrap.fs b/bootstrap.fs index 3315744..53b7d13 100644 --- a/bootstrap.fs +++ b/bootstrap.fs @@ -2512,13 +2512,51 @@ need-defined (read) ( === File Include === ) -: included ( c-addr -- ) +: loaded ( c-addr -- ) R/O open-file throw push-inputstream ['] interpret-outer catch drop pop-inputstream close-file throw ; +: load ( "name" -- ) + word throw loaded +; + +struct + char% FILENAME-MAX * field included-list>path + cell% field included-list>next +end-struct included-list% + +variable included-list +0 included-list ! + +: already-included? ( c-addr -- n ) + included-list @ + begin ?dup while + ( c-addr entry ) + dup included-list>path + ( c-addr entry path ) + 2 pick streq if 2drop true exit then + included-list>next @ + repeat + drop false +; + +: push-included-list ( c-addr -- ) + included-list% %allocate throw + ( c-addr entry ) + tuck included-list>path FILENAME-MAX strncpy + included-list @ over included-list>next ! + included-list ! +; + +: included ( c-addr -- ) + dup already-included? if drop exit then + dup push-included-list + loaded +; + : include ( "name" -- ) word throw included ; @@ -2605,7 +2643,7 @@ need-defined (read) insn:docol insn:exit insn:lit insn:litstring insn:branch insn:0branch words id. name>string name>link - include included source >in sourcefilename + load loaded include included source >in sourcefilename next-arg shift-args arg argv argc version runtime copyright [if] [unless] [else] [then] defined? private{ }private export