Skip to content

Commit 4986ebe

Browse files
committed
docs: update documents
1 parent 2392375 commit 4986ebe

File tree

6 files changed

+292
-5
lines changed

6 files changed

+292
-5
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ You can now translate JavaScript to wenyan-lang using the [wenyanizer](https://g
9595

9696
### Advance Usage
9797

98-
[API Specification](./documentation/API.md)
98+
[Compiler API Specification](./documentation/Compiler-API.md)
9999

100100
## Syntax Cheatsheet
101101

@@ -197,11 +197,16 @@ Arrays are 1-indexed.
197197
|`注曰。「「文言備矣」」。` | `/*文言備矣*/` |
198198
|`疏曰。「「居第一之位故稱初。以其陽爻故稱九」」。` | `/*居第一之位故稱初。以其陽爻故稱九*/` |
199199

200+
### Advance Features
201+
202+
- [Try...Catch](./documentation/Try-Catch.md)
203+
- [Nested Function Calls](./documentation/Nested-Function-Calls.md)
204+
- [Importing and Standard Library](./documentation/Importing.md)
200205

201206
## Renderer
202207

203208
```bash
204-
wenyan examples/turing.wy --render 圖靈機 --output .
209+
wenyan examples/turing.wy --render --title 圖靈機
205210
```
206211

207212
Render a wenyan script into an image that resembles pages from historical printed books.

documentation/API.md renamed to documentation/Compiler-API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# API Specification
2-
31
[Back to README](../README.md)
42

3+
# Compiler API Specification
4+
55
> 🚧 Please note this project is still under heavy development. The API might be changed frequently and this doc any not be always update to date. If you get any questions, feel free to raise an issue.
66
77

documentation/Importing.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
[Back to README](../README.md)
2+
3+
# Importing Scripts
4+
5+
## 1. Import statement (experimental)
6+
7+
Below is the new import syntax. Thanks everyone for the inspiration!
8+
9+
```
10+
吾嘗觀「「算經」」之書。方悟「正弦」「餘弦」「圓周率」之義。
11+
```
12+
13+
```
14+
from math import sin, cos, pi
15+
```
16+
17+
New usage of `今有`. `今有` is now used for exported/public variables, while `吾有` is private/scoped. Think of `今有` like `module.exports.x=`. Think of `吾有` like `var x=`
18+
19+
Example:
20+
21+
`易經.wy`(a.k.a. Random)
22+
23+
```
24+
吾有一數。曰四十二。名之曰「運數」。
25+
26+
今有一術。名之曰「運」。欲行是術。必先得一數。曰「甲」。乃行是術曰。
27+
注曰「「運者。隨機種子也」」
28+
昔之「運數」者。今「甲」是矣。
29+
是謂「運」之術也。
30+
31+
今有一術。名之曰「占」。是術曰。
32+
注曰「「線性同餘方法所得隨機數也」」
33+
有數四十二億九千四百九十六萬七千二百九十六。名之曰「模」。
34+
有數二千二百六十九萬五千四百七十七。名之曰「倍」。
35+
有數一。名之曰「增」。
36+
乘「倍」以「運數」。加其以「增」。除其以「模」。所餘幾何。昔之「運數」者。今其是矣。
37+
除「運數」以「模」。名之曰「卦」。
38+
乃得「卦」。
39+
是謂「占」之術也。
40+
```
41+
42+
`some_example.wy`(where you import random)
43+
44+
```
45+
吾嘗觀「「易經」」之書。方悟「占」之義。
46+
施「占」。書之。
47+
```
48+
49+
Notice that in `易經.wy` the random seed (運數) is not exported. while its setter (運) is exported, but not imported by `some_example.wy`. Only `` the generator is exported and imported, and can be used directly.
50+
51+
52+
### JS implementation details
53+
54+
(Python, Ruby are not implemented yet)
55+
56+
57+
JS Implementation uses `var MODULE_NAME = new function(){ ... }` trick to wrap imported modules. `今有` maps to `this.` So they can be accessed using `MOUDLE_NAME.identifier`. The import statements specifies which identifiers are actually required, and those that are are extracted from its scope using `var identifier = MODULE_NAME.identifier`. Therefore, `some_example.wy` compiles to this:
58+
59+
```
60+
var 易經 = new function() {
61+
var 運數 = 42;
62+
this.運 = () => 0;
63+
this.運 = function(甲) {
64+
/*"運者。隨機種子也"*/
65+
運數 = 甲;
66+
};
67+
this.占 = () => 0;
68+
this.占 = function() {
69+
/*"線性同餘方法所得隨機數也"*/
70+
var 模 = 4294967296;
71+
var 倍 = 22695477;
72+
var 增 = 1;
73+
var _ans49 = 倍 * 運數;
74+
var _ans50 = _ans49 + 增;
75+
var _ans51 = _ans50 % 模;
76+
運數 = _ans51;
77+
var _ans52 = 運數 / 模;
78+
var 卦 = _ans52;
79+
return 卦
80+
};
81+
};
82+
var 占 = 易經.占;
83+
var _ans48 = 占();
84+
console.log(_ans48);
85+
```
86+
87+
You can check out a more sophisticated example on the online IDE. In the IDE, you can import an example from another example, or the a module from standard lib.
88+
89+
`parser.compiler` has a new option called `reader`, which is a function you can provide to tell compiler how to read a module. The default for node.js is to look in current directory plus one directory down. For browser-side you might give it something fancy like AJAX calls or something.
90+
91+
When you build the CLI compiler, the source of the standard libraries are included, so you can still use it without having the `./lib` folder.
92+
93+
Please let me know if found any issue or have any suggestion!
94+
95+
96+
## 2. Standard Library implementers needed!
97+
98+
You think you can write wenyan? Please join us!
99+
100+
Currently in the `./lib` folder there are a couple of "stubs" such as `算經`(math) `位經`(bit ops) `易經`(random).
101+
102+
They contain many functions to be implemented in wenyan. e.g. The `sin()` function currently contains this HACK:
103+
104+
```
105+
今有一術。名之曰「正弦」。欲行是術。必先得一數。曰「甲」。乃行是術曰。
106+
施「Math.sin」於「甲」。名之曰「乙」。乃得「乙」。
107+
是謂「正弦」之術也。
108+
```
109+
110+
What we need to do is to replace `Math.sin` hack to a proper implementation (Taylor series?).
111+
112+
Our goal is to implement the most commonly used library functions. If you are familiar with one or two of them, please submit a pull request!
113+
114+
115+
## 3. Thanks!!
116+
117+
As you might have noticed, much of the syntax and many ideas are inspired by / borrowed from numerous posts and feature requests. Therefore, a thank you to everyone!
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[Back to README](../README.md)
2+
3+
# Nested Function Calls
4+
5+
## Stack-based way to nest function calls
6+
7+
Due to popular demand, nested function calls is now a feature. Here is a taste:
8+
9+
```
10+
注曰「「辛 = 戊(甲,丁(乙,丙))」」
11+
12+
夫「甲」。夫「乙」。夫「丙」。取二以施「丁」。取二以施「戊」。名之曰「辛」。
13+
```
14+
15+
More complex example for the courageous:
16+
17+
```
18+
注曰「「壬 = 丁(己(戊(甲,丁(乙,丙))),甲,乙)」」
19+
20+
夫「甲」。夫「乙」。夫「丙」。取二以施「丁」。取二以施「戊」。取一以施「己」。夫「甲」。夫「乙」。取三以施「丁」。名之曰「壬」。
21+
```
22+
23+
## How it works
24+
25+
The original syntax of wenyan already employs a stack-like behavior. You can use `` to refer to the previous unnamed variable, you can use `書之` to print all the unnamed variables, use `名之曰` to name them etc. Therefore, it is natural to use the same system for function calls.
26+
27+
Now I'll refer to the "list of unnamed variables" as the "stack" for convenience.
28+
29+
`` keyword pushes a variable in into the stack. `取n` marks the last `n` items on the stack as "usable". `以施f` takes all the items marked "usable" and apply function `f` that takes those items as arguments, and push the result onto the stack. Finally you can use `名之曰` to take stuff from back the stack and give them names for later use.
30+
31+
Recap on other features that already existed before this update:
32+
33+
- `` Clears the stack, discarding everything.
34+
- `` Refers to the most recent item on the stack. It also clears the stack. The reason for this is not techincal, but is purely done from Classical Chinese readability standpoint: if you use multiple `` in a row, it can be very confusing: `加其以其。乘其以其。昔之其者。今其是矣。`. Hence.
35+
36+
More examples, showing that you can also push items of an Array, math results, and so on to stack:
37+
38+
```
39+
注曰「「庚 = 丁(甲+乙,癸[3],癸.length)」」
40+
41+
加「甲」以「乙」。夫「癸」之三。夫「癸」之長。取三以施「丁」。名之曰「庚」。
42+
```
43+
44+
You can find more examples in `./examples/nested_fun.wy`
45+
46+
A thank you to @Lotayou and everyone for the discussion in #285 and #301. More functional programming goodies are on their way!
47+
48+
## Automatically curried functions
49+
50+
functions in wenyan are now automatically curried, that is, partially applied to return a new function.
51+
52+
```
53+
吾有一術。名之曰「丁」。欲行是術。必先得三數。曰「寅」曰「卯」曰「辰」乃行是術曰。
54+
乘「寅」以「卯」。加其以「辰」。乃得其。
55+
是謂「丁」之術也。
56+
57+
施「丁」於一於二。名之曰「半丁」。
58+
施「半丁」於三。書之。
59+
```
60+
61+
compiles to
62+
63+
```
64+
var 丁 = () => 0;
65+
丁 = function(寅) {
66+
return function(卯) {
67+
return function(辰) {
68+
var _ans1 = 寅 * 卯;
69+
var _ans2 = _ans1 + 辰;
70+
return _ans2
71+
};
72+
};
73+
};
74+
var _ans3 = 丁(1)(2);
75+
var 半丁 = _ans3;
76+
var _ans4 = 半丁(3);
77+
console.log(_ans4);
78+
```
79+
80+
Again, more FP on its way.

documentation/Testing.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
[Back to README](../README.md)
2+
13
## Testing
24

35
This project uses [Mocha](https://mochajs.org/) and [Chai](https://www.chaijs.com/) for unit testing and snapshot testing.
46

5-
You can run all the tests by
7+
You will need to build first
8+
9+
```bash
10+
npm run build
11+
```
12+
13+
Then you can run all the tests by
614

715
```bash
816
npm test

documentation/Try-Catch.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[Back to README](../README.md)
2+
3+
# Try-Catch
4+
5+
```
6+
姑妄行此。
7+
嗚呼。「「滅頂」」之禍。
8+
9+
如事不諧。豈「「ReferenceError」」之禍歟。
10+
吾有一言。曰「「本無此物。奈何用之」」。書之。
11+
12+
豈「「SyntaxError」」之禍歟。
13+
吾有一言。曰「「不合文法。不通之甚」」。書之。
14+
15+
豈「「TypeError」」之禍歟。
16+
吾有一言。曰「「物各其類。豈可混同」」。書之。
17+
18+
豈「「滅頂」」之禍歟。
19+
吾有一言。曰「「嗚呼哀哉。伏维尚飨」」。書之。
20+
21+
不知何禍歟。名之曰「奇禍」。
22+
吾有一言。曰「「人坐家中。禍從天降」」。書之。
23+
24+
乃作罷。
25+
26+
```
27+
28+
Which is roughly equivalent to this JavaScript:
29+
30+
```JavaScript
31+
try {
32+
var e = new Error(); e.name = "滅頂"; throw e;
33+
} catch(e) {
34+
if (e.name == "ReferenceError") {console.log("本無此物。奈何用之")}
35+
else if (e.name == "SyntaxError") {console.log("不合文法。不通之甚")}
36+
else if (e.name == "TypeError") {console.log("物各其類。豈可混同")}
37+
else if (e.name == "滅頂") {console.log("嗚呼哀哉。伏维尚飨")}
38+
else {var err = e; console.log("人坐家中。禍從天降")}
39+
}
40+
41+
```
42+
43+
If you do not need to catch the errors, you can do:
44+
45+
```
46+
姑妄行此。
47+
嗚呼。「「無足輕重」」之禍。
48+
如事不諧乃作罷。
49+
```
50+
51+
Which roughly equals to:
52+
53+
```JavaScript
54+
try{
55+
var e = new Error(); e.name = "無足輕重"; throw e;
56+
}catch(_){}
57+
```
58+
59+
You can also do this to catch any error:
60+
61+
```
62+
姑妄行此。
63+
嗚呼。「「事不關心」」之禍。
64+
如事不諧。不知何禍歟。書之。乃作罷。
65+
```
66+
67+
Which roughly equals to:
68+
69+
```JavaScript
70+
try{
71+
var e = new Error(); e.name = "事不關心"; throw e;
72+
}catch(e){
73+
console.log(e);
74+
}
75+
```
76+
77+
You can see details about the usage in `./examples/try.wy`.

0 commit comments

Comments
 (0)