Skip to content

Commit 4d04193

Browse files
authored
Merge pull request #429 from LingDong-/master
v0.1.3
2 parents 88d0eb0 + a69b9b1 commit 4d04193

35 files changed

+2388
-435
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
with:
1414
node-version: 10
1515
- run: npm ci
16-
- run: npm test
1716
- run: npm run build
17+
- run: npm test
1818

1919
publish-npm:
2020
needs: build
@@ -25,7 +25,6 @@ jobs:
2525
with:
2626
node-version: 10
2727
registry-url: https://registry.npmjs.org/
28-
- run: npm ci
2928
- run: npm run publish:ci
3029
env:
3130
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scripts-prepend-node-path=true
2+

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ You can now translate JavaScript to wenyan-lang using the [wenyanizer](https://g
9595
- [Plugin for Vim](https://github.com/voldikss/vim-wenyan) by [voldikss](https://github.com/voldikss)
9696
- [Plugin for Sublime Text](https://github.com/absop/SublimeWenyan) by [absop](https://github.com/absop)
9797

98+
### Browser Runtime
99+
100+
You can now run Wenyan scripts right in your html file.
101+
102+
Please refer to [Browser Runtime](./documentation/Runtime.md)
103+
98104
### Advance Usage
99105

100106
[Compiler API Specification](./documentation/Compiler-API.md)
@@ -201,9 +207,11 @@ Arrays are 1-indexed.
201207

202208
### Advance Features
203209

210+
- [Standard Library](./documentation/Standard-Lib.md)
204211
- [Try...Catch](./documentation/Try-Catch.md)
205212
- [Nested Function Calls](./documentation/Nested-Function-Calls.md)
206-
- [Importing and Standard Library](./documentation/Importing.md)
213+
- [Importing](./documentation/Importing.md)
214+
- [Macros](./documentation/Macros.md)
207215

208216
## Renderer
209217

@@ -230,14 +238,14 @@ Before opening an Issue, please check if it belongs to the below categories:
230238
|Standard library ([Math](https://github.com/LingDong-/wenyan-lang/issues/55)/[Bitwise ops](https://github.com/LingDong-/wenyan-lang/issues/2)/[Random](https://github.com/LingDong-/wenyan-lang/issues/87)) | ***** || In progress |
231239
|[Test suite](https://github.com/LingDong-/wenyan-lang/issues/38)| **** || In progress |
232240
|[Switch statements](https://github.com/LingDong-/wenyan-lang/issues/53)| *** | |
233-
|[Functional programming](https://github.com/LingDong-/wenyan-lang/issues/99) | *** | |
241+
|[Functional programming](https://github.com/LingDong-/wenyan-lang/issues/99) | *** | | Currying; Hi.-Ord. functions supported |
234242
|Stricter compiler | **** | |
235243
|Compiler for other languages | ** ||
236244
|[Plugins for editors](https://github.com/LingDong-/wenyan-lang/issues/77) | ** || VSCode, Vim, Sublime added |
237245
|Convert [js](https://github.com/LingDong-/wenyan-lang/issues/47)/[py](https://github.com/LingDong-/wenyan-lang/issues/67)/[anything](https://github.com/LingDong-/wenyan-lang/issues/73) back to wenyan | * ||
238246
|[Escaping](https://github.com/LingDong-/wenyan-lang/issues/84)/[generating](https://github.com/LingDong-/wenyan-lang/issues/128) special characters | *** | |
239-
|[Alternative syntax for 「「」」](https://github.com/LingDong-/wenyan-lang/issues/81)| ** | |
240-
|[Alternative symbols for 。](https://github.com/LingDong-/wenyan-lang/issues/93)| ** | |
247+
|[Alternative syntax for 「「」」](https://github.com/LingDong-/wenyan-lang/issues/81)| ** | | `『』` supported |
248+
|[Alternative symbols for 。](https://github.com/LingDong-/wenyan-lang/issues/93)| ** | | ``supported |
241249
|Online IDE [fonts](https://github.com/LingDong-/wenyan-lang/issues/5) and [vertical text](https://github.com/LingDong-/wenyan-lang/issues/9) | ** | |
242250
|[Rendering comment as small inline text](https://github.com/LingDong-/wenyan-lang/issues/148) | ** | |
243251
|More examples | ** ||

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!

documentation/Runtime.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[Back to README](../README.md)
2+
3+
# Wenyan Browser Runtime
4+
5+
You can now run Wenyan as normal Javscript script right in your html.
6+
7+
[**Check out the demo**](https://jsfiddle.net/antfu/u532ny49/2/)
8+
9+
## Installation
10+
11+
Add following script in the `<head>` of your html file.
12+
13+
```html
14+
<script src='https://unpkg.com/@wenyanlang/runtime'></script>
15+
```
16+
17+
That's all, you are good to go!
18+
19+
## Usage
20+
21+
To use wenyan in script, you **HAVE TO** specify `type="application/wenyan"` for the `<script>` tag. For example:
22+
23+
```html
24+
<script type="application/wenyan">
25+
吾有一數。曰三。名之曰「甲」。
26+
為是「甲」遍。
27+
吾有一言。曰「「問天地好在。」」。書之。
28+
云云。
29+
</script>
30+
```
31+
32+
### Scoped script
33+
34+
By default, all the variables in wenyan will exposed globally. For the previous example, `` is accessible by `window.甲`. If you do not want to messed up your globals, you can specify the `scoped` attr.
35+
36+
```html
37+
<script type="application/wenyan" scoped>
38+
吾有一數。曰三。名之曰「甲」。
39+
</script>
40+
```
41+
42+
### Remote scripts
43+
44+
You can import remote scripts as you will do for Javascript.
45+
46+
```html
47+
<script type="application/wenyan" src="https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/fizzbuzz.wy"></script>
48+
```
49+
50+
### DOM Hacks
51+
52+
There are some hacks you can do to access the DOM and browser APIs. This allows wenyan to do some realworld applications.
53+
54+
```html
55+
<script type="application/wenyan">
56+
施「((title)=>document.title=title)」於「「文言」」。
57+
施「((text)=>document.body.innerText=text)」於「「問天地好在。」」。
58+
</script>
59+
```

documentation/Standard-Lib.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!-- GENERATED FILE, DO NOT MODIFY-->
2+
3+
[Back to README](../README.md)
4+
5+
# Standard Library Cheatsheet
6+
7+
Last updated: Mon, 30 Dec 2019 15:32:37 GMT
8+
9+
10+
## Usage
11+
12+
```
13+
吾嘗觀「「算經」」之書。方悟「正弦」「餘弦」「圓周率」之義。
14+
```
15+
16+
## [易經](../lib/易經.wy)
17+
18+
| Wenyan | Javascript Equivalent |
19+
|---|---|
20+
21+
## [算經](../lib/算經.wy)
22+
23+
| Wenyan | Javascript Equivalent |
24+
|---|---|
25+
| [`圓周率`](../lib/算經.wy#L166) | `Math.PI` |
26+
| [`倍圓周率`](../lib/算經.wy#L169) | `Math.PI * 2` |
27+
| [`半圓周率`](../lib/算經.wy#L172) | `Math.PI / 2` |
28+
| [`四分圓周率`](../lib/算經.wy#L175) | `Math.PI / 4` |
29+
| [`自然常數`](../lib/算經.wy#L177) | `Math.E` |
30+
| [`歐拉常數`](../lib/算經.wy#L179) | `0.5772156649015329` |
31+
| [`黃金分割數`](../lib/算經.wy#L181) | `1.618033988749895` |
32+
| [`二之平方根`](../lib/算經.wy#L183) | `Math.SQRT2` |
33+
| [`二之對數`](../lib/算經.wy#L185) | `Math.LN2` |
34+
| [`十之對數`](../lib/算經.wy#L187) | `Math.LN10` |
35+
| [`不可算數乎`](../lib/算經.wy#L190) | `Number.isNaN` |
36+
| [`浮點移位`](../lib/算經.wy#L392) | `x * Math.pow(2, y), y is integer` |
37+
| [`析浮點數`](../lib/算經.wy#L428) | `N/A` |
38+
| [`正弦`](../lib/算經.wy#L472) | `Math.sin` |
39+
| [`餘弦`](../lib/算經.wy#L502) | `Math.cos` |
40+
| [`反正弦`](../lib/算經.wy#L510) | `Math.asin` |
41+
| [`反餘弦`](../lib/算經.wy#L537) | `Math.acos` |
42+
| [`正切`](../lib/算經.wy#L544) | `Math.tan` |
43+
| [`反正切`](../lib/算經.wy#L581) | `Math.atan` |
44+
| [`勾股求角`](../lib/算經.wy#L615) | `Math.atan2` |
45+
| [`勾股求弦`](../lib/算經.wy#L633) | `Math.hypot` |
46+
| [`對數`](../lib/算經.wy#L670) | `Math.log` |
47+
| [`指數`](../lib/算經.wy#L713) | `Math.exp` |
48+
| [``](../lib/算經.wy#L749) | `Math.pow` |
49+
| [`平方根`](../lib/算經.wy#L773) | `Math.sqrt` |
50+
| [`絕對`](../lib/算經.wy#L834) | `Math.abs` |
51+
| [`取頂`](../lib/算經.wy#L839) | `Math.ceil` |
52+
| [`取底`](../lib/算經.wy#L844) | `Math.floor` |
53+
| [`取整`](../lib/算經.wy#L859) | `Math.round, but rounded away from zero when the fractional part is exactly 0.5` |
54+
| [`捨餘`](../lib/算經.wy#L873) | `Math.trunc` |
55+
| [`正負`](../lib/算經.wy#L883) | `Math.sign` |
56+
57+
## [籌經](../lib/籌經.wy)
58+
59+
| Wenyan | Javascript Equivalent |
60+
|---|---|
61+
| [`求和`](../lib/籌經.wy#L1) | `reduce((a,b)=>a+b)` |
62+
63+
## [位經](../lib/js/位經.wy)
64+
65+
| Wenyan | Javascript Equivalent |
66+
|---|---|
67+
68+
69+
70+
71+
## Help update this cheatsheet
72+
73+
This cheatsheet is generated direct from stdlibs. There are still a lot of functions are not listed above. If you would like to hep update this cheatsheet, here is the steps.
74+
75+
Add comments in the stdlib files (one line above the function/value), the format should look like this:
76+
```
77+
注曰「「餘弦。同Javascript之Math.cos也。」」
78+
```
79+
80+
After you fill the comments, you need to update the document by running
81+
```bash
82+
npm run docs:update
83+
```
84+
85+
Check the output document out and submit a pull request. Thank you!

examples/README.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525
| [nested_fun.wy][22] | 函数 |
2626
| [obj.wy][23] | 对象 |
2727
| [pi_leibniz.wy][24] | 萊布尼茲圓周率估算 |
28-
| [quicksort.wy][25] | 快速排序 |
29-
| [quicksort_inplace.wy][26] | 快速排序2 |
30-
| [quine.wy][27] | [自產生程式][2] |
31-
| [quine2.wy][28] | 自產生程式2 |
32-
| [selectionsort.wy][29] | 選擇排序 |
33-
| [sieve.wy][30] | 埃氏篩 |
34-
| [sqrt_newton.wy][31] | 牛頓求根法 |
35-
| [turing.wy][32] | 圖靈機 |
36-
| [draw_heart.wy][33] | 畫心 |
28+
| [pi_liuhui.wy][25] | 劉徽割圓術求圓周率 |
29+
| [quicksort.wy][26] | 快速排序 |
30+
| [quicksort_inplace.wy][27] | 快速排序2 |
31+
| [quine.wy][28] | [自產生程式][2] |
32+
| [quine2.wy][29] | 自產生程式2 |
33+
| [selectionsort.wy][30] | 選擇排序 |
34+
| [sieve.wy][31] | 埃氏篩 |
35+
| [sqrt_newton.wy][32] | 牛頓求根法 |
36+
| [turing.wy][33] | 圖靈機 |
37+
| [draw_heart.wy][34] | 畫心 |
3738

3839
[1]: https://en.wikipedia.org/wiki/Fizz_buzz "Fizz buzz"
3940
[2]: https://zh.wikipedia.org/wiki/自產生程式 "自產生程式"
@@ -59,12 +60,13 @@
5960
[22]: nested_fun.wy
6061
[23]: obj.wy
6162
[24]: pi_leobniz.wy
62-
[25]: quicksort.wy
63-
[26]: quicksort_inplace.wy
64-
[27]: quine.wy
65-
[28]: quine2.wy
66-
[29]: selectionsort.wy
67-
[30]: sieve.wy
68-
[31]: sqrt_newton.wy
69-
[32]: turing.wy
70-
[33]: draw_heart.wy
63+
[25]: pi_liuhui.wy
64+
[26]: quicksort.wy
65+
[27]: quicksort_inplace.wy
66+
[28]: quine.wy
67+
[29]: quine2.wy
68+
[30]: selectionsort.wy
69+
[31]: sieve.wy
70+
[32]: sqrt_newton.wy
71+
[33]: turing.wy
72+
[34]: draw_heart.wy

examples/macro.wy

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
或云「「書「甲」焉」」。
2+
蓋謂「「吾有一言。曰「甲」。書之」」。
3+
4+
書「「問天地好在」」焉。
5+
6+
注曰「「書「「問天地好在」」焉。」」
7+
疏曰「「凡居「「」」與『』中之宏。不予展開」」
8+
9+
吾嘗觀「「列經」」之書。方悟「遍施」「篩剔」「左併」之義。
10+
11+
12+
吾有一術。名之曰「加一」。欲行是術。必先得一數曰「甲」。是術曰。
13+
加一於「甲」。乃得矣。
14+
是謂「加一」之術也。
15+
16+
吾有一術。名之曰「比三」。欲行是術。必先得一數曰「甲」。是術曰。
17+
若「甲」大於三者乃得陽。若非乃得陰也。
18+
是謂「比三」之術也。
19+
20+
吾有一術。名之曰「相加」。欲行是術。必先得二數曰「甲」曰「乙」。是術曰。
21+
加「甲」以「乙」。乃得矣。
22+
是謂「相加」之術也。
23+
24+
25+
吾有一列。名之曰「某列」。充「某列」以一以二以三以四以五以六。
26+
27+
28+
施「遍施」於「加一」於「某列」。書之。
29+
30+
凡「某列」皆「加一」其上者。書之。
31+
32+
33+
施「篩剔」於「比三」於「某列」。書之。
34+
35+
唯「某列」中得「比三」者。書之。
36+
37+
38+
施「左併」於「相加」於零於「某列」。書之。
39+
40+
併「某列」於零以「相加」者。書之。
41+
42+
43+
施「左併」於「相加」於零。名之曰「求和」。
44+
45+
施「求和」於「某列」。書之。

0 commit comments

Comments
 (0)