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

View File

@@ -65,6 +65,8 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
darray_push(declaration->parameters, &parameter_name); darray_push(declaration->parameters, &parameter_name);
(*index)++; (*index)++;
error_if_finished(file, tokens, index); error_if_finished(file, tokens, index);
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index); token = darray_get(tokens, *index);
if (token->type == TOKEN_RPAREN) { if (token->type == TOKEN_RPAREN) {
(*index)++; (*index)++;
@@ -89,6 +91,11 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
free(declaration->from); free(declaration->from);
declaration->from = parse_token(file, tokens, index, true); 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) if ((*index) >= tokens->size)
break; break;
token = darray_get(tokens, *index); token = darray_get(tokens, *index);
@@ -97,6 +104,8 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
break; break;
(*index)++; (*index)++;
error_if_finished(file, tokens, index); error_if_finished(file, tokens, index);
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index); token = darray_get(tokens, *index);
} }
return parsedValue; 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, ParsedValue *parse_token(char *file, DArray *tokens, size_t *index,
bool inline_flag) { bool inline_flag) {
Token *token = darray_get(tokens, *index); Token *token = darray_get(tokens, *index);
@@ -61,12 +76,8 @@ ParsedValue *parse_token(char *file, DArray *tokens, size_t *index,
output = parse_string(*token); output = parse_string(*token);
break; break;
case TOKEN_NEW_LINE: case TOKEN_NEW_LINE:
while (token->type == TOKEN_NEW_LINE && ++(*index) < tokens->size) { (*index)++;
token = darray_get(tokens, *index); return NULL;
}
if (token->type == TOKEN_NEW_LINE)
break;
output = parse_token(file, tokens, index, inline_flag);
break; break;
case TOKEN_INDENT: case TOKEN_INDENT:
fprintf(stderr, "%s:%zu:%zu error: invalid indentation\n", file, token->line, 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_MODULO:
case TOKEN_ASSIGN_PLUS: case TOKEN_ASSIGN_PLUS:
case TOKEN_ASSIGN_SLASH: case TOKEN_ASSIGN_SLASH:
case TOKEN_ASSIGN_STAR:; case TOKEN_ASSIGN_STAR:
output = parse_assign(file, tokens, output, index); output = parse_assign(file, tokens, output, index);
break; break;
case TOKEN_LPAREN: 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 error_if_finished(char *file,DArray *tokens, size_t *index);
void skip_newlines_and_indents(DArray *tokens, size_t *index);
#endif // PARSER_H #endif // PARSER_H

24
test.ar
View File

@@ -1,3 +1,21 @@
let a, b = 1, c, d = 42, temp_result, compute_area(radius) = 3.1415, identity(x) = x, f(x), g(y, z), result, z = 0, extremely_long_variable_name_to_test_limits, cache_value = compute_area(5), placeholder_fn_with_no_body(arg1, arg2, arg3), total = identity(100), deeply_nested_args_function(arg1, arg2, arg3, arg4, arg5), sum = a, another, another_function(), just_null_here let a,
x = 10 b = 1,
x.epic_test_long_variable_name = "hello world" c,
d = 42,
temp_result,
compute_area(radius) = 3.1415,
identity(x) = x,
f(x),
g(y, z),
result,
z = 0,
extremely_long_variable_name_to_test_limits,
cache_value = compute_area(5),
placeholder_fn_with_no_body(arg1, arg2, arg3),
total = identity(100),
deeply_nested_args_function(arg1, arg2, arg3, arg4, arg5),
sum = a,
another,
another_function(),
just_null_here
if (x) term.log("hello world")