TinyTotVM includes 67 comprehensive example programs demonstrating all language features. This guide walks through the key examples and what they demonstrate.
Complete feature demonstration showing:
- Basic arithmetic and string operations
- Variable storage and retrieval
- Conditional logic and comparisons
- Object creation and manipulation
- Function calls and returns
ttvm examples/showcase.ttvmDemonstrates profiling and tracing capabilities:
- Function calls with parameters
- Performance measurement
- Instruction counting
ttvm --profile --trace examples/simple_profiling_test.ttvmIEEE 754 floating-point operations:
- Float arithmetic (ADD_F, SUB_F, MUL_F, DIV_F)
- Float comparisons with epsilon handling
- Type coercion between int and float
Dynamic object manipulation:
- Object creation with MAKE_OBJECT
- Field operations (SET_FIELD, GET_FIELD, HAS_FIELD)
- Object introspection with KEYS
- Nested object structures
Dynamic list operations:
- List creation with MAKE_LIST
- Element access with INDEX
- Length operations
- Heterogeneous data storage
Basic function definitions and calls:
- Simple function with no parameters
- Function call and return mechanism
- Stack-based parameter passing
Function parameter handling:
- Multiple parameter functions
- Parameter passing and retrieval
- Local variable scoping
First-class functions:
- Creating function pointers with MAKE_FUNCTION
- Dynamic function calls with CALL_FUNCTION
- Functions as values
Higher-order programming:
- Functions taking other functions as parameters
- Function composition
- Functional programming patterns
Basic closure functionality:
- Variable capture with CAPTURE
- Anonymous functions with MAKE_LAMBDA
- Lexical scoping demonstration
Advanced closure patterns:
- Multiple variable capture
- Closure factories
- Environment isolation
Complex closure scenarios:
- Nested closure creation
- Multi-level variable capture
- Closure composition
Lambda expression examples:
- Anonymous function creation
- Captured environment handling
- Lambda as first-class values
Basic module functionality:
- Module import with IMPORT
- Function export with EXPORT
- Cross-module function calls
Advanced module patterns:
- Multiple module imports
- Complex function sharing
- Module namespace isolation
Cross-module closures:
- Exporting closure factories
- Importing and using closures
- Captured variable preservation
Advanced cross-module closures:
- Nested closures across modules
- Complex environment sharing
- Module boundary testing
Basic exception handling:
- TRY/CATCH/END_TRY blocks
- Exception throwing with THROW
- Error message handling
Function-level exception handling:
- Exceptions in function calls
- Stack unwinding demonstration
- Exception propagation
Nested exception scenarios:
- Multiple exception levels
- Inner and outer exception handling
- Exception re-throwing
VM error handling:
- Built-in VM errors as exceptions
- Type mismatch handling
- Stack underflow recovery
Math library demonstration:
- Mathematical constants (PI, E)
- Math functions (sqrt, abs, max, min)
- Factorial implementation
String utility functions:
- String concatenation and repetition
- Case conversion (upper, lower)
- String trimming and length
Prelude module usage:
- Common function imports
- Simplified standard library access
Complete standard library showcase:
- Math, string, list, and I/O operations
- Type conversion utilities
- Combined library usage patterns
Basic optimization demonstration:
- Simple constant folding
- Dead code elimination
- Performance improvements
Constant folding optimization:
- Compile-time arithmetic evaluation
- Boolean expression optimization
- Constant propagation
Dead code elimination:
- Unreachable code removal
- Conditional branch optimization
- Jump optimization
Tail call optimization:
- Recursive function optimization
- Tail call detection
- Performance improvement demonstration
All optimization passes:
- Combined optimization techniques
- Performance measurement
- Instruction count reduction
Basic I/O operations:
- File creation and reading
- FILE_EXISTS checking
- Simple file manipulation
Advanced I/O features:
- Environment variable access
- Command-line argument handling
- File and directory operations
- Process execution
Basic networking:
- DNS resolution
- HTTP GET requests
- Simple network operations
TCP socket operations:
- TCP connection establishment
- Data sending and receiving
- Connection management
Circular dependency detection:
- Demonstrates import cycle detection
- Shows proper error handling
- Tests module system robustness
Variable deletion:
- DELETE instruction usage
- Variable scope management
- Error handling for undefined variables
# Basic functionality
ttvm examples/showcase.ttvm
ttvm examples/function_pointer_test.ttvm
ttvm examples/closure_test.ttvm
# Advanced features
ttvm examples/module_test.ttvm
ttvm examples/exception_test.ttvm
ttvm examples/stdlib_comprehensive_test.ttvm# Debug mode
ttvm --debug examples/showcase.ttvm
# Optimization
ttvm --optimize examples/constant_folding_test.ttvm
# Profiling and tracing
ttvm --profile --trace examples/simple_profiling_test.ttvm
# Combined flags
ttvm --debug --optimize --gc-stats examples/showcase.ttvm# Run all examples
ttvm test-all
# Test specific categories
for test in examples/function*.ttvm; do ttvm "$test"; done
for test in examples/closure*.ttvm; do ttvm "$test"; done
for test in examples/stdlib*.ttvm; do ttvm "$test"; doneMost examples produce specific output demonstrating their functionality. For example:
30
1
1
Not equal
Now equal!
=== Simple Profiling Test ===
15
Hello World
=== Profiling Results ===
┌───────────┬───────┬───────────┬──────────────┬────────────────────┐
│ Function ┆ Calls ┆ Time (ms) ┆ Instructions ┆ Avg Time/Call (μs) │
╞═══════════╪═══════╪═══════════╪══════════════╪════════════════════╡
│ fn@0x000B ┆ 1 ┆ 0.026 ┆ 4 ┆ 26.0 │
│ fn@0x000F ┆ 1 ┆ 0.022 ┆ 4 ┆ 22.0 │
└───────────┴───────┴───────────┴──────────────┴────────────────────┘
When creating new examples, follow these patterns:
- Clear documentation - Comment the purpose and features
- Progressive complexity - Start simple, build up
- Expected output - Include comments showing expected results
- Error handling - Demonstrate proper exception handling
- Feature focus - Each example should focus on specific features
; Example Name: Feature Description
; Demonstrates: List of features being shown
PUSH_STR "=== Example Name ==="
PRINT
; Feature demonstration code here
; ...
HALT