-
Notifications
You must be signed in to change notification settings - Fork 11
RiverScript
RiverScript is an interpreted scripting language that is available on SphereOS. Its syntax is similar to that of the C and Python programming languages.
Currently, it is only available on SphereOS. A Windows version is in the works.
- Number, string, range, null, boolean, and function types.
- If and if-else statements
- For-endfor loops
- While-endwhile loops
- Arithmetic, logic, and unary operators
- Null values
You can run a RiverScript script by running the rs
command in SphereOS, which will run the specified file.
If no file is specified, it will enter the REPL (read-eval-print-loop), in which you can run expressions and scripts sequentially by typing them and pressing enter. Variables and program state are never cleared in the REPL. You can use the exit() function to exit the REPL.
Alternatively, you can also use the Text Editor (command: edit <file>
), and press Ctrl+R to run your script.
You should save RiverScript script files with the .rs
file extension.
You can print any object with the print
function:
print("Hello World!")
print(123)
Variables are declared and assigned with simple syntax:
x = 123
And can be accessed with just their name:
print(x)
You can read input from the user with the read
function:
input = read()
print(input)
for (i in 1..5) {
print(i)
}
This program prints the numbers 1 to 5. It uses a range operator (..
) to define the range to loop with.
For-loops are defined with a range, which is a special object type in RiverScript. They can be either used in a for-loop directly or stored in a variable.
Ranges can be created with the range operator (..
):
range = 1..10
Ranges can also utilise existing variables for their lower or upper bounds:
x = 10
range = 1..x
while (true) {
print("Hello World!")
}
A while loop runs continously, provided its condition (in parentheses) is truthy.
This program prints "Hello World" endlessly, because the condition in the while loop is true
.
You can declare functions with the def
keyword, and call them with their name followed by parentheses.
def hello() {
print("Hello world!")
}
hello()
You can pass arguments to functions like this:
def myfunc(text) {
print(text)
}
myfunc("Hello World!")
Functions can also have multiple arguments:
def printsum(a, b) {
print(a + b)
}
printsum(1, 2)
RiverScript supports booleans and boolean logic:
trueBool = true
falseBool = false
print(trueBool)
If-statements can be created with the if
keyword.
bool = true
if (bool) {
print("Bool is truthy.")
}
You can also use the else
keyword to execute a block if the condition is not truthy.
bool = false
if (bool) {
print("Bool is truthy.")
} else {
print("Bool is not truthy.")
}
Comments are prefixed with #
:
# This is a comment.
print("Hello world!") # This is a comment.
You can use the null
keyword to create a null value. Empty expressions will also evaluate to null:
print(())
# Prints 'null'.
You can check if a variable is null by equating it with null
:
nullvar = null
print(nullvar == null)
# Prints 'True'.
In RiverScript, all variables are truthy, except false
and null
.
RiverScript supports all of the standard operators you would expect, and you can combine them in expressions.
Note: Expressions are evaluated from right to left.
-
+
- Addition -
-
- Subtraction (unary) -
*
- Multiplication -
/
- Division -
%
- Modulo
-
==
- Equals -
!=
- Does not equal -
>=
- Greater than or equal -
<=
- Less than or equal -
>
- Greater than -
<
- Less than -
and
- Returns the first non-truthy operand -
or
- Returns the left-hand operand if it is truthy, otherwise the right-hand operand -
not
- Returns false if its right-hand operand is truthy, otherwise true (unary only)
You can concatenate strings with other strings using the +
operator:
print("Enter your name:")
input = read()
print("Hello " + input + "!")
This program asks the user to enter their name. It then uses string concatenation to print out a customised message with the user's name.
You cannot concatenate strings and numbers directly:
print("Hello world!" + 123)
# Error: Cannot add Hello world! and 123.
Instead, you should convert the number to a string with the str
function.
print("Hello world!" + str(123))
# Hello world!123
RiverScript provides a standard library with useful functions for mathematics, type conversion, I/O, and more.
- Description: Prints the provided string to the console.
- Returns: null
- Description: Reads input from the user and stores it in a string.
- Returns: string
- Description: Returns the floor of a number.
- Returns: number
- Description: Rounds a number.
- Returns: number
- Description: Returns the ceiling of a number.
- Returns: number
- Description: Converts an object to a string.
- Returns: string
- Description: Converts a string to a number.
- Returns: number
- Description: Returns the type of an object.
- Returns: string ('function', 'boolean', 'null', 'number', 'range', 'string', or 'object')
- Description: The environment that RiverScript is running on.
- Value: string ("RiverScript (SphereOS)" , or "RiverScript")
- Description: The version of SphereOS that RiverScript is running on. Only available on SphereOS.
- Value: string (example: "0.1.4 Preview")