Version: v0.6.1 - Beta.  We welcome contributors & feedback.  THanks!

Syntax Cheat Sheet A quick overview for experienced programmers

// This is a one-line comment

/*
    This is a block comment
*/


//========================================================
//                       STRINGS
//========================================================

// Strings are created with a single-quote (')
'Hello World!';

// Strings are joined with the 'stringy' operator (~)
'Hello, ' ~ ' World!';  //= 'Hello, World!'

// Backticks (`) are converted to single quotes
'That`s terrific!';  //= 'That's terrific!'

// Multi-line strings are surrounded by quote fences
'''
    This is
    a multiline
    string!
''';


//========================================================
//                  BOOLEANS / EQUALITY
//========================================================

// Booleans
true;
false;

// Negation
!true;  //= false
!false; //= true

// Equality check with '=='.
// Acts like '===' in PHP (no type coercion).
1 == 1;    //= true
1 == '1';  //= false

// Inequality
1 != 1; //= false
1 != 2; //= true

// Comparison
1 < 2;  //= true
1 > 2;  //= false
2 >= 2; //= true
2 <= 2; //= true


//========================================================
//                     VARIABLES
//========================================================

// Initializing a variable
$letters = 'abcde';
$year = 2020;

// Built-in types have methods via 'dot'
'abdce'.length();   //= 5
'abcde'.reverse();  //= 'edcba'

// Short-hand math assignment
$year += 1;  // same as 'year = year + 1'

$greeting = 'Hi';
$greeting ~= '!';  // 'Hi!' (stringy assignment)


//========================================================
//                       LISTS
//========================================================

// Lists are an ordered sequence of values, of any type.
$colors = ['red', 'blue', 'yellow'];

// Built-in methods
$colors.length();    //= 3
$colors.join(', ');  //= 'red, blue, yellow'

// Indexes are zero-based
$colors[0];  //= 'red'
$colors[1];  //= 'blue'

// Directly assign a value
$colors[2] = 'orange';

// Add & remove items
$colors.push('green');  // Add to the end
$colors.pop();          //= 'green'

// List Add operator
$colors #= 'purple';  // Same as 'push'
$colors.pop();        //= 'purple'


//========================================================
//                        MAPS
//========================================================

// Maps are key/value pairs.
// They are like associative arrays in PHP, but
// with JavaScript object literal syntax.

$user = {
    name: 'Taylor',
    age: 25,
};

$user['age'];  //= 25

// Assign new fields.
$user['address'] = '22 Treetop Road';

// Or use dot notation
$user.address = '22 Treetop Road';

// Dot (.) notation is strict.
$user.job;  // ERROR. 'job' key doesn't exist.

// Bracket notation [] is not strict.
$user['job'];  //= '' (a safe falsey value)



//========================================================
//                       IF / ELSE
//========================================================

if condition1 {
    // ...
} else if condition2 {
    // ...
} else {
    // ...
}


// Parens are not required in control flow statements.
if $a % 2 == 0 {
    print('Number is even.');
}

// One-liner syntax if block has a single statement.
if $a % 2 == 0: print('Number is even.');



//========================================================
//                       LOOPS
//========================================================

// 'foreach' loops iterate through a list.
foreach $colors as $color {
    print($color);
}

// Loop through numbers, using the 'range' function.
foreach range(1, 4) as $n {
    print('Color Number: ' ~ $n);
}

// Create a variable for key and value of a Map or List
$colorNums = { red: 3, blue: 5 };
foreach $colorNums as $color, $num {
    print($color ~ ': ' ~ $num);
}

// 'loop' repeats forever until you exit with 'break'.
// Use this instead of 'while'.
loop {
    $color = Palette.getNextColor();
    if !$color: break;
    if $color.isBoring: continue;
    print($color.name);
}


//========================================================
//                      FUNCTIONS
//========================================================

// Declaring a function
function doSomething($arg) { ... }

// Use 'F' as a shortcut
F hello($name) {
    return 'Hello ' ~ $name ~ '!'
}

hello('Tina');  //= 'Hello Tina!'


// An optional argument with default value
F hello($name, $greeting = 'Hello') {
    return $greeting ~ ' ' ~ $name ~ '!'
}

hello('Tim');        //= 'Hello Tim!'
hello('Tim', 'Hi');  //= 'Hi Tim!'


//========================================================
//                    SPECIAL STRINGS
//========================================================

// TypeStrings are secure literal strings that are
// prefixed with a type token.  They take parameterized
// values via fill().
$query = sql'select * from user where id = {}';
$query.fill($userId);
$user = Db.select($query);

// Regular Expression strings are prefixed with 'r'.
$regex = r'\w+=(\d+)';
'foo=1234'.match($regex)[1]; //= '1234'


//========================================================
//                       MODULES
//========================================================

// You can organize functions into separate module files,
// each in its own namespace. Modules names are
// always UpperCamelCase.

// --- modules/ForumUser.tht ---

F hello($name) {
    print('Hello ' ~ $name ~ '!');
}

// --- pages/home.tht ---

// Modules are automatically imported from the 'modules'
// folder when called.
ForumUser.hello('Tina');  //= 'Hello, Tina!'

// Or import a module directly
import('other/OtherModule.tht');

OtherModule.otherFunction();