aiscript
aiscript copied to clipboard
ブロックをカッコではなくインデントで表現するようにしたい
オブジェクトリテラルとカッコが一緒で紛らわしいし、その方が簡潔 ただ破壊的変更になる
やりたいけどパーサが面倒そう
やっぱ曖昧性が生じそう
やっぱやりたい
構文例
@fn(x)
? (x = 42) << yes
? (x = 21)
no
.
yes
for #i, 100
<: ? ((i % 15) = 0) "FizzBuzz"
.? ((i % 3) = 0) "Fizz"
.? ((i % 5) = 0) "Buzz"
. i
#foo = do
#a = 1
#b = 2
(a + b)
<: foo // 3
@foo(x)
? (x = 42)
<< "foo"
"bar"
YAMLのパーサとか参考になりそう https://github.com/nodeca/js-yaml/blob/master/lib/loader.js
YAMLのPEGらしきものあった https://github.com/cierelabs/yaml_spirit/blob/master/doc/specs/yaml-peg.txt
メモ
The off-side rule can be implemented in the lexical analysis phase, as in Python, where increasing the indenting results in the lexer outputting an INDENT token, and decreasing the indenting results in the lexer outputting a DEDENT token.[5] These tokens correspond to the opening brace { and closing brace } in languages that use braces for blocks, and means that the phrase grammar does not depend on whether braces or indentation are used. This requires that the lexer hold state, namely the current indent level, and thus can detect changes in indentation when this changes, and thus the lexical grammar is not context-free: INDENT and DEDENT depend on the contextual information of the prior indent level.
https://en.wikipedia.org/wiki/Off-side_rule
👀 https://gist.github.com/biogeo/586d660a74d1f59d667763a1cdd7309b
コロン要るかなあ
コロン無しにするか
オフサイドルールだと関数に複数のコールバック関数渡す時とか曖昧性生じそう
こう?
@max3(x, y, z)
@max(a, b)
? (a > b)
a
.
b
max(max(x, y), z)
<: max3(1, 2, 3)
だね
無名関数が書けなさそう?
Arr:filter(arr, @(item)
Str:incl(item, "ai")
)
でいける
これだと、インデント数を見て後から構造化するって方法が取れないな
字句解析でブロックの開始終了を検出するならPEGじゃないほうが良いかも
オフサイドルールだと関数に複数のコールバック関数渡す時とか曖昧性生じそう
Pythonとかはこういうのどう対処するんだろう
これ今更だけど実装も仕様もとんでもなく複雑になるのであまりやりたくないかなあ
この案まだ生きてる?
うーむ
いろいろとめんどくさそう(構文解析的にも変更の影響の大きさ的にも)だからやめるか やるにしてもいっそ別言語かaltAiScriptみたいな感じにするとか
インデントで意味変えるとかは、構文もその前提で設計したいところかも
閉じます