Skip to content

Instantly share code, notes, and snippets.

@GiuseppeChillemi
Last active January 30, 2024 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GiuseppeChillemi/a654ebaa519fb042038906175cd73ace to your computer and use it in GitHub Desktop.
Save GiuseppeChillemi/a654ebaa519fb042038906175cd73ace to your computer and use it in GitHub Desktop.
Collect-Functions
Red [
title: "collect-functions"
description: "collects all the function names found inside a block (statically and not at runtime)"
]
add-function: func [
"Append the function name to the target container"
container [block!] "The container of functions"
name [word! set-word! lit-word! paren!] "The function to add or the paren that will be reduced to the name"
/index "Adds the path to reach the function from the top"
levels [block! path!] "The container of all the indexes needed to reach the current container block"
pos [integer!] "The position of the function relative to the last block"
/local
my-func
out-data
] [
my-func: either paren? name [name] [to-word name]
append container my-func
if index [
append levels pos
append/only container copy either path? levels [to-path levels] [levels]
take back tail levels
]
out-data
]
collect-functions: func [
"Collect al the function names found inside a block"
bl [block! file! url!] "the block of code"
/index "Adds the path to reach the function from the top as block"
/as-path "Add the path to the function as path in place of block"
/local
levels
rule
name
out-data
pos
] [
out-data: copy []
either as-path [levels: to-path copy []] [levels: copy []]
rule: [
pos: set name [set-word! | paren!]
['function | 'func | 'has | 'does | 'funct | 'make [quote function! | quote op! | quote routine!]] (add-function/:index out-data name levels index? pos )
|
pos: 'set
set name
[lit-word! | paren! ]
['function | 'func | 'has | 'does | 'funct | 'make [quote function! | quote op! | quote routine!]] (add-function/:index out-data name levels index? pos )
|
pos:
ahead [block! | paren!]
into [(if index [append levels index? pos]) any rule]
(take back tail levels)
|
skip
]
parse bl [any rule]
either index [new-line/all/skip out-data true 2] [new-line/all/skip out-data true 1]
]
;=== Examples ===
; collecting just the function names
;
;page: load https://codeberg.org/hiiamboris/red-spaces/raw/branch/master/programs/red-inspector.red
;probe collect-functions page
[
pinned?
wrap-data
pinned?
too-deep
draw
draw
inspect
history-move
history-back
history-forward
navigate
get-path
a-an
set-details
reload
jump
global-keys
on-resizing
red-inspector
]
;--- Collecting the function names and the path indexes neded to reach the function name
;The last value is should be removed from the path and AT should be used with it like: `at code/24/15/4/8/7/8 9`
;
;probe collect-functions/index page
;
[
pinned? [24 15 4 8 7 8 9]
wrap-data [24 15 4 10 4 13]
pinned? [24 15 4 10 4 16 4 3 5]
too-deep [24 18 11]
draw [24 18 15]
draw [24 18 21 5]
inspect [24 19]
history-move [24 23 8]
history-back [24 23 12]
history-forward [24 23 15]
navigate [24 23 18]
get-path [24 23 22]
a-an [24 23 26]
set-details [24 23 30]
reload [24 23 34]
jump [24 23 37]
global-keys [24 23 43]
on-resizing [24 23 50 3 2]
red-inspector [28]
]
;--- Picking the path indexes to reach a function with selection. Join it to the word containing the code
; to reach the block where the function definition is located. The last value is should be removed from
; the path and AT should be used with it like; `at code/24/15/4/8/7/8 9`
;
;
;probe collect-functions/index/as-path page
;
[
pinned? 24/15/4/8/7/8/9
wrap-data 24/15/4/10/4/13
pinned? 24/15/4/10/4/16/4/3/5
too-deep 24/18/11
draw 24/18/15
draw 24/18/21/5
inspect 24/19
history-move 24/23/8
history-back 24/23/12
history-forward 24/23/15
navigate 24/23/18
get-path 24/23/22
a-an 24/23/26
set-details 24/23/30
reload 24/23/34
jump 24/23/37
global-keys 24/23/43
on-resizing 24/23/50/3/2
red-inspector 28
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment