[Feature request] Smarter macros

Aperta
#220 5 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
25/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
lua
Ambito
compilers

Direzione di ricerca

Inizia con l’implementazione attuale del parser e delle macro nel compilatore YueScript, quindi esamina come sono rappresentate le chiamate e le definizioni delle macro. Confronta la sintassi proposta e il comportamento di espansione con le macro esistenti basate su stringhe. Il lavoro si considera completato quando è supportata la sintassi dichiarativa semplificata, inclusi il parsing degli argomenti e le regole del parser generate, con la copertura del compilatore per l’esempio d’uso.

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

Descrizione

The current string-based macros, while powerfull, are also very difficult to actually use (at least in any way that couldn't just be done by a function). My suggestion would thus be to implement a simplified macro syntax which behaves more like Rust's declarative macros.

The idea is that one could write code like this:

macro typed_func = ( ( $( $arg:ident : $type:expr $( = $default_value:expr )? ),* $(,)? ) $arrow:token $body:block ) ->
    ( $(arg $( = $default_value )? ),* ) $arrow
        $(
        assert(type($arg) == $type)
        )*
        $block

--- Usage example:
f = $typed_func((x: "number", y: "number") -> x + y)
--- Expansion:
f = (x, y) ->
    assert(type(x) == "number")
    assert(type(y) == "number")
    x + y

Now, it would be very difficult (if not impossible) to actually implement this with the current parser. I would suggest the following solution to that: Whenever the parser hits a macro call, it could be made to parse a very loose subset of the language. It would essentially just need to be able to handle strings and comments - everything else does not get parsed, but read as literal text. Then, the macro gets used to generate new parser rules. Finally, the macro call is parsed with the now-extended parser.

This would by far be the easiest solution, but it would have the downside of requireing parenthesis around the macro's arguments. I would argue that two extra characters are a good tradeoff to make for getting a significantly more useful feature in return.

Alternatively, if the parsing library used by the YueScript compiler supports injecting new rules on-the-fly, parsing could also be done by making each macro definition immediatley modify the parser.

Lingua principale
Lua
Stelle
620
Fork
45
Merge medio
9h 5m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 IppClub/YueScript

Tutte le issue di IppClub/YueScript

Issue simili

Altre issue su Lua

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.