LeekScript Next

LeekScript Next

> LeekScript

Overview

LeekScript Next is a future version of the LeekScript language, based on a new, faster compiler that uses LLVM. Work on this version is on hold for now, to focus on adding features to the current LeekScript (versions 2, 3, etc.)

Project git repository: https://github.com/leek-wars/leekscript-v2 Forum topic: https://leekwars.com/forum/category-6/topic-7833

This is a draft based on the LSv1 tutorial, feel free to contribute!

Here is a pastebin listing all the functions under their module.

Literal values

Literals are values written out as-is in the code. LeekScript lets you write values of the following types directly:

Collections accept all types of literals as values (including collections). As a reminder:

Expressions

An expression is made up of literals and applications of operators or functions. Its simplest form is a single literal: 0. A more familiar form is a combination of mathematical operators and numbers: -1, 1 + 1, 0 * 3, etc. Finally, the most advanced form uses function applications: max(1, 2), rand(), etc.

In LeekScript, you can evaluate several expressions in a row, either ignoring the results or storing them in variables.

1 + 1 2 * 2 Number.cos(π * 2)

The code above evaluates to 1.

Variables

A variable associates a name with a value. It can hold any object, by assigning it a literal or the result of an expression.

let me = Fight.getEntity() var y = 1 + 1 const RAD = 180.0 / π global DEBUG = false

Of these four declarations, two allow the declared variable to be modified, and two make it accessible from anywhere in the code. It is recommended to use them in the following order of priority:

The const and global keywords are not currently implemented in the language. You can get the same behavior with let and var by declaring all your functions with the anonymous syntax, which takes advantage of how variable scope works.

For more details, see the Variables page. Although it was written for the old version of LeekScript, everything still works the same way: the new keywords only add the new features mentioned above. A draft of the version planned for LS 2 is available at Variables V2.

Control structures

We have seen that several expressions can be evaluated in a row, which allows somewhat more complex programs. That is what the basic artificial intelligence does when you create a new file. However, that alone is far from the most useful, which is why we have tools that let us change the order in which expressions are evaluated.

Branches

We can evaluate some expressions rather than others depending on the result of a given expression: for example, by checking which weapon is currently equipped, in order to equip a different weapon or avoid equipping the same weapon again.

IF

This can be done with the if control structure. if expects an expression that evaluates to a boolean, a series of expressions to evaluate if the result is true, and a series of expressions to evaluate if the result is false. The second series of expressions can be left out.

if me.getWeapon() == null me.setWeapon(Weapon.PISTOL)

if weapon != Weapon.PISTOL { me.setWeapon(Weapon.PISTOL) weapon = Weapon.PISTOL } else { me.say("I'm ready for the duel.") }

if !me.setWeapon(Weapon.PISTOL) then me.say("Where is my pistol?") end

let fruit = if me.getWeapon() == Weapon.AXE then "apple" else "banana" end

For more information on if, you can visit the page written for the current version of LeekScript; everything it explains still applies to LeekScript 2 (except for the difference between == and ===): Conditions

MATCH

You can do the same thing with the match control structure, and even somewhat more advanced things. This time, any expression can be evaluated, and a list of results, each with the expressions to evaluate, is expected. This makes it easy to reproduce the behavior of if; some languages even treat it as mere shorthand that gets replaced by a match.

For more information on branches, you can visit the Conditions page.

Functions

A function is a block of code that associates parameters with an expression, or with a sequence of statements. Its simplest form is anonymous. For example, a function that leaves its parameter unchanged can be written in any of the following ways:

x -> x (x) -> x x -> { x } (x) => { x } function(x) { x }

For arrow functions, the parentheses are optional, and so are the braces if the body is a single expression/statement. The result of the last expression is returned by default. You can also use the return keyword.

You can also give your functions a name, either by assigning them to variables or by using the dedicated syntax:

let id = x -> x function id(x) { x }

This makes them easier to reuse in your expressions: max(id(1), 0).

Loops

You can repeat a series of statements with the control structures called loops.

WHILE

The simplest form of loop just keeps repeating its block of code as long as the condition at the top of the block holds:

This loop uses the following syntax: while condition { instructions }. You can leave out the braces when the block of code contains a single statement, although this is not recommended.

FOR

Slightly more complex, this loop is made up of an initialization, a condition and a step:

This loop uses the following syntax: for initialization; condition; step { instructions }. The same remark applies to the braces. You can also leave out some or all of the steps at the top of the block. So for ; condition ; { instructions } is equivalent to while. However, avoid using a for loop when a while loop does exactly the same job.

FOREACH

This loop iterates over the elements of a data structure, such as an array, a map, an anonymous object, etc.

This loop uses the following syntax: for elem in struct { instructions }. struct can of course come from somewhere other than the statement itself, as it does in the example above. elem is a pattern that depends on the data structure and on what you want to use. For maps, a syntax similar to LS 1 associative arrays is possible (for k : v in map), but the more common syntax is for k, v in map. You can of course omit the key and iterate over the elements only: for v in map. This applies to all iterable structures. [To be checked]

BREAK
CONTINUE
Array comprehension

Data structures