Repository navigation
Language Syntax
CupidScript uses a C-like surface syntax.
Supported comment forms:
// line comment
/* block comment */Comments are skipped by the lexer alongside whitespace.
- Start: letter or
_ - Continue: letters, digits,
_
Decimal and hexadecimal integers with optional underscores:
0
12345
1_000_000
0xFF
0xFF_FF_FF- Decimal:
0-9digits - Hexadecimal:
0xor0Xfollowed by hex digits (0-9,a-f,A-F) - Underscores
_can appear anywhere in the number (ignored)
Floating-point literals with decimal point:
3.14
2.5
0.001
1.5e-3
2.0E+10- Must contain a decimal point:
. - Optional exponent:
eorEfollowed by optional+/-and digits - Scientific notation:
1.5e-3equals0.0015
Double-quoted strings:
"hello"
"with \n escapes"
"quote: \""
"backslash: \\"Raw (backtick) strings:
`C:\\temp\\file`
`line1
line2`Notes:
- Backtick strings do not process escapes.
- Backtick strings can span multiple lines.
- String interpolation is not supported inside backtick strings.
- A backtick cannot appear inside a backtick string.
String interpolation is supported with ${...} inside a string:
let name = "Ada";
print("Hello ${name}");
print("sum=${1 + 2}");Escape handling (in runtime unescape):
-
\n,\t,\r,\",\\ -
\e(ASCII ESC, 27) -
\xHH(single byte from two hex digits) - Any unknown escape
\xbecomes literalx
[]
[1, 2, 3]
["a", "b", "c"]Trailing commas are allowed:
[1, 2, 3,]
[]{}
{"name": "Frank", "age": 30}
{"key": value_expr}Trailing commas are allowed:
{"a": 1, "b": 2,}
{}Keys must be string expressions (can be any expression that evaluates to a string).
( ) [ ] { } , ; .
Assignment:
=-
+=,-=,*=,/=(compound assignment, works on variables, fields, and index expressions)
Arithmetic:
+ - * / %
Unary:
! -
Comparison:
== != < <= > >=
Boolean:
&& ||
Nullish coalescing:
-
??(returns left operand if notnil, otherwise right)
Range:
-
..(exclusive range, e.g.,0..5→[0,1,2,3,4]) -
..=(inclusive range, e.g.,0..=5→[0,1,2,3,4,5])
- Primary: literals, identifiers, parenthesized
(expr), list literals[...], map literals{...} - Postfix: calls
f(...), indexinga[b], field accessa.b(field access works after any expression, e.g.(expr).field) - Unary:
!expr,-expr - Multiplicative:
* / % - Additive:
+ - - Range:
start..end,start..=end - Comparison:
< <= > >= - Equality:
== != - AND:
&& - OR:
|| - Nullish:
a ?? b - Ternary:
cond ? then : else
Optional chaining accesses a field only if the target is not nil:
user?.nameIf the target is nil, the result is nil (no error). Otherwise, it behaves like normal field access.
CupidScript supports switch as a statement:
switch (x) {
case 1 { print("one"); }
case 2 { print("two"); }
default { print("other"); }
}Notes:
- Cases are compared using the same equality rules as
==for basic types. - Non-fallthrough: only the first matching case runs.
Schedule cleanup work to run when the current block exits:
defer close(file);
defer { print("cleanup"); }Notes:
- Defers run in LIFO order when the block exits.
- Defers run even if the block exits via
return,break,continue, orthrow.
match is like switch, but returns a value and is used inside expressions.
let label = match (x) {
case 1: "one";
case 2: "two";
default: "other";
};Patterns and guards:
let label = match (value) {
case [a, b]: a + b;
case {x, y: z}: x + z;
case n if n > 10: "big";
case _: "small";
};Notes:
- Patterns can bind identifiers; bindings are visible in the guard and case result.
- Guards (
if expr) are optional and run only when the pattern matches. - The first matching case expression is evaluated and returned.
-
defaultis optional; if omitted and no case matches, the result isnil.
Syntax:
try {
// body
} catch (e) {
// handler
} finally {
// cleanup
}
// finally can also be a single expression:
try { work(); } catch (e) { print(e); } finally cleanup();Notes:
-
catchis required;finallyis optional. -
finallyexecutes after thetry/catchregardless ofreturnorthrow. -
finallycan be a block or a single expression statement.
Syntax:
export name = expr;
export {name, local as exported};Defines a value in the module's implicit exports map. require() returns this map.
Syntax:
import "./path/to/module.cs"; // side effects only
import mod from "./path/to/module.cs"; // bind exports map
import {foo, bar as baz} from "./path/to/module.cs";
import mod, {foo} from "./path/to/module.cs";Notes:
-
importis syntax sugar overrequire(). - Named imports read fields from the module's exports map.
- If an imported name is missing, the binding is set to
nil.
Use const to create an immutable binding:
const x = 10;Notes:
- Reassignment to a
constbinding raises a runtime error. -
constrequires an initializer. - Destructuring also works with
const:
const [a, b] = [1, 2];
const {name, age} = {"name": "Ada", "age": 36};List and map destructuring are supported in let declarations.
let [a, b, c] = [1, 2];
// a=1, b=2, c=nillet {x, y} = {"x": 10, "y": 20};
// x=10, y=20
let {key: alias} = {"key": "value"};
// alias="value"Notes:
- Missing list elements or map keys produce
nil. -
_can be used to ignore a binding. - Destructuring requires an initializer.
- List destructuring expects a list; map destructuring expects a map (runtime error otherwise).