Arithmetic
Expressions
Rockstar expressions are heavily inspired by JavaScript, in that they will almost always return something rather than failing.
Basic Arithmetic
Rockstar supports the standard infix arithmetic operators +
, -
, *
, /
, with several aliases for each operator so you can write lyrically pleasing expressions:
Operation | Operator | Aliases |
---|---|---|
Addition | + | with , plus |
Subtraction | - | minus , without |
Multiplication | * | times , of |
Division | / | over , between |
The moon is like a kaleidoscope, out over quiet winter wonderland
The river is like a razorblade
Let my loneliness be the moon over the river
Shout my loneliness (prints: 123456)
A dream says tomorrow
A girl says no
My heart is empty
Let it be a girl with a dream
Shout my heart (prints: no tomorrow)
The wings are like a heartbreak
the fire is like a tomb
Let the silence be the wings of the fire
Scream the silence (prints: 140)
Operator precedence obeys the convention of multiplication, division, addition, subtraction.
Rockstar doesn’t support parentheses in expressions. If the default operator precedence doesn’t do what you need, you’ll have to decompose your expression into multiple evaluations and assignments.
x is 5. y is 2. z is 4.
Print x * y (prints: 10)
Print x / y (prints: 2.5)
Print x + y (prints: 7)
Print x - y (prints: 3)
Print 1 + 5 / 2 (prints: 3.5)
Print 1 + 2 * 3 (prints: 7)
Print 3 - 2 - 1 (prints: 0)
Print 1 + 2 * 3 / 4 - 5 + 6 * 7 / 8 - 9 (prints: -6.25)
Here’s how Rockstar operators are defined for various combinations of types.
As a rough rule, operations involving numbers will return numbers. For arithmetic purposes, true
is 1
, null
and false
are 0
. Operations involving strings will generally return strings. Any operation involving mysterious
will always return mysterious
.
Addition
Addition adds numbers and concatenates strings.
print 1 + 2 (prints: 3)
print 1 + "2" (prints: 12)
print 1 + true (prints: 2)
print 1 + false (prints: 1)
print "2" + 1 (prints: 21)
print "hello" + "world" (prints: helloworld)
print "hello" + true (prints: hellotrue)
print "hello" + null (prints: hellonull)
print true + true (prints: 2)
print true + 1 (prints: 2)
print true + "hello" (prints: truehello)
print true + null (prints: 1)
print null + null (prints: 0)
print null + true (prints: 1)
print null + "hello" (prints: nullhello)
print null + 1 (prints: 1)
Subtraction
If both operands have a numeric value, subtraction is numeric. If either or both operands is a string, Rockstar will “subtract” strings by removing the the bit you’re taking away (the subtrahend, if you want to get technical) from the bit you’re taking it away from (the minuend). Most of the time, this has no effect and subtraction just returns the string representation of the left-hand argument, but hey – you’re the one trying to subtract strings here; let’s not make out like I’m the one who has a problem.
print 1 + 2 (prints: 3)
print 1 + "2" (prints: 12)
print 1 + true (prints: 2)
print 1 + false (prints: 1)
print "2" + 1 (prints: 21)
print "hello" + "world" (prints: helloworld)
print "hello" + true (prints: hellotrue)
print "hello" + null (prints: hellonull)
print true + true (prints: 2)
print true + 1 (prints: 2)
print true + "hello" (prints: truehello)
print true + null (prints: 1)
print null + null (prints: 0)
print null + true (prints: 1)
print null + "hello" (prints: nullhello)
print null + 1 (prints: 1)
Multiplication
Rockstar lets you multiply strings. Multiplying by positive integers will repeat the string. Multiply by -1
to reverse the string, multiplying by decimal fractions will extract substrings. You’ll figure it out.
print 2 * 2 (prints: 4)
print 2 * "rock" (prints: rockrock)
print 2 * true (prints: 2)
print 2 * false (prints: 0)
print "rock" * 2 (prints: rockrock)
print "rock" * 0 (prints: )
print "rock" * -1 (prints: kcor)
print "rock" * -2 (prints: kcorkcor)
print "rock" * 1.5 (prints: rockro)
print "rock" * -0.2 (prints: k)
print true * true (prints: 1)
print true * 1 (prints: 1)
print true * "hello" (prints: hello)
print false * "hello" (prints: )
print true * null (prints: 0)
print null * null (prints: 0)
print null * true (prints: 0)
print null * "hello" (prints: )
print null * 1 (prints: 0)
Division
You know where this is going now. Numbers divided by numbers give you numbers, and yes, you can divide strings. A string divided by 2 gives you the first half. A string divided by half will be repeated. A string divided by -1 will reverse.
Dividing anything by a string will tell you how many times the second string occurs in the string representation of the first operand. Dividing haystack / needle
will be zero if it didn’t find needle
in haystack
, and positive if it found it.
Or, y’know, you can just stick to dividing numbers.
print 2 / 2 (prints: 1)
print 2 / "rock" (prints: 0)
print 2 / true (prints: 2)
print "rock" / 2 (prints: ro)
print "12345" / 5 (prints: 1)
print "haystack" / -1 (prints: kcatsyah)
print "helloworld" / -2 (prints: dlrow)
print "haystack haystack needle haystack" / "haystack" (prints: 3)
print "haystack haystack needle haystack" / "needle" (prints: 1)
print "haystack haystack needle haystack" / "banana" (prints: 0)
print true / true (prints: 1)
print true / 1 (prints: 1)
print true / "hello" (prints: 0)
print true / "e" (prints: 1)
print false / "alse" (prints: 1)
print null / true (prints: 0)
print null / "hello" (prints: 0)
print null / 1 (prints: 0)
Compound Expressions
Languages like C support shorthand expressions like x++
, x += 2
, and so on.
The equivalent in Rockstar looks like this:
X is 1
X is with 2
Print X (prints: 3)
Y is 5
It is without 3
Print it. (prints: 2)
Z is 100
It is over 5, 4, & 2
Shout it (prints: 2.5)