SimBa Programming GuideBeta
Learn how to harness the power of Python's simplicity with Rust's performance and safety
Introduction to SimBa
SimBa is a revolutionary hybrid programming language that combines the best of two worlds: Python's intuitive syntax and rapid development capabilities with Rust's blazing performance and memory safety guarantees.
Unlike traditional languages that force you to choose between ease-of-use and performance, SimBa allows you to write expressive, readable code that compiles to efficient machine code while preventing common programming errors like null pointer dereferences and buffer overflows.
Basic SimBa Syntax
SimBa uses Python's familiar indentation-based syntax, making it immediately accessible to Python developers while adding static typing for better performance and safety.
# Variable declaration with static typing
name: str = "SimBa"
age: int = 2024
is_fast: bool = True
# Function definition
def greet(user: str) -> str:
return f"Hello, {user}! Welcome to SimBa."
# Control flow (familiar Python syntax)
def fibonacci(n: int) -> int:
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
# Main execution
def main():
message = greet("Developer")
print(message)
print(f"Fibonacci(10) = {fibonacci(10)}")
if __name__ == "__main__":
main()Key syntax features:
- Indentation-based code blocks (like Python)
- Static type annotations for variables and functions
- Familiar keywords:
def,if,else,for,while - F-string formatting for easy string interpolation
Integrating Python Code
SimBa allows you to seamlessly embed Python code for rapid prototyping and accessing the vast Python ecosystem.
# Embed Python code blocks
python {
import numpy as np
import matplotlib.pyplot as plt
def create_plot(data):
plt.plot(data)
plt.show()
return "Plot created"
}
# Call Python functions from SimBa
def analyze_data(values: list[float]) -> str:
# Convert SimBa data to Python
python_result = python.create_plot(values)
return python_result
# Use Python libraries
def calculate_stats(numbers: list[float]) -> dict:
python {
mean = np.mean(numbers)
std = np.std(numbers)
return {"mean": mean, "std": std}
}Python integration features:
- Direct access to Python libraries
- Seamless data type conversion
- Runtime Python execution
Integrating Rust Code
For performance-critical sections, SimBa allows you to embed Rust code that compiles to native machine code.
# Embed Rust code for performance
rust {
pub extern "C" fn fast_fibonacci(n: u64) -> u64 {
match n {
0 => 0,
1 => 1,
_ => fast_fibonacci(n - 1) + fast_fibonacci(n - 2)
}
}
pub extern "C" fn process_array(
data: *const f64,
len: usize
) -> f64 {
let slice = unsafe {
std::slice::from_raw_parts(data, len)
};
slice.iter().sum()
}
}
# Call Rust functions from SimBa
def compute_large_fibonacci(n: int) -> int:
return rust.fast_fibonacci(n)
def sum_array(numbers: list[float]) -> float:
return rust.process_array(numbers)Rust integration features:
- Zero-cost abstractions
- Memory safety guarantees
- Native performance
Memory Safety & Concurrency
SimBa adopts Rust's ownership model to prevent memory leaks and data races while maintaining Python's ease of use.
# Safe buffer management
def safe_buffer_example():
# SimBa prevents buffer overflows
buffer = SafeBuffer::new(10)
# Ownership transfer
data = vec![1, 2, 3, 4, 5]
buffer.extend(data) # data is moved, not copied
return buffer.len()
# Concurrent processing without GIL
async def concurrent_processing(tasks: list[str]) -> list[str]:
results = []
# True parallelism (no GIL)
for task in tasks.parallel():
result = await process_task(task)
results.append(result)
return results
# Borrowing and references
def borrow_example(data: &mut list[int]):
# Mutable borrow - no data copying
data.append(42)
data.sort() # In-place sortingSafety features:
- Ownership system prevents memory leaks
- Borrowing eliminates unnecessary copying
- No Global Interpreter Lock (GIL) for true parallelism
- Compile-time prevention of data races
Using the SimBa Playground
The SimBa Playground provides an interactive environment to experiment with SimBa code, manage files, and see real-time results.
Available Commands:
run <filename>- Execute a SimBa fileexec <code>- Execute SimBa code directlyls- List all files in the workspaceclear- Clear the terminal outputhelp- Show available commandsexamples- Load example SimBa programs
$ exec def greet(): print("Hello, SimBa!")
$ run hello.smba
Hello, SimBa!
$ ls
hello.smba
fibonacci.smba
examples/
$ examples
Loaded example files:
- basic_syntax.smba
- python_integration.smba
- rust_performance.smba
$ run examples/fibonacci.smba
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34Playground features:
- Real-time code execution and feedback
- File management and organization
- Built-in examples and tutorials
- Error reporting with helpful suggestions
Best Practices
Follow these guidelines to write efficient and maintainable SimBa code:
- Use static typing: Always specify types for function parameters and return values
- Leverage Python for prototyping: Use Python blocks for rapid development and library access
- Optimize with Rust: Move performance-critical code to Rust blocks
- Embrace ownership: Use borrowing to avoid unnecessary data copying
- Handle errors explicitly: Use Result types for error-prone operations
- Write readable code: SimBa's syntax encourages clear, expressive programming
- Test thoroughly: Use the playground to experiment and validate your code
# Good: Clear types and error handling
def process_file(filename: str) -> Result[str, str]:
try:
content = read_file(filename)
processed = content.upper().strip()
return Ok(processed)
except FileNotFoundError:
return Err(f"File {filename} not found")
# Good: Efficient data processing
def analyze_large_dataset(data: &list[float]) -> Statistics:
# Use Rust for heavy computation
rust {
pub extern "C" fn compute_stats(
data: *const f64,
len: usize
) -> (f64, f64, f64) {
// Fast statistical computation
}
}
mean, median, std = rust.compute_stats(data)
return Statistics(mean, median, std)Ready to Start Coding?
Jump into the SimBa Playground and start experimenting with hybrid Python/Rust programming today!
Open Playground