start working on supporting operations

This commit is contained in:
William Bell
2025-08-14 04:51:11 +01:00
parent d4528e44f6
commit 340843c99c
15 changed files with 393 additions and 67 deletions

View File

@@ -24,7 +24,7 @@ int parse_exponent(const char *exp_str, long *exp_val) {
return 0;
}
int mpq_set_decimal_str_exp(mpq_t r, const char *str) {
int mpq_set_decimal_str_exp(mpq_t r, const char *str, size_t len) {
// Skip leading whitespace
while (isspace(*str))
str++;
@@ -39,11 +39,11 @@ int mpq_set_decimal_str_exp(mpq_t r, const char *str) {
}
// Copy input to a buffer for manipulation
size_t len = strlen(str);
char *buf = malloc(len + 1);
if (!buf)
return -1;
strcpy(buf, str);
memcpy(buf, str, len);
buf[len] = '\0';
// Find 'e' or 'E'
char *e_ptr = strchr(buf, 'e');
@@ -178,7 +178,7 @@ ParsedValueReturn parse_number(Token *token, char *path) {
parsedValue->type = AST_NUMBER;
mpq_t *r_ptr = malloc(sizeof(mpq_t));
mpq_init(*r_ptr);
int err = mpq_set_decimal_str_exp(*r_ptr, token->value);
int err = mpq_set_decimal_str_exp(*r_ptr, token->value, token->length);
if (err) {
free_parsed(parsedValue);
free(parsedValue);

View File

@@ -12,4 +12,6 @@
// Function declaration for parsing an identifier
ParsedValueReturn parse_number(Token *token, char*path);
int mpq_set_decimal_str_exp(mpq_t r, const char *str, size_t len);
#endif // NUMBER_H

View File

@@ -12,18 +12,22 @@
#include <stdlib.h>
#include <string.h>
struct operation {};
ParsedValue convert_to_operation(DArray *to_operate_on, DArray *operations) {
if (to_operate_on->size == 1) {
return *((ParsedValue *)darray_get(to_operate_on, 0));
}
ArTokenType operation = 0;
ArTokenType operation_type = 0;
Token operation = {};
DArray positions;
for (size_t i = 0; i < operations->size; i++) {
ArTokenType *current_operation = darray_get(operations, i);
if (operation < *current_operation) {
if (operation != 0) {
Token *current_operation = darray_get(operations, i);
if (operation_type < current_operation->type) {
if (operation_type != 0) {
darray_free(&positions, NULL);
}
operation_type = current_operation->type;
operation = *current_operation;
darray_init(&positions, sizeof(size_t));
}
@@ -33,7 +37,10 @@ ParsedValue convert_to_operation(DArray *to_operate_on, DArray *operations) {
parsedValue.type = AST_OPERATION;
ParsedOperation *operationStruct = checked_malloc(sizeof(ParsedOperation));
parsedValue.data = operationStruct;
operationStruct->operation = operation;
operationStruct->operation = operation_type;
operationStruct->line = operation.line;
operationStruct->column = operation.column;
operationStruct->length = operation.length;
darray_init(&operationStruct->to_operate_on, sizeof(ParsedValue));
size_t last_position = 0;
size_t to_operate_on_last_position = 0;
@@ -68,7 +75,7 @@ ParsedValueReturn parse_operations(char *file, DArray *tokens, size_t *index,
free(first_parsed_value);
DArray operations;
darray_init(&operations, sizeof(ArTokenType));
darray_init(&operations, sizeof(Token));
while (tokens->size > *index) {
bool to_break = false;
@@ -81,7 +88,7 @@ ParsedValueReturn parse_operations(char *file, DArray *tokens, size_t *index,
}
if (to_break)
break;
darray_push(&operations, &token->type);
darray_push(&operations, token);
(*index)++;
ArErr err = error_if_finished(file, tokens, index);
if (err.exists) {

View File

@@ -12,6 +12,9 @@
typedef struct {
ArTokenType operation;
DArray to_operate_on; // ParsedValue[]
size_t line;
size_t column;
size_t length;
} ParsedOperation;
ParsedValueReturn parse_operations(char *file, DArray *tokens, size_t *index,