|
| 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! |
0 commit comments