gittech. site

for different kinds of informations and explorations.

JavaScript but It's Ancient Chinese

Published at
Feb 8, 2025

文言轉換器 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