What is HeraScript
This is a programming language I wrote in my spare time during the pandemic, compiled into JavaScript for execution. Designed to add some fun features to JavaScript, such as support for partial Apply, user-defined-operator, operator as function, and so on. (Without much consideration of the grammatical definition, I add 🐶 for fun
Hera-Lang:github.com/HeraScript/… Hera – IDE: github.com/HeraScript/…
Some syntax implementations
As mentioned above, we implemented some interesting syntax in Hera. Let’s take a look at what they are and how they are implemented.
partial apply
Syntax fragment can see here: herascript lot. IO/index. The HTML? …
In JavaScript, we’re going to apply partial apply to a function that has to go through curry, for example:
Hera doesn’t have to be partial apply to partial apply functions. The Hera compiler does this for you.
The compiled JavaScript code looks like this:
{
}
user-defined-operator
Syntax fragment can see here: herascript lot. IO/index. The HTML? …
In Hera, custom operators are supported, for example
infixl 1!!!!! -> x !!y= > x[y]
Copy the code
So we can pass [1, 2, 3]!! 1 Access the access array in this way
So how does this work?
-
Say to the operator, the first think of is the priority between the operator: 2 + 3 * 5 is to first calculate 3 * 5 because higher priority than + *, so we custom operator also needs to deal with the issue of priority. Our approach is to create an array for the op, the order of which represents the priority, so that we can parse expression according to the order of the array during processing, so that the operator with the highest priority will always be evaluated first. See Haskell’s buildExpressionParser for detailed expression calculation rules
-
After processing the priority, we need to deal with how to map Hera expression to native expression. Here, we put Hera’s expression rule, operator and native expression rule into a table: (he, op, NE), so that when we parse to operator Apply, we can extract the corresponding rule from the table and parse twice, and then replace the corresponding token to complete the syntax conversion.