LifestyleYuescript is a dynamic language that compiles to Lua

Yuescript is a dynamic language that compiles to Lua

-

- Advertisment -

logo

# Introduction

Yuescript is a dynamic language that compiles to Lua. And it’s a Moonscript (opens new window) dialect. The codes written in Yuescript are expressive and extremely concise. And it is suitable for writing some changing application logic with more maintainable codes and runs in a Lua embeded environment such as games or website servers.

Yue (月) is the name of moon in Chinese and it’s pronounced as [jyɛ].

# An Overview of Yuescript

# Installation

  • Lua Module
- Advertisement -

  Install luarocks (opens new window), a package manager for Lua modules. Then install it as a Lua module and executable with:

  Or you can build yue.so file with:

  Then get the binary file from path bin/shared/yue.so.

  • Binary Tool
- Advertisement -

  Clone this repo, then build and install executable with:

  Build Yuescript tool without macro feature:

  Build Yuescript tool without built-in Lua binary:

# Usage

  Require the Yuescript module in Lua:

  Use Yuescript tool with:

- Advertisement -

  Use cases:


  Recursively compile every Yuescript file with extension .yue under current path: yue .


  Compile and save results to a target path: yue -t /target/path/ .


  Compile and reserve debug info: yue -l .


  Compile and generate minified codes: yue -m .


  Execute raw codes: yue -e ‘print 123’


  Execute a Yuescript file: yue -e main.yue

# Macro

# Common Usage

Macro function is used for evaluating a string in the compile time and insert the generated codes into final compilation.

# Insert Raw Codes

A macro function can either return a Yuescript string or a config table containing Lua codes.

# Export Macro

Macro functions can be exported from a module and get imported in another module. It is recommanded to export macro functions in a single file to speed up compilation.

# Operator

All of Lua’s binary and unary operators are available. Additionally != is as an alias for ~=, and either or :: can be used to write a chaining function call like tbfunc! or tb::func!. And Yuescipt offers some other special operators to write more expressive codes.

# Metatable

The # operator can be used as a shortcut for metatable manipulation.

  • Metatable Creation


    Create normal table with key # or metamethod key that ends with #.
  • Metatable Accessing


    Accessing metatable with key # or metamethod key that ends with #.
  • Metatable Destructure


    Destruct metatable with metamethod key that ends with #.

# Existence

The ? operator can be used in a variety of contexts to check for existence.

# Piping

Instead of a series of nested function calls, you can pipe values with operator |>.

# Nil Coalescing

The nil-coalescing operator ?? returns the value of its left-hand operand if it isn’t nil; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn’t evaluate its right-hand operand if the left-hand operand evaluates to non-nil.

# Module

# Import

The import statement is a syntax sugar for requiring a module or help extracting items from an imported module.

# Export

The export statement offers a concise way to define modules.

  • Named Export


    Named export will define a local variable as well as adding a field in the exported table.
  • Unnamed Export


    Unnamed export will add the target item into the array part of the exported table.
  • Default Export


    Using the default keyword in export statement to replace the exported table with any thing.

# Whitespace

Yuescript is a whitespace significant language. You have to write some code block in the same indent with space ‘ ‘ or tab ‘t’ like function body, value list and some control blocks. And expressions containing different whitespaces might mean different things. Tab is treated like 4 space, but it’s better not mix the use of spaces and tabs.

# Multiline Chaining

You can write multi-line chaining function calls with a same indent.

# Assignment

The variable is dynamic typed and is defined as local by default. But you can change the scope of declaration by local and global statement.

  • Common Use
  • Explicit Locals
  • Explicit Globals

# Literals

All of the primitive literals in Lua can be used. This applies to numbers, strings, booleans, and nil.

Unlike Lua, Line breaks are allowed inside of single and double quote strings without an escape sequence:

# Function Literals

All functions are created using a function expression. A simple function is denoted using the arrow: ->.

The body of the function can either be one statement placed directly after the arrow, or it can be a series of statements indented on the following lines:

If a function has no arguments, it can be called using the ! operator, instead of empty parentheses. The ! invocation is the preferred way to call functions with no arguments.

Functions with arguments can be created by preceding the arrow with a list of argument names in parentheses:

Functions can be called by listing the arguments after the name of an expression that evaluates to a function. When chaining together function calls, the arguments are applied to the closest function to the left.

In order to avoid ambiguity in when calling functions, parentheses can also be used to surround the arguments. This is required here in order to make sure the right arguments get sent to the right functions.

There must not be any space between the opening parenthesis and the function.

Functions will coerce the last statement in their body into a return statement, this is called implicit return:

And if you need to explicitly return, you can use the return keyword:

Just like in Lua, functions can return multiple values. The last statement must be a list of values separated by commas:

# Fat Arrows

Because it is an idiom in Lua to send an object as the first argument when calling a method, a special syntax is provided for creating functions which automatically includes a self argument.

# Argument Defaults

It is possible to provide default values for the arguments of a function. An argument is determined to be empty if its value is nil. Any nil arguments that have a default value will be replace before the body of the function is run.

An argument default value expression is evaluated in the body of the function in the order of the argument declarations. For this reason default values have access to previously declared arguments.

# Considerations

Because of the expressive parentheses-less way of calling functions, some restrictions must be put in place to avoid parsing ambiguity involving whitespace.

The minus sign plays two roles, a unary negation operator and a binary subtraction operator. Consider how the following examples compile:

The precedence of the first argument of a function call can be controlled using whitespace if the argument is a literal string. In Lua, it is common to leave off parentheses when calling a function with a single string or table literal.

When there is no space between a variable and a string literal, the function call takes precedence over any following expressions. No other arguments can be passed to the function when it is called this way.

Where there is a space following a variable and a string literal, the function call acts as show above. The string literal belongs to any following expressions (if they exist), which serves as the argument list.

# Multi-line arguments

When calling functions that take a large number of arguments, it is convenient to split the argument list over multiple lines. Because of the white-space sensitive nature of the language, care must be taken when splitting up the argument list.

If an argument list is to be continued onto the next line, the current line must end in a comma. And the following line must be indented more than the current indentation. Once indented, all other argument lines must be at the same level of indentation to be part of the argument list

This type of invocation can be nested. The level of indentation is used to determine to which function the arguments belong to.

Because tables also use the comma as a delimiter, this indentation syntax is helpful for letting values be part of the argument list instead of being part of the table.

Although uncommon, notice how we can give a deeper indentation for function arguments if we know we will be using a lower indentation further on.

The same thing can be done with other block level statements like conditionals. We can use indentation level to determine what statement a value belongs to:

# Table Literals

Like in Lua, tables are delimited in curly braces.

Unlike Lua, assigning a value to a key in a table is done with : (instead of =).

The curly braces can be left off if a single table of key value pairs is being assigned.

Newlines can be used to delimit values instead of a comma (or both):

When creating a single line table literal, the curly braces can also be left off:

The keys of a table literal can be language keywords without being escaped:

If you are constructing a table out of variables and wish the keys to be the same as the variable names, then the : prefix operator can be used:

If you want the key of a field in the table to to be result of an expression, then you can wrap it in [ ], just like in Lua. You can also use a string literal directly as a key, leaving out the square brackets. This is useful if your key has any special characters.

# Comprehensions

Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration.

# List Comprehensions

The following creates a copy of the items table but with all the values doubled.

The items included in the new table can be restricted with a when clause:

Because it is common to iterate over the values of a numerically indexed table, an * operator is introduced. The doubled example can be rewritten as:

The for and when clauses can be chained as much as desired. The only requirement is that a comprehension has at least one for clause.

Using multiple for clauses is the same as using nested loops:

Numeric for loops can also be used in comprehensions:

# Table Comprehensions

The syntax for table comprehensions is very similar, only differing by using { and } and taking two values from each iteration.

This example makes a copy of the tablething:

The * operator is also supported. Here we create a square root look up table for a few numbers.

The key-value tuple in a table comprehension can also come from a single expression, in which case the expression should return two values. The first is used as the key and the second is used as the value:

In this example we convert an array of pairs to a table where the first item in the pair is the key and the second is the value.

# Slicing

A special syntax is provided to restrict the items that are iterated over when using the * operator. This is equivalent to setting the iteration bounds and a step size in a for loop.

Here we can set the minimum and maximum bounds, taking all items with indexes between 1 and 5 inclusive:

Any of the slice arguments can be left off to use a sensible default. In this example, if the max index is left off it defaults to the length of the table. This will take everything but the first element:

If the minimum bound is left out, it defaults to 1. Here we only provide a step size and leave the other bounds blank. This takes all odd indexed items: (1, 3, 5, …)

# For Loop

There are two for loop forms, just like in Lua. A numeric one and a generic one:

The slicing and * operators can be used, just like with comprehensions:

A shorter syntax is also available for all variations when the body is only a single line:

A for loop can also be used as an expression. The last statement in the body of the for loop is coerced into an expression and appended to an accumulating array table.

Doubling every even number:

You can also filter values by combining the for loop expression with the continue statement.

For loops at the end of a function body are not accumulated into a table for a return value (Instead the function will return nil). Either an explicit return statement can be used, or the loop can be converted into a list comprehension.

This is done to avoid the needless creation of tables for functions that don’t need to return the results of the loop.

# While Loop

The while loop also comes in two variations:

Like for loops, the while loop can also be used an expression. Additionally, for a function to return the accumulated value of a while loop, the statement must be explicitly returned.

# Continue

A continue statement can be used to skip the current iteration in a loop.

continue can also be used with loop expressions to prevent that iteration from accumulating into the result. This examples filters the array table into just even numbers:

# Conditionals

A short syntax for single statements can also be used:

Because if statements can be used as expressions, this can also be written as:

Conditionals can also be used in return statements and assignments:

The opposite of if is unless:

# If Assignment

if and elseif blocks can take an assignment in place of a conditional expression. Upon evaluating the conditional, the assignment will take place and the value that was assigned to will be used as the conditional expression. The assigned variable is only in scope for the body of the conditional, meaning it is never available if the value is not truthy.

# Line Decorators

For convenience, the for loop and if statement can be applied to single statements at the end of the line:

And with basic loops:

# Switch

The switch statement is shorthand for writing a series of if statements that check against the same value. Note that the value is only evaluated once. Like if statements, switches can have an else block to handle no matches. Comparison is done with the == operator.

A switch when clause can match against multiple values by listing them out comma separated.

Switches can be used as expressions as well, here we can assign the result of the switch to a variable:

We can use the then keyword to write a switch’s when block on a single line. No extra keyword is needed to write the else block on a single line.

It is worth noting the order of the case comparison expression. The case’s expression is on the left hand side. This can be useful if the case’s expression wants to overwrite how the comparison is done by defining an eq metamethod.

# Object Oriented Programming

In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the Yuescript code at first, then look into the Lua code if you wish to know the implementation details.

A simple class:

A class is declared with a class statement followed by a table-like declaration where all of the methods and properties are listed.

The new property is special in that it will become the constructor.

Notice how all the methods in the class use the fat arrow function syntax. When calling methods on a instance, the instance itself is sent in as the first argument. The fat arrow handles the creation of a self argument.

The @ prefix on a variable name is shorthand for self.. @items becomes self.items.

Creating an instance of the class is done by calling the name of the class as a function.

Because the instance of the class needs to be sent to the methods when they are called, the operator is used.

All properties of a class are shared among the instances. This is fine for functions, but for other types of objects, undesired results may occur.

Consider the example below, the clothes property is shared amongst all instances, so modifications to it in one instance will show up in another:

The proper way to avoid this problem is to create the mutable state of the object in the constructor:

# Inheritance

The extends keyword can be used in a class declaration to inherit the properties and methods from another class.

Here we extend our Inventory class, and limit the amount of items it can carry.

In this example, we don’t define a constructor on the subclass, so the parent class’ constructor is called when we make a new instance. If we did define a constructor then we can use the super method to call the parent constructor.

Whenever a class inherits from another, it sends a message to the parent class by calling the method __inherited on the parent class if it exists. The function receives two arguments, the class that is being inherited and the child class.

# Super

super is a special keyword that can be used in two different ways: It can be treated as an object, or it can be called like a function. It only has special functionality when inside a class.

When called as a function, it will call the function of the same name in the parent class. The current self will automatically be passed as the first argument. (As seen in the inheritance example above)

When super is used as a normal value, it is a reference to the parent class object.

It can be accessed like any of object in order to retrieve values in the parent class that might have been shadowed by the child class.

When the calling operator is used with super, self is inserted as the first argument instead of the value of super itself. When using . to retrieve a function, the raw function is returned.

A few examples of using super in different ways:

super can also be used on left side of a Function Stub. The only major difference is that instead of the resulting function being bound to the value of super, it is bound to self.

# Types

Every instance of a class carries its type with it. This is stored in the special __class property. This property holds the class object. The class object is what we call to build a new instance. We can also index the class object to retrieve class methods and properties.

# Class Objects

The class object is what we create when we use a class statement. The class object is stored in a variable of the same name of the class.

The class object can be called like a function in order to create new instances. That’s how we created instances of classes in the examples above.

A class is made up of two tables. The class table itself, and the base table. The base is used as the metatable for all the instances. All properties listed in the class declaration are placed in the base.

The class object’s metatable reads properties from the base if they don’t exist in the class object. This means we can access functions and properties directly from the class.

It is important to note that assigning to the class object does not assign into the base, so it’s not a valid way to add new methods to instances. Instead the base must explicitly be changed. See the __base field below.

The class object has a couple special properties:

The name of the class as when it was declared is stored as a string in the __name field of the class object.

The base object is stored in __base. We can modify this table to add functionality to instances that have already been created and ones that are yet to be created.

If the class extends from anything, the parent class object is stored in __parent.

# Class Variables

We can create variables directly in the class object instead of in the base by using @ in the front of the property name in a class declaration.

In expressions, we can use @@ to access a value that is stored in the __class of self. Thus, @@hello is shorthand for self.__class.hello.

The calling semantics of @@ are similar to @. Calling a @@ name will pass the class in as the first argument using Lua’s colon syntax.

# Class Declaration Statements

In the body of a class declaration, we can have normal expressions in addition to key/value pairs. In this context, self is equal to the class object.

Here is an alternative way to create a class variable compared to what’s described above:

These expressions are executed after all the properties have been added to the base.

All variables declared in the body of the class are local to the classes properties. This is convenient for placing private values or helper functions that only the class methods can access:

# @ and @@ Values

When @ and @@ are prefixed in front of a name they represent, respectively, that name accessed in self and self.__class.

If they are used all by themselves, they are aliases for self and self.__class.

For example, a quick way to create a new instance of the same class from an instance method using @@:

# Class Expressions

The class syntax can also be used as an expression which can be assigned to a variable or explicitly returned.

# Anonymous classes

The name can be left out when declaring a class. The __name attribute will be nil, unless the class expression is in an assignment. The name on the left hand side of the assignment is used instead of nil.

You can even leave off the body, meaning you can write a blank anonymous class like this:

# With Statement

A common pattern involving the creation of an object is calling a series of functions and setting a series of properties immediately after creating it.

This results in repeating the name of the object multiple times in code, adding unnecessary noise. A common solution to this is to pass a table in as an argument which contains a collection of keys and values to overwrite. The downside to this is that the constructor of this object must support this form.

The with block helps to alleviate this. Within a with block we can use a special statements that begin with either . or which represent those operations applied to the object we are using with on.

For example, we work with a newly created object:

The with statement can also be used as an expression which returns the value it has been giving access to.

Or…

In this usage, with can be seen as a special form of the K combinator.

The expression in the with statement can also be an assignment, if you want to give a name to the expression.

# Do

When used as a statement, do works just like it does in Lua.

Yuescript’s do can also be used an expression . Allowing you to combine multiple lines into one. The result of the do expression is the last statement in its body.

# Destructuring Assignment

Destructuring assignment is a way to quickly extract values from a table by their name or position in array based tables.

Typically when you see a table literal, {1,2,3}, it is on the right hand side of an assignment because it is a value. Destructuring assignment swaps the role of the table literal, and puts it on the left hand side of an assign statement.

This is best explained with examples. Here is how you would unpack the first two values from a table:

In the destructuring table literal, the key represents the key to read from the right hand side, and the value represents the name the read value will be assigned to.

This also works with nested data structures as well:

If the destructuring statement is complicated, feel free to spread it out over a few lines. A slightly more complicated example:

It’s common to extract values from at table and assign them the local variables that have the same name as the key. In order to avoid repetition we can use the : prefix operator:

This is effectively the same as import, but we can rename fields we want to extract by mixing the syntax:

You can write default values while doing destructuring like:

# Destructuring In Other Places

Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop:

We know each element in the array table is a two item tuple, so we can unpack it directly in the names clause of the for statement using a destructure.

# Function Stubs

It is common to pass a function from an object around as a value, for example, passing an instance method into a function as a callback. If the function expects the object it is operating on as the first argument then you must somehow bundle that object with the function so it can be called properly.

The function stub syntax is a shorthand for creating a new closure function that bundles both the object and function. This new function calls the wrapped function in the correct context of the object.

Its syntax is the same as calling an instance method with the operator but with no argument list provided.

# The Using Clause; Controlling Destructive Assignment

While lexical scoping can be a great help in reducing the complexity of the code we write, things can get unwieldy as the code size increases. Consider the following snippet:

In my_func, we’ve overwritten the value of i mistakenly. In this example it is quite obvious, but consider a large, or foreign code base where it isn’t clear what names have already been declared.

It would be helpful to say which variables from the enclosing scope we intend on change, in order to prevent us from changing others by accident.

The using keyword lets us do that. using nil makes sure that no closed variables are overwritten in assignment. The using clause is placed after the argument list in a function, or in place of it if there are no arguments.

Multiple names can be separated by commas. Closure values can still be accessed, they just cant be modified:

# Licence: MIT

Copyright (c) 2021 Li Jin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.













NOW WITH OVER +8500 USERS. people can Join Knowasiak for free. Sign up on Knowasiak.com
Read More

- Advertisement -

1 Comment

  1. >>> expressive and extremely concise. And it is suitable for writing some changing application logic with more maintainable code

    Assuming you won't write large code bases, I fail to see why would I use this instead of Lua.

    (I have been running a SMS gateway for the last 15 years. Full code base is Lua (Openresty). Gateway configuration is also Lua; several hundreds of Lua code per configuration file)

You might also likeRELATED
Recommended to you

Show HN: I made a documentation websites builder on top of Notion

Notaku BetaBuild your product's docs with content from NotionNotaku transforms your Notion workspace into a beautiful documentation site.Custom DomainsUse...

GCC 12 Provides Toughen for The spend of the Mould Linker

A small but noteworthy change that landed today for the GCC 12 compiler itself is support for using the Mold linker. Released last week was Mold 1.0 as a high-speed linker that can deliver better performance than GNU's old Gold linker and even LLVM's LLD. Mold was designed by Rui Ueyama who originally working on…

High-tail Fuzzing

Go supports fuzzing in its standard toolchain beginning in Go 1.18. Overview Fuzzing is a type of automated testing which continuously manipulates inputs to a program to find bugs. Go fuzzing uses coverage guidance to intelligently walk through the code being fuzzed to find and report failures to the user. Since it can reach edge…

Narrate HN: We initiating sourced our new Mac developer setup script

This script will help with the quick setup and installation of tools and applications for new developers at Vendasta. Tested in Mac OS 10.14 to 12. This script hasn't been tested on M1 Macs. You can run this script multiple times without issue. You can also run it on a partially set-up computer and it…
- Advertisement -

Thich Nhat Hanh, Vietnamese Zen Master, Dies at 95

Thich Nhat Hanh at the Plum Village monastery in southern France | Courtesy Plum Village Community of Engaged Buddhism Vietnamese Zen Master Thich Nhat Hanh—a world-renowned spiritual leader, author, poet, and peace activist—died on January 22, 2022 at midnight (ICT) at his root temple, Tu Hien Temple, in Hue, Vietnam. He was 95. “Our beloved…

Before wave of train thefts, Union Pacific laid off some of its police force

News organizations both locally and nationally have been covering the rise of cargo theft in L.A.’s northeast train tracks in the past few days. Anchors on morning news have been quick to point out that there have been over 100 arrests, and even Forbes have been quick to point out the staggering $5 million worth…

Must read

Is human behavior just elaborate running and tumbling?

1. IntroductionYou know that scene in the movie, A...
- Advertisement -