• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

代碼如詩 Parse JavaScript Code to Wenyan Lang

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

More Repositories

1

PomodoroLogger

Pomodoro Logger 🕤 -- When a time tracker meets Pomodoro and Kanban board
TypeScript
1,003
star
2

tidy

A tree visualization tool written in Rust
Rust
300
star
3

image-labeler-react

A react component that helps labeling images for object detection
TypeScript
41
star
4

lww-table

Lightweight & Fast LWW CRDT Table
Rust
30
star
5

throwable

A type-safe way to work with error (Haskell's Either | Rust's Result)
TypeScript
13
star
6

moon-crdt

13
star
7

fast-graph

Graph visualization with parallel optimization
Rust
9
star
8

loro-talk

9
star
9

loro-react-flow-example

TypeScript
8
star
10

ts-rust-template

TypeScript
7
star
11

priority_memory

Prioritized sampling method for prioritized experience replay
Python
7
star
12

wasm-size-inspector

TypeScript
7
star
13

SimUniverse

A funny project that simulates the civilizations in universe. Inspired by novel The Three-Body Problem.
Jupyter Notebook
5
star
14

get-app-icon

Get desktop application icons by Node.js
TypeScript
5
star
15

loro-websocket-deno-server-example

TypeScript
5
star
16

ts-rust-parallel-template

JavaScript
4
star
17

append-only-bytes

Rust
4
star
18

PulsarDetection

An EDA project on pulsar detection problem where the data is highly unbalanced
Jupyter Notebook
3
star
19

arref

Getting mutable references from the same array safely in Rust
Rust
3
star
20

moon-dag-view

JavaScript
3
star
21

twenty-four

Rust
3
star
22

GithubIssueCrawler

Crawling github issues (as bug report) for your auto software engineering research
Python
2
star
23

debug-log

Rust
2
star
24

bloom-filter

TypeScript
2
star
25

crdt-bench-native

Rust
2
star
26

vitest-ext-basic-example

JavaScript
2
star
27

Kaggle-Santa-s-Uncertain-Bags

Jupyter Notebook
1
star
28

udaML-student_intervention

HTML
1
star
29

ts-boilerplate

JavaScript
1
star
30

RageExpressionsMaker

Jupyter Notebook
1
star
31

inverted_pendulum

Jupyter Notebook
1
star
32

color-blind

JavaScript
1
star
33

udaML-boston_housing

Jupyter Notebook
1
star
34

udaML-creating_customer_segments

Jupyter Notebook
1
star
35

udCarND-LaneLines-P1

HTML
1
star
36

texas-odds

Texas Hold'em Odds Calculator
Rust
1
star
37

object-detection-labeler

TypeScript
1
star
38

wasm-template

JavaScript
1
star
39

udaML-smart_cab

Jupyter Notebook
1
star
40

Kaggle_DogVsCat

Python
1
star
41

zxch3n

1
star
42

chatgpt

TypeScript
1
star
43

js13k

TypeScript
1
star
44

no_std_test

Rust
1
star
45

whoTouchMyWiFi

Analysis the behaviors of the devices which connect to the same WiFi
Jupyter Notebook
1
star
46

crdt-bench

JavaScript
1
star
47

moon-expect

1
star
48

react-flow-example

Created with CodeSandbox
TypeScript
1
star