From 910159f7abcc39bd61d13242d6b5927cf726fba2 Mon Sep 17 00:00:00 2001 From: Luxferre Date: Sun, 14 Jun 2026 09:28:29 +0300 Subject: [PATCH] updated listing/saving algos --- lttb-impl-notes.md | 17 ++++++++++++----- lttb.tcl | 8 ++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lttb-impl-notes.md b/lttb-impl-notes.md index 603d1f3..819ebf1 100644 --- a/lttb-impl-notes.md +++ b/lttb-impl-notes.md @@ -226,6 +226,16 @@ Run the program currently loaded into the working memory. Output program listing. +Two algorithms are provided: for the platforms that can extract associative array keys and for those that cannot. + +Algorithm 1: + +1. Extract key list `LNOS` from `PROGMEM` associative array. Sort the list in the integer numeric order. +2. Fetch the next key `LNO` from `LNOS`. Upon exhaustion, return from the routine. +3. If `PROGMEM[LNO]` is not empty, output the value of `LNO`, TAB character (ASCII 9) and `PROGMEM[LNO]` with a newline at the end. Go to step 2. + +Algorithm 2: + 1. Set `I = 1`. 2. If `PROGMEM[I]` exists and is not empty, output the value of `I`, TAB character (ASCII 9) and `PROGMEM[I]` with a newline at the end. 3. Increment `I` by 1. @@ -263,11 +273,8 @@ Program saving routine into external storage media. The provided algorithm works 1. Prompt the user for a file name/path `FNAME`. 2. If the file name/path isn't writable, notify the user about this error and return. 3. Open a file handle `FH` for writing into the file under the path `FNAME`. -4. Set `I = 1`. -5. If `PROGMEM[I]` exists and is not empty, write the value of `I`, space character (ASCII 32) and `PROGMEM[I]` with a newline at the end to the file handle `FH`. -6. Increment `I` by 1. -7. If `I > 0` and `I < 32768`, go to step 5. -8. Close the handle `FH` and notify the user about successful program saving. +4. Perform the algorithm described for the `LIST` routine, but using the specified file handle instead of standard output and space character (ASCII 32) instead of TAB character (ASCII 9). +5. Close the handle `FH` and notify the user about successful program saving. ## Closing notes diff --git a/lttb.tcl b/lttb.tcl index 827adf9..1f89efe 100755 --- a/lttb.tcl +++ b/lttb.tcl @@ -245,8 +245,8 @@ proc lttb_EN {stmt} { proc int_LIST {} { global progmem - loop i 1 32768 { - if {[dict exists $progmem $i] && [string length $progmem($i)]} { + foreach i [lsort -integer [dict keys $progmem]] { + if {$progmem($i) ne {}} { puts [format "%d\t%s" $i $progmem($i)] } } @@ -292,8 +292,8 @@ proc int_SAVE {} { stdout flush stdin gets fname set fh [open $fname w] - loop i 1 32768 { - if {[dict exists $progmem $i]} { + foreach i [lsort -integer [dict keys $progmem]] { + if {$progmem($i) ne {}} { $fh puts [format {%d %s} $i $progmem($i)] } }