wenyan-lang / wenyan

文言文編程語言 A programming language for the ancient Chinese.
https://wy-lang.org/
MIT License
19.78k stars 1.1k forks source link

Syntax Proposal for end marker of script file #474

Open lymslive opened 4 years ago

lymslive commented 4 years ago

Syntax Proposal for end marker of script file

Inspired Background

In the less well-know VimL language, there is finish statement to finish the current script, let compiler ignore anything after that to the really end of script file. In Perl language there is __END__ marker, and further more, __DATA__ marker has additional functionality beside end the script. The content lines after __DATA__ can be accessed by a special file handle <DATA> in current script.

The useful thing for __END__ marker is putting larger documentation in the end of script file. And __DATA__ can be served to self-unit test that need to read from input, which normally from STDIN or external file, while with __DATA__ the test data goes together with test code and main script function.

By the way, the self-test also common in python like if __name__ == __main__.

I think these functionality is useful for self-test/doc for stdlib, or may more handy for user custom util lib scripts, since stdlib may worth more professional automatical unit-test tools.

Syntax Keyword Proposal

For __END__ marker, I think of several characters:

盖棺定论
钦此
缄

And since __END__ marker is only optional appear once in a script file, it may acceptable to use a special punctuation, solely or after a keyword.

For __DATA__ marker, how about just 缄曰?

For if __name__ == __main__, we may say 开天辟地?

Assumption Example

吾有一術。名之曰「葵花宝典」。是術曰。
  ⋯⋯
是謂「葵花宝典」之術也。
缄
脚本结束了,咱们随便聊些什么吧,编译器不 care 的。
“葵花宝典”出自金庸先生武侠名著《笑傲江湖》
……
这可以唠很长
吾有一術。名之曰「葵花宝典」。欲行是術。必先得一言。曰「侠客」。是術曰。
  夫「侠客」,书之。
  吾有一言,曰,“正在自宫”,书之。
是謂「葵花宝典」之術也。
若开天辟地,乃
  恆為是
    启缄,// read input, like in perl: $_ = <DATA>
    名之曰「侠客」,
    施「葵花宝典」于「侠客」。
  云云
也
缄曰
任我行
东方不败
林平之
岳不群

Since the lines after 缄曰 is data, the newline is sensible, but the first newline after 缄曰 may skip.

 Dependent

__END__ is easy, it will be another issue if we want to normalize the documentation after __END__.

While __DATA__ is dependent the stdlib functionality for read input or file handle alike.

lymslive commented 4 years ago

After write this sample, I suddenly think that we may express the concept of debug/test in wenyan-lang as 辟邪. “辟邪剑法” is alias to “葵花宝典” in 《笑傲江湖》.

吾有一術。名之曰「辟邪剑法」。欲行是術。必先得一言。曰「侠客」。是術曰。
  夫「侠客」,书之。
  吾有一言,曰,“扫荡群魔”,书之。
是謂「辟邪剑法」之術也。
若为辟邪故,乃
  恆為是
    启缄,
    名之曰「侠客」,
    施「辟邪剑法」于「侠客」。
  云云
也
缄曰
林远图
MountLion commented 4 years ago

也可以考虑乱曰