add multi line support for more stuff

This commit is contained in:
2025-06-05 15:13:57 +01:00
parent acc432ed79
commit c0ee99fd54
6 changed files with 64 additions and 15 deletions

View File

@@ -18,11 +18,11 @@ ParsedValue *parse_assign(char *file, DArray *tokens, ParsedValue *assign_to,
ParsedCall *call = assign_to->data;
for (size_t i = 0; i < call->args->size; i++) {
if (((ParsedValue *)darray_get(call->args, i))->type != AST_IDENTIFIER) {
fprintf(
stderr,
"%s:%zu:%zu error: parameter names need to start with a letter or _, "
"only use letters, digits, or _, and can't be keywords.\n",
file, token->line, token->column);
fprintf(stderr,
"%s:%zu:%zu error: parameter names need to start with a letter "
"or _, "
"only use letters, digits, or _, and can't be keywords.\n",
file, token->line, token->column);
exit(EXIT_FAILURE);
}
}
@@ -39,6 +39,11 @@ ParsedValue *parse_assign(char *file, DArray *tokens, ParsedValue *assign_to,
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
assign->from = parse_token(file, tokens, index, true);
if (!assign->from) {
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
token->column);
exit(EXIT_FAILURE);
}
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
parsedValue->type = AST_ASSIGN;
parsedValue->data = assign;

View File

@@ -25,10 +25,14 @@ ParsedValue *parse_call(char *file, DArray *tokens, size_t *index,
token = darray_get(tokens, *index);
} else {
while ((*index) < tokens->size) {
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
ParsedValue *parsedArg = parse_token(file, tokens, index, true);
darray_push(call->args, parsedArg);
free(parsedArg);
error_if_finished(file, tokens, index);
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
if (token->type == TOKEN_RPAREN) {
(*index)++;

View File

@@ -65,6 +65,8 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
darray_push(declaration->parameters, &parameter_name);
(*index)++;
error_if_finished(file, tokens, index);
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
if (token->type == TOKEN_RPAREN) {
(*index)++;
@@ -89,6 +91,11 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
free(declaration->from);
declaration->from = parse_token(file, tokens, index, true);
if (!declaration->from) {
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
token->column);
exit(EXIT_FAILURE);
}
if ((*index) >= tokens->size)
break;
token = darray_get(tokens, *index);
@@ -97,6 +104,8 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
break;
(*index)++;
error_if_finished(file, tokens, index);
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
}
return parsedValue;

View File

@@ -29,6 +29,21 @@ void error_if_finished(char *file, DArray *tokens, size_t *index) {
}
}
void skip_newlines_and_indents(DArray *tokens, size_t *index) {
bool passed = false;
while (!passed && (*index) < tokens->size) {
Token *token = darray_get(tokens, *index);
switch (token->type) {
case TOKEN_NEW_LINE:
case TOKEN_INDENT:
(*index)++;
break;
default:
passed = true;
}
}
}
ParsedValue *parse_token(char *file, DArray *tokens, size_t *index,
bool inline_flag) {
Token *token = darray_get(tokens, *index);
@@ -61,12 +76,8 @@ ParsedValue *parse_token(char *file, DArray *tokens, size_t *index,
output = parse_string(*token);
break;
case TOKEN_NEW_LINE:
while (token->type == TOKEN_NEW_LINE && ++(*index) < tokens->size) {
token = darray_get(tokens, *index);
}
if (token->type == TOKEN_NEW_LINE)
break;
output = parse_token(file, tokens, index, inline_flag);
(*index)++;
return NULL;
break;
case TOKEN_INDENT:
fprintf(stderr, "%s:%zu:%zu error: invalid indentation\n", file, token->line,
@@ -101,7 +112,7 @@ ParsedValue *parse_token(char *file, DArray *tokens, size_t *index,
case TOKEN_ASSIGN_MODULO:
case TOKEN_ASSIGN_PLUS:
case TOKEN_ASSIGN_SLASH:
case TOKEN_ASSIGN_STAR:;
case TOKEN_ASSIGN_STAR:
output = parse_assign(file, tokens, output, index);
break;
case TOKEN_LPAREN:

View File

@@ -36,4 +36,6 @@ void free_parsed(void *ptr);
void error_if_finished(char *file,DArray *tokens, size_t *index);
void skip_newlines_and_indents(DArray *tokens, size_t *index);
#endif // PARSER_H