From 4b73bf960895a139330bf4c0d0317e5d2f48e06b Mon Sep 17 00:00:00 2001 From: Ákos Kőrösi Date: Thu, 21 May 2026 20:15:58 +0200 Subject: fix var expansion in double quotes --- src/lexparse.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'src/lexparse.c') diff --git a/src/lexparse.c b/src/lexparse.c index de14c0a..14566f5 100644 --- a/src/lexparse.c +++ b/src/lexparse.c @@ -1,5 +1,6 @@ #include #include +#include #include "syntax.h" #include "../src/utils.h" @@ -47,6 +48,7 @@ static MaybeChunk handle_dollar_expansion(char* line, size_t* pos, QuotationMode } else if (line[*pos] == '_' || isalnum(line[*pos])){ String buf = make_String(256); while (line[*pos] == '_' || isalnum(line[*pos])) { + printf("%c\n", line[*pos]); push_to_String(&buf, line[(*pos)++]); } return (MaybeChunk){ @@ -95,22 +97,34 @@ static Chunk collect_single_q_section(char* line, size_t* pos) { }; } -static Chunk collect_double_q_section(char* line, size_t* pos) { +static void collect_double_q_section(char* line, Word* result_buf, size_t* pos) { // TODO: add the extra double-q expansions (*pos)++; - String buf = make_String(256); - size_t i; - for (i = *pos; line[i] != '\"' && line[i] != '\0'; i++) { - push_to_String(&buf, line[i]); + printf("%c\n", line[*pos]); + String lit_buf = make_String(256); + + while (line[*pos] != '\"' && line[*pos] != '\0') { + // TODO: handle escapes + if (line[*pos] == '$') { + MaybeChunk m_dollar = handle_dollar_expansion(line, pos, DOUBLE_Q); + if (m_dollar.some) { + if (lit_buf.count>0) { + push_to_Word(result_buf, (Chunk){.qmode = DOUBLE_Q, .kind = CHUNK_LIT, .as.lit_str = inner(&lit_buf)}); + lit_buf = make_String(256); + } + push_to_Word(result_buf, m_dollar.value); + } else { + push_to_String(&lit_buf, '$'); + } + } else { + push_to_String(&lit_buf, line[*pos]); + } } - if (line[i] == '\0') exit(44); + if (line[(*pos)++] == '\0') exit(46); - *pos = ++i; - return (Chunk){ - .qmode = DOUBLE_Q, - .kind = CHUNK_LIT, - .as.lit_str = inner(&buf) - }; + if (lit_buf.count > 0) { + push_to_Word(result_buf, (Chunk){.qmode = DOUBLE_Q, .kind = CHUNK_LIT, .as.lit_str = inner(&lit_buf)}); + } } DECLARE_MAYBE(SpecialToken, MaybeSpecialToken) @@ -199,8 +213,7 @@ TokenVec* lex_level(char* line, size_t* pos, char until, TokenVecVec* tok_seq_po Chunk chunk = collect_single_q_section(line, pos); push_to_Word(&buf.word_germ, chunk); } else if (line[*pos] == '\"') { - Chunk chunk = collect_double_q_section(line, pos); - push_to_Word(&buf.word_germ, chunk); + collect_double_q_section(line, &buf.word_germ,pos); } else if (line[*pos] == '$') { MaybeChunk dollar_chunk = handle_dollar_expansion(line, pos, UN_Q); if (dollar_chunk.some) { -- cgit v1.2.3