Skip to content

Z v0.5: Function Forward

Choose a tag to compare

@Prime-Lasking Prime-Lasking released this 01 Oct 04:37
· 84 commits to main since this release
3d14bff

Z Compiler Release Notes

Version 0.5 - Function Support Update (2025-10-01)

🎉 Major Features

This release introduces full function support to the Z programming language, transforming it from a simple assembly-like language into a more powerful, structured programming language while maintaining its minimalist philosophy.

✨ New Features

1. Function Definitions

Functions are now first-class citizens in Z, compiling directly to C functions with proper type signatures.

Syntax:

FN function_name(param1, param2, ...):
    // Function body
    instruction1;
    instruction2;
    RET return_value;
END

Example:

FN add(a, b):
    ADD a, b, result;
    RET result;
END

Compiles to:

double add(double a, double b) {
    result = a + b;
    return result;
}

Key Points:

  • Functions compile to real C functions (not macros or inline code)
  • All parameters are typed as double
  • Function prototypes are automatically generated
  • Functions can be called from anywhere in the program

2. Return Statement (RET)

New RET instruction for returning values from functions.

Syntax:

RET value;    // Return a value
RET;          // Return without a value (void return)

Features:

  • Returns are properly translated to C return statements
  • Supports both value and void returns
  • Must end with semicolon inside functions

3. Function Calls with Return Values

Enhanced CALL instruction now supports capturing return values.

Syntax:

CALL function_name(arg1, arg2, ...), result_var

Examples:

CALL add(x, y), sum              // sum = add(x, y)
CALL calculate(a, b, c), result  // result = calculate(a, b, c)
CALL print_hello()               // void call (no return value)

Features:

  • Return values are automatically assigned to the specified variable
  • Supports both space and comma-separated arguments
  • Works with any number of parameters

4. Flexible Operand Formatting

The parser now accepts multiple operand formatting styles for improved readability.

Supported Formats:

// Comma-separated (recommended for clarity)
MOV x, 10
ADD a, b, result

// Space-separated (traditional assembly style)
MOV x 10
ADD a b result

// Function calls with commas
CALL add(x, y), sum

Benefits:

  • Choose the style that best fits your code
  • Commas improve readability in complex expressions
  • Backward compatible with existing code

5. Semicolon Requirements

Structured semicolon rules for better code organization.

Rules:

  • Inside functions: Semicolons are required at the end of each statement
  • Outside functions: Semicolons are optional

Example:

FN multiply(a, b):
    MUL a, b, result;   // Semicolon required
    RET result;         // Semicolon required
END

MOV x, 10              // Semicolon optional
MOV y, 20              // Semicolon optional
CALL multiply(x, y), product

Benefits:

  • Clear distinction between function and main code
  • Enforces better structure inside functions
  • Maintains flexibility in main program code

6. Inline Comments

Updated comment syntax with inline comment support.

Syntax:

// This is a comment
MOV x, 10            // Initialize x to 10
ADD a, b, result;    // Add a and b (inside function)

Features:

  • Comments start with // (C-style)
  • Can appear on their own line or after code
  • Properly handled after semicolons

🔧 Technical Improvements

Parser Enhancements

  • Improved tokenization: Better handling of commas, semicolons, and parentheses
  • Function-aware parsing: Tracks function context for proper semicolon enforcement
  • Inline comment stripping: Comments are removed before parsing to avoid conflicts
  • Better error messages: More descriptive error messages with line numbers

Code Generation

  • Function prototypes: Automatically generated for all functions
  • Proper scoping: Functions are generated before main code
  • Clean C output: Well-formatted, readable C code
  • Optimized structure: Separates function definitions from main program logic

Bug Fixes

  • Fixed double-token bug when parsing semicolons in functions
  • Fixed operand order for 3-operand ADD instruction (now: ADD src1, src2, dest)
  • Fixed function call parsing with space-separated arguments
  • Fixed main function brace generation (removed double braces)
  • Corrected function parameter handling in code generation

📝 Updated Documentation

  • README.md: Comprehensive documentation of all new features
  • Syntax section: Added operand formatting and semicolon rules
  • Functions section: Complete guide with examples
  • Example programs: Added function-based examples
  • Best practices: Updated with function usage guidelines

🔄 Breaking Changes

⚠️ Comment Syntax Change

  • Old: Comments started with ;
  • New: Comments start with //
  • Migration: Replace all ; comment markers with //

⚠️ Three-Operand ADD Instruction

  • Old behavior: ADD dest, src1, src2 → dest = src1 + src2
  • New behavior: ADD src1, src2, dest → dest = src1 + src2
  • Migration: Reorder operands in 3-operand ADD instructions

### 🚀 Performance

- **Compilation speed**: No significant impact on compilation times
- **Generated code**: Functions compile to efficient C code
- **Runtime performance**: Native C function calls (no overhead)

### 🐛 Known Limitations

- Function parameters share the global namespace
- No nested function definitions
- No function overloading
- No local variables (all variables are global)
- No recursion optimization (relies on C compiler)

### 📦 Compatibility

- **Python**: Requires Python 3.8 or higher
- **GCC**: Requires GCC for executable compilation
- **Backward compatibility**: Most v1.x programs will work with minor syntax updates

### 🙏 Acknowledgments

This release represents a significant evolution of the Z language, bringing it closer to a full-featured programming language while maintaining its simplistic focus.

### 📅 Release Information

- **Version**: 0.5
- **Release Date**: October 1, 2025
- **Codename**: "Function Forward"
- **Type**: Major Release

---