Skip to content

Compiler API

Anthony Fu edited this page Apr 27, 2020 · 3 revisions

🚧 Please note this project is still under heavy development. The API might be changed frequently and this doc any not be always update to date. If you get any questions, feel free to raise an issue.

Installation

We provide a compiler runs both in Node.js and modern browsers.

Node.js

You can install the dependence by the following command.

npm install @wenyan/core
const Wenyan = require('@wenyan/core')
// or
const { compile } = require('@wenyan/core')
// or
import { compile } from '@wenyan/core'

Browsers

You can add the following line to the head of your html body.

<script src='https://unpkg.com/@wenyan/core/index.min.js'></script>
<script>
// scripts will be exposed to window.Wenyan
const compiled = Wenyan.compile('吾有一言。曰「「問天地好在。」」。書之。')
</script>

Exposed Functions

Execute

Source

function execute(source: string, options?: ExecuteOptions)

Parameters

Name Type Note
source string The Wenyan source code
options object Execute Options

Compile

Source

function compile(source: string, options?: CompilerOptions)

Parameters

Name Type Note
source string The Wenyan source code
options object Compiler Options

Compiler Options

Fields Default Value Type Note
lang "js" "js" | "py" | "rb" Target language, can be
romanizeIdentifiers "none" "none" | "pinyin" | "baxter" | "unicode" Romanize variable identifiers (e.g. to JIA2), can
strict false boolean Enables static type checking
importPaths [] string[] Specify the searching dirs for importing, the first match will returned. If you are using cli, it will automatically inject process.cwd() in to it.
allowHttp false boolean Allow importing from web resources
logCallback console.log function Redirect verbose debug log
errorCallback process.exit function Redirect error log
resetVarCnt true boolean Reset temporary variable counter

Execute Options

Execute Options extends all field in Compiler Options

Fields Default Value Note
outputHanzi true Convert numbers and bools to Hanzi
output console.log Redirect the output if needed