Skip to content

Commit

Permalink
Format all documents
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidchu committed Feb 19, 2025
1 parent 94d5619 commit d9d9438
Show file tree
Hide file tree
Showing 11 changed files with 920 additions and 259 deletions.
87 changes: 60 additions & 27 deletions interpreter/array.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "array.h"

#define FILTER(func_call) \
do { \
int err = (func_call); \
if (err) { return err; } \
#define FILTER(func_call) \
do { \
int err = (func_call); \
if (err) { \
return err; \
} \
} while (0)

static size_t const DEFAULT_ARRAY_CAPACITY = 8;
Expand All @@ -19,24 +21,35 @@ static double const DEFAULT_GROWTH_FACTOR = 2;
static bool
ok(struct Array const *const me)
{
if (me == NULL) { return false; }
if (me->array == NULL) { return me->capacity == 0; }
if (me->capacity < me->length) { return false; }
if (me == NULL) {
return false;
}
if (me->array == NULL) {
return me->capacity == 0;
}
if (me->capacity < me->length) {
return false;
}
return true;
}


static int
resize(struct Array *const me, size_t const new_capacity)
{
if (me == NULL) { return -1; }
if (me == NULL) {
return -1;
}
// Assert data is not corrupted.
assert(ok(me));
// We do not allow the user to destroy data through resizing.
if (me->length > new_capacity) { return -1; }
if (me->length > new_capacity) {
return -1;
}
// TODO Ensure no overflow in multiplication.
void **new_array = realloc(me->array, sizeof(*me->array) * new_capacity);
if (new_capacity != 0 && new_array == NULL) { return -1; }
if (new_capacity != 0 && new_array == NULL) {
return -1;
}
me->array = new_array;
me->capacity = new_capacity;
return 0;
Expand All @@ -45,7 +58,9 @@ resize(struct Array *const me, size_t const new_capacity)
static int
grow(struct Array *const me)
{
if (me == NULL) { return -1; }
if (me == NULL) {
return -1;
}
if (me->capacity == 0) {
return resize(me, DEFAULT_ARRAY_CAPACITY);
}
Expand Down Expand Up @@ -86,11 +101,12 @@ shift_left(struct Array *const me, size_t const victim_idx)
return 0;
}


int
array_ctor(struct Array *const me)
{
if (me == NULL) { return -1; }
if (me == NULL) {
return -1;
}
*me = (struct Array){0};
assert(ok(me));
return grow(me);
Expand All @@ -99,7 +115,9 @@ array_ctor(struct Array *const me)
int
array_dtor(struct Array *const me)
{
if (me == NULL) { return -1; }
if (me == NULL) {
return -1;
}
assert(ok(me));
// TODO Destroy elements in this array recursively.
free(me->array);
Expand All @@ -122,45 +140,60 @@ array_fprint(struct Array const *const me, FILE *const fp, bool newline)
int
array_insert(struct Array *const me, size_t const idx, void *const item)
{
if (me == NULL || me->array == NULL) { return -1; }
if (me == NULL || me->array == NULL) {
return -1;
}
assert(ok(me));
// Check for out of bounds access.
if (idx > me->length) { return -1; }
if (idx > me->length) {
return -1;
}
if (me->length + 1 > me->capacity) {
FILTER(grow(me));
}
FILTER(shift_right(me, idx));
me->array[idx] = item;
++me->length;
return 0;
}
}

int
array_get(struct Array *const me, size_t const idx, void **const result)
{
if (me == NULL || me->array == NULL) { return -1; }
if (me == NULL || me->array == NULL) {
return -1;
}
assert(ok(me));
// Check for out of bounds access.
if (idx >= me->length) { return -1; }
if (idx >= me->length) {
return -1;
}
// Return the result.
if (result == NULL) { return -1; }
if (result == NULL) {
return -1;
}
*result = me->array[idx];
return 0;
}

int
array_remove(struct Array *const me, size_t const idx, void **const victim)
{
if (me == NULL || me->array == NULL) { return -1; }
if (me == NULL || me->array == NULL) {
return -1;
}
assert(ok(me));
// Check for out of bounds access.
if (idx >= me->length) { return -1; }
if (idx >= me->length) {
return -1;
}
// Return the victim for deletion.
if (victim == NULL) { return -1; }
if (victim == NULL) {
return -1;
}
*victim = me->array[idx];
// TODO Shrink if necessary.
FILTER(shift_left(me, idx));
--me->length;
return 0;
}

}
1 change: 0 additions & 1 deletion interpreter/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ array_get(struct Array *const me, size_t const idx, void **const result);
/// @note The caller is responsible for freeing the resources of the victim.
int
array_remove(struct Array *const me, size_t const idx, void **const victim);

90 changes: 68 additions & 22 deletions interpreter/boolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,37 @@ bool_stringify(bool const x)
static int
boolean_error(struct Object const *const me)
{
if (me == NULL || me->type == NULL || me->type->type != OBJECT_TYPE_BOOLEAN) { return -1; }
if (me == NULL || me->type == NULL ||
me->type->type != OBJECT_TYPE_BOOLEAN) {
return -1;
}
assert(me->data.boolean == true || me->data.boolean == false);
return 0;
}

int
boolean_ctor(struct Object *me, struct ObjectType const *const type, union ObjectData data)
boolean_ctor(struct Object *me,
struct ObjectType const *const type,
union ObjectData data)
{
if (me == NULL) { return -1; }
if (me == NULL) {
return -1;
}
me->type = type;
// TODO Check if data is valid.
me->data = data;
return 0;
}

int
boolean_from_cstr(struct Object *const me, struct ObjectType const *const type, char const *const cstr, char const **cstr_end)
boolean_from_cstr(struct Object *const me,
struct ObjectType const *const type,
char const *const cstr,
char const **cstr_end)
{
if (me == NULL || type == NULL || cstr == NULL) { return -1; }
if (me == NULL || type == NULL || cstr == NULL) {
return -1;
}
me->type = type;
if (strncmp(cstr, "true", sizeof("true")) == 0) {
me->data.boolean = true;
Expand All @@ -52,7 +64,9 @@ int
boolean_dtor(struct Object *me)
{
int err = 0;
if ((err = boolean_error(me))) { return err; }
if ((err = boolean_error(me))) {
return err;
}
*me = (struct Object){0};
return 0;
}
Expand All @@ -61,31 +75,55 @@ int
boolean_cmp(struct Object const *me, struct Object const *other, int *result)
{
int err = 0;
if ((err = boolean_error(me))) { return err; }
if (me == NULL || other == NULL || result == NULL) { return -1; }
if (me->type->type != OBJECT_TYPE_BOOLEAN) { return -1; }
if (me == other) { *result = 0; return 0; }
if (other->type->type == OBJECT_TYPE_BOOLEAN && me->data.boolean == other->data.boolean) { *result = 1; return 0; }
if ((err = boolean_error(me))) {
return err;
}
if (me == NULL || other == NULL || result == NULL) {
return -1;
}
if (me->type->type != OBJECT_TYPE_BOOLEAN) {
return -1;
}
if (me == other) {
*result = 0;
return 0;
}
if (other->type->type == OBJECT_TYPE_BOOLEAN &&
me->data.boolean == other->data.boolean) {
*result = 1;
return 0;
}
*result = 3;
return 0;
}

int
boolean_fprint(struct Object const *const me, FILE *const fp, bool const newline)
boolean_fprint(struct Object const *const me,
FILE *const fp,
bool const newline)
{
int err = 0;
if ((err = boolean_error(me))) { return err; }
if ((err = boolean_error(me))) {
return err;
}
// TODO Handle errors in fprintf(...).
fprintf(fp, "%s%s", bool_stringify(me->data.boolean), newline ? "\n" : "");
return 0;
}

static int
generic_boolean_op(struct Object const *const lhs, struct Object const *const rhs, bool *const result, int const op)
generic_boolean_op(struct Object const *const lhs,
struct Object const *const rhs,
bool *const result,
int const op)
{
int err = 0;
if ((err = boolean_error(lhs))) { return err; }
if ((err = boolean_error(rhs))) { return err; }
if ((err = boolean_error(lhs))) {
return err;
}
if ((err = boolean_error(rhs))) {
return err;
}
bool ans = false;
switch (op) {
case 0:
Expand All @@ -102,31 +140,39 @@ generic_boolean_op(struct Object const *const lhs, struct Object const *const rh
}

int
boolean_not(struct Object const *const me, bool *const result)
boolean_not(struct Object const *const me, bool *const result)
{
int err = 0;
if ((err = boolean_error(me))) { return err; }
if ((err = boolean_error(me))) {
return err;
}
*result = !me->data.boolean;
return 0;
}

int
boolean_and(struct Object const *const me, struct Object const *const other, bool *const result)
boolean_and(struct Object const *const me,
struct Object const *const other,
bool *const result)
{
return generic_boolean_op(me, other, result, 0);
}

int
boolean_or(struct Object const *const me, struct Object const *const other, bool *const result)
boolean_or(struct Object const *const me,
struct Object const *const other,
bool *const result)
{
return generic_boolean_op(me, other, result, 1);
}

int
boolean_truthiness(struct Object const *const me, bool *const result)
boolean_truthiness(struct Object const *const me, bool *const result)
{
int err = 0;
if ((err = boolean_error(me))) { return err; }
if ((err = boolean_error(me))) {
return err;
}
*result = me->data.boolean;
return 0;
}
Loading

0 comments on commit d9d9438

Please sign in to comment.