Released Exevalator 2.2: Now Compatible with TypeScript and Usable in Web Browsers

RINEARN recently released version 2.2 of the open-source expression evaluation library, Exevalator.

With this update, Exevalator now supports TypeScript and can run directly in web browsers! This enables web pages and the front-end of web applications to safely and easily calculate user-input expressions.

Let's try it out:

- Demonstration App -


When you click the 'Calculate' button above, the input expression and the value of the variable 'x' will be evaluated, and the result will be displayed. This calculation is powered by the newly added TypeScript version of Exevalator introduced in this update.

Now, let's take a deeper look into the details!

- Table of Contents -

What is Exevalator?

There Are Many Situations Where You Want to Evaluate Strings as Expressions, but It's Surprisingly Tricky to Do

Exevalator is a library designed for parsing and evaluating strings as mathematical expressions, as demonstrated in the example above. It can be used not only for user inputs but also for scenarios such as evaluating expressions read from configuration files. Situations like these occur quite often, don't they?

However, this kind of functionality - interpreting and executing strings as expressions - is generally not supported natively by compiled languages. While many interpreted (scripting) languages support such features, they often lack fine-grained restrictions on what operations can be executed. For this reason, they are often considered impractical or insecure for use in production environments. The 'eval' function of JavaScript is prime examples of this.

In short, even though the need to evaluate simple expressions given as string values is quite common across various projects, there aren't many universally accessible and straightforward solutions available. Developers often resort to either writing their own expression parsers or embedding an interpreter for a scripting language to handle such tasks.

Enter Exevalator: A Compact, Expression-Focused Interpreter Usable Across Many Languages

To address this issue, we developed Exevalator with the idea that "a lightweight and portable interpreter, focused solely on evaluating expressions, would be extremely convenient if made available across various programming languages."

Exevalator is implemented as a single-file library, which means you can simply drop the file into your project's source code folder and start using it right away.

As of the latest version, 2.2, Exevalator has been ported to six programming languages: Java, C#, C++, Rust, Visual Basic (.NET), and TypeScript, covering a wide range of use cases across different fields.

Notably, with the addition of the TypeScript version in this release, Exevalator now supports four major platforms:

Open Source and Public Domain

Exevalator is an open-source library, and its source code is publicly available on GitHub at the following repository:

Usage instructions are thoroughly explained in the documentation within the repository.

The library is dual-licensed under Unlicense and CC0, both of which place the code in the Public Domain. You are free to modify and reuse the code as you like.

For example, error messages often need to be customized for the application you're embedding Exevalator in. Feel free to tweak them as much as you need! You'll find them listed in a centralized format near the beginning of the source code.

Surprisingly Fast

So far, we've highlighted the simplicity and compactness of Exevalator, but it's worth noting that it is also surprisingly fast. RINEARN has been developing various interpreters for over a decade, and we've incorporated a decent balance of optimization techniques into Exevalator.

Specifically, in repeated evaluations of the same expression (e.g., only the values of variables change), Exevalator can achieve up to hundreds of millions of calculations per second (hundreds of MFLOPS) in benchmark tests. Tasks like numerical integration finish almost instantly, and it can handle moderately sized data processing tasks with ease.

In cases where the expression changes with each evaluation, the additional cost of parsing the syntax reduces performance, but it still handles tens of thousands to hundreds of thousands of expression-evaluations per second.

Although evaluating and calculating expressions from strings might seem like a computationally heavy task, as shown above, Exevalator is actually quite fast and lightweight. We encourage you to try it out in various scenarios!

About TypeScript

As mentioned earlier, TypeScript has been newly added as a supported language in Exevalator 2.2. You can find usage instructions on this page.

Now, let's take a moment to briefly answer the question: "What is TypeScript?"

That said, diving too deeply into its background might unintentionally spark a bloody language debate, so let's stick to a light and general overview.

First, About JavaScript

To explain TypeScript, we first need to touch on its foundation: JavaScript.

JavaScript is an interpreted (scripting) language that runs on web browsers. Because JavaScript interpreters are built into web browsers themselves, it runs without requiring any special development or execution environment - just a web browser is enough.

Modern web pages aren't just simple, static article pages like Wikipedia. Many pages are highly interactive and respond dynamically to user actions. Many parts of these dynamic behaviors is powered by programs written in JavaScript.

JavaScript alone is powerful enough to create certain level of functional web applications. (If you want to go beyond certain limits, you'll also need to integrate server-side programming to handle more complex tasks.)

Challenges Accumulated in JavaScript → Rise of "AltJS"

JavaScript has been around for quite a while - it was introduced almost 30 years ago.

As is often the case with widely adopted, long-standing programming languages, certain issues began to emerge over time: "Maybe this part of the language specification wasn't ideal after all?", "Recent trends in language design lean toward different approaches,", or "For large-scale development, these kinds of features would be helpful, but they haven't been adopted into the language." Additionally, there's a group of people who simply "don't like the syntax."

Of course, this isn't the fault of the language developers - it's impossible to predict these issues before the language is used in so many different scenarios. This is simply the fate of a long-lasting language. Moreover, for many years, JavaScript was practically the only choice for writing browser-compatible code, which made it natural for such concerns to accumulate, because you couldn't just gswitch to a different language.h

By the way, I don't dislike JavaScript at all - just to be clear.

While JavaScript has evolved (via ECMAScript specifications) to address many of these issues, there has also been a trend of creating entirely new languages, at a level where they could be considered JavaScript alternatives. These are often referred to as "AltJS" (short for "Alternative JavaScript"). You can find more about this concept by searching for AltJS.

Languages categorized as AltJS typically introduce their own unique syntax. Developers write code in this new syntax, and then a compiler translates it into JavaScript. This approach works well because, while it's challenging to get all major browsers to support a new language natively, compiled JavaScript runs seamlessly in today's browsers.

You might think of JavaScript's role for AltJS as being similar to assembly languages for natively compiled languages. Indeed, a new virtual assembly language called WebAssembly has recently emerged and gained popularity. It runs directly on web browsers, offering better support for some use cases compared to JavaScript.

And Then Came TypeScript

Now, let's talk about TypeScript. TypeScript emerged as part of the AltJS trend and was developed and released by Microsoft. Here are its key features:

Because of these characteristics, TypeScript has become quite popular and widely adopted.

One of its strongest points is probably that it's a superset of JavaScript. It feels ideal for situations where developers want to stick to the general "feel" and knowledge of JavaScript but write code that's more structured, robust, or efficient for larger projects.

On the other hand, developers who have been working with JavaScript for years might find TypeScript's features a bit cumbersome for writing small, quick scripts. In such cases, they might end up skipping a lot of the features, ultimately deciding, "Well, plain JavaScript works fine for this."

To sum up, TypeScript is a language with the above characteristics in a nutshell.

Let's Use Exevalator with TypeScript!

Preparation

Finally, let's quickly go through the steps to use Exevalator in TypeScript.

First, install Node.js on your computer. Node.js provides the environment needed to run JavaScript code locally (outside the browser) and also serves as the foundation for developing with TypeScript.

Next, clone the Exevalator repository to a suitable location on your local machine and navigate to the typescript folder:

cd <your_preferred_location>
git clone https://github.com/RINEARN/exevalator.git
cd typescript

Inside the 'typescript' folder, you'll find the TypeScript version of Exevalator, along with sample code, test code, and other files. To compile and execute the code, set up the necessary environment with the following commands:

npm install --save-dev typescript
npm install --save-dev @types/node
npm install --save-dev esbuild

The '--save-dev' flag is used to ensure that the environment is installed only within this folder (and does not affect the global environment or other folders).

Running the Simplest Example: 'example1.ts'

With the setup complete, let's try running a sample program. We'll start with the first example, 'example1.ts':


    // Import Exevalator (adjust the relative path as needed)
    import Exevalator from "./exevalator";

    // Create an Exevalator interpreter
    let exevalator: Exevalator = new Exevalator();

    // Evaluate the value of an expression
    const result: number = exevalator.eval("1.2 + 3.4");

    // Display the result
    console.log(`result: ${result}`);

Now, compile and execute it:

npx tsc example1.ts    # Compile to JavaScript
node example.js          # Run the program

The output will be:

4.6

As you can see, the expression "1.2 + 3.4" was evaluated correctly.

Running 'example6ex.ts' in a Web Browser

Next, let's run a slightly more practical example: a simple calculator app that runs directly in a web browser, similar to the demonstration app introduced at the beginning of this article.

Specifically, we'll use 'example6ex'. You can find the TypeScript code here and the HTML code here.

When running TypeScript code in a browser, it's often convenient to bundle multiple TypeScript files (including library files) into a single JavaScript file. This approach helps avoid issues related to module loading, which can be tricky to handle in a browser environment.

To accomplish this, we'll use a type of tool called a bundler. In this case, let's use 'esbuild':

npx esbuild example6ex.ts --bundle --outfile=example6ex.bundle.js

This will generate a bundled, compiled JavaScript file named 'example6ex.bundle.js'.

After that, simply double-click on the 'example6ex.html' file to open it in your web browser. This will load and execute the bundled JavaScript code. The behavior will be almost identical to the demonstration app shown at the beginning of this article.

In Closing

That's all for this announcement. We'll continue to provide updates about Exevalator in this News section, so stay tuned!