[Feature request] Smarter macros

Abierto
#220 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
lua
Área
compilers

Línea de trabajo

Comienza con la implementación actual del parser y de las macros en el compilador de YueScript y, a continuación, inspecciona cómo se representan las llamadas y definiciones de macros. Compara la sintaxis propuesta y el comportamiento de expansión con las macros existentes basadas en strings. Se considera terminado cuando se admita la sintaxis declarativa simplificada, incluido el análisis de argumentos y las reglas de parser generadas, con cobertura del compilador para el ejemplo de uso.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Lua
Estrellas
620
Forks
45
Merge medio
9 h 5 min
PR fusionados (30 d)
1

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de IppClub/YueScript

Todos los issues de IppClub/YueScript

Issues similares

Más issues de Lua

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.