Skip to content

maydaythecoder/cortex

Repository files navigation

Cortex Logo

A high-performance AI/ML programming language built in Rust

Cortex is a compiled programming language designed for AI and machine learning.
Its goal is to make building, training, and experimenting with models simpler and faster by combining:

  • Readable, bracket-based syntax β†’ focused on clarity and flow
  • Rust-powered performance β†’ memory-safe and blazingly fast
  • AI-first primitives β†’ tensors, datasets, models, training utilities
  • Professional tooling β†’ CLI, VSCode extension, language server, debugger

Instead of writing AI in bloated frameworks, Cortex gives you a language designed from the ground up for machine intelligence.


✨ Why Cortex?

  • AI-first design β†’ control flow, data structures, and math built with ML in mind
  • Concise syntax β†’ if [x > 5] | print[x] ^
  • Rust-powered β†’ memory-safe, zero-cost abstractions, and blazingly fast
  • Integrated ML stack β†’ arrays β†’ tensors, objects β†’ models, dicts β†’ datasets
  • Professional tooling β†’ Complete development environment with debugging, IntelliSense, and testing
  • Extendable β†’ pluggable backend (CPU now, GPU later)

πŸš€ Quick Start

Prerequisites

  • Rust 1.70+
  • Node.js 16+ (for VSCode extension)
  • Git

Installation

# Clone the repository
git clone https://github.com/muhyadinmohamed/cortex
cd cortex

# Build the compiler (Rust)
cd packages/compiler
cargo build --release

# The compiler binary will be at: target/release/cortexc

Hello World

# Create a new project
cortex new hello-world
cd hello-world

# Run the program
cortex run src/main.ctx

# Or run directly
cortex run examples/hello_world.ctx

πŸŽ‰ RUST IMPLEMENTATION: High-Performance Interpreter!

Cortex now has a fully functional Rust interpreter with syntax highlighting and formatting support!

βœ… Currently Working Features

  • πŸ”₯ High-Performance Interpreter: Rust-powered execution with memory safety
  • πŸ”₯ Syntax Highlighting: Beautiful VS Code extension with custom color palette
  • πŸ”₯ Code Formatting: Automatic formatting with configurable indentation
  • πŸ”₯ Arithmetic Operations: All math operations (+, -, *, /, %, **) with correct type handling
  • πŸ”₯ String & Number Printing: Proper output formatting for both strings and numbers
  • πŸ”₯ While Loops: Complete loop implementation with condition evaluation
  • πŸ”₯ Functions: Function definitions, calls, and parameter handling
  • πŸ”₯ Variables: Variable assignment, scoping, and expression evaluation
  • πŸ”₯ Cross-Platform: Works on macOS, Linux, and Windows

πŸš€ Working Examples

# Run arithmetic operations
cd rust
cargo run -- run ../examples/arithmetic.ctx
# Output: Arithmetic Operations with all calculations

# Run loops with factorial
cargo run -- run ../examples/simple_loops.ctx
# Output: Loop iterations and factorial calculation

# Run hello world
cargo run -- run ../examples/hello_world.ctx
# Output: Hello, World!
#         Welcome to Cortex - the AI/ML programming language!

πŸ“– Example Code

Basic Variables and Functions

let greeting := "Hello"

func add[a, b] |
  return[a + b]
^

func main[] |
  print[greeting + " from Cortex!"]
  let sum := add[10, 20]
  print["Result: " + str[sum]]
^

main[]

Working Arithmetic Example

func main[] |
  let a := 10
  let b := 3
  
  let sum := a + b
  let diff := a - b
  let product := a * b
  let quotient := a / b
  
  print[sum]
  print[diff]
  print[product]
  print[quotient]
^

main[]

Working Loop Example

func main[] |
  let counter := 1
  
  while [counter <= 5] |
    print[counter]
    let counter := counter + 1
  ^
  
  // Factorial calculation
  let n := 5
  let factorial := 1
  let temp := 1
  
  while [temp <= n] |
    let factorial := factorial * temp
    let temp := temp + 1
  ^
  
  print[factorial]
^

main[]

🧩 Core Features (Currently Implemented)

βœ… Working Language Features

  • Variables: let x := 42 with type inference
  • Functions: func name[params] | body ^ with parameter handling
  • Arithmetic: All basic operations (+, -, *, /, %, **)
  • Printing: print[value] for strings and numbers
  • Loops: while [condition] | body ^ with proper condition evaluation
  • Control Flow: if [condition] | body ^ with else support
  • Comments: // single-line and /* */ multi-line

πŸ”„ In Development

  • Arrays: Basic array data structures
  • String Concatenation: "hello" + "world"
  • For Loops: for [i in range[10]] | body ^
  • Advanced Types: Enhanced type system

πŸ“‹ Planned AI/ML Primitives

Tensor Primitives

  • Multi-dimensional tensors: tensor[[1,2],[3,4]] with shape inference
  • Tensor operations: A @ B (matrix multiplication), βˆ‡[loss] (gradients)
  • Memory optimization: Contiguous storage, lazy evaluation, sparse tensors
  • Device support: CPU/GPU/TPU tensor operations

Neural Network Primitives

  • Modular neurons: Neuron { weights, bias, activation, domain }
  • Composable layers: Layer { neurons, learning_curve, capabilities }
  • Network assembly: compose_network[medical_layer, educational_layer]
  • Layer registry: Import/export specialized domain layers

Automatic Differentiation

  • Computational graphs: Forward/backward pass infrastructure
  • Gradient operators: βˆ‡[loss, params], βˆ‚[f, x] (partial derivatives)
  • Chain rule: Automatic gradient computation for all operations
  • Optimization: SGD, Adam, RMSprop with learning rate scheduling

Advanced Features

  • GPU acceleration: CUDA/OpenCL backends for tensor operations
  • Distributed training: Multi-GPU/multi-node support
  • Model serving: High-performance inference with sub-millisecond latency
  • Package system: Community-driven layer and model sharing

πŸ“‚ Project Structure

cortex/
β”œβ”€ packages/                    # Core packages
β”‚  β”œβ”€ compiler/                # Rust compiler/interpreter ⭐
β”‚  β”‚  β”œβ”€ src/                 # Source code
β”‚  β”‚  β”‚  β”œβ”€ main.rs           # CLI interface
β”‚  β”‚  β”‚  β”œβ”€ lib.rs            # Library interface
β”‚  β”‚  β”‚  β”œβ”€ lexer.rs          # Tokenizer (with tests)
β”‚  β”‚  β”‚  β”œβ”€ parser.rs         # Parser (with tests)
β”‚  β”‚  β”‚  β”œβ”€ ast.rs            # AST definitions (with tests)
β”‚  β”‚  β”‚  β”œβ”€ codegen.rs        # Interpreter (with tests)
β”‚  β”‚  β”‚  β”œβ”€ error.rs          # Error handling
β”‚  β”‚  β”‚  └─ debugger.rs       # Debugger
β”‚  β”‚  β”œβ”€ tests/               # Integration tests
β”‚  β”‚  β”‚  └─ integration_tests.rs  # 28 end-to-end tests
β”‚  β”‚  └─ Cargo.toml           # Rust dependencies
β”‚  └─ vscode-extension/        # VSCode extension
β”‚     β”œβ”€ extension.js         # Extension implementation
β”‚     β”œβ”€ package.json         # Extension manifest
β”‚     β”œβ”€ syntaxes/            # Syntax highlighting
β”‚     └─ icons/               # Extension icons
β”œβ”€ docs/                       # Comprehensive documentation
β”‚  β”œβ”€ api/                    # API reference
β”‚  β”œβ”€ examples/               # Example .ctx programs
β”‚  β”œβ”€ ADVANCED_FEATURES.md
β”‚  β”œβ”€ AI_ML_IMPLEMENTATION_GUIDE.md
β”‚  β”œβ”€ SYNTAX_REFERENCE.md
β”‚  └─ ... (20+ documentation files)
β”œβ”€ assets/                     # Logos and branding
β”‚  β”œβ”€ cortex-with-text.svg
β”‚  └─ cortex.svg
β”œβ”€ cortex.toml                 # Project configuration
β”œβ”€ README.MD                   # Main documentation
β”œβ”€ TEST_SUMMARY.md             # Test suite report
└─ run_cortex.sh              # Convenience script

Note: The project has been cleaned to include only functional, tested components. CLI and language-server packages can be added in the future when fully implemented.


πŸƒβ€β™‚οΈ Running Programs

Rust Implementation Commands

# Navigate to the rust directory
cd rust

# Run a program (interpreter mode)
cargo run -- run program.ctx

# Check syntax
cargo run -- check program.ctx

# Build executable
cargo run -- build program.ctx -o program

Running Examples from Project Root

# From the project root directory
cd /path/to/cortex

# Run any example
cd rust && cargo run -- run ../examples/hello_world.ctx

# Or use the provided shell script
./run_cortex.sh examples/hello_world.ctx

VS Code Integration

If you have the Cortex VS Code extension installed:

  1. Open any .ctx file in VS Code
  2. Press F5 or use "Run and Debug" to execute the file
  3. Use Ctrl+Shift+P and search for "Cortex" commands
  4. Format on save is automatically enabled for .ctx files

Try the Working Examples

Shorthand Commands (Recommended):

# From project root - use the shorthand scripts
./ctx hello_world
./ctx function_demo
./ctx arithmetic
./ctx simple_algorithms
./ctx fibonacci
./ctx factorial
./ctx prime_numbers
./ctx bubble_sort
./ctx comprehensive_features
./ctx ai_ml_comprehensive
./ctx simple_loops
./ctx loops
./ctx printing
./ctx simple_arithmetic

# Check syntax
./ctxc hello_world

# Run any .ctx file
./ctx run examples/hello_world.ctx
./ctxc check examples/hello_world.ctx

Full Commands:

# Navigate to the rust directory
cd rust

# Basic hello world
cargo run -- run ../examples/hello_world.ctx

# Functions and variables
cargo run -- run ../examples/function_demo.ctx

# Arithmetic operations
cargo run -- run ../examples/arithmetic.ctx

# Simple algorithms (prime, factorial, fibonacci, GCD)
cargo run -- run ../examples/simple_algorithms.ctx

# Fibonacci sequence
cargo run -- run ../examples/fibonacci.ctx

# Factorial calculation
cargo run -- run ../examples/factorial.ctx

# Prime number checking
cargo run -- run ../examples/prime_numbers.ctx

# Bubble sort demo
cargo run -- run ../examples/bubble_sort.ctx

# Comprehensive features showcase
cargo run -- run ../examples/comprehensive_features.ctx

# AI/ML algorithms demo
cargo run -- run ../examples/ai_ml_comprehensive.ctx

# Loop examples
cargo run -- run ../examples/simple_loops.ctx
cargo run -- run ../examples/loops.ctx

# Printing examples
cargo run -- run ../examples/printing.ctx

# Simple arithmetic
cargo run -- run ../examples/simple_arithmetic.ctx

# Check syntax of any example
cargo run -- check ../examples/hello_world.ctx

πŸ› οΈ Development Status

  • Core Language: Variables, functions, control flow
  • Rust Interpreter: High-performance execution with memory safety
  • VS Code Extension: Syntax highlighting and code formatting
  • Standard Library: Math, string operations
  • Tensor Primitives: Multi-dimensional arrays with shape inference
  • Neural Network Primitives: Modular neurons, layers, and networks
  • Automatic Differentiation: Computational graphs and gradient operators
  • GPU Support: CUDA/OpenCL backends for tensor operations
  • Layer Registry: Community-driven specialized layer sharing

πŸš€ Roadmap

Phase 1: Core Language βœ… COMPLETED

  • Lexer and parser
  • AST and interpreter
  • Basic syntax and operations
  • Function calls and control flow
  • Rust interpreter βœ… COMPLETED!
  • VS Code extension βœ… COMPLETED!

Phase 2: AI/ML Primitives (In Progress)

  • Tensor Primitives: Multi-dimensional arrays with shape inference
  • Neural Network Primitives: Modular neurons, layers, and networks
  • Automatic Differentiation: Computational graphs and gradient operators
  • Optimization Framework: SGD, Adam, RMSprop with scheduling
  • Layer Registry: Community-driven specialized layer sharing

Phase 3: Performance (In Progress)

  • Rust interpreter βœ… COMPLETED!
  • Memory safety βœ… COMPLETED!
  • Advanced memory optimization
  • Parallel execution
  • GPU acceleration

Phase 4: Ecosystem

  • Package manager
  • Standard ML library
  • Enhanced IDE support and debugging
  • Community and documentation

🀝 Contributing

We welcome contributions! Here's how to get started:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/new-feature
  3. Make your changes and add tests
  4. Run the test suite: python3 -m pytest tests/
  5. Submit a pull request

Development Setup

Rust Implementation:

git clone https://github.com/yourname/cortex
cd cortex/packages/compiler
cargo build

# Run examples
cargo run -- run ../../docs/examples/hello_world.ctx

# Check syntax
cargo run -- check ../../docs/examples/hello_world.ctx

Running Tests

The Cortex compiler has comprehensive test coverage including unit tests, integration tests, and doc tests.

# Run all tests
cd packages/compiler
cargo test

# Run tests with output
cargo test -- --nocapture

# Run specific test
cargo test test_simple_variable_assignment

# Run tests and show ignored tests
cargo test -- --include-ignored

# Generate and open documentation (includes doc tests)
cargo doc --no-deps --open

Test Organization:

  • Unit tests: Located in each source file within #[cfg(test)] modules
  • Integration tests: Located in packages/compiler/tests/ directory
  • Doc tests: Embedded in documentation comments throughout the codebase

πŸ“œ License

MIT License - see LICENSE for details.


πŸ”— Links


Ready to try Cortex? Start with our Getting Started Guide and explore the examples!

Rust implementation is ready! High-performance interpreter with VS Code support! πŸš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors