# Importing

# Import statement

吾嘗觀「「算經」」之書方悟「正弦」「餘弦」「圓周率」之義
from math import sin, cos, pi

今有 is used for exported/public variables, while 吾有 is private/scoped.

Example:

易經.wy (a.k.a. Random)

吾有名之曰「運數」

今有名之曰「運」欲行是術必先得「甲」乃行是術曰
	注曰「「運者。隨機種子也」」
	昔之「運數」「甲」是矣
是謂「運」之術也

今有名之曰「占」是術曰
	注曰「「線性同餘方法所得隨機數也」」
	名之曰「模」
	名之曰「倍」
	名之曰「增」
	「倍」「運數」「增」「模」所餘幾何昔之「運數」是矣
	「運數」「模」名之曰「卦」
	乃得「卦」
是謂「占」之術也

some_example.wy (where you import random)

吾嘗觀「「易經」」之書方悟「占」之義
「占」書之

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.

# Implementation details (Javascript)

(Python, Ruby are not implemented yet)

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:

var 易經 = new function() {
    var 運數 = 42;
    this. = () => 0;
    this. = function() {
        /*"運者。隨機種子也"*/
        運數 =;
    };
    this. = () => 0;
    this. = function() {
        /*"線性同餘方法所得隨機數也"*/
        var= 4294967296;
        var= 22695477;
        var= 1;
        var _ans49 =* 運數;
        var _ans50 = _ans49 +;
        var _ans51 = _ans50 %;
        運數 = _ans51;
        var _ans52 = 運數 /;
        var= _ans52;
        return};
};
var= 易經.;
var _ans48 = ();
console.log(_ans48);

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.

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.

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.

Please let me know if found any issue or have any suggestion!

# Standard Library implementers needed!

You think you can write wenyan? Please join us!

Currently in the ./lib folder there are a couple of "stubs" such as 算經(math) 位經(bit ops) 易經(random).

They contain many functions to be implemented in wenyan. e.g. The sin() function currently contains this HACK:

今有一術。名之曰「正弦」。欲行是術。必先得一數。曰「甲」。乃行是術曰。
	施「Math.sin」於「甲」。名之曰「乙」。乃得「乙」。
是謂「正弦」之術也。

What we need to do is to replace Math.sin hack to a proper implementation (Taylor series?).

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!