Syntax Basics
This page documents the core language syntax of RayQuiro 0.0.2.
Source Files
The current source extension is:
.rq
Examples:
main.rq
hello.rq
game.rq
Comments
Single-line comments
// this is a comment
print("hello");
Block comments
/*
multi-line comment
*/
print("hello");
Strings
Normal strings
var name = "RayQuiro";
Supported escape sequences in normal strings:
\n\t\"\\
Triple-quoted multiline strings
0.0.2 also supports triple-quoted strings:
var html = """
<section class="hero">
<h1>RayQuiro</h1>
</section>
""";
This is especially useful with web.html(...).
Numbers
RayQuiro supports integer-like and decimal numeric literals:
var a = 12;
var b = 3.14;
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";
Functions
Declare functions with fn:
fn greet(name) {
return "Hello, " + name;
}
Multiple parameters are supported:
fn mix(a, b, c) {
return a + b + c;
}
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.2 uses a C-style for loop:
for (var i = 0; i < 5; i = i + 1) {
print(i);
}
You can omit parts:
var i = 0;
for (; i < 3; i = i + 1) {
print(i);
}
Arrays And Indexing
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 works with string indexing:
print("Ray"[0]);
Values returned from json.parse(...) can also be indexed like object-style maps:
var info = json.parse("{\"name\":\"RayQuiro\"}");
print(info["name"]);
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 accepts a log-style statement form:
log.info => "Hello";
This prints through the same output path as print(...).
Semicolons
Use semicolons consistently:
var name = "RayQuiro";
print(name);
Core Built-In Functions
print(...values)
Prints values separated by spaces.
len(value)
Returns the length of:
- a string
- an array
- an object-like runtime value
str(value)
num(value)
bool(value)
type(value)
Basic conversion and introspection helpers.
range(end)
range(start, end)
range(start, end, step)
Creates an array of numbers.
push(array, value)
pop(array)
Array helpers.
join(array, separator)
split(text, separator)
String and array conversion helpers.
upper(text)
lower(text)
trim(text)
replace(text, needle, replacement)
slice(value, start, end)
contains(value, search)
String and collection helpers.
floor(number)
ceil(number)
round(number)
min(a, b, ...)
max(a, b, ...)
clamp(value, minValue, maxValue)
Numeric helpers.
sleep(milliseconds)
clock.ms()
General runtime timing helpers.
random()
random(max)
random(min, max)
random.int()
random.int(max)
random.int(min, max)
Random number helpers.
Example
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));