-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[ANNOUNCEMENT] (Experimental) Macros #440
Comments
This looks so cool! Trying to find out what hacks we can do from this 😄 I also dumped this issue to the document. Thanks for the great work! |
Cool!
|
Good if you can implement as much as the |
The same macro above but with 元杂剧 flavor: 或云「「(冲末扮「甲」同外扮「乙」上)(「甲」诗云)坐守寒窗二十春,虀盐乐道不知贫。腹中晓尽古今事,命里不如天下人。小生「甲」便是。这一个是我同堂学业八拜交的弟兄,是「乙」。今有術「甲」。已知「乙」。遂 蓋謂「「吾有一術。名之曰「甲」。欲行是術。必先得「乙」。是術曰。「丙」。是謂「甲」之術也。」」 |
宏机制果然可以玩出很多花样。 仍以最基本的变量定义为例。它应包含类型、名字与值三段信息,所以理论上只要三个关键字就可以。
关键字“有”用于创建变量,相当于 这种极简语法我们或可称之为“诗经体”或“易经体”。以此为根本,加以宏机制,应该也易定制出像如今的“骈赋体”变体,不过多加一句导入模块语句,例如:
所以 wenyan-lang 的下一步(也许是长远)重要目标,就是制定最小集,原则上以单词关键字与繁体字为宜。 路漫漫其修远兮。 Happy New Year @LingDong- |
@LingDong- |
@lymslive
Compiles to: var 馬={}; |
As you might (not) have noticed, wenyan-lang strives to be more readable (for ancient Chinese people). Macros provide syntactic sugars to bring the 文采 of your code to the next level.
E.g. Now you can patch wenyan-lang's notorius print function like so:
Since we're beating JavaScript to macros, here is a rough C equivalence:
See
./examples/macro.wy
for detailed usage!How it works
In the
或云
statement, you specify the alternative syntax, using「甲」
,「乙」
,「丙」
etc. (十天干) to denote arguments, and in the蓋謂
statement you specify what that statment should be replaced by, referencing the arguments using the same 天干. (more than 10 arugments is considered too hideous and therefore not possible).It's just like regex.
Macros potentially solve other problems such as using 爲 instead of 為 etc. Currently, stdlib
列經
uses a couple of macros to make Higher-order functions such asmap
filter
andreduce
look prettier.So instead of
you can also do
where the syntax is defined by this macro in
列經
:Cautions
Your macros cannot start or end with identifier/arguments (e.g.
或云「「書「甲」」」
or或云「「「甲」焉」」
are BAD. It has to be或云「「書「甲」焉」」
). Otherwise the preprocesser cannot find out where it starts/ends and might not work as expected.Implementation details
The preprocessing is done before tokenization, and it searches all imported modules and gather all the macros and do the finding and replacing. (It might not be the most efficient way to do it, so if you have a better idea please let me know!). See
src/macro.js
for details.Your opinion
As you can see the macros are very powerful (maybe too powerful). And the downside is that it opens the door to a new dimension of hacks (probably wilder than I can imagine :)
Please let me know what you think! Thanks!
The text was updated successfully, but these errors were encountered: