findGlobals(): Distinguish between functions and non-functions

Aperta
#95 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
r
Ambito
tooling

Direzione di ricerca

Inizia leggendo gli entry point esistenti findGlobals() e globalsOf() e seguendo il modo in cui vengono classificati i simboli nelle chiamate quotate. Confronta i simboli in posizione di funzione con i simboli in posizione di valore usando gli esempi sum(), sum, sum + 2 e -sum dell’issue. Il lavoro è completato quando la chiamata di funzione viene risolta in base::sum() senza trattare erroneamente una variabile numerica locale con lo stesso nome come funzione globale.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Below is an example, where a local numeric variable sum is picked up by globalsOf(), despite the expression uses a function sum().

sum <- 42
expr <- quote( sum(1:10) )

names <- globals::findGlobals(expr)
print(names)
#> "{"   "sum" ":"

objs <- globals::globalsOf(expr)
str(objs)
#> List of 3
#>  $ {  :.Primitive("{") 
#>  $ sum: num 42
#>  $ :  :.Primitive(":") 
#>  - attr(*, "where")=List of 3
#>   ..$ {  :<environment: base> 
#>   ..$ sum:<environment: R_GlobalEnv> 
#>   ..$ :  :<environment: base> 
#>  - attr(*, "class")= chr [1:2] "Globals" "list"

Ideally, globalsOf() would pick up base::sum() instead. For that to happen, we need to refine globals::findGlobals() to record whether symbol sum refers to a function or not. That could be achieved by findGlobals() inspect the language object that sum is part of. For instance, from:

expr <- quote( sum(1:10) )
str(list(first = expr[[1]], class = class(expr), typeof = typeof(expr), length = length(expr)))
#> List of 4
#>  $ first : symbol sum
#>  $ class : chr "call"
#>  $ typeof: chr "language"
#>  $ length: int 2

expr <- quote( sum )
str(list(class = class(expr), typeof = typeof(expr), length = length(expr)))
#> List of 3
#>  $ class : chr "name"
#>  $ typeof: chr "symbol"
#>  $ length: int 1

## But it's not that easy

expr <- quote( sum + 2 )
str(list(first = expr[[1]], class = class(expr), typeof = typeof(expr), length = length(expr)))
#> List of 4
 $ first : symbol +
#>  $ class : chr "call"
#>  $ typeof: chr "language"
#>  $ length: int 3
 
expr <- quote( -sum )
str(list(first = expr[[1]], class = class(expr), typeof = typeof(expr), length = length(expr)))
#> List of 4
 $ first : symbol -
#>  $ class : chr "call"
#>  $ typeof: chr "language"
#>  $ length: int 2

we see that we can infer when symbol sum is the name of a function call or not.

One could have findGlobals(expr, annotate = TRUE) return this information as well, e.g. as attributes. But, is that shoehorning the original design of globals to much, which is basically an enhancement of codetools? It would be worth investigating if there are better AST tools for what's trying to be achieved. OTH, the above improvement might be good enough for 99% of the use cases.

Lingua principale
R
Stelle
29
Fork
3
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di futureverse/globals

Tutte le issue di futureverse/globals

Issue simili

Altre issue su R

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.