Back to Home

XenonCode - Documentation

Introduction

XenonCode is a lightweight programming language designed as high level scripting within games for virtual devices.
It is used in the game Galaxy4D where players can code in a virtual computer.


Syntax

XenonCode is designed with a very basic syntax in mind.

Each statement is to be very short and easy to read.

Less unnecessary symbols and special characters.

No need to declare variables before usage.

No semicolon at the end of each statement.

You may use spaces or tabs anywhere you want in expressions and before any line of code.

Structure

A File is a program, which is also a function.

A Statement is a command that expresses an action to be carried out by the function.

An Expression is a group of one or more words or values that define a single resulting value. Ex. 4+2

Each file must start with a file definition line

One line of code defines One statement. A statement can contain Expressions.

Each line/statement must begin with a statement definition word or a variable assignment


Variables

A variable is a word that stores information to be used in the code.

It needs to start with a $, followed by up to 50 characters including letters, numbers and underscode _.

A variable name cannot contain accented letters, other special characters or spaces.

Variable names are case insensitive, which means that $BaNANas and $bananas are the same variable.

To assign a variable, first name it followed by a space and the expression you want to assign it to.

You can store 3 types of values with variables. Numbers, Booleans and Text.

Numbers include integers and decimal (floating point). Ex. 84 or 3.1416

Boolean means True or False. It's value can either be respectively true or false

Text values need to be surrounded by quotes like this "text"

Here are a few examples:

$bananas 4 The variable bananas is assigned the numeric value 4

$is_alive false The variable is_alive is assigned the boolean value false

$message "I like potatoes!" The variable message is assigned the text value I like potatoes!

$total $bananas+2 The variable total is assigned the resulting value of bananas+2 which is 4+2, resulting in the value 6.


File (program) definitions

The first line of each file must be the File Definition Line.
This line starts by the function name, followed by an arbitrary number of variables.
The function name can only contain letters, numbers and underscore _.
Example:
func2 $param1 $param2 $param3

The function with the name start will be executed automatically upon computer startup, and does not need any parameters.

Any function which the name is "input" + an input number (ex: input0) will be executed automatically when receiving data from something plugged into that input.

Any number of parameters can be defined in input functions, and must all be used when sending data to this computer, otherwise will be ignored.

Any input function that is not defined will run the first parameter (string) of any input received as a complete command.

A keyboard used for inputing pure commands must be plugged into an input on which a function is not defined, otherwise the total input string will be passed as the first parameter to that function.

Hence, if all available input functions are defined and all do nothing, the computer becomes completely unusable and unhackable except for what's already in the start function.
This can be useful to hardcode a computer to only run a specific command upon startup and will never be hacked nor modified.


Statements

Statement definition words define which type of statement is used for an entire line of code.

Hence, each line of code must start with one of the following words, unless it is a system function.

However, a variable assignment statement will start with the name of the variable (which starts by $) followed by the statement that defines its value.


System functions

System functions are predefined functions that must be executed without the exec keyword.

Math functions


Operators

Operators are used in expressions for arithmetics and comparisons.

Here are the available operators:


Conditions

Conditional statements are very useful in any programming language.

For instance, maybe I want to do an action only if a certain condition is met.

A condition definition starts with if followed by a space and an expression that will determine if the condition is met.

The following lines of code will be executed only if that condition is met, until the next endif, else or elseif

If the condition is not met, the next statement to be executed will be the one following the next endif or else

The else statement means that we want to execute the following code only if the condition is not met, until the next endif

The elseif statement means that we want to execute the following code only if the previous condition is not met and another condition is met instead.

After the endif, the previous behavior of the program continues.

You may use many elseif with different conditions within the same conditional statement and the program will execute only the first one which the condition is met. You may then follow them with a else statement that will be executed only if none of the previous conditions are met.

You may also write many conditional statements within another conditional statement.

The syntax of a conditional statement looks like this :
if expression1
    code to execute if the expression results in a condition that is met
  elseif expression2
    code to execute if the first condition is not met but the second is
  else
    code to execute if none of the above conditions are met
endif

Here is an example of a complete conditional statement :

if $bananas > 3 
    $bananas 10 
    $message "I had more than 3 bananas, but now I have exactly 10 bananas
  elseif $bananas = 0 
    $message "I don't have any bananas, but let's have one
    $bananas 1 
  elseif $is_alive
    $message "I had 1 or 2 bananas and I am alive, but I'm gonna die now.
    $is_alive false
  else 
    $message "I am already dead... I need lots of bananas to revive!
    $bananas 100 
    if $bananas >= 100
        $is_alive true
    endif
endif


Loops

Loops are useful for automatically repeating a bunch of code.

There are different kinds of loops: repeat and while

The repeat loop repeats a bunch of code for a given number of times.
The index of N times it was repeated is stored in the first following variable.
The next expression is the number of times the actions will be executed.
In the following example, the value of $index will go from 0 to 99 repeats for a total of 100 times.
repeat $index 100
    $message "Repeated $index times"
endrepeat

Note the string concatenation using a variable name within is.
When a word starting with a $ sign is found within a string, it will be replaced by the value of the mathing variable name.

The while loop repeats a bunch of code as long as a certain condition is met.
Example:
while $is_alive
    $message "I am still alive !"
    if $livingtime > 100
        $message "Dude you lived too long, just die already !"
        $is_alive false
        break
    endif
    $livingtime + 1
endwhile

break and continue can be used anywhere within the loop to stop executing the remaining statements inside that current loop.

break will also stop and bypass the loop immediately.

continue will simply go directly to the beginning of the next iteration of that loop.


Functions (Programs)

Any function can be called from anywhere in the code.
To call a function, use the statement exec followed by the function name.
If the function takes parameters, simply add their values after the function name, separated by a space.
Example using a 3-parameter function:
exec func1 $var1 "text2false
If you would like to retrieve a value from the called function, simply prepend the variable where you want to store it.
Example:
$returnVal exec func1 $var1 "text2false
This will put the value that the func1 returns in the $returnval variable.
To return a value from within a function, the statement return was used, followed by the value to be returned.


Output

The output statement is used for sending information to an external device.
Each device has an index based on which input it is plugged into.
Example to display a message on an external console plugged into input 0:
output 0 "Hello World!"
The index parameter can also be stored in a variable for clarity.
Example:
$console = 0
output $console "Hello !"


Input

The input keyword is used to read data from an external device.
Example reading the input 0 into the $value variable:
$value input 0
The device index 0 is usually the keyboard (the typed keys on the keyboard until the enter key is pressed).
The program will be paused and wait for an input to arrive, bypassing any defined input function, grabbing only the first parameter.


Comments

Comments are lines that are not compiled, only used to write tips for easier understanding of what a function is doing.
To write a comment, simply start the line with two slashes // .
Example:
// Multiplying two values and showing them on the console
$value 4 * 8
output 0 "Multiplied values results in $value"