Skip to content

RiverScript

Jspa2 edited this page Jan 5, 2023 · 11 revisions

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.

RiverScript REPL

Features

  • 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

Using RiverScript

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.

Syntax

Printing

You can print any object with the print function:

print("Hello World!")
print(123)

Variables

Variables are declared and assigned with simple syntax:

x = 123

And can be accessed with just their name:

print(x)

Reading input

You can read input from the user with the read function:

input = read()
print(input)

For-loops

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.

Ranges

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 loops

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.

Functions

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)

Booleans

RiverScript supports booleans and boolean logic:

trueBool = true
falseBool = false
print(trueBool)

If and if-else statements

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

Comments are prefixed with #:

# This is a comment.

print("Hello world!") # This is a comment.

Null

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'.

Truthiness

In RiverScript, all variables are truthy, except false and null.

Expressions

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.

Arithmetic operators

  • + - Addition
  • - - Subtraction (unary)
  • * - Multiplication
  • / - Division
  • % - Modulo

Logical operators

  • == - 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)

Concatenation

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

Standard library

RiverScript provides a standard library with useful functions for mathematics, type conversion, I/O, and more.

I/O

print(string)

  • Description: Prints the provided string to the console.
  • Returns: null

read()

  • Description: Reads input from the user and stores it in a string.
  • Returns: string

Maths

floor(number)

  • Description: Returns the floor of a number.
  • Returns: number

round(number)

  • Description: Rounds a number.
  • Returns: number

ceil(number)

  • Description: Returns the ceiling of a number.
  • Returns: number

Types

str(object)

  • Description: Converts an object to a string.
  • Returns: string

num(string)

  • Description: Converts a string to a number.
  • Returns: number

typeof(object)

  • Description: Returns the type of an object.
  • Returns: string ('function', 'boolean', 'null', 'number', 'range', 'string', or 'object')

Built-in variables

_ENV

  • Description: The environment that RiverScript is running on.
  • Value: string ("RiverScript (SphereOS)" , or "RiverScript")

_SPHEREOS

  • Description: The version of SphereOS that RiverScript is running on. Only available on SphereOS.
  • Value: string (example: "0.1.4 Preview")