Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ Cj Public

Jit Compilable language with GC and VM

License

Flickyyy/Cj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CJ Language

A modern, JIT-compilable programming language with automatic memory management (garbage collection) and a virtual machine runtime.

Build Status License: MIT

Overview

CJ is a modern programming language with a hybrid type system that supports both static typing with explicit type annotations and dynamic typing with type inference:

  • Virtual Machine: Custom bytecode VM with stack-based execution
  • Garbage Collection: Automatic memory management with configurable GC algorithms
  • JIT Compilation: Just-in-time compilation for performance optimization
  • Modern Syntax: Clean, expressive syntax similar to JavaScript/TypeScript
  • Built-in Types: Numbers, strings, booleans, arrays, objects, functions
  • Error Handling: Comprehensive error reporting and recovery

Features

Language Features

  • Hybrid Type System: Static typing with type annotations and dynamic typing with inference
  • Variables (var, const) with optional type annotations
  • Functions with closures
  • Control flow (if/else, while, for)
  • Object-oriented programming (class, inheritance)
  • Exception handling (try/catch/finally)
  • Module system (import/export)

Runtime Features

  • Virtual Machine: Stack-based bytecode interpreter
  • Garbage Collector: Mark-and-sweep, copying, and generational GC
  • JIT Compiler: x86-64 native code generation with optimizations
  • Memory Management: Automatic allocation and deallocation
  • Built-in Functions: Type checking, I/O, string manipulation

Development Tools

  • Compiler: Source-to-bytecode compilation
  • REPL: Interactive read-eval-print loop
  • Debugger: Breakpoints, stack traces, variable inspection
  • Profiler: Performance analysis and optimization hints

Quick Start

Prerequisites

  • C++20 compatible compiler (GCC 10+, Clang 12+, MSVC 2019+)
  • CMake 3.20+
  • Git

Building

git clone https://github.com/Flickyyy/Cj.git
cd Cj
mkdir build && cd build
cmake ..
make -j$(nproc)

Running Examples

# Run the REPL
./bin/cj-repl

# Execute a CJ file
./bin/cj-vm ../examples/hello.cj

# Compile a CJ file
./bin/cj-compiler ../examples/fibonacci.cj -o fibonacci.cjb

# Run the complete language demo
./bin/examples/complete_demo

# Run the SSA (Static Single Assignment) demo
./bin/examples/ssa_demo

# View CJ standard library examples (future syntax)
cat ../stdlib/README.md
cat ../examples/stdlib_demo.cj

Language Syntax

Variables and Types

// Dynamic typing with type inference
var name = "CJ Language";
var version = 0.1;
var is_ready = true;

// Static typing with explicit type annotations
var age: int = 25;
var price: float = 19.99;
var message: string = "Hello";
var active: bool = true;

// Constants
const PI = 3.14159;
const MAX_SIZE = 1000;

Functions

// Function declaration
function greet(name) {
    return "Hello, " + name + "!";
}

// Function expression
var add = function(a, b) {
    return a + b;
};

// Arrow function (planned)
var multiply = (a, b) => a * b;

Control Flow

// Conditional statements
if (age >= 18) {
    print("Adult");
} else {
    print("Minor");
}

// Loops
for (var i = 0; i < 10; i++) {
    print(i);
}

while (condition) {
    // loop body
}

Objects and Arrays

// Arrays
var numbers = [1, 2, 3, 4, 5];
var mixed = [1, "hello", true, nil];

// Objects
var person = {
    name: "Alice",
    age: 30,
    greet: function() {
        return "Hi, I'm " + this.name;
    }
};

Classes (Planned)

class Animal {
    constructor(name) {
        this.name = name;
    }
    
    speak() {
        print(this.name + " makes a sound");
    }
}

class Dog extends Animal {
    speak() {
        print(this.name + " barks");
    }
}

Architecture

Project Structure

Cj/
├── include/cj/          # Header files
│   ├── common.h         # Common definitions
│   ├── lexer/           # Lexical analysis
│   ├── parser/          # Syntax analysis
│   ├── ast/             # Abstract syntax tree
│   ├── ir/              # Intermediate representation
│   ├── vm/              # Virtual machine
│   ├── gc/              # Garbage collector
│   ├── jit/             # JIT compiler
│   └── types/           # Type system
├── src/                 # Source files
│   ├── lexer/           # Lexer implementation
│   ├── parser/          # Parser implementation  
│   ├── ast/             # AST implementation
│   ├── ir/              # IR implementation
│   ├── vm/              # VM implementation
│   ├── gc/              # GC implementation
│   ├── jit/             # JIT implementation
│   └── utils/           # Utilities
├── tests/               # Test files
├── examples/            # Example programs
├── docs/                # Documentation
└── tools/               # Development tools

Components

Lexer

  • Tokenizes source code into lexical tokens
  • Handles keywords, operators, literals, identifiers
  • Supports comments and whitespace handling

Parser

  • Recursive descent parser
  • Generates Abstract Syntax Tree (AST)
  • Error recovery and reporting

AST

  • Tree representation of parsed code
  • Visitor pattern for traversal
  • Pretty printing and analysis

IR (Intermediate Representation)

  • Stack-based bytecode instructions
  • Control flow graph representation
  • Optimization passes

Virtual Machine

  • Stack-based execution engine
  • Built-in function support
  • Error handling and debugging

Garbage Collector

  • Automatic memory management
  • Multiple GC algorithms (mark-and-sweep, copying, generational)
  • Concurrent and incremental collection

JIT Compiler

  • Native code generation
  • Optimization passes (constant folding, dead code elimination)
  • Platform-specific backends (x86-64, ARM64)

Development

Building with Debug Information

cmake -DCMAKE_BUILD_TYPE=Debug ..
make -j$(nproc)

Running Tests

# Build and run all tests
make test

# Run specific test
./bin/cj-tests

# Run manual tests
./bin/tests/manual_test_lexer
./bin/tests/manual_test_parser

Code Style

The project follows the Google C++ Style Guide. Use clang-format for consistent formatting:

# Format all source files
find . -name "*.cpp" -o -name "*.h" -o -name "*.hpp" | xargs clang-format -i

Documentation

Comprehensive documentation is available in the docs/ directory:

Key Concepts Explained

The documentation covers essential compiler concepts including:

  • Compilation Pipeline: Lexer → Parser → AST → IR → Optimization → Code Generation
  • Static Single Assignment (SSA): Modern intermediate representation for advanced optimizations
  • Dominance Analysis: Control flow analysis for phi function placement
  • JIT Compilation: Runtime code generation and optimization
  • Garbage Collection: Automatic memory management
  • Type System: Static typing with inference and compatibility rules

API Documentation

Generate API documentation with Doxygen:

# In build directory
make docs
# Open docs/html/index.html

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Coding Guidelines

  • Follow Google C++ Style Guide
  • Write comprehensive tests for new features
  • Document public APIs with Doxygen comments
  • Use meaningful variable and function names
  • Keep functions small and focused

Roadmap

Version 0.1.0 (Current)

  • Basic lexer and parser
  • AST generation and traversal
  • IR generation
  • Virtual machine with basic instructions
  • Garbage collector (mark-and-sweep)
  • JIT compiler framework
  • SSA (Static Single Assignment) form
  • SSA-based optimizations (constant propagation, dead code elimination)
  • REPL and command-line tools

Version 0.2.0 (Planned)

  • Complete language feature implementation
  • Module system
  • Standard library
  • Improved error messages
  • Performance optimizations

Version 0.3.0 (Future)

  • Object-oriented programming
  • Advanced GC algorithms
  • Debugging tools
  • Package manager
  • Language server protocol

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Inspired by modern language design principles
  • Built with modern C++ best practices
  • Uses Google Test for unit testing
  • Documentation generated with Doxygen

Contact


CJ Language - A modern language for the future

About

Jit Compilable language with GC and VM

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •