gittech. site

for different kinds of informations and explorations.

JavaScript but It's Ancient Chinese

Published at
4 days ago

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

Earn $100 Fast: AI + Notion Templates

Earn $100 Fast: AI + Notion Templates

Get the guide here

Do you want to make extra money quickly? This guide shows you how to create and sell Notion templates step by step. Perfect for beginners or anyone looking for an easy way to start earning online.

Why Download This Guide?

  • Start Making Money Fast: Follow a simple process to create templates people want and will buy.
  • Save Time with AI: Learn to use tools like ChatGPT to design and improve templates.
  • Join a Growing Market: More people are using Notion every day, and they need templates to save time and stay organized.

Includes Helpful Tools:

  • ChatGPT Prompts PDF: Ready-made prompts to spark ideas and create templates faster.
  • Checklist PDF: Stay on track as you work.

What’s Inside?

  • Clear Steps to Follow: Learn everything from idea to sale.
  • How to Find Popular Ideas: Research trends and needs.
  • Using AI to Create: Tips for improving templates with AI tools.
  • Making Templates User-Friendly: Simple tips for better design.
  • Selling Your Templates: Advice on sharing and selling on platforms like Gumroad or Etsy.
  • Fixing Common Problems: Solutions for issues like low sales or tricky designs.

Who Is This For?

  • Anyone who wants to make extra money online.
  • People who love using Notion and want to share their ideas.
  • Creators looking for a simple way to start selling digital products.

Get your copy now and start making money today!


πŸ’° Want to Earn 40% Commission?

Join our affiliate program and start making money by promoting well crafter prodicts! Earn 40% on every sale you refer.

πŸ”— Sign up as an affiliate here: Become an Affiliate