Tutorial _ Rockstar
Tutorial _ Rockstar
Hello, World
“Hello, World” in Rockstar looks like this:
All the examples in this tutorial are interactive: click on “Try It ” to open the control panel, then press the “Rock ▶” button to run the
code and see what it does.
Because Rockstar’s designed to write programs that look like song lyrics, it’s very relaxed when it comes to syntax. Almost everything in Rockstar is
case-insensitive, and most keywords have several different aliases so you can pick the one that suits the mood of your program.
Try it out. Edit the example above and replace London with your home town.
Variables in Rockstar
A variable stores a value so you can refer back to it later. In most programming languages, a variable name can’t contain spaces, so programmers
have to use names like customerTaxPayable or year_end_date , or customer-shipping-address .
Rockstar is not like most programming languages: if you want to put spaces in your variables, you go right ahead. After all, nobody ever wrote a
power ballad about customerTaxPayable .
Common Variables
Common variables in Rockstar start with a , the , my , your , their , his or her . To assign a value to a variable, use put , is , or let 1
1 My heart is 123
2 Let your love be 456
3 Put 789 into the night
4
5 Shout my heart. Scream your love. Whisper the night.
6
Try It
You might also notice we’ve got three statements on the same line there. That’s just fine, because each statement ends with full stop -
just like regular English. Statements in Rockstar can end with a full stop . , question mark ? , exclamation mark ! , semi-colon ; , or a line break.
Proper Variables
Proper variables in Rockstar are two or more words which must all begin with a capital letter. Initials are allowed – Johnny B. Goode – but
abbreviations aren’t: you can’t have a variable called Mr. Crowley because Rockstar treats the . in Mr. as the end of a statement:
Simple Variables
If you really want to, you can use simple variables, which work just like variables in Python, Ruby, and many other programming languages:
1 x is 123
2 let myVariable be 456
3 put 789 into customerId
4 print x. print myVariable. print customerId.
5
6
7
Try It
1 Print 1 + 2
2 Print 3 * 4
3 Print 5 - 6
4 Print 7 / 8
5 Print 1 + 2 * 3 - 4 / 5
6
7
Try It
Problem is, there’s only one good song about mathematics and Little Boots already wrote it, so all the arithmetic operators in Rockstar
support aliases. Instead of + , use plus or with . - can be minus or without , * can be times or of , and / is over or divided by :
This is also probably a good time to mention that you can’t use brackets in Rockstar. Well, you can, but they’re used to indicate
comments (like this) - because that’s how lyrics work.
Rockstar will let you add, multiply, subtract and divide just about anything - check out the docs to find out exactly how this works, or
just try stuff out and see what happens. Remember, this is a joke language based on Bon Jovi songs. You’re not gonna break anything important.
Poetic Numbers
You notice in the last example, I wrote Let Tommy be a boy with a dream - and not Tommy is a boy with a dream ?
Try this:
When you initialise a variable using is , was or were , if the thing on the right-hand side doesn’t start with a digit 0-9 , or with one of the
arithmetic operator keywords ( plus , with , etc.) Rockstar treats it as a poetic number. It’ll take the length of each word, modulo 10, and interpret
those word lengths as decimal digits.
A poetic number includes everything up to the end of the line, so watch out for statements like Lucy was a dancer. Say Lucy! - that’s not
going to print Lucy , it’s going to assign Lucy the value 1634. Poetic numbers count hyphens ( - ) and ignore all other punctuation, so you can use
phrases like cold-hearted for the digit 2 instead of having to think of 12-letter words.
If you want to use a poetic number anywhere else in your Rockstar program, prefix it with the like keyword: Let my variable be like a
rolling stone will initialise my variable with the value 175 .
Strings
No, not that kind of strings. Strings are how Rockstar handles text. A string in Rockstar is surrounded by double quotes; to include double quotes
in a string, use two sets of double quotes. You can also use poetic string syntax using the says keyword:
Reading Input
To read input from the console, use listen . Listen on its own will read a line of input from STDIN and discard it. Listen to <variable> will
read the next line of input from STDIN and store it in <variable> as a string.
For all kinds of complicated reasons, the Rockstar engine that runs on this website can’t prompt you for input, so you’ll need to provide the
input in advance using the text box below the Rockstar editor.
Ninja Strings
The problem with literal strings is they often don’t fit the mood of the song you’re trying to write. FizzBuzz is all well & good, but shouting the
word “fizz” in the middle of power ballad just isn’t gonna work.
To get around this, Rockstar includes a feature that lets you build strings without ever having to refer to them directly: we call these ninja strings,
because like ninjas, they are both stealthy and awesome.
<variable> holds <poetic number> - will convert <poetic number> to the Unicode character with the corresponding code point, and
assign variable to the resulting string.
rock <variable> <number> - will add characters to the end of a string based on their code points - and because you can use the like
keyword:
rock <variable> like <poetic_number> will build a string out of pure poetry.
1 My soul is empty
2 Rock my soul with 70 (ASCII code for F)
3 Rock my soul 105 (ASCII i)
4 Rock my soul with 122, 122 (ASCII z, z)
5
6 The night holds Eddie's guitar ( = 66 = B)
7 Rock the night like a heartbroken heroine ( = 117 = u)
8 Fury is a rage-twisted thundercloud ( = 122 = z)
9 Rock the night with fury, fury (z, z)
10
11 Whisper my soul with the night (prints: FizzBuzz)
12
Try It
Booleans are true - aliases right , yes , and ok - and false , with its aliases wrong , no , and lies .
null means a value that’s missing or not available yet - aliases nothing , nowhere , nobody .
Rockstar also has a type called mysterious , which works like undefined in JavaScript; it’s the language’s way of saying “not only do I not know
what this is, I don’t even know how to tell you what’s wrong with it.”
1 X is 0
2 Until X is 10
3 Write X with ": "
4 If X is 5 Print "X is 5!" Else Print "X is not 5" (one-line If statement)
5 If X is 2
6 Print X with " is 2"
7 End (end of multi-line If block)
8 Put X plus 1 into X
9 End (end of Until loop)
10
Try It
Multi-line conditionals and loops have to end with an end of block. In previous versions of Rockstar, this had to be a blank line.
Rockstar 2 adds an explicit end keyword, along with the aliases yeah and baby .
To exit a loop immediately, use the break keyword. To skip the rest of the current iteration and restart the loop, use the continue keyword or the
alias take
break and take in Rockstar are wildcard keywords: you can follow them with anything you like, and everything up until the next end of
statement ( ,.!?; ) or newline will be ignored. This is mainly because the original Rockstar draft used take it to the top as a synonym for
continue , which sounded cool but is actually incredibly stupid, even by Rockstar standards.
1 My love is nothing
2 While my love ain't like a rose,
3 Whisper my love
4 If my love is like fire
5 Break my heart, yeah
6 Build my love up, baby
7
8 My love is nothing
9 While my love ain't like a rose,
10 Build my love up
11 If my love is like fire
12 Take me to the river, baby
13 Whisper my love, yeah
14
15
16
Try It
You can also end a Rockstar block with the keyword oh . Ooh ends two blocks, oooh ends three blocks, and so on until you get bored or your
computer runs out of memory. Think of this like the Rockstar equivalent of }}}} in C-style languages, or the ))))) that ends most Lisp programs
1 If true
2 if true
3 if true
4 if true
5 if true
6 Shout "Yep, it's DEFINITELY true"
7 Oooh yeah baby
8
Try It
Pronouns
Oh, yes, Rockstar has pronouns.2 In natural languages, a pronoun is just a way to refer to something based on context, instead of explicitly having
to name things every time - it’s the difference between “Tommy put his guitar in the back of his car, he drove out into the night” and “Tommy put
Tommy’s guitar in the back of Tommy’s car, Tommy drove out into the night”.
Rockstar supports the pronouns it , he , she , him , her , they , them , and a whole lot more - see the docs for the full list.
A Rockstar pronoun refers to the last variable which was assigned, or the last variable that appeared as the left-hand side of the test in a
conditional or loop statement. That sounds complicated, but it’s not: most of the time, you can just use it , him or her in your programs as you
would in regular English, and it’ll probably work.
1 My variable is true
2
3 If it's true print "it's true!"
4
5
6 The counter is 0
7 While it's less than 10
8 Shout it
9
10 Build it up, baby.
11
12
13 Gina's empty
14 Until she's like a star
Build her up
Whisper her, baby
Try It
Remember that although Rockstar has many different pronouns, at any given point in your program, every pronoun points to the
same variable – you can’t have him , her and it pointing to different things. Trying to update pronoun subjects based on assumptions about
gendered names would be hard enough even if rock’n’roll wasn’t full of dudes called Tracii, Alice and Rachel… you know that on the cover of
“Rumours” by Fleetwood Mac, Stevie is the woman and Lindsay is the man? Yeah. You’re gonna have to keep track of your own pronouns.
The combination of pronouns and ninja strings means that if you ever really need to push an ASCII DC3 control code onto the end of a string, you
can do it using this line of code:
Try It
1 X is 1
2
3 If x = 1 print "Axl"
4 If X is 1 print "Slash"
5
6 If X != 2 print "Izzy"
7 If X is not 2 print "Dizzy"
8 If X isn't 2 print "Duff"
If X ain't nothing print "Matt"
Try It
..hang on, what happened to Dizzy? not in Rockstar is a unary operator. X is not 2 is going to evaluate not 2 first - and 2 in
Rockstar is truthy, so not 2 is falsey, and then it’ll compare X is falsey , and X is 1, and 1 is truthy, and truthy is not equal to falsey… and so 1
is not 2 is actually false. Check out the docs to find out more about things which are truthy, things which are falsey, and how they all fit
together.
1 X is 5
2
3 If x >= 5 print "Ozzy"
4 If x <= 5 print "Tony"
5
6 If x < 6 print "Geezer"
7 if x > 4 print "Bill"
8
9
10 My heart is crying
11 The sky is on fire
12
13 If the sky is greater than my heart print "Vince"
If the sky is as high as my heart print "Mick"
If my heart is weaker than the sky print "Nikki"
If my heart is as low as the sky print "Tommy"
Try It
Rockstar also has the Boolean logical operators and , or , nor , and not .
1 My heart is true
2
3 Your love is true
4 His dreams are lies
5
6 Her heart is wrong
7
8 If my heart and your love shout "Joan"
9
10 If my heart or her dreams shout "Cherie"
11 If my heart and not her dreams shout "Lita"
If his dreams and her heart shout "Micki"
If his dreams nor her heart shout "Sandy"
Try It
Boolean operators in Rockstar will short-circuit - if you evaluate X and Y , and X is false, then Y will never be evaluated because
there’s no way X and Y can be true - and, like JavaScript, they’ll return the last evaluated operand necessary to resolve the expression:
1 Print 3 or 5 (prints: 3)
2
3 Print 0 or "rock!" (prints: rock!)
4 Print false and 1/0 (prints: false) (and never divides by zero)
5 Print "yeah!" or 1/0 (prints: yeah!) (and never divides by zero)
Try It
Functions
Functions in Rockstar are declared with the wants or takes keywords, followed by the list of variables denoting the function’s arguments. If you
want to declare a function that has no arguments, specify null , wants nothing or takes nothing .
1 Sum takes x, y
2
3 Give back x + y
4 End
5
6
7 Print sum taking 5, 6
8
9
10 Product takes x, y giving x * y
11
12 Foo is 5
13
Bar is 6
Print product taking foo, bar
Try It
The arguments in a function call must be separated with commas, ampersands, nactons, or the Oxford comma.
Nacton (n.) The ‘n’ with which cheap advertising copywriters replace the word ‘and’ (as in ‘fish ‘n’ chips’, ‘mix ‘n’ match’, ‘assault ‘n’ battery’), in
the mistaken belief that this is in some way chummy or endearing.
– “The Meaning of Liff”, Douglas Adams & John Lloyd
Rockstar supports both the UK nacton ( fish'n'chips ) and the US nacton ( Guns n' Roses ).
When you declare a function, you can even use and to separate the arguments – because at that point in the language, it can’t possibly mean
anything else.
1 The jungle takes Axl, Slash & Duff n' Izzy, and Steven
2
3 Write Axl. Write Slash. Write Duff. Write Izzy. Print Steven.
4 Yeah.
5
6
7 Call the jungle with "Oh, " & "oh " n' "oh, ", and "sweet child", " o' mine"
8
9
10 Led Zeppelin takes Robert and Jimmy and John Paul and Bonzo
11 Give back Robert with Jimmy with John Paul with Bonzo
12 Baby
Print Led Zeppelin taking "Stair", "way", " to ", and "Heaven"
Try It
Rock’n’Roll Arrays
Arrays in Rockstar are created with the rock keyword, alias push .
As we’ve already seen when we learned about ninja strings, rocking a string with a number will turn the numbers into a character and append it to
the end of the string. This is a special case, because it’s so incredibly useful for building strings.
In all other cases, rock x will turn x from a scalar into a single-element array [ x ] , and rock x with y will append y to the end of the array
denoted by x .
If you just rock a new variable, it’ll create an empty array. If you rock a new variable with a list of things, it’ll add those things to the new array:
1 Rock my array
2
3 Print my array (prints: [ ])
4
5 Rock the night with 1, 2, 3
6
7 Print the night (prints: [ 1, 2, 3 ])
8
Try It
Naturally, if you can rock , you can roll . Roll will remove and return the first element of the array.
Try It
Pop? Really?
Yes, pop. If you rock and roll arrays, they work like queues - first in, first out. If you want your array to behave like a stack, use push and pop :
Try It
push is actually an alias for rock - it adds the provided element to the end of the array; roll removes and returns the last
element, while pop removes and returns the first element.
Splitting Strings
To split a string in Rockstar, use the cut mutation, or aliases split and shatter :
Try It
Joining Arrays
To join an array in Rockstar, use the join mutation, or the aliases unite or gather :
Try It
Type Conversions
The built-in cast function (aka burn ) will parse strings into numbers, or convert a number into a Unicode character corresponding to the
number’s code point.
1 Let X be "123.45"
2
3 Shout X + X (prints: 123.45123.45)
4 Cast X with 10
5
6 Shout X + X (prints: 246.9)
7 Let X be "FF"
8 Cast X with 16 (cast using base 16)
9
10 Shout X (prints: 255)
11
12
13 Cast 65 into result. Shout result (prints: A)
14
15 Cast result. Shout result (prints: 65)
16
17
18 Cast 1046 into result. Shout result (prints: Ж)
19
20
21 Guitar is "1F3B8". Cast it with 16. Cast it.
22 Shout it. (prints: 🎸)
23
Try It
If you just want to convert a value into a string, add it to the empty string:
1 My boolean is true
2
3 My number is 12.34
4 The night is empty
5
6
7 My boolean is with the night
8 Shatter my boolean
9
10 Whisper my boolean (prints: [ "t", "r", "u", "e" ])
11
12 My number is with the night
13
Shatter my number
Whisper my number (prints: [ "1", "2", ".", "3", "4" ])
Try It
Cast
1. Technically let will declare a new variable in local scope, where put and is will declare or assign a global variable. It’s complicated. See
the documentation on variable scope if you really care. ↩
2. Rockstar is also woke, fetch, rizz, cheugy, and skibidi, no cap – but that’s not why it has pronouns. ↩