From 944d9287dc97c4b429b6038b00ab636e2da4d976 Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 8 May 2020 06:18:20 +0100 Subject: [PATCH] Increased token size! Added more doc comments. --- src/calc.c | 2 +- src/object.c | 82 ++++++++++++++++++++++++++++++++++++++-------------- src/object.h | 4 +-- 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/calc.c b/src/calc.c index 153283e..7e502ec 100644 --- a/src/calc.c +++ b/src/calc.c @@ -182,7 +182,7 @@ void code_window_load(Window *window) GRect result_bounds = GRect(6, 128, 132, 132); s_result_text_layer = text_layer_create(result_bounds); text_layer_set_text(s_result_text_layer, "R: "); - text_layer_set_font(s_result_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD)); + text_layer_set_font(s_result_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD)); text_layer_set_text_alignment(s_result_text_layer, GTextAlignmentRight); layer_add_child(window_get_root_layer(s_code_window), text_layer_get_layer(s_result_text_layer)); diff --git a/src/object.c b/src/object.c index 8555bfe..cee606d 100644 --- a/src/object.c +++ b/src/object.c @@ -56,7 +56,11 @@ Object *itemAt(const Object *listObj, int n) return march; } -// Returns last object in list (->forward == NULL) +/** + * Returns a pointer to the last Object in the given list. + * @param listObj The list to find the tail of + * @return A pointer to the last Object if it is found, or NULL on an error + */ Object *tail(const Object *listObj) { if(!listObj || listObj->type != TYPE_LIST) @@ -70,11 +74,13 @@ Object *tail(const Object *listObj) } /** - * Checks if a given object is empty + * Checks if a given object is empty. + * * BOOL, NUMBER, and ERROR types return true only when 0 * LIST, LAMBDA, and FUNC types return true only when NULL * SYMBOL returns true only when it begins with a nullchar - * Returns false if obj is NULL + * @param obj A pointer to the object to check + * @return True only if the non-null Object is empty/non-zero */ inline int isEmpty(const Object *obj) { @@ -100,6 +106,12 @@ inline int isEmpty(const Object *obj) } } +/** + * Allocate a copy of a given object into the given pointer. + * Does nothing if `spot` is NULL + * @param spot A pointer to the Object pointer that needs allocating + * @param src The Object to copy from + */ void allocObject(Object **spot, const Object src) { if(!spot) @@ -109,12 +121,21 @@ void allocObject(Object **spot, const Object src) (*spot)->forward = NULL; } -// Insert `src` into list `dest` before index `ind` -// Adds to the end if the index is too high. -void insertIntoList(Object *dest, int ind, const Object src) +/** + * Inserts a given Object into a list at the given index. + * Immediately returns if `dest` is NULL or not a list type + * Adds to the end of the list if the index is higher than the list length + + * @param dest The list to insert into + * @param ind The index to insert at + * @param src The Object to add to the list + + * @warning UNTESTED + */ +void insertIntoList(Object *dest, unsigned ind, const Object src) { // Only work with non-null list types - if(!dest || dest->type != TYPE_LIST || ind < 0) + if(!dest || dest->type != TYPE_LIST) return; if(isEmpty(dest)) { @@ -125,7 +146,7 @@ void insertIntoList(Object *dest, int ind, const Object src) // TODO Check for off-by-one errors // ensure pointers connect old and new Object *march = dest->list; - for(int i = 1; i < ind; i++) { + for(unsigned i = 1; i < ind; i++) { if(march->forward == NULL) { allocObject(&march->forward, src); return; @@ -139,15 +160,30 @@ void insertIntoList(Object *dest, int ind, const Object src) march->forward->forward = oldForward; } +/** + * Replace an object in a list at a given index with a given object + * Attempts to clean the replaced Object before adding the new one + * Immediately returns if `list` is NULL or not a list type + * @param list The list Object to replace an element of + * @param i The index of the element to replace + * @param src The Object to copy into the list + */ void replaceListing(Object *list, int i, const Object src) { - if(!list) + if(!list || list->type != TYPE_LIST) return; - list[i] = src; - list[i].forward = NULL; + Object *replace = itemAt(list, i); + cleanObject(replace); + *replace = src; + replace->forward = NULL; } -// Adds an object to the end of a list object +/** + * Adds an Object to the end of a list + * Does nothing if `dest` is NULL or not a list type + * @param dest The list to append to + * @param src The Object to copy into the list + */ void nf_addToList(Object *dest, const Object src) { if(!dest || dest->type != TYPE_LIST) @@ -158,8 +194,7 @@ void nf_addToList(Object *dest, const Object src) return; } - Object *end = tail(dest); - allocObject(&end->forward, src); + allocObject(&tail(dest)->forward, src); } void printErr(const Object *obj) @@ -302,24 +337,27 @@ void printAndClean(Object *target) // Frees all objects in a list void deleteList(const Object *dest) { + if(!dest) + return; + if(dest->type != TYPE_LIST) { printf("Tried to delete something other than a list\n"); return; } - Object *tail = dest->list; - while(tail != NULL) { - Object *prevTail = tail; - tail = tail->forward; - cleanObject(prevTail); - free(prevTail); + Object *march = dest->list; + while(march != NULL) { + Object *prevMarch = march; + march = march->forward; + cleanObject(prevMarch); + free(prevMarch); } } /** * Copies all items from `src` to `dest` - * Does nothing if either is NULL, or neither are lists - * First deletes all items from `dest` + * Does nothing if either is NULL, or not a list type + * Deletes all items from `dest` before copying */ void copyList(Object *dest, const Object *src) { diff --git a/src/object.h b/src/object.h index 8ecd906..80470c3 100644 --- a/src/object.h +++ b/src/object.h @@ -1,7 +1,7 @@ #ifndef OBJECT_H #define OBJECT_H -#define MAX_TOK_LEN 4 // 11 +#define MAX_TOK_LEN 10 // 11 #define MAX_TOK_CNT 128 // 128 #define MAX_ENV_ELM 15 // 50 @@ -79,7 +79,7 @@ int getType(const Object *obj); int isEmpty(const Object *obj); Object *tail(const Object *listObj); -void insertIntoList(Object *dest, int ind, const Object src); +void insertIntoList(Object *dest, unsigned ind, const Object src); void replaceListing(Object *list, int i, const Object src); void nf_addToList(Object *dest, Object src); void deleteList(const Object *dest);