Backcalls in macros get incorrectly compiled

オープン
#139 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
lua
領域
compilers

調査の方向性

最小限の backcall マクロの例を使い、生成された Lua を手書きのバージョンと比較する。まず、backcall に対するコンパイラのマクロ展開パスから始める。マクロで生成された backcall が、連続する $await 呼び出しを含め、明示的なコードのネストと戻り値の挙動を維持すれば完了とする。issue ではソースファイルもテストも指定されていない。

索引モデルが issue の本文から書いたものです。

説明

When defining a macro that 'returns' a backcall, it will not produce the expected code.

The given Yuescript code:

macro backcall = (fn) -> "() <- #{fn}()"

do
    $backcall async_fn
    print()

    () <- async_fn()
    print()

The above will compile to the following Lua code:

do
    async_fn(function() end)
    print(); --- Unrelated sidenote: Why the semicolon? Did you add it in the compiler for testing purposes and forgot to remove it?
    async_fn(function()
        return print()
    end)
end

I would expect the macro to produce the same code as the written-out version below it.


Just in the case that a more real-world example is useful: I stumbled across this issue when using this macro:

--- The code
--- 
--- ```yuescript
--- $await output_stream::write("foo", "bar")
--- ```
---
--- becomes
---
--- ```yuescript
--- (_, __async_result) <- output_stream::write_async("foo", "bar", nil)
--- output_stream::write_finish(__async_result)
--- ```
macro await = (call) ->
    object, method, args = call::match([[([a-zA-Z_][a-zA-Z0-9_]*)::([a-zA-Z_][a-zA-Z0-9_]*)%((.*)%)]])

    "
(_, __async_result) <- #{object}::#{method}_async(#{args}, nil)
#{object}::#{method}_finish(__async_result)
"

It's intended for working with Gio, a library for asynchronous IO https://docs.gtk.org/gio/method.OutputStream.write_async.html

Each successive $await call is intended to become nested in the previous backcall. However, what happens instead is that only the #{object}::#{method}_finish(__async_result) gets nested, then the block ends.

主要言語
Lua
スター
620
フォーク
45
平均マージ
9時間 5分
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

IppClub/YueScript のほかの issue

IppClub/YueScript の issue をすべて見る

似ている issue

Lua の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。