add mulitple variable declaration, with null default. add call

This commit is contained in:
2025-06-05 04:04:41 +01:00
parent 7b76b0d888
commit 61d8bc61c3
14 changed files with 186 additions and 111 deletions

View File

@@ -12,7 +12,7 @@ ParsedValue *parse_access(char*file,DArray *tokens, size_t * index, ParsedValue
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
ParsedAccess *parsedAccess = checked_malloc(sizeof(ParsedAccess));
parsedAccess->to_access = to_access;
parsedAccess->access = strcpy(checked_malloc(sizeof(token->value)), token->value);
parsedAccess->access = strcpy(checked_malloc(strlen(token->value) + 1), token->value);
parsedValue->type = AST_ACCESS;
parsedValue->data = parsedAccess;
return parsedValue;

View File

@@ -1,28 +1,42 @@
#include "assign.h"
#include "../../../lexer/token.h"
#include "../../../memory.h"
#include "../../parser.h"
#include "../call/call.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../../memory.h"
ParsedValue *parse_assign(char *file, DArray *tokens,
ParsedValue *assign_to, size_t *index) {
ParsedValue *parse_assign(char *file, DArray *tokens, ParsedValue *assign_to,
size_t *index) {
Token *token = darray_get(tokens, *index);
switch (assign_to->type) {
case AST_IDENTIFIER:
case AST_ASSIGN:
break;
default:
fprintf(stderr, "%s:%u:%u error: can't assign to %s\n", file, token->line,
token->column, ValueTypeNames[assign_to->type]);
exit(EXIT_FAILURE);
case AST_IDENTIFIER:
case AST_ASSIGN:
break;
case AST_CALL:;
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);
exit(EXIT_FAILURE);
}
}
break;
default:
fprintf(stderr, "%s:%zu:%zu error: can't assign to %s\n", file, token->line,
token->column, ValueTypeNames[assign_to->type]);
exit(EXIT_FAILURE);
}
ParsedAssign *assign = checked_malloc(sizeof(ParsedAssign));
assign->to = assign_to;
assign->type = token->type;
(*index)++;
error_if_finished(file,tokens,index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
assign->from = parse_token(file, tokens, index, true);
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));

View File

@@ -8,42 +8,41 @@
ParsedValue *parse_call(char *file, DArray *tokens, size_t *index,
ParsedValue *to_call) {
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
ParsedCall *call = checked_malloc(sizeof(ParsedCall));
call->to_call = to_call;
parsedValue->data = call;
parsedValue->type = AST_CALL;
call->args = checked_malloc(sizeof(DArray));
darray_init(call->args, sizeof(ParsedValue));
(*index)++;
error_if_finished(file, tokens, index);
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
ParsedCall *parsedCall = checked_malloc(sizeof(ParsedCall));
DArray *args = checked_malloc(sizeof(DArray));
darray_init(args, sizeof(ParsedValue));
parsedCall->to_call = to_call;
parsedCall->args = args;
parsedValue->type = AST_ACCESS;
parsedValue->data = parsedCall;
Token *token = darray_get(tokens, *index);
DArray *arg = checked_malloc(sizeof(DArray));
darray_init(arg, sizeof(ParsedValue));
while (true) {
bool to_break = false;
switch (token->type) {
case TOKEN_RPAREN:
to_break = true;
break;
default:
break;
}
if (to_break)
break;
ParsedValue *parsedValue = parse_token(file, tokens, index, true);
darray_push(arg, parsedValue);
free(parsedValue);
switch (token->type) {
case TOKEN_COMMA:
darray_push(args, arg);
free(arg);
arg = checked_malloc(sizeof(DArray));
darray_init(arg, sizeof(ParsedValue));
break;
default:
break;
if (token->type == TOKEN_RPAREN) {
(*index)++;
if ((*index) >= tokens->size)
return parsedValue;
token = darray_get(tokens, *index);
} else {
while ((*index) < tokens->size) {
ParsedValue *parsedArg = parse_token(file, tokens, index, true);
darray_push(call->args, parsedArg);
free(parsedArg);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
if (token->type == TOKEN_RPAREN) {
(*index)++;
if ((*index) >= tokens->size)
break;
token = darray_get(tokens, *index);
break;
} else if (token->type != TOKEN_COMMA) {
fprintf(stderr, "%s:%zu:%zu error: expected comma\n", file, token->line,
token->column);
exit(EXIT_FAILURE);
}
(*index)++;
error_if_finished(file, tokens, index);
}
}
return parsedValue;
@@ -53,9 +52,7 @@ void free_parse_call(void *ptr) {
ParsedValue *parsedValue = ptr;
ParsedCall *parsedCall = parsedValue->data;
darray_free(parsedCall->args, free_parsed);
free_parsed(parsedCall->to_call);
free(parsedCall);
}

View File

@@ -7,6 +7,6 @@
ParsedValue *parse_identifier(Token *token) {
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
parsedValue->type = AST_IDENTIFIER;
parsedValue->data = strcpy(checked_malloc(sizeof(token->value)), token->value);
parsedValue->data = strcpy(checked_malloc(strlen(token->value) + 1), token->value);
return parsedValue;
}