Skip to content

Commit

Permalink
fixed parsing and printing for f64 arrays. other cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
reginaldford committed Aug 29, 2024
1 parent a048b6d commit bc812cf
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
22 changes: 22 additions & 0 deletions src/main/engine/file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "../sms.h"

// Function to write a buffer to a file in chunks of specified size
sm_object *write_buffer_to_file(FILE *fptr, char *buffer, size_t buffer_size, size_t chunk_size,
sm_expr *sme) {
size_t total_written = 0;
while (total_written < buffer_size) {
size_t to_write = chunk_size;
if (total_written + chunk_size > buffer_size) {
to_write = buffer_size - total_written;
}
size_t written = fwrite(buffer + total_written, 1, to_write, fptr);
if (written != to_write) {
sm_symbol *title = sm_new_symbol("fileWriteError", strlen("fileWriteError"));
sm_string *message = sm_new_string(strlen("fileWrite failed during write operation"),
"fileWrite failed during write operation");
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}
total_written += written;
}
return (sm_object *)sms_true;
}
4 changes: 4 additions & 0 deletions src/main/engine/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Read https://raw.githubusercontent.com/reginaldford/sms/main/LICENSE.txt for license information

sm_object *write_buffer_to_file(FILE *fptr, char *buffer, size_t buffer_size, size_t chunk_size,
sm_expr *sme);
29 changes: 14 additions & 15 deletions src/main/object/sm_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ sm_array *sm_new_array(uint32_t type, uint32_t size, sm_object *content, uint32_
return output;
}

f64 *sm_f64_array_get_start(sm_array *a) { return (f64 *)(&((sm_space *)a->content)[1]); }

f64 sm_f64_array_get_bare(sm_array *a, uint32_t index) { return sm_f64_array_get_start(a)[index]; }

f64 *sm_f64_array_get_start(sm_array *a) { return (f64 *)(&((sm_space *)a->content)[1]); }

ui8 *sm_ui8_array_get_start(sm_array *a) { return (ui8 *)(&((sm_space *)a->content)[1]); }

ui8 sm_ui8_array_get_bare(sm_array *a, uint32_t index) { return sm_ui8_array_get_start(a)[index]; }
Expand Down Expand Up @@ -71,27 +70,28 @@ uint32_t sm_f64_array_contents_sprint(sm_array *array, char *buffer, bool fake)
uint32_t cursor = 0;
uint32_t bufferCursor = 0;
uint32_t i = 0;
char fakeBuf[25];
if (fake) {
char fakeBuf[64]; // Temporary buffer used when fake is true
if (fake)
buffer = fakeBuf;
}
for (i = 0; i < array->size; i++) {
if (fake) {
if (fake)
bufferCursor = 0; // Reset bufferCursor to 0 if we are faking
}
f64 value = sm_f64_array_get_bare(array, i);
bufferCursor = snprintf(&buffer[cursor], 24, "%.16g", value);
f64 value = sm_f64_array_get_bare(array, i);
// Write the value to the buffer, ensuring no overflow
bufferCursor = snprintf(&buffer[cursor], 25, "%.16g", value); // Write value to the buffer
if (bufferCursor >= 25)
bufferCursor = 24; // Safeguard against overflow from snprintf
cursor += bufferCursor;
// Add a comma if it's not the last element
if (i + 1 < array->size) {
if (!fake) {
if (!fake)
buffer[cursor] = ',';
}
cursor++;
}
}
if (!fake) {
// Null-terminate the buffer if not faking
if (!fake)
buffer[cursor] = '\0';
}
return cursor;
}

Expand All @@ -109,9 +109,8 @@ uint32_t sm_ui8_array_contents_sprint(sm_array *array, char *buffer, bool fake)
buffer[cursor] = ',';
cursor++;
}
if (array->size > 0) {
if (array->size > 0)
cursor += sprintf(&buffer[cursor], "%i", sm_ui8_array_get_bare(array, array->size - 1));
}
return cursor;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/object/sm_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ uint32_t sm_sizeof(sm_object *obj1) {
case SM_LOCAL_TYPE:
return sizeof(sm_local);
case SM_SPACE_TYPE:
return sizeof(sm_space) + sm_round_size64(((sm_space *)obj1)->size);
return sizeof(sm_space) + sm_round_size(((sm_space *)obj1)->size);
case SM_SELF_TYPE:
return sizeof(struct sm_self);
case SM_ERR_TYPE:
Expand Down
2 changes: 1 addition & 1 deletion src/main/object/sm_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// General purpose space of memory
sm_space *sm_new_space(uint32_t size) {
sm_space *new_space = sm_malloc(sizeof(sm_space) + sm_round_size64(size));
sm_space *new_space = sm_malloc(sizeof(sm_space) + sm_round_size(size));
new_space->my_type = SM_SPACE_TYPE;
new_space->size = size;
return new_space;
Expand Down
4 changes: 4 additions & 0 deletions src/main/parser/sms.y
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,14 @@ F64_ARRAY_LIST : F64_ARRAY_OPEN F64 ',' F64 {
}
| F64_ARRAY_LIST ',' INTEGER {
$$->size++;
sm_space * space = (sm_space*)$$->content;
space->size+=sizeof(f64);
sm_f64_array_set($$,$$->size-1,$3);
};
| F64_ARRAY_LIST ',' F64 {
$$->size++;
sm_space* space = (sm_space*)$$->content;
space->size+=sizeof(f64);
sm_f64_array_set($$,$$->size-1,((sm_f64*)$3)->value);
};

Expand Down

0 comments on commit bc812cf

Please sign in to comment.