Skip to content

Commit 9cd746e

Browse files
LingDong-Lingdong Huang
authored andcommitted
2 parents ad5e422 + 4c5f000 commit 9cd746e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ Arrays are 1-indexed.
211211
- [Try...Catch](./documentation/Try-Catch.md)
212212
- [Nested Function Calls](./documentation/Nested-Function-Calls.md)
213213
- [Importing](./documentation/Importing.md)
214+
- [Macros](./documentation/Macros.md)
214215

215216
## Renderer
216217

documentation/Macros.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[Back to README](../README.md)
2+
3+
# Macros (Experimental)
4+
5+
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.
6+
7+
E.g. Now you can patch wenyan-lang's notorius print function like so:
8+
9+
```
10+
或云「「書「甲」焉」」。
11+
蓋謂「「吾有一言。曰「甲」。書之」」。
12+
13+
書「「問天地好在」」焉。
14+
```
15+
16+
Since we're beating JavaScript to macros, here is a rough C equivalence:
17+
18+
```
19+
#define 書(甲)焉 吾有一言。曰甲。書之
20+
書("問天地好在")焉。
21+
```
22+
23+
See `./examples/macro.wy` for detailed usage!
24+
25+
## How it works
26+
27+
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).
28+
29+
It's just like regex.
30+
31+
Macros potentially solve other problems such as using 爲 instead of 為 etc. Currently, stdlib `列經` uses a couple of macros to make Higher-order functions such as `map` `filter` and `reduce` look prettier.
32+
33+
So instead of
34+
35+
```
36+
施「遍施」於「加一」於「某列」。書之。
37+
```
38+
39+
you can also do
40+
41+
```
42+
凡「某列」皆「加一」其上者。書之。
43+
44+
```
45+
46+
where the syntax is defined by this macro in `列經`:
47+
48+
```
49+
或云「「凡「甲」皆「乙」其上者」」。
50+
蓋謂「「施「遍施」於「乙」於「甲」」」
51+
```
52+
53+
## Cautions
54+
55+
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.
56+
57+
58+
## Implementation details
59+
60+
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.
61+
62+
## Your opinion
63+
64+
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 :)
65+
66+
Please let me know what you think! Thanks!

0 commit comments

Comments
 (0)