diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2020-04-10 10:37:51 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2020-04-10 10:37:51 -0700 |
commit | 48d917f2f18f91e418189b7fc41f7a9651e40b42 (patch) | |
tree | 4c9dcef7bf3fc63f86d3348972c9574cc5f5ae5b /parser.y | |
parent | 40211feffd1af2164674b1b4aa01f38197f75336 (diff) | |
download | txr-48d917f2f18f91e418189b7fc41f7a9651e40b42.tar.gz txr-48d917f2f18f91e418189b7fc41f7a9651e40b42.tar.bz2 txr-48d917f2f18f91e418189b7fc41f7a9651e40b42.zip |
parser: eliminate struct list_accum.
As a final round of this recent work, we observe that
since the accumulator structure has been reduced to two
members, we can eliminate one of them by using a cons
cell as the accumulator, and threading the second value
through the cdr.
That is to say, the listacc grammar rule's semantic
value will now be the tail cons of the list being
constructed. The cdr of this cons will, temporarily,
be a back pointer to the head (making the list temporarily
circular).
The n_exprs reduction will fix this up; it will put the
correct terminating atom in place of the head (either nil,
or the dotted item if there is one), and yield the head as the
semantic value.
* lib.h (struct list_accum): Removed. (Thus, finding a better
home for this would, after all, have been a waste of time).
* parser.y (lacc, splacc): Static functions removed.
(union YYSTYPE): lacc membber removed.
(n_exprs): Adjust to new semantic value coming from listacc.
(listacc): Now of type val again. Yields pointer to tail cons
as semantic value, whose cdr points to the head of the
list.
Diffstat (limited to 'parser.y')
-rw-r--r-- | parser.y | 64 |
1 files changed, 22 insertions, 42 deletions
@@ -71,8 +71,6 @@ static val uref_helper(parser_t *, val expr); static val uoref_helper(parser_t *, val expr); static val qref_helper(parser_t *, val lexpr, val rexpr); static val fname_helper(parser_t *, val name); -static struct list_accum lacc(val obj); -static struct list_accum splacc(val list); #if YYBISON union YYSTYPE; @@ -114,7 +112,6 @@ INLINE val expand_form_ver(val form, int ver) union obj *val; wchar_t chr; cnum lineno; - struct list_accum lacc; } %token <lexeme> SPACE TEXT SYMTOK @@ -146,8 +143,7 @@ INLINE val expand_form_ver(val form, int ver) %type <val> line elems_opt elems clause_parts_h additional_parts_h %type <val> text texts elem var var_op modifiers %type <val> vector hash struct range tnode tree -%type <val> exprs exprs_opt n_exprs i_expr i_dot_expr -%type <lacc> listacc +%type <val> exprs exprs_opt n_exprs listacc i_expr i_dot_expr %type <val> n_expr n_exprs_opt n_dot_expr %type <val> list dwim meta compound %type <val> out_clauses out_clauses_opt out_clause @@ -944,37 +940,38 @@ exprs_opt : exprs { $$ = $1; } | /* empty */ { $$ = nil; } ; -n_exprs : listacc { $$ = $1.head; } +n_exprs : listacc { $$ = $1->c.cdr; + $1->c.cdr = nil; } | listacc CONSDOT n_expr - { rplacd($1.tail, $3); - $$ = $1.head; } + { $$ = $1->c.cdr; + $1->c.cdr = $3; } ; -listacc : n_expr { $$ = lacc($1); - rlc($$.head, $1); } +listacc : n_expr { $$ = cons($1, nil); + rlc($$, $1); + $$->c.cdr = $$; } | HASH_SEMI { parser->ignore = 1; } n_expr { parser->ignore = 0; - $$ = lacc(nil); } + $$ = cons(nil, nil); + $$->c.cdr = $$; } | listacc HASH_SEMI { parser->ignore = 1; } n_expr { parser->ignore = 0; $$ = $1; } | listacc n_expr { uses_or2; - val cell = rlc(cons($2, nil), or2($2, $1.head)); - rplacd($1.tail, cell); - $1.tail = cell; - $$ = $1; } - | WSPLICE wordslit { $$ = splacc(rl($2, num($1))); } + $$ = rlc(cons($2, $1->c.cdr), or2($2, $1->c.cdr)); + $1->c.cdr = $$; } + | WSPLICE wordslit { $$ = lastcons(rl($2, num($1))); + $$->c.cdr = $2; } | listacc WSPLICE - wordslit { val list = rl($3, num($2)); - rplacd($1.tail, list); - $1.tail = lastcons(list); - $$ = $1; } - | QWSPLICE wordsqlit { $$ = splacc(rl($2, num($1))); } + wordslit { $$ = lastcons(rl($3, num($2))); + $$->c.cdr = $1->c.cdr; + $1->c.cdr = $3; } + | QWSPLICE wordsqlit { $$ = lastcons(rl($2, num($1))); + $$->c.cdr = $2; } | listacc QWSPLICE - wordsqlit { val list = rl($3, num($2)); - rplacd($1.tail, list); - $1.tail = lastcons(list); - $$ = $1; } + wordsqlit { $$ = lastcons(rl($3, num($2))); + $$->c.cdr = $1->c.cdr; + $1->c.cdr = $3; } ; i_expr : SYMTOK { $$ = ifnign(symhlpr($1, t)); } @@ -1818,23 +1815,6 @@ static val fname_helper(parser_t *parser, val name) return nil; } -static struct list_accum lacc(val obj) -{ - struct list_accum la; - val cell = cons(obj, nil); - la.head = cell; - la.tail = cell; - return la; -} - -static struct list_accum splacc(val list) -{ - struct list_accum la; - la.head = list; - la.tail = lastcons(list); - return la; -} - #ifndef YYEOF #define YYEOF 0 #endif |