Examples Guide
This guide provides detailed explanations of all example programs included with Twinkies.
Example Organization
Examples are organized into categories:
- basic/ - Simple programs demonstrating core language features
- advanced/ - Complex examples with advanced features
- ffi/ - Foreign Function Interface examples
- modules/ - Module system demonstrations
- debugging/ - Error handling and debugging examples
Basic Examples
hello.tl
The simplest Twinkies program:
func main() -> int {
print(42);
return 0;
}
What it demonstrates:
- Basic program structure
- The
main()function - The
print()statement - Return statement
arithmetic.tl
Demonstrates basic arithmetic operations:
func main() -> int {
let a: int = 10;
let b: int = 5;
let sum: int = a + b;
let diff: int = a - b;
let product: int = a * b;
let quotient: int = a / b;
let remainder: int = a % b;
print(sum);
print(diff);
print(product);
print(quotient);
print(remainder);
return 0;
}
What it demonstrates:
- Variable declarations
- Arithmetic operators (+, -, *, /, %)
- Integer division
- Modulo operation
factorial.tl
Recursive factorial calculation:
func factorial(n: int) -> int {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
func main() -> int {
let result: int = factorial(5);
print(result);
return 0;
}
What it demonstrates:
- Function definition
- Recursion
- Conditional statements
- Return values
string_test.tl
Comprehensive string operations:
func main() -> int {
let message: string = "Hello, World!";
print(message);
// String concatenation
let a: string = "foo ";
let b: string = "bar ";
let c: string = a + b;
print(c);
// String functions
let len: int = strlen(message);
let sub: string = substr(message, 0, 5);
let cmp: int = strcmp("hello", "world");
print(len);
print(sub);
print(cmp);
// String indexing
let first_char: string = message[0];
print(first_char);
return 0;
}
What it demonstrates:
- String literals
- String concatenation
- String functions (strlen, substr, strcmp)
- String indexing
float_basics.tl
Floating-point operations:
func main() -> int {
let my_float: float = 3.14;
let my_double: double = 2.718;
// Arithmetic
let sum: float = my_float + 1.0;
let product: float = my_float * 2.0;
// Mixed arithmetic
let mixed: double = my_float + 5;
// Comparisons
if (my_float > 3.0) {
print(1);
}
return 0;
}
What it demonstrates:
- Float and double types
- Floating-point arithmetic
- Mixed numeric operations
- Floating-point comparisons
break_continue_test.tl
Loop control statements:
func main() -> int {
let i: int = 0;
// Test break
while (i < 10) {
if (i == 5) {
break;
}
print(i);
i = i + 1;
}
// Test continue
let j: int = 0;
while (j < 10) {
j = j + 1;
if (j == 5) {
continue;
}
print(j);
}
return 0;
}
What it demonstrates:
- While loops
- Break statement
- Continue statement
- Loop control flow
input_test.tl
Interactive input example:
func main() -> int {
print("Enter a number: ");
let num: int = input();
print("You entered:", num);
// Menu system
print("1. Option 1");
print("2. Option 2");
let choice: int = input();
if (choice == 1) {
print("You chose option 1");
} else if (choice == 2) {
print("You chose option 2");
}
return 0;
}
What it demonstrates:
- Input function
- Interactive programs
- Menu systems
- User interaction
Advanced Examples
overload_test.tl
Function overloading:
func add(x: int) -> int {
return x + 1;
}
func add(x: int, y: int) -> int {
return x + y;
}
func add(flag: bool) -> int {
if (flag) {
return 42;
}
return 0;
}
func main() -> int {
let a: int = add(5); // Calls first overload
let b: int = add(2, 3); // Calls second overload
let c: int = add(true); // Calls third overload
return 0;
}
What it demonstrates:
- Function overloading
- Overload resolution
- Different parameter types
array_in_bounds.tl
Array operations with bounds checking:
func main() -> int {
let arr: int[5];
let i: int = 0;
// Initialize array
while (i < 5) {
arr[i] = i * 2;
i = i + 1;
}
// Print array
i = 0;
while (i < 5) {
print(arr[i]);
i = i + 1;
}
// Valid access
print(arr[4]); // In bounds
return 0;
}
What it demonstrates:
- Array declaration
- Array initialization
- Array iteration
- Bounds checking
null.tl
Null handling:
func main() -> int {
let null_int: int = null;
let null_string: string = null;
// Null comparisons
if (null_int == null) {
print("null_int is null");
}
// Null in arithmetic
let x: int = 10;
let result: int = x + null_int;
// Null in strings
let str: string = "Hello";
let concat: string = str + null_string;
return 0;
}
What it demonstrates:
- Null literal
- Null comparisons
- Null in expressions
- Null handling
inline_asm.tl
Comprehensive inline assembly examples:
func simple_asm() -> int {
let result: int;
asm {
"mov $42, %%rax\n\t"
"mov %%rax, %0"
: "=r" (result)
:
: "rax"
};
return result;
}
func get_cpu_info() -> int {
let eax: int;
asm {
"cpuid"
: "=a" (eax)
: "a" (1)
: "ebx", "ecx", "edx", "cc"
};
return eax;
}
func main() -> int {
print(simple_asm());
print(get_cpu_info());
return 0;
}
What it demonstrates:
- Inline assembly syntax
- Register constraints
- CPUID instruction
- Assembly output/input operands
FFI Examples
simple_ffi_test.tl
Basic FFI usage:
extern "cdecl" from "kernel32.dll" {
func GetTickCount() -> int;
func GetCurrentProcessId() -> int;
}
func main() -> int {
let ticks: int = GetTickCount();
let pid: int = GetCurrentProcessId();
print("Ticks:", ticks);
print("Process ID:", pid);
return 0;
}
What it demonstrates:
- FFI declaration
- Calling external functions
- Windows API calls
comprehensive_ffi_test.tl
Advanced FFI features:
extern "stdcall" from "user32.dll" {
func MessageBoxA(hWnd: int, lpText: string, lpCaption: string, uType: int) -> int;
}
extern "stdcall" from "kernel32.dll" {
func GetTickCount() -> int;
func Sleep(dwMilliseconds: int) -> int;
}
func main() -> int {
let ticks: int = GetTickCount();
Sleep(1000);
MessageBoxA(0, "FFI Test", "Hello", 0);
return 0;
}
What it demonstrates:
- Multiple FFI declarations
- Different calling conventions
- String parameters
- System functions
Module Examples
modules/main.tl
Module system usage:
#include "math.tlh"
#include "string.tlh"
func main() -> int {
let fact: int = factorial(5);
let combined: string = string_concat("Hello", "World");
print(fact);
print(combined);
return 0;
}
Note: The actual example file has a typo (#iinclude instead of #include). The correct syntax is #include.
What it demonstrates:
- Include directives
- Module usage
- Function calls from modules
modules/math.tlh and math.tl
Module definition:
// math.tlh
func factorial(n: int) -> int;
func gcd(a: int, b: int) -> int;
// math.tl
func factorial(n: int) -> int {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
func gcd(a: int, b: int) -> int {
if (b == 0) return a;
return gcd(b, a % b);
}
What it demonstrates:
- Header file structure
- Implementation file structure
- Module organization
Running Examples
Compile Basic Example
./compiler examples/basic/hello.tl -o hello.c
Compile with Modules
./compiler examples/modules/main.tl -o main.c \
examples/modules/math.tl examples/modules/string.tl --modules
Compile FFI Example
./compiler examples/ffi/simple_ffi_test.tl -o ffi_test.c
Debug Options
./compiler examples/basic/hello.tl -o hello.c --debug --verbose --ast
Example Patterns
Pattern: Array Processing
let arr: int[10];
let i: int = 0;
// Initialize
while (i < 10) {
arr[i] = i * 2;
i = i + 1;
}
// Process
i = 0;
while (i < 10) {
print(arr[i]);
i = i + 1;
}
Pattern: Input Validation
let choice: int = input();
while (choice < 1 || choice > 5) {
print("Invalid! Enter 1-5: ");
choice = input();
}
Pattern: Search
let arr: int[10];
let target: int = 42;
let found: bool = false;
let i: int = 0;
while (i < 10 && !found) {
if (arr[i] == target) {
found = true;
}
i = i + 1;
}
Next Steps
- Try modifying the examples
- Create your own programs
- Read the Quick Reference for syntax help
- Check other documentation sections for detailed explanations
