allow expressions to be terminated with unescaped newlines or ';'

This commit is contained in:
icst 2024-06-17 20:32:58 -04:00
parent 6cf9296ac6
commit 4c17bf96bf

18
lapis.c
View file

@ -81,8 +81,12 @@ lapis_token_t *lapis_parse_tokens(size_t *ntokens, FILE *f) {
lapis_token_t *tokens = NULL;
lapis_token_t tmptok = { .type = LAPIS_TTYPE_NONE, .str = NULL, .len = 0 };
_Bool end_of_expression = false;
int c;
while ( (c = fgetc(f)) != EOF ) {
while ( (c = fgetc(f)) != EOF && !end_of_expression ) {
_Bool in_escape = false;
// skip over comments
if ( c == '#' ) {
@ -90,6 +94,18 @@ lapis_token_t *lapis_parse_tokens(size_t *ntokens, FILE *f) {
if ( c == EOF ) break;
}
// detect line escapes
if ( c == '\\' ) {
in_escape = true;
c = fgetc(f);
if ( c == EOF ) break;
}
if ( c == ';' || (!in_escape && c == '\n') ) {
c = ' ';
if ( num_tokens ) end_of_expression = true;
}
enum lapis_ttype cur_ttype;
if ( (tmptok.type == LAPIS_TTYPE_IDENT && isdigit(c)) || isalpha(c) || c == '_' ) {