go-twig/PROGRESS.md
semihalev 1915c0e8a1 Fix short-circuit evaluation for logical operators
This fixes expressions like {% if foo is defined and foo > 5 %} when foo is undefined.

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-11 10:12:35 +03:00

7 KiB

Twig Implementation Progress

Completed Features

  1. Parser Improvements

    • Added parser support for function syntax with arguments
    • Added parser support for new operators: 'is', 'is not', 'matches', 'starts with', 'ends with', 'not in'
    • Added parser support for test conditions ('is defined', 'is empty', etc.)
    • Added conditional expressions (ternary operators)
    • Added array literal support
    • Added evaluator implementations for the new node types
  2. Full Filter Support

    • Fixed filter parsing and evaluation to properly handle the pipe (|) character in the tokenizer/parser
    • Ensured filter arguments are passed correctly to filter functions
    • Implemented support for chained filters (e.g., var|filter1|filter2)
    • Verified comprehensive set of standard Twig filters work correctly:
      • Text manipulation: upper, lower, capitalize, trim, etc.
      • Array manipulation: join, split, slice, sort, etc.
      • Type conversion: length/count, keys, etc.
      • Formatting: date, number_format, etc.
    • Added extensive tests for all filter functionality
  3. Documentation

    • Updated README with filter information and examples
    • Added comprehensive test suite for filters with various use cases

Recent Improvements

  1. Custom Filter and Function Registration

    • Added AddFilter() method to easily register custom filters
    • Added AddFunction() method to easily register custom functions
    • Added AddTest() method to register custom tests
    • Created CustomExtension type for comprehensive extensions
    • Added RegisterExtension() method for registering extension bundles
    • Created example application demonstrating custom extensions
  2. Development Mode and Template Caching

    • Added SetDevelopmentMode() method that sets appropriate debug/cache settings
    • Added SetDebug() and SetCache() methods for individual control
    • Improved template loading to respect cache settings
    • Templates are not cached when cache is disabled
    • Added tests to verify caching behavior
  3. Template Modification Checking

    • Added TimestampAwareLoader interface for loaders that support modification time checking
    • Implemented modification time tracking in the FileSystemLoader
    • Added cache invalidation based on file modification times
    • Automatic template reloading when files change and auto-reload is enabled
    • Updated Template struct to track the source loader and modification time
    • Added comprehensive tests for file modification detection
    • Added example application demonstrating auto-reload in action
  4. Optimized Filter Chain Processing

    • Implemented a specialized filter chain detection and evaluation algorithm
    • Added support for detecting and processing filter chains in a single pass
    • Reduced the number of intermediate allocations for chained filters
    • Improved performance for templates with multiple filters applied to the same value
    • Added tests and benchmarks to verify correctness and performance gains

Recent Improvements

  1. Whitespace Control Features
    • Added support for whitespace control modifiers ({{-, -}}, {%-, -%})
    • Implemented the {% spaceless %} tag to remove whitespace between HTML tags
    • Added direct tokenizer support for whitespace control tokens
    • Improved text handling to preserve spaces between words
    • Added tests for all whitespace control features

Recent Improvements

  1. Template Compilation
    • Implemented a compiled template format for faster rendering
    • Added pre-compilation capabilities for production use
    • Created a CompiledLoader for loading and saving compiled templates
    • Added support for auto-reload of compiled templates
    • Added benchmark tests comparing direct vs compiled template rendering
    • Created example application demonstrating template compilation workflow
    • Updated documentation with detailed information about the compilation feature

Recent Improvements

  1. Performance and Stability Enhancements
    • Fixed concurrency safety issues in template caching
    • Implemented attribute access caching to reduce reflection usage
    • Added memory pooling for render contexts and string buffers
    • Improved error handling for more resilient template loading
    • Laid groundwork for AST caching in compiled templates
    • Added detailed benchmarks showing performance improvements
    • Created comprehensive documentation of all improvements

Recent Improvements

  1. String Escape Sequence Handling
    • Fixed string literal parsing to properly handle escape sequences
    • Added support for escaping Twig syntax elements with \{ and \}
    • Improved HTML attribute handling with escaped braces
    • Fixed macro string parameter handling with special characters
    • Added comprehensive tests for string escape sequences
    • Updated documentation with escape sequence examples and use cases

Recent Improvements

  1. Operator Expression Handling

    • Fixed exponentiation operator (^) parsing and evaluation
    • Fixed string concatenation operator (~) for multiple concatenations
    • Implemented proper operator precedence system for expressions
    • Added support for modulo operator (%)
    • Fixed short-circuit evaluation for logical operators (and/or) to properly handle variable existence checks
    • Fixed critical issue with {% if foo is defined and foo > 5 %} patterns when foo is undefined
    • Enhanced parser to correctly handle complex expressions
    • Added comprehensive tests for all operators and precedence rules
    • Fixed tokenizer to properly identify operators in all contexts
    • Improved expression tree building to respect operator precedence
  2. Function Support in For Loops

    • Fixed direct usage of range function in for loops
    • Added tokenizer support for function calls in for loop sequences
    • Enhanced the renderer to directly handle function call nodes
    • Added special handling for range function to simplify template syntax
    • Improved the parser to correctly process function calls with parameters
    • Added support for negative step values via variables: {% set step = -1 %}{% for i in range(5, 1, step) %}
    • Added tests to verify range function works with steps and loop variables

Future Improvements

  1. More Tests

    • Add more comprehensive tests for edge cases
    • Add more benchmarks for different template scenarios
    • Create specific tests for complex nested expressions
  2. Error Handling

    • Improve error messages for filter-related issues
    • Add better debugging support
    • Enhance error reporting for macro parse errors
  3. Advanced Compilation

    • Implement full AST serialization with exportable node fields
    • Create a more efficient binary format for compiled templates
    • Skip parsing step completely for compiled templates
  4. Memory Optimization

    • Implement additional memory pools for other frequently allocated objects
    • Add size-limited template cache with LRU eviction policy