[Feature request] Smarter macros

未关闭
#220 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
lua
领域
compilers

调研方向

先从 YueScript 编译器中当前的 parser 和 macro 实现开始,然后检查 macro 调用和定义是如何表示的。将提议的语法和展开行为与现有的基于字符串的 macro 进行比较。完成的标准是支持简化的声明式语法,包括参数解析和生成的 parser 规则,并为使用示例提供编译器覆盖。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Lua
星标
620
派生
45
平均合并
9 小时 5 分钟
30 天内合并 PR
1

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

IppClub/YueScript 的其他 Issue

查看 IppClub/YueScript 的全部 Issue

相似的 Issue

更多 Lua Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。