Quickstart
Just want to jump in? Well, plug your nose and in you go! All of these examples are complete and valid Brat programs.
Print something
Adds newline, too
No newline
Creating a new object
Subclassing:
Setting ‘fields’
Defining functions
Functions return their last value. Parameters go in a list before a |
You may also have functions with default arguments, variable arguments, or a mix of those and required arguments. Using an asterisk *
on the final formal parameter will gather up remaining arguments into an array.
Calling a function
Parentheses are optional, as are commas (sort of).
You can also use a hash-like notation to automatically pass in a hashtable of values. This hashtable will always be assigned to the last parameter in the function.
Multiple functions (or closures, or blocks) can be passed in like this:
Getting a function
For ease of use, -> actually returns the ‘value’ in a variable, so you can use it with functions or objects without getting into trouble.
Accessing current object
Strings
Go in single or double “quotes.”
You can do string interpolation with double quotes like this:
Booleans
False and null are false, true and everything else are true.
false?, true?, and null? can each take 0 to 3 parameters. If a parameter is a function, it will be called with no arguments.
This returns second parameter if first is true, otherwise returns false.
This returns second parameter if first is true, otherwise returns second parameter.
Numbers
Numbers are numbers. Most of the usual operators with the typical precedence are defined for them, too.
Arrays
Arrays are zero-based, dynamically resized lists which can contain anything.
Hashes
Hash tables, associative arrays, maps, dictionaries…whatever you want to call them, these are lists which can be indexed by an arbitrary value.
Binary Operators
You can define certain binary operators. These should always take one parameter. They can then be used as ‘infix’ notation. The ‘usual’ operators will have their typical precedence.
Regular Expressions
Regular expressions live in between /
and another /
.
Comments
Comments mark lines of code that will not be parsed or executed. They may be nested.
Comments may be nested.
Keywords
Brat does not have any keywords.
Semicolons
Semicolons are optional line enders and no one will cry if you don’t use them. They may if you do, though.