Extend your app with a new language

Background

Nowadays, more and more web apps need to provide an extension system for their web app. We need the clients to extend our systems, but we can’t trust the clients’ code. We need to prevent the clients from destroying or hacking the host system. It’s not safe, and we need an isolated environment on the web.
We have some solutions. Some apps run the extensions in the web workers. Some run them in the iframe. Even more, the WebAssembly, or using an interpreter in JS to provide an isolated environment.
They have their advantages and disadvantages. Some of them have terrible performances. Some of them are not entirely isolated. And the most annoying thing is it’s hard to use the same solution on some native apps (mobile).

The new language

I am introducing a new language named LichenScript. “Lichen” represents a thin layer. LichenScript is close to TypeScript. They have similar syntaxes and APIs.

https://www.lichenscript.com/

LichenScript is designed to be portable. We choose simple and stable grammar to make LichenScript easy to compile to C and JavaScript. The compiler is not complex because the relationship between the product and the source code is not complex. The code compiled to C can run on the most popular platforms, including the mobile platforms and WebAssembly(with the power of Emscripten). It’s great to share the code on both Web and Mobile.

fibonacci

LichenScript is statically typed, which is similar to TypeScript. The different thing is types help the code generation. When you compile LichenScript to C, the compiler uses the typing information in the product code to make the program faster. It’s is very difficult for the complex TypeScript to do this because it’s too flexible. Although performance is not the primary goal of LichenScript, we will try to make it as fast as possible.

When you compile LichenScript to JavaScript, the code can not access the js variable except you provide the bridge between them. The type checker ensures that.

Playground link.

You can try the language in the playground. Actually, it’s not hard to integrate the compiler into your app. Here we have a good example.

Use the compiler of LichenScript in JavaScript

The syntaxes

The most different syntax between LichenScript and TypeScript is pattern matching. The most different syntax between LichenScript and TypeScript is pattern matching. I am pretty excited to introduce this feature. Because many years ago, I tried to introduce pattern matching to JavaScript. I failed. Today, I introduced it to the language I designed.

It’s a handy feature to solve complex logic. I hope you like it. To know more about this, you can read the documents.

LichenScript uses most of the common language features in JavaScript such as lambda expressions. People can learn it very quickly. Advanced features such as async/await are under development.

LichenScript also borrows some concepts from OCaml and Rust. For example, `if` is an expression in LichenScript. Talk is cheap. How about downloading and trying it? 😉

Interoperability

The module system of LichenScript is quite interesting. You can still use the traditional npm to assemble the modules. Here is a very good example: lichenscript-fs.

Import the File Systems from npm

In the source code of lichenscript-fs, the module can directly import the source code of JavaScript and C to interoperate with them. It’s quite easy to provide the native API to LichenScript. In the future, the Rust library will be imported too.

We found that the binary’s size is quite small of LichenScript because the runtime is very small. A simple program that reads and writes JSON data uses only 100kb on macOS.

Further

The goal of LichenScript is to be a practical and stable language. We will try to keep it small and simple. Aggressive syntaxes will not be introduced. It’s trying to be an excellent “glue” language.

Conclusion

LichenScript is a new language. It’s still a lot of work to do. And I have a lot of interesting things about this language to share. If you are interested, please join us. Submit issues or PRs.

Discord: https://discord.gg/TJntnVwr

Twitter: https://twitter.com/cdz_solo

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store