Syntax and Basics
Program Structure
A Twinkies program consists of:
- Function declarations
- Variable declarations
- Statements
- Optional module includes
- Optional FFI declarations
Every program must have a main() function as the entry point:
func main() -> int {
// Your code here
return 0;
}
Comments
Twinkies supports two comment styles:
// Single-line comment
/* Multi-line
comment */
Identifiers
Identifiers (variable names, function names) must:
- Start with a letter or underscore
- Contain only letters, digits, and underscores
- Are case-sensitive
let myVar: int = 10; // ✓ Valid
let _private: int = 5; // ✓ Valid
let 2invalid: int = 0; // ✗ Invalid: starts with digit
let my-var: int = 0; // ✗ Invalid: contains hyphen
Keywords
Twinkies has 25 reserved keywords that cannot be used as identifiers:
Control Flow & Declarations:
func,let,if,else,while,break,continue,return,print,extern,from
Type Keywords:
int,int8,int16,int32,int64,bool,float,double,string
Literal Keywords:
true,false,null
Other:
asm,volatile
Variable Declarations
Variables are declared using the let keyword with explicit type annotations:
let x: int = 10;
let name: string = "Twinkies";
let isActive: bool = true;
let pi: float = 3.14;
Variables can be declared without initialization, but must be assigned before use:
let x: int; // Declaration
x = 10; // Assignment
Variable Assignment
Variables are assigned using the = operator:
let x: int = 10;
x = 20; // Reassign x
x = x + 5; // Update x
Literals
Integer Literals
let decimal: int = 42;
let negative: int = -10;
Floating-Point Literals
let pi: float = 3.14;
let e: double = 2.718;
let scientific: float = 1.5e-3;
Boolean Literals
let yes: bool = true;
let no: bool = false;
String Literals
let greeting: string = "Hello, World!";
let empty: string = "";
Strings support escape sequences:
\n- newline\t- tab\\- backslash\"- double quote
Null Literal
let x: int = null;
let s: string = null;
Operators
Arithmetic Operators
let a: int = 10;
let b: int = 3;
let sum: int = a + b; // 13
let diff: int = a - b; // 7
let product: int = a * b; // 30
let quotient: int = a / b; // 3 (integer division)
let remainder: int = a % b; // 1
Comparison Operators
let x: int = 10;
let y: int = 5;
x > y // true
x >= y // true
x < y // false
x <= y // false
x == y // false
x != y // true
Logical Operators
let a: bool = true;
let b: bool = false;
a && b // false (logical AND)
a || b // true (logical OR)
!a // false (logical NOT)
Unary Operators
let x: int = 10;
let neg: int = -x; // -10
let not: bool = !true; // false
Operator Precedence
Operators are evaluated in this order (highest to lowest):
- Unary:
!,- - Multiplicative:
*,/,% - Additive:
+,- - Comparison:
<,>,<=,>= - Equality:
==,!= - Logical AND:
&& - Logical OR:
||
Use parentheses to override precedence:
let result: int = (2 + 3) * 4; // 20, not 14
Statements
Expression Statements
Any expression followed by a semicolon is a statement:
x + y; // Valid but does nothing
print(x); // Valid function call
Block Statements
Multiple statements can be grouped in braces:
{
let x: int = 10;
let y: int = 20;
print(x + y);
}
Blocks create a new scope for variables.
Semicolons
Semicolons are required to terminate statements:
let x: int = 10; // ✓ Correct
let y: int = 20 // ✗ Error: missing semicolon
Whitespace
Twinkies is whitespace-insensitive (except in strings). These are equivalent:
let x:int=10;
let x : int = 10;
let x : int = 10 ;
Case Sensitivity
Twinkies is case-sensitive:
let myVar: int = 10;
let MyVar: int = 20; // Different variable
let MYVAR: int = 30; // Different variable
Function Calls
Functions are called using parentheses:
print("Hello");
let result: int = factorial(5);
let sum: int = add(10, 20);
Array Access
Arrays are accessed using square brackets:
let arr: int[5];
arr[0] = 10;
let value: int = arr[0];
String Indexing
Strings can be indexed to get individual characters:
let str: string = "Hello";
let first: string = str[0]; // "H"
Next Steps
- Learn about Types for detailed type information
- Read Control Flow for if/else and loops
- Check Functions for function syntax
