diff options
-rwxr-xr-x | awk/forth/f.awk | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/awk/forth/f.awk b/awk/forth/f.awk index 960fae5..6f6f546 100755 --- a/awk/forth/f.awk +++ b/awk/forth/f.awk @@ -42,7 +42,7 @@ BEGIN { } function repl() { - print "Welcome to the f.awk, a naive forth interpreter. Use 'bye' to exit." + print "Welcome to the f.awk, a naive forth interpreter.\nUse 'bye' to exit.\nUse 'words' to list all available words.\n" while (1) { printf "f> " if (getline input < "/dev/tty" <= 0) break @@ -288,13 +288,48 @@ function exit_program() { # New function to list all available words function list_words() { print "Available words:" + + # Arrays to hold built-in and user-defined words + split("", built_in_words) + split("", user_defined_words) + + # Populate the arrays for (w in dict) { split(dict[w], parts, ": ") if (parts[1] ~ /^word /) { - # If it's a user-defined word, display its name - print w ": " parts[2] " (User-defined)" + # Store user-defined words + user_defined_words[w] = parts[2] } else { - print parts[1] ": " parts[2] + # Store built-in words + built_in_words[w] = parts[2] + } + } + + # Sort built-in words manually + n = 0 + for (w in built_in_words) { + sorted_words[n++] = w + } + + # Simple bubble sort for the sorted_words array + for (i = 0; i < n; i++) { + for (j = i + 1; j < n; j++) { + if (sorted_words[i] > sorted_words[j]) { + # Swap + temp = sorted_words[i] + sorted_words[i] = sorted_words[j] + sorted_words[j] = temp + } } } + + # Print sorted built-in words + for (i = 0; i < n; i++) { + print sorted_words[i] ": " built_in_words[sorted_words[i]] + } + + # Print user-defined words + for (w in user_defined_words) { + print w ": " user_defined_words[w] " (User-defined)" + } } \ No newline at end of file |