A fast, simple toy programming language that compiles to C, Rust, and also compiles Solana programs
// Variables and arithmetic
let greeting = "Hello, So Lang!"
let version = 2
let result = 42 * 2
// Output
print(greeting)
print(version)
print(result)
// Functions
fn add(a, b) {
return a + b
}
let sum = add(10, 20)
print(sum)
// Control flow
if sum > 25 {
print("big number")
} else {
print("small number")
}
program Counter {
// State structure
state CounterAccount {
count: u64,
authority: pubkey
}
// Initialize counter
instruction initialize(
@account(init, signer, writable) counter: CounterAccount,
@account(signer) user: pubkey
) {
counter.count = 0
counter.authority = user.key
print("Counter initialized")
}
// Increment counter
instruction increment(
@account(writable) counter: CounterAccount,
@account(signer) user: pubkey
) {
require(user.key == counter.authority, "Unauthorized")
counter.count = counter.count + 1
print("Counter incremented")
}
// Get current count
instruction get_count(
@account counter: CounterAccount
) {
print(counter.count)
}
}
# Basic development tools
sudo apt update && sudo apt install gcc make
# For Solana development (optional but recommended)
sh -c "$(curl -sSfL https://release.solana.com/v1.16.0/install)"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh# Clone/download the project
# Navigate to the project directory
# Build the optimized compiler
make
# Test it works
make test
# Test Solana functionality (if Solana CLI installed)
make test-solana
# Create example programs
make examples# Create a simple program
echo 'let message = "Hello, So Lang!"
let number = 42
print(message)
print(number)' > hello.so
# Compile to C
./bin/solang hello.so
gcc output.c -o hello && ./hello
# Or compile to Rust
./bin/solang hello.so --rust
rustc output.rs -o hello && ./hello# Create a Solana program
echo 'program HelloSolana {
instruction say_hello() {
print("Hello from Solana blockchain!")
}
}' > hello_solana.so
# Compile (automatically detected as Solana program)
./bin/solang hello_solana.so
# Output:
# ✓ Detected Solana program
# Program ID for HelloSolana: ABC123...
# Generated: program.rsSo Lang automatically detects Solana programs by looking for:
programkeyword with program declarations- Solana-specific constructs like
@account,instruction,state - Use of
--solana,--anchor, or--native-solanaflags
# Program IDs are automatically generated and managed
./bin/solang MyProgram.so --solana
# Output:
# Generating new program keypair for MyProgram...
# Program ID for MyProgram: HeLp1c5B5yfu6f1ryx5VvH5CffGgWg8F7auC7Dg1PWwr
# Keypair saved: keypairs/MyProgram-keypair.jsonProgram keypairs are stored in keypairs/ directory:
keypairs/
├── Counter-keypair.json
├── TokenTransfer-keypair.json
└── VotingDAO-keypair.json
@account(signer) // Must sign transaction
@account(writable) // Can modify account data
@account(init, signer, writable) // Initialize new account
@account(seeds = ["user", user.key], bump) // Program Derived Address
instruction transfer_tokens(
@account(writable) from: TokenAccount,
@account(writable) to: TokenAccount,
@account(signer) authority: pubkey,
amount: u64
) {
require(from.amount >= amount, "Insufficient balance")
require(authority.key == from.authority, "Unauthorized")
transfer(from, to, authority, amount)
print("Transfer completed")
}
state UserProfile {
owner: pubkey,
balance: u64,
created_at: u64,
is_active: bool
}
# Complete end-to-end test with real Solana deployment
make test-solana
# This automatically:
# 1. Builds So Lang compiler
# 2. Starts solana-test-validator
# 3. Creates and compiles test program
# 4. Builds Rust project
# 5. Deploys to local Solana testnet
# 6. Verifies deployment success# Setup Solana environment
make setup-solana
# Start local validator
solana-test-validator --reset &
# Compile Solana program
./bin/solang examples/counter.so --native-solana
# Build and deploy (example for counter program)
# ... create Rust project structure ...
# cargo build-bpf
# solana program deploy target/deploy/program.so./bin/solang program.so [options]
Options:
--rust Compile to Rust (regular program)
--solana Force Solana program compilation
--anchor Use Anchor framework (implies --solana --rust)
--native-solana Use native Solana (implies --solana --rust)
--output FILE Specify output file nameSo Lang Source (.so)
↓
Smart Detection/Explicit Flag
↓
┌─────────────────┬─────────────────┬─────────────────┐
│ Regular C │ Regular Rust │ Solana Rust │
│ │ │ │
│ output.c │ output.rs │ program.rs │
│ ↓ │ ↓ │ ↓ │
│ GCC │ rustc │ cargo build-bpf│
│ ↓ │ ↓ │ ↓ │
│ Executable │ Executable │ .so (Solana) │
└─────────────────┴─────────────────┴─────────────────┘
The So Lang compiler follows a clean, traditional pipeline:
- Lexer: Tokenizes source code into meaningful symbols
- Parser: Builds Abstract Syntax Tree (AST) from tokens
- Detector: Automatically identifies Solana programs
- Code Generator: Emits C, Rust, or Solana-specific Rust code
- Lexer (
lexer_*functions): Fast character-by-character tokenization with Solana keywords - Parser (
parser_*functions): Recursive descent parser with Solana syntax support - AST (
ast_*functions): Tree-based intermediate representation - Detector (
detect_solana_program): Smart program type detection - Compiler (
compiler_*functions): Multi-target code generation - Solana Utils: Program ID generation, keypair management, validation
- Automatic Account Validation:
@accountconstraints enforced at compile time - Signer Verification:
@account(signer)automatically generates validation code - Ownership Checks: Account ownership verified in generated code
- Integer Overflow Protection: Safe arithmetic operations
- Program ID Validation: Validates program IDs are valid Solana pubkeys
instruction secure_transfer(
@account(writable, signer) from: TokenAccount,
@account(writable) to: TokenAccount,
amount: u64
) {
require(from.amount >= amount, "Insufficient funds")
require(amount > 0, "Amount must be positive")
// Safe operations automatically generated
}
- Compilation Speed:
- Basic programs: ~0.001s
- Solana programs: ~0.003s
- Complex programs: ~0.01s
- Generated Code: Zero-overhead, equivalent to hand-written code
- Memory Usage: Minimal allocation during compilation
- Runtime Performance: No runtime overhead (compiled languages)
# Core build targets
make # Build optimized release version (default)
make debug # Build debug version with symbols
make test # Run basic test suite
make test-solana # Run complete Solana integration test
# Development targets
make examples # Create example programs
make benchmark # Performance testing
make memcheck # Memory leak detection (requires valgrind)
make format # Code formatting (requires clang-format)
# Environment setup
make setup-solana # Install Solana development environment
make install # Install So Lang to system PATH
make clean # Remove build artifacts
make help # Show all available targetsContributions are welcome! The codebase is designed to be educational and extensible:
- Fork the repository
- Create a feature branch:
git checkout -b feature/new-feature - Make your changes to
so_lang.candso_lang.h - Test thoroughly:
make test && make test-solana - Follow the coding style established in the project
- Submit a pull request with a clear description
- New Token Types: Add to
TokenTypeenum and lexer - New AST Nodes: Add to
NodeTypeenum and parser - New Backends: Extend compiler with new target support
- Solana Features: Add new account constraints or instructions
# Setup and build
make setup-solana && make
# Test everything
make test && make test-solana
# Basic compilation
./bin/solang program.so # Auto-detect target
./bin/solang program.so --rust # Force Rust
./bin/solang program.so --solana # Force Solana
# Solana development
echo 'program Test { instruction hello() { print("Hello!") } }' > test.so
./bin/solang test.so # Auto-detects Solana program
# Get help
make help
./bin/solang --help🚀 Happy compiling!