lrama icon indicating copy to clipboard operation
lrama copied to clipboard

Enhance grammar handling with linked list support and memory management functions

Open ydah opened this issue 1 month ago • 0 comments

Overview

This PR adds a complete generic linked list implementation to stdlib.y, enabling users to immediately use list(), nonempty_list(), and separated_list() parameterized rules without writing any boilerplate code.

What Changed

  1. Added linked list implementation to stdlib.y

New data structure and functions available via %code requires:

  • lrama_list_node_t - Generic singly-linked list node
  • lrama_list_new(void*) - Create new list
  • lrama_list_append(list, void*) - Append element
  • lrama_list_free(list) - Free list structure
  • lrama_list_length(list) - Count elements
  • lrama_list_get(list, index) - Get nth element
  1. Added semantic actions to list rules

Before:

%rule nonempty_list(X)
    : X
    | nonempty_list(X) X
    ;

After:

%rule nonempty_list(X)
    : X { $$ = lrama_list_new((void*)$1); }
    | nonempty_list(X) X { $$ = lrama_list_append($1, (void*)$2); }
    ;
  1. Simplified separated_list() implementation

Before: separated_list(sep, X) → option(separated_nonempty_list(sep, X))

  • Generated 3 non-terminals: option_..., separated_nonempty_list_..., separated_list_...

After: separated_list(sep, X) directly expands to 3 rules

  • Generated 1 non-terminal: separated_list_...
  • Fewer symbols, simpler grammar
  1. Enhanced stdlib merging in lib/lrama/command.rb
  • Merges prologue sections from stdlib.y into user grammar
  • Prepends %code directives from stdlib.y before user code
  • Ensures helper functions are available when list rules are used

ydah avatar Nov 23 '25 10:11 ydah