Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Values

Here’s everything a value can be.

lives = 3                 // a whole number
ratio = 2.5               // a number with a fraction
mask = 0xFF               // hex; 0b1010 is binary
name = "Ada"              // text
ready = TRUE              // TRUE is 1 and FALSE is 0
nothing = NULL            // no value at all
PRINT lives, ratio, mask, name, ready, nothing
3 2.5 255 Ada 1 NULL

A variable is made by giving it a value. There’s no DIM, let, var or local to write first.

Any name you like

I come with a lot of built-in things, and whatever program I’m running inside adds more. You don’t have to learn their names before you choose yours. If you make a variable with the same name as one of them, yours wins.

len = 12                  // LEN is one of mine. In this program, it's yours
PRINT len + 1
13

Yours wins everywhere in your program: in every file, inside every function, even on lines above the one that makes it. That means the built-in is out of reach for as long as your variable exists. In the program above, LEN("four") wouldn’t count letters any more. It would try to look inside the number 12. If you want the built-in back, give your variable another name.

Two kinds of name stay taken:

  • A keyword’s words. PRINT = 3 can’t be a variable, because I couldn’t tell it from the statement.
  • Your own functions and types. If your program declares FUNCTION score(), then score = 3 is a mistake, and I’ll say so.

From BASIC: No $ or % on the end of a name. name$ is just name, and a variable can hold a different kind of value later on.

From Python: TRUE, FALSE and NULL are your True, False and None. TRUE really is the number 1.

From JavaScript: There’s one “nothing”, NULL. No undefined.

From Lua: NULL is your nil.

Text

Text goes in double quotes. + joins it to anything, and * repeats it.

name = "Ada"
PRINT "Hello, " + name + "!"
PRINT "lives: " + 3
PRINT "ab" * 3
PRINT "She said \"hi\"\tand left."
PRINT LEN(name), UPPER(name), LOWER(name)
Hello, Ada!
lives: 3
ababab
She said "hi"	and left.
3 ADA ada

You read text the way you’ll read a list: a position in parentheses, counting from 0. One position gives one character. A range gives a run of them, both ends included.

name = "Crash-tan"
PRINT name(0), name(5)
PRINT name(0 ... 4)
PRINT name(8 ... 0)           // a range that counts down reads backwards
C -
Crash
nat-hsarC

From BASIC: MID$, LEFT$ and RIGHT$ are a range in parentheses now: LEFT$(name, 5) is name(0 ... 4).

From Python and JavaScript: name[0:5] and name.slice(0, 5) are name(0 ... 4): the end is included.

From Lua: name:sub(1, 5) is name(0 ... 4): counting starts at 0.

When you need text that runs over several lines, or one full of quotes, put it between backticks. I take it exactly as written — nothing is escaped. If it starts on the line after the backtick, I drop the indent the lines share.

poem = `
    Roses are "red",
      violets are \blue\,
    this text is exactly as you see it.
    `
PRINT poem
Roses are "red",
  violets are \blue\,
this text is exactly as you see it.

From Python: Backticks are your triple quotes. Single quotes don’t make a string here.

From JavaScript: Backticks give you the many lines of a template literal, but there’s no ${} inside one. Join with +.

From Lua: Backticks are your [[long strings]], and + is your ...

Two values you might not have met

A coordinate is two numbers that travel together, and a vector is the trip from one coordinate to another. I’m made for games, so I have them built in.

spot = (10, 20)
PRINT spot, spot.x, spot.y
PRINT spot + (1, 1), spot * 2

trip = (0, 0) -> (30, 40)
PRINT trip, trip.start, trip.stop.y
(10, 20) 10 20
(11, 21) (20, 40)
(0, 0) -> (30, 40) (0, 0) 40

A range is every whole number from one end to the other — both ends included. You’ll use them in loops.

PRINT 1 ... 5
PRINT 1 TO 5          // the same thing, the way BASIC writes it
1 ... 5
1 ... 5

From Python: 1 ... 5 includes the 5. range(1, 5) wouldn’t.

Values that never change

CONST gives a variable its value once. If any line tries to give it another, I’ll refuse to run the program at all. Better that than have it go wrong later.

CONST max_lives = 5
PRINT "you get", max_lives, "lives"
you get 5 lives
CONST max_lives = 5
max_lives = 99

Crash-tan’s Tip: Notice I caught that before running a single line. I read the whole program first and check everything I can. If it starts, the names and the words in it are all real.