Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ラッパーとしてのreturn,break,continueが残ってしまっている #237

Open
salano-ym opened this issue Feb 19, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@salano-ym
Copy link
Member

salano-ym commented Feb 19, 2023

if式・match式・ブロック式で、return文・break文・continue文を使用するとラッパーが外れずに残ってしまう。
break文・continue文は普通の関数でも残ってしまう。

<: eval { return 1 } // == return
<: eval { break } // == break
<: eval { continue } // == continue
<: if true { return 1 } // == return
<: if true { break } // == break
<: if true { continue } // == continue

@r() { return 1 }
@b() { break }
@c() { continue }
<: r() // == 1 (これが正常)
<: b() // == break
<: c() // == continue

解決案

return文に関しては関数呼び出しでしているのと同様にブロック式の処理に外すべき場所でunWrapRetを挟む。
if式のブロックとブロック式のブロックが同じノードでreturnをどう処理すべきか区別できないので、ブロックの呼び出し元(代入文等)で処理する?

https://github.com/syuilo/aiscript/blob/master/src/interpreter/index.ts#L427-L429

@syuilo syuilo added the bug Something isn't working label Mar 15, 2023
@FineArchs
Copy link
Collaborator

グローバルスコープ直下のifやブロック式にreturn、関数の直下にbreakやcontinueが存在していること自体がおかしいのでエラーにしてもいいような気がします…

@salano-ym
Copy link
Member Author

returnに関しては関数内でも普通に取り出せてしまうけどどう扱うべきだろう
禁止するとして構文解析レベルで処理するかプラグインの段階で処理するか

@fff() {
  let a = eval {
    return 1
  }
  <: a // return<null>

  let b = if true {
    return 1
  }
  <: b // return<null>
}

@FineArchs
Copy link
Collaborator

  1. グローバルスコープ直下にreturn|break|continueがあったらエラー
  2. 関数スコープ直下にbreak|continueがあったらエラー
  3. return|break|continueを含むif|evalがexprとして扱われていたらエラー

これらを実装できればよさそうですかね?
個人的には、1.と2.はreturn文の処理内でスコープ名をチェックするような処理で済ませられるならそれがシンプルでいいと思います。
3.に関しては構文解析レベルのエラーにしたほうが早い気がします。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants