#3 Quoting functions to inject function references in macros or other operations

Otwarty
otworzone 4 dni temu przez Archivist · 0 komentarzy
Archivist skomentował(-a) 4 dni temu

The goal would be to be able to insert a word-ref or native-word-ref type that would decay into a word or native-word upon either an unquoting operation or being read in a native function. This would open the way to have array.map and other powerful high order functions, as well as allow macros to contain executable code (for example, by making them selectively unquote elements) allowing some better creative prospects.

Syntax

'token creates a function reference to the applicable token that will be pushed to the stack.

fn.unquote extracts, pops, then runs the function that is on top of the stack (schedules it instead of the current function with before now as its new index).

# array
# fn-ref array 
fn array.foreach do
    dup array.size 0
    # fn-ref array end it
    2 pluck 2 pluck == end_loop jump_if
    # fn-ref array end it
    loop:
        dup 4 pluck
        # fn-ref array end it it array
        array.index 
        # fn-ref array end it elem
        5 pluck fn.unquote
        # fn-ref array end it
        1 +
   2 pluck 2 pluck > loop jump_if
   end_loop: drop drop drop drop
end
[ 1 2 3 ] 'print_int swap array.foreach
expected output:
1 2 3
The goal would be to be able to insert a `word-ref` or `native-word-ref` type that would decay into a `word` or `native-word` upon either an unquoting operation or being read in a native function. This would open the way to have `array.map` and other powerful high order functions, as well as allow macros to contain executable code (for example, by making them selectively unquote elements) allowing some better creative prospects. ## Syntax `'token` creates a function reference to the applicable token that will be pushed to the stack. `fn.unquote` extracts, pops, then runs the function that is on top of the stack (schedules it instead of the current function with before now as its new index). ``` # array # fn-ref array fn array.foreach do dup array.size 0 # fn-ref array end it 2 pluck 2 pluck == end_loop jump_if # fn-ref array end it loop: dup 4 pluck # fn-ref array end it it array array.index # fn-ref array end it elem 5 pluck fn.unquote # fn-ref array end it 1 + 2 pluck 2 pluck > loop jump_if end_loop: drop drop drop drop end ``` ``` [ 1 2 3 ] 'print_int swap array.foreach ``` ###### expected output: ``` 1 2 3 ```
Archivist dodał(-ęła) etykietę
enhancement
4 dni temu
Archivist przypisuje to na siebie 4 dni temu
Zaloguj się, aby dołączyć do tej rozmowy.
Brak kamienia milowego
Brak przypisanych
Uczestnicy 1
Powiadomienia
Termin realizacji

Brak ustawionego terminu realizacji.

Zależności

To zgłoszenie nie ma w tej chwili żadnych zależności.

Ładowanie…
Nie ma jeszcze treści.