From 05e59d02e76a1eed55f04f63257a7bb65326bcb0 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Mon, 7 Sep 2026 23:53:55 -0700 Subject: add indentation and blank lines as magic-comment features the output is really starting to look like something! Force-Push: yes Change-Id: Ic41c8b1702d884ff89693a86d457af7cc752aa86 --- transform.e | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 6 deletions(-) (limited to 'transform.e') diff --git a/transform.e b/transform.e index ce51255..e24e4b7 100644 --- a/transform.e +++ b/transform.e @@ -2721,7 +2721,8 @@ allocate-transformation-state s" transformation-state" variable : hex-output-metadata-label-loop-buffer-length 8 + ; : hex-output-metadata-latest-output-point 2 8 * + ; : hex-output-metadata-is-fresh-line 3 8 * + ; -: hex-output-metadata-first-entry 4 8 * + ; +: hex-output-metadata-indentation-depth 4 8 * + ; +: hex-output-metadata-first-entry 5 8 * + ; : hex-output-metadata-entry-data-start ; : hex-output-metadata-entry-data-length 8 + ; : hex-output-metadata-entry-type 2 8 * + ; @@ -2729,8 +2730,9 @@ allocate-transformation-state s" transformation-state" variable : hex-output-metadata-next-entry 4 8 * + ; : hex-output-metadata-entry-type-line-comment 0 ; : hex-output-metadata-entry-type-line-break 1 ; -: hex-output-metadata-entry-type-string-literal 2 ; -: hex-output-metadata-entry-type-alignment 3 ; +: hex-output-metadata-entry-type-indent 2 ; +: hex-output-metadata-entry-type-string-literal 3 ; +: hex-output-metadata-entry-type-alignment 4 ; ~ Initialize the contents of the output metadata to all zeroes. This is ~ called from hex-transform at its top level, at the very start, to make sure @@ -2744,6 +2746,7 @@ allocate-transformation-state s" transformation-state" variable dup hex-output-metadata-label-loop-buffer-length 0 swap ! dup hex-output-metadata-latest-output-point 0 swap ! dup hex-output-metadata-is-fresh-line 1 swap ! + dup hex-output-metadata-indentation-depth 0 swap ! dup hex-output-metadata-first-entry 0 swap ! ; ~ (pointer -- boolean) @@ -2809,7 +2812,7 @@ allocate-transformation-state s" transformation-state" variable ~ This likely won't affect anything, because every call to pack* ~ overwrites it, but we initialize the latest output point to the start of ~ the label loop buffer here. That way, in the event the very first thing in - ~ the output is something that's supposed to have a special comment on it, + ~ the output is something that's supposed to have a magic comment on it, ~ hex-tilde-replacement will know where to attach the metadata entry. ~ ~ If that was word soup, go read about hex-tilde-replacement and @@ -2949,6 +2952,14 @@ allocate-transformation-state s" transformation-state" variable : fresh-line is-fresh-line@ not { newline } if ; +: indentation-depth@ + transformation-state transformation-state-output-metadata @ + hex-output-metadata-indentation-depth @ ; + +: indentation-depth! + transformation-state transformation-state-output-metadata @ + hex-output-metadata-indentation-depth ! ; + ~ This "replacement" is a little different from an alternate: When the code ~ under transformation attempts to compile its own version of sys-write, it ~ gets a stub that calls this word instead. It's swapped out by @@ -2976,23 +2987,36 @@ allocate-transformation-state s" transformation-state" variable dup hex-output-metadata-entry-type @ hex-output-metadata-entry-type-line-comment = { fresh-line + indentation-depth@ indent + dup hex-output-metadata-entry-string @ ." ~ " emitstring newline + 1 is-fresh-line! } if dup hex-output-metadata-entry-type @ hex-output-metadata-entry-type-line-break = { + fresh-line newline 1 is-fresh-line! } if + dup hex-output-metadata-entry-type @ + hex-output-metadata-entry-type-indent = { + dup hex-output-metadata-entry-string @ + indentation-depth@ + indentation-depth! + } if + dup hex-output-metadata-entry-type @ hex-output-metadata-entry-type-string-literal = { fresh-line + indentation-depth@ indent + ." ~ Null-terminated string literal: " 0x22 value@ emitstring drop dup hex-output-metadata-entry-string @ emitstring 0x22 value@ emitstring drop + newline 1 is-fresh-line! } if @@ -3000,8 +3024,11 @@ allocate-transformation-state s" transformation-state" variable dup hex-output-metadata-entry-type @ hex-output-metadata-entry-type-alignment = { fresh-line + indentation-depth@ indent + ." ~ Alignment padding to width " dup hex-output-metadata-entry-string @ . + newline 1 is-fresh-line! } if @@ -3011,6 +3038,7 @@ allocate-transformation-state s" transformation-state" variable } while drop + is-fresh-line@ { indentation-depth@ indent } if dup 8@ .hex8 space 0 is-fresh-line! @@ -3703,11 +3731,32 @@ allocate-transformation-state s" transformation-state" variable transformation-state transformation-state-output-metadata @ hex-output-metadata-latest-output-point @ 0 - hex-output-metadata-entry-type-line-comment - 4 roll + 3roll + ~ (data start, data length, string pointer) + + dup s" blank-line" stringcmp 0 = { + drop hex-output-metadata-entry-type-line-break 0 + add-hex-output-metadata-entry + exit + } if + + dup s" indent" stringcmp 0 = { + drop hex-output-metadata-entry-type-indent 2 + add-hex-output-metadata-entry + exit + } if + + dup s" deindent" stringcmp 0 = { + drop hex-output-metadata-entry-type-indent -2 + add-hex-output-metadata-entry + exit + } if + + hex-output-metadata-entry-type-line-comment swap add-hex-output-metadata-entry ; + ~ The tilde alternate is a very important word for the hex transform; it has ~ the task of implementing a special comment syntax which can be used by words ~ that generate binary output, to define comments which will appear as part of -- cgit 1.4.1