
gittech. site
for different kinds of informations and explorations.
JavaScript but It's Ancient Chinese
ζθ¨θ½ζε¨ Wenyanizer
Introduction
Inspired by the great Wenyan Lang project, which compile ancient Chinese Language to javascript code, I build this project to do the opposite thing -- to parse javascript to Wenyan.
Usage
Install wenyanizer. Run
yarn add wenyanizer
Import and use it in your project
import {js2wy} from 'wenyanizer';
// Or use it in node.js
// const {js2wy} = require('wenyanizer');
js2wy("while(true){console.log('ε¦δΉ ')}")
// Output: 'ζηΊζ―γ\n εΎζδΈθ¨γζ°γγε¦δΉ γγγ\n ζΈδΉγ\nδΊδΊγ\n'
Play Online
VSCode Plugin by antfu
You can use Wenyanizer in Wenyan Lang VSCode plugin now.
Examples
Basic Grammars
for(var _rand = 0; _rand < 100; _rand++){
console.log("ε");
while(true){
var ε€©ε½ = Math.random();
if (ε€©ε½ < 0.5){
break
}
}
}
// ηΊζ―δΈηΎιγ
// εΎζδΈθ¨γζ°γγεγγγ
// ζΈδΉγ
// ζηΊζ―γ
// εΎζδΈθ‘γεδΉζ°γεγ
// 欲θ‘ζ―θ‘γεΏ
ε
εΎ
// δΉθ‘ζ―θ‘ζ°γ
// δΉεΎ Math.random()
// ζ―θ¬γεγδΉθ‘δΉγ
// ζ½γεγ
// εδΉζ°γ倩ε½γγ
// θ₯γ倩ε½γε°ζΌιΆεδΊεθ
γ
// δΉζ’γ
// δΊδΊγ
// δΊδΊγ
// δΊδΊγ
Wrapping JS Native Function Call
var ζ΅θ― = JSON.stringify(100);
var δΉ = JSON.stringify({});
// εΎζδΈθ‘γεδΉζ°γεγ
// 欲θ‘ζ―θ‘γεΏ
ε
εΎδΈη©γζ°γ_a0γγ
// δΉθ‘ζ―θ‘ζ°γ
// δΉεΎ JSON.stringify(_a0,)
// ζ―θ¬γεγδΉθ‘δΉγ
//
// ζ½γεγζΌδΈηΎγ
// εδΉζ°γζ΅θ―γγ
// εΎζδΈη©γεδΉζ°γδΈγ
// ζ½γεγζΌγδΈγγ
// εδΉζ°γδΉγγ
Wrapping Nested Structure
var η² = 100 % 99;
var δΉ = 100 * 200 + 35 * (48 - 10) * η²;
// ι€δΈηΎδ»₯δΉεδΉγζι€εΉΎδ½γ
// εδΉζ°γη²γγ
// δΉδΈηΎδ»₯δΊηΎγ
// εδΉζ°γε―
γγ
// ζΈεεε
«δ»₯δΈεγ
// δΉδΈεδΊδ»₯ε
Άγ
// εδΉζ°γηΈγγ
// δΉγηΈγδ»₯γη²γγ
// ε γε―
γδ»₯ε
Άγ
// εδΉζ°γδΉγγ
Hacks
Restricted by current Wenyan grammar, some hacks are required to make this compiler work. When you use grammar that is not well supported by Wenyan, there will be a bunch of functions being added to the top of the compiled file.
Wenyan is still evolving fast, thanks to the devoted author and the great community. We may not need to hack anymore very soon.
You can check the full list of polyfills in HACKS.md.
Roadmap
Name | Status |
---|---|
JS Code Compiled From Wenyan | β |
Closure | β |
Control: While/For/If/Else | β |
Object var a = {a: 0, b: 1, c: 2} |
β |
Array [[[0 ,2], [2, []]]] |
β |
+-*/ |
β |
--, ++, +=, -=, /= |
β |
Distinguish a++ and ++a |
β |
Wraping global object when necessary, such as Math , JSON |
β |
console.log |
β |
a.b = 3 |
β |
NewExpression | β |
Function | β |
this keyword |
β |
a[b] = 3 |
β |
ForOfStatement | β |
Wenyan Lib | WIP |
Wenyan Nested Function Call | β |
Optimize Curried Function | β |
RegExp | β |
switch | β |
bit op | β |
null undefined |
β |
DebugExpression | β |
WithStatement | β |
continue |
β |
debug exp | β |
ForInStatement | β |
throw try...catch...finally | β |
Modules (import, export, require) | β |
=== (will be compiled to == ) |
β |
let & const (will be compiled to var ) |
WON'T FIX |
es6 ^ (Class, Promise, yield, await...) | WON'T FIX |