Lexical Grammar
Catspeak source code is stored as a sequence of Unicode characters using UTF-8 encoding. This means that most text rendered in the GameMaker IDE or an external text editor will be interpreted as individual characters. Unicode character clusters (such as skin tone emojis) are an exception to this, and are instead interpreted as multiple characters, depending on the size of the cluster.
In order for the computer to understand what these characters mean, they first need to be transformed into larger groups of characters called tokens. The following sections will cover what each of these tokens are, and how they are parsed.
§ Whitespacetop ^
For the most part Catspeak is whitespace insensitive, ignoring any of the following characters:
Character tabulation
'\t'
(U+0009)Line feed
'\n'
(U+000A)Line tabulation
'\v'
(U+000B)Form feed
'\f'
(U+000C)Carriage return
'\r'
(U+000D)Space (U+0020)
Next Line (U+0085)
Comments are also ignored by the Catspeak parser.
§ Commentstop ^
Comments in Catspeak are prefixed by --
. This will ignore all of the
characters following it until a new line is reached:
Catspeak (.meow)Copy-- this is a Catspeak comment!
(Catspeak reserves the //
syntax for integer division. See the section on
Operators for more information.)
Unlike GML, the Catspeak programming language does not include multi-line comments. Use multiple comments instead:
Catspeak (.meow)Copy-- comment
-- split
-- over multiple
-- lines
--
-- ~( ̄▽ ̄)~*
§ Numberstop ^
Simple numbers like 123
are a sequence of ASCII digits (0‑9),
optionally separated by a single decimal point 3.1415
.
Catspeak also supports writing numbers in binary
or hexadecimal notation, by
prefixing the number literal with either 0b
or 0x
. For example, the binary
representation of the number 10 is 0b1010
, and its hexadecimal representation
is 0xA
. Catspeak also supports CSS-style colour literals by prefixing a
number with #
instead of 0x
. See the Colour Codes section
for more information.
Numbers may also contain underscores _
to improve readability. For example,
1_000
is the same as 1000
, and 0b0111_1000
is the same as 0b01111000
.
Catspeak (.meow)Copy1_000_000 -- integer number
6.2831 -- fractional number
0b0110 -- binary number
0xDEAD_BEEF -- hexadecimal number
§ Colour Codestop ^
Just like GameMaker Language (GML)
and CSS, Catspeak
supports colour codes by typing a #
symbol, followed by three, four, six, or
eight hexadecimal digits which
express the colour:
(RR
is the red colour channel value, written in hexadecimal; GG
is the
green colour channel, BB
is the blue colour channel, and AA
is the (optional)
alpha colour channel.)
#RGB
Three-value colour syntax: same as#RRGGBB
.#RGBA
Four-value colour syntax: same as#RRGGBBAA
, includes an alpha channel.#RRGGBB
Six-value colour syntax: creates an RGB colour from the red, green, and blue colour values.#RRGGBBAA
Eight-value colour syntax: creates an RGBA colour with transparency from the red, green, blue, and alpha colour values.
If a colour channel is only one character wide R
, then it is repeated as RR
.
For example, the colour #1AF
is the same as the colour #11AAFF
.
Catspeak (.meow)Copy#A83256 -- represents the colour rbg(168, 50, 86)
#FAE -- represents the colour rbg(255, 170, 238)
#FFAAEE -- ALSO represents the colour rbg(255, 170, 238)
§ Character Literalstop ^
A common pattern in GML is to use ord("A")
for keyboard input, for example:
GameMaker Language (.gml)Copyif (keyboard_check(ord("A"))) {
do_stuff();
}
Catspeak has a special syntax for ord("A")
in the form of character literals
'A'
. These are a single UTF-8 encoded character surrounded by apostrophes '
.
Character literals are converted into numbers by the Catspeak programming language, and as a result you can do arithmetic on them:
Catspeak (.meow)Copy'A' + 1 == 'B' -- since 'A' == 65, 'B' == 66, and 65 + 1 == 66
This allows the previous GML code to be written as
Catspeak (.meow)Copyif keyboard_check('A') {
do_stuff()
}
in Catspeak.
§ Stringstop ^
There are two types of string literal in Catspeak. The most common is a sequence
of characters starting and ending in double quotes ("
):
Catspeak (.meow)Copylet hi = "hello world"
This type of string allows the following escape sequences to be used:
\"
Quotation mark\
Escape new line\\
Backslash\t
Character tabulation\n
Line feed\v
Line tabulation\f
Form feed\r
Carriage return
The other type of string is the raw string. Similarly to GML,
a raw string is prefixed by the address symbol @
, and does not interpret any
of the previously mentioned escape sequences:
Catspeak (.meow)Copylet hi_again = @"\hello\" -- \h and \" here are not escape sequences
All strings in Catspeak can be multi-line.
§ Identifierstop ^
Mostly used for variable names; identifiers start with either a letter
(A‑Z or a‑z) or an underscore (_
), followed by a sequence of other
letters, numbers, or underscores.
Examples of valid identifiers are:
Catspeak (.meow)Copybag_pipes
oneShot
__IMPALA_666__
abc123XYZ
Normal identifiers can not start with numbers, e.g. 1st
is not a valid
identifier. In this case, you can use raw identifiers:
Catspeak (.meow)Copylet `1st-of-march` = "2022-03-01"
Where raw identifiers start and end with a backtick symbol (`
).
1st-of-march
may not be a valid identifier, but it is a valid raw identifier.
You can use any non-whitespace, non-backtick character between the backticks when writing a raw identifier.
§ Keywordstop ^
Keywords are special identifiers which are reserved for use by Catspeak, and cannot be used as variables; these are:
true
Evaluates to boolean True.false
Evaluates to boolean False.undefined
GameMakerundefined
.infinity
Floating-point positive infinity.NaN
Floating-point NaN.and
See Logical Expressions.or
See Logical Expressions.xor
See Logical Expressions.do
See Block Expressions.if
See If Expressions.else
See If Expressions.while
See While Expressions.for
Reserved in case offor
loops.loop
Reserved in case of infinite loops.with
See With Expressions.match
See Match Expressions.let
See Let Statements.fun
See Function Expressions.params
Reserved in case of spread operator.break
See Break Expressions.continue
See Continue Expressions.return
See Return Expressions.new
See New Expressions.impl
Reserved in case of constructor functions.self
See Self Expressions.
If you ever need to use one of these keywords as a variable name, you should use Raw Identifiers instead.
§ Operatorstop ^
Catspeak shares many operators with GML, but there are some differences. Below is a table of all operators, organised from highest-to-lowest precedence (which operators are evaluated first when there are no parenthesis); including information about each operators associativity, usage, and syntax:
Precedence | Operator | Description | Associativity |
---|---|---|---|
12 |
(a) [a, ...] { k : v, ... } |
Grouping Array initialisation Struct/object initialisation |
N/a |
11 |
a(...) new a(...) a.b a[i]
|
Function call Constructor function call Member access Collection indexing (computed member access) |
Left‑to‑right → |
10 Unary |
!a ~a -a +a
|
Logical NOT Bitwise NOT Unary minus (negation) Unary plus |
N/a |
9 Multiplicative |
a * b a / b a // b a % b
|
Multiplication Division Integer division Remainder |
Left‑to‑right → |
8 Additive | a + b a - b |
Addition Subtraction |
Left‑to‑right → |
7 Bitwise |
a & b a | b a ^ b a << b a >> b
|
Bitwise AND Bitwise OR Bitwise XOR Bitwise left shift Bitwise right shift |
Left‑to‑right → |
6 Relational |
a < b a <= b a > b a >= b
|
Relational less-than < Relational less-than-or-equal ≤ Relational greater-than > Relational greater-than-or-equal ≥ |
Left‑to‑right → |
5 Equality | a == b a != b |
Equality = Inequality ≠ |
Left‑to‑right → |
4 Pipe | a <| b a |> b |
Left pipe Right pipe |
Left‑to‑right → |
3 Logical | a and b |
Logical AND | Left‑to‑right → |
2 Logical |
a or b a xor b
|
Logical OR Logical XOR |
Left‑to‑right → |
1 Assignment |
a = b a *= b a /= b a -= b a += b
|
Direct assignment Multiplication assignment Division assignment Subtraction assignment Addition assignment |
Right‑to‑left ← |
0 | N/a |
Control flow (e.g.
return ,
if , and
while )
|
N/a |