eecee

Crates.ioeecee
lib.rseecee
version0.1.0
created_at2025-10-15 17:15:51.949948+00
updated_at2025-10-15 17:15:51.949948+00
descriptionSimple AST representation and formatter for the C programming language
homepage
repository
max_upload_size
id1884710
size227,322
Steve Fan (stevefan1999-personal)

documentation

README

eecee

A comprehensive Rust library for representing and pretty-printing C programming language Abstract Syntax Trees (AST). The library provides a complete type-safe representation of C syntax following the C11 standards.

Features

  • Complete C AST Representation - Full coverage of C language constructs including declarations, expressions, statements, types, and more
  • Pretty Printing - Built-in indented formatting for human-readable output
  • no_std Compatible - Works in embedded and bare-metal environments
  • Memory Efficient - Uses interning to reduce memory overhead for repeated identifiers and literals
  • Type Safe - Leverages Rust's type system to prevent invalid AST construction
  • Well Tested - Comprehensive test suite covering all AST components

Installation

Add this to your Cargo.toml:

[dependencies]
eecee = "0.1.0"

Quick Start

use eecee::ast::*;
use internment::Intern;

// Create a simple function: int add(int a, int b) { return a + b; }
let function = FunctionDefinition::builder()
    .specifiers(SpecifierList::builder()
        .specifiers(vec![Specifier::Type(TypeSpecifier::Int)])
        .build())
    .declarator(Declarator::builder()
        .pointer(None)
        .direct(DirectDeclarator::Function {
            declarator: Intern::new(DirectDeclarator::Identifier(
                Intern::new("add".to_string())
            )),
            params: ParameterTypeList::builder()
                .parameters(vec![
                    ParameterDeclaration::builder()
                        .specifiers(SpecifierList::builder()
                            .specifiers(vec![Specifier::Type(TypeSpecifier::Int)])
                            .build())
                        .declarator(Some(ParameterDeclarator::Declarator(
                            Declarator::builder()
                                .pointer(None)
                                .direct(DirectDeclarator::Identifier(
                                    Intern::new("a".to_string())
                                ))
                                .build()
                        )))
                        .build(),
                    ParameterDeclaration::builder()
                        .specifiers(SpecifierList::builder()
                            .specifiers(vec![Specifier::Type(TypeSpecifier::Int)])
                            .build())
                        .declarator(Some(ParameterDeclarator::Declarator(
                            Declarator::builder()
                                .pointer(None)
                                .direct(DirectDeclarator::Identifier(
                                    Intern::new("b".to_string())
                                ))
                                .build()
                        )))
                        .build(),
                ])
                .variadic(false)
                .build(),
        })
        .build())
    .declarations(vec![])
    .body(CompoundStatement::builder()
        .declarations(vec![])
        .statements(vec![
            Statement::Return {
                value: Some(Expression::Binary {
                    left: Intern::new(Expression::Identifier(
                        Intern::new("a".to_string())
                    )),
                    op: BinaryOperator::Add,
                    right: Intern::new(Expression::Identifier(
                        Intern::new("b".to_string())
                    )),
                }),
            }
        ])
        .build())
    .build();

// Print the function
println!("{}", function);

API Overview

Core Types

AST Nodes

Type System

  • TypeSpecifier - Basic and complex types (int, struct, union, enum, etc.)
  • TypeQualifier - Type qualifiers (const, volatile, restrict, _Atomic)
  • StorageClassSpecifier - Storage classes (static, extern, auto, register, typedef)
  • Declarator - Variable/function declarators with pointers and dimensions
  • TypeName - Abstract type names for casts and sizeof

Declarations

Statements

  • CompoundStatement - Block with declarations and statements
  • Control flow: If, While, For, DoWhile, Switch
  • Jump statements: Return, Break, Continue, Goto
  • Labeled statements: Case, Default, Label

Expressions

Pretty Printing

The library includes a sophisticated indentation system for formatting output:

use eecee::indent_printer::{IndentContext, IndentDisplay};

let context = IndentContext::new("    "); // 4-space indentation
println!("{}", context.display(&translation_unit));

Language Support

The library aims to support C11 standards with the following features:

  • ✅ Complete type system (basic types, pointers, arrays, functions)
  • ✅ Struct, union, and enum declarations
  • ✅ All operators (arithmetic, logical, bitwise, assignment)
  • ✅ Control flow statements (if, while, for, do-while, switch)
  • ✅ Jump statements (goto, break, continue, return)
  • ✅ Compound literals and designated initializers
  • ✅ Type qualifiers (const, volatile, restrict, _Atomic)
  • ✅ Storage class specifiers (static, extern, typedef, etc.)
  • ✅ Function specifiers (inline, _Noreturn)
  • ✅ Alignment specifiers (_Alignas, _Alignof)
  • ✅ Generic selections (_Generic)
  • ✅ Static assertions (_Static_assert)

no_std Support

This library is no_std compatible by default. The library uses alloc for dynamic allocations (Vec, String).

Contributing

Contributions are welcome! Areas for contribution:

  • Additional test cases for edge cases
  • Performance optimizations
  • Documentation improvements
  • Bug fixes

References

Commit count: 0

cargo fmt