Syntax Basics
This page documents the core language syntax of RayQuiro 0.0.1.
Source Files
The current source extension is:
.rq
Example:
main.rq
hello.rq
math.rq
Comments
Single-line comments
// this is a comment
print("hello");
Block comments
/*
multi-line comment
*/
print("hello");
Strings
Strings use double quotes:
var name = "RayQuiro";
Supported escape sequences in 0.0.1:
\n\t\"\\
Example:
print("line 1\nline 2");
Numbers
RayQuiro supports integer-like and decimal numeric literals:
var a = 12;
var b = 3.14;
Internally, numbers are handled as numeric values and can be passed through math-style built-ins.
Booleans And Null
var online = true;
var done = false;
var value = null;
Variables
var
Mutable binding:
var score = 0;
score = score + 1;
let
Read-only binding:
let title = "RayQuiro";
Trying to assign to a let binding raises an error.
Functions
Declare functions with fn:
fn greet(name) {
return "Hello, " + name;
}
Multiple parameters are supported:
fn mix(a, b, c) {
return a + b + c;
}
Functions are called by name:
print(greet("world"));
Statements
RayQuiro supports:
- variable declarations
- function declarations
- expression statements
ifwhileforreturnbreakcontinue- block statements with
{ ... }
if / else
if (score > 10) {
print("High score");
} else {
print("Keep going");
}
while
var i = 0;
while (i < 5) {
print(i);
i = i + 1;
}
for
RayQuiro 0.0.1 uses a C-style for loop:
for (var i = 0; i < 5; i = i + 1) {
print(i);
}
You can also omit parts:
var i = 0;
for (; i < 3; i = i + 1) {
print(i);
}
Arrays
Array literals use square brackets:
var items = [1, 2, 3];
var color = [98, 204, 255, 255];
Indexing also uses square brackets:
print(items[0]);
The same syntax can index strings:
print("Ray"[0]);
Operators
Arithmetic
+-*/%
Comparison
==!=<<=>>=
Logical
!&&||
Assignment
=
Truthiness
The interpreter treats these values as false:
nullfalse0- empty string
- empty array
- empty object-like runtime value
Everything else is considered true.
Logging Shortcut
RayQuiro also accepts a log-style statement form:
log.info => "Hello";
This prints the same way as print(...).
Semicolons
Semicolons are used across the examples and are strongly recommended:
var name = "RayQuiro";
print(name);
Keep using them consistently even when a simple statement may parse without one.
Core Built-in Functions
This section documents the built-ins available in the core language.
print(...values)
Prints all arguments separated by spaces.
print("score", 12, true);
len(value)
Returns the length of:
- a string
- an array
- an object-like runtime value
For unsupported values, it returns 0.
str(value)
Converts a value to string.
num(value)
Converts a value to number.
bool(value)
Converts a value to RayQuiro truthiness.
type(value)
Returns one of:
"null""number""string""bool""array""object""unknown"
range(end)
range(start, end)
range(start, end, step)
Creates an array of numbers.
print(range(5)); // [0, 1, 2, 3, 4]
print(range(2, 6)); // [2, 3, 4, 5]
print(range(10, 4, -2)); // [10, 8, 6]
push(array, value)
Pushes a value into an array and returns the array.
var items = [1, 2];
push(items, 3);
print(items);
pop(array)
Removes and returns the last item of an array.
join(array, separator)
Joins array items into a string.
split(text, separator)
Splits text into an array.
If separator is empty or omitted, the string is split into characters.
upper(text)
lower(text)
Uppercase and lowercase conversion.
contains(value, search)
Supports:
- string contains substring
- array contains value
trim(text)
Trims leading and trailing whitespace.
replace(text, needle, replacement)
Replaces all occurrences of needle.
slice(value, start, end)
Supports:
- string slices
- array slices
The end index is exclusive.
floor(number)
ceil(number)
round(number)
Number helpers.
min(a, b, ...)
max(a, b, ...)
Returns the minimum or maximum numeric value.
clamp(value, minValue, maxValue)
Clamps a number into a range.
sleep(milliseconds)
Pauses the script.
clock.ms()
Returns the current monotonic clock value in milliseconds.
random()
random(max)
random(min, max)
Returns a random floating-point number.
random.int()
random.int(max)
random.int(min, max)
Returns a random integer value.
Example: Core Language Showcase
var nums = range(1, 6);
push(nums, 9);
print("len:", len(nums));
print("joined:", join(nums, ", "));
print("slice:", slice(nums, 1, 4));
print("contains 3:", contains(nums, 3));
print("random:", random(10, 20));
print("dice:", random.int(1, 6));