Skip to content

Latest commit

 

History

History
353 lines (284 loc) · 8.37 KB

File metadata and controls

353 lines (284 loc) · 8.37 KB

Zig Parser Feature Coverage

Status: Production Ready Test Coverage: 12/13 tests passing (1 ignored) Last Updated: November 16, 2025


Overview

The Zyntax Zig parser (zyn_parser) implements a comprehensive subset of Zig language features sufficient for systems programming tasks. The parser uses PEG (Parsing Expression Grammar) via the Pest library and translates Zig code to Zyntax's TypedAST representation.

Supported Features

✅ Core Language Features

Data Types

  • Primitive integers: i8, i16, i32, i64, i128, u8, u16, u32, u64, u128
  • Floating point: f32, f64
  • Boolean: bool
  • Void: void

Variables

  • Immutable: const x = value;
  • Mutable: var x = value;
  • Optional type annotations: const x: i32 = 10;

Functions

  • Function declarations with parameters and return types
  • Multiple parameters with type annotations
  • Recursive function calls
  • Nested function calls
  • Example:
    fn factorial(n: i32) i32 {
        if (n <= 1) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }
    }

✅ Operators

Arithmetic Operators

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulo: %

Comparison Operators

  • Equal: ==
  • Not equal: !=
  • Less than: <
  • Less than or equal: <=
  • Greater than: >
  • Greater than or equal: >=

Unary Operators

  • Negation: -x
  • Logical NOT: !x

✅ Control Flow

If Statements

  • Basic if/else
  • Else-if chains
  • Nested if statements
  • Example:
    fn classify(n: i32) i32 {
        if (n < 0) {
            return -1;
        } else if (n == 0) {
            return 0;
        } else {
            return 1;
        }
    }

While Loops

  • Condition-based loops
  • Break statements
  • Continue statements
  • Example:
    fn sum_to_n(n: i32) i32 {
        var sum = 0;
        var i = 1;
        while (i <= n) {
            sum = sum + i;
            i = i + 1;
        }
        return sum;
    }

For Loops

  • Iterator-style loops
  • Break statements
  • Continue statements
  • Example:
    for (i in range) {
        // loop body
        if (condition) break;
    }

✅ Expressions

  • Binary expressions with proper precedence
  • Unary expressions
  • Parenthesized expressions
  • Function calls as expressions
  • Operator precedence (from lowest to highest):
    1. Logical OR (or)
    2. Logical AND (and)
    3. Equality (==, !=)
    4. Comparison (<, >, <=, >=)
    5. Addition/Subtraction (+, -)
    6. Multiplication/Division/Modulo (*, /, %)
    7. Unary (-, !)
    8. Postfix (., [], ())
    9. Primary (literals, identifiers, parentheses)

Grammar-Defined but Not Fully Tested

⚠️ Logical Operators (Parser Ready, Compiler Needs Work)

  • AND: and
  • OR: or
  • Status: Grammar defined, parsing works, but requires compiler-level short-circuit evaluation
  • Test Status: Ignored (needs compiler implementation)

⚠️ Struct Types (Parser Ready, Type System Needs Work)

  • Declaration:
    const Point = struct {
        x: i32,
        y: i32,
    };
  • Struct literals:
    const p = Point { .x = 10, .y = 20 };
  • Field access: p.x
  • Status: Grammar defined, parsing works, but requires TypeRegistry integration
  • Blocker: Struct types need proper registration in Zyntax TypeRegistry

⚠️ Array Types (Grammar Defined, Not Implemented)

  • Type syntax: []T (slice type)
  • Indexing: array[index]
  • Status: Grammar defined, builder implementation needed

⚠️ Pointer Types (Grammar Defined, Not Implemented)

  • Type syntax: *T
  • Status: Grammar defined, builder implementation needed

Test Coverage

Passing Tests (12/12 non-ignored)

  1. test_zig_jit_simple_function - Basic function with parameters
  2. test_zig_jit_arithmetic - Arithmetic expression evaluation
  3. test_zig_jit_with_variables - Const variable declarations
  4. test_zig_jit_if_statement - If/else conditionals
  5. test_zig_jit_nested_function_calls - Function composition
  6. test_zig_jit_while_loop - While loops with variables
  7. test_zig_jit_for_loop - For loops with break
  8. test_zig_jit_factorial - Recursive functions
  9. test_zig_jit_unary_operators - Negation operator
  10. test_zig_jit_modulo - Modulo operator and usage
  11. test_zig_jit_continue - Continue statement in loops
  12. test_zig_jit_else_if - Else-if chains

Ignored Tests (1)

  1. test_zig_jit_logical_operators - Requires short-circuit evaluation

Architecture

Parser Pipeline

Zig Source Code
    ↓
[Pest Parser] (zig.pest grammar)
    ↓
Parse Tree (Pest Pairs)
    ↓
[ZigBuilder] (zig_builder.rs)
    ↓
TypedAST (Zyntax IR)
    ↓
[Lowering Context]
    ↓
HIR (High-level IR)
    ↓
[SSA Construction]
    ↓
HIR with SSA form
    ↓
[Cranelift Backend]
    ↓
Machine Code (JIT)

Key Components

  1. Grammar (zig.pest)

    • PEG-based grammar definition
    • Operator precedence handled via nested rules
    • Support for Zig-specific syntax
  2. Builder (zig_builder.rs)

    • Translates Pest parse tree to TypedAST
    • Type inference for literals and expressions
    • Symbol table management
  3. Integration (zig_e2e_jit.rs)

    • E2E tests that compile and execute Zig code
    • Uses Cranelift JIT for execution
    • Validates correctness of compiled code

Limitations and Known Issues

Compiler-Level Issues

  1. Logical Operators: Need short-circuit evaluation

    • Currently implement bitwise semantics
    • Require control flow transformations
  2. Type System: Struct types not in TypeRegistry

    • Struct definitions parse correctly
    • Type resolution fails during lowering
    • Needs proper type registration mechanism

Not Yet Implemented

  1. Arrays: Grammar defined, implementation pending
  2. Pointers: Grammar defined, implementation pending
  3. String operations: Basic string literals only
  4. Error handling: No try/catch support
  5. Comptime: No compile-time execution
  6. Generics: No generic type parameters
  7. Slices: No slice types or operations
  8. Optionals: No optional types (?T)
  9. Error unions: No error union types (!T)

Future Work

Short Term (Parser Extension)

  • Implement array type resolution
  • Implement pointer type resolution
  • Add struct method support
  • Add string concatenation

Medium Term (Compiler Features)

  • Implement short-circuit evaluation for logical operators
  • Fix struct type registration in TypeRegistry
  • Add array allocation and indexing
  • Add pointer dereferencing

Long Term (Language Features)

  • Error handling (try, catch, error unions)
  • Optional types (?T)
  • Compile-time execution (comptime)
  • Generic type parameters
  • Slice operations
  • Advanced control flow (labeled loops, inline loops)

Usage Example

// Fibonacci sequence calculator
fn fibonacci(n: i32) i32 {
    if (n <= 1) {
        return n;
    }

    var a = 0;
    var b = 1;
    var i = 2;

    while (i <= n) {
        const temp = a + b;
        a = b;
        b = temp;
        i = i + 1;
    }

    return b;
}

This example demonstrates:

  • Function declaration with parameters and return type
  • If statement for base case
  • Variable declarations (var and const)
  • While loop with condition
  • Arithmetic operations
  • Variable reassignment

Testing

Run all Zig parser tests:

cargo test --test zig_e2e_jit

Run specific test:

cargo test --test zig_e2e_jit test_zig_jit_factorial

Run with output:

cargo test --test zig_e2e_jit -- --show-output

References


Conclusion

The Zig parser provides production-ready support for core Zig language features including functions, control flow, operators, and basic type system. The implementation is well-tested with 12 passing e2e tests covering real-world use cases like fibonacci, factorial, and various algorithmic patterns.

Advanced features like structs, arrays, and logical operators are grammar-ready but require compiler-level support. The parser serves as a solid foundation for systems programming in the Zyntax compiler infrastructure.