Macros in 'when' clause don't capture variables
まだ誰も着手していません。
評価
調査の方向性
switch/when のパターンマッチングにおけるマクロ展開の経路から始め、直接的な配列パターンの例と比較してください。示されている $binop のケースを再現し、展開後に識別子が束縛されるかを追跡してください。完了の条件は、マクロベースのパターンが直接的なパターンと同じように a、b、c をキャプチャすること、またはその挙動が意図されたものとして文書化されることです。
索引モデルが issue の本文から書いたものです。
説明
I want to have something at least resembling algebraic data types in Yuescript - these are called enums in Rust, variants in OCaml, tagged unions in C. These are types that you can construct and they carry the underlying variant and some fields that the variant has. An example of this in Rust can be seen here: https://doc.rust-lang.org/rust-by-example/custom_types/enum.html
To that end I've been trying to use existing pattern matching functionality and macros to get this behavior. For the sake of example, let's say we want to encode variants of an enum as an array ['VariantName', ...fields], for example:
lang =
BinOp: (lhs, op, rhs) => ['BinOp', lhs, op, rhs]
Var: (name, expr) => ['Var', name, expr]
Const: (val) => ['Const', val]
Then I'd like to do pattern matching on different variants, but have a bit nicer syntax:
macro binop = (x,y,z) -> {
code: "['Binop', #{x}, #{y}, #{z}]"
type: "yue"
}
switch e
when $binop a,b,c
print "foo"
But this compiles to:
if {
'Binop',
a,
b,
c
} == e then
return print("foo")
end
But this isn't what I want, as it does not assign a, b, c variables for me to use inside the 'when' block. If I use the expected output of the macro directly:
switch e
when ['Binop', a, b, c]
print "bar"
I get what I expected - a, b, c are assigned values of e[2..4]:
local _type_0 = type(e)
local _tab_0 = "table" == _type_0 or "userdata" == _type_0
if _tab_0 then
local a = e[2]
local b = e[3]
local c = e[4]
if 'Binop' == e[1] and a ~= nil and b ~= nil and c ~= nil then
return print("bar")
end
end
This behavior is a bit unexpected, seems like macro evaluation happens at some later stage of compilation so instead of variable capture I get table comparison. Is this a bug or working as intended?
- 主要言語
- Lua
- スター
- 620
- フォーク
- 45
- 平均マージ
- 9時間 5分
- マージ済み PR(30日)
- 1
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
IppClub/YueScript のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 64/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
IppClub/YueScript の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 2 日以内に返信
-
follow-up
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
ksysoev/duet.nvim#265 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug: accuracy
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
PathOfBuildingCommunity/PathOfBuilding#10649 ·
メンテナーはふだん 1 日以内に返信
-
reMarkable
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
koreader/koreader#16485 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
nvim-orgmode/orgmode#1213 ·