Cleaner and more optimized (cat) implementation.

stringObj() will now _add_ to its length parameter.
More global scalloc and smalloc.
Bit of Makefile re-org.
This commit is contained in:
Sage Vaillancourt 2022-03-31 11:41:02 -04:00 committed by Sage Vaillancourt
parent 32e21bf758
commit d2a0abeff3
4 changed files with 51 additions and 54 deletions

View File

@ -12,21 +12,21 @@ mkfile_dir := $(dir $(mkfile_path))
GCC_ARGS ?= -g -Wall -o $(exe) -D WEBSERVER -D STANDALONE -DSCRIPTDIR=\"$(SCRIPTDIR)\"
GCC_COM ?= gcc -O0 $(GCC_ARGS)
release:
gcc -O3 $(GCC_ARGS) $(file_libs) && strip ./$(exe) && echo && ./tests.sh
all: notest
echo && ./tests.sh
notest:
$(GCC_COM) $(file_libs)
all: notest
val: notest
./tests.sh -val
release_notest:
gcc -O3 $(GCC_ARGS) $(file_libs) && strip ./$(exe)
release: release_notest
echo && ./tests.sh
local:
gcc -g -O0 -Wall -o $(exe) -D STANDALONE -DSCRIPTDIR=\"$(mkfile_dir)/examples\" $(file_libs)
val:
$(GCC_COM) $(file_libs) && ./tests.sh -val
debug:
$(GCC_COM) -D DEBUG $(file_libs)

View File

@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#ifdef STANDALONE
size_t bytes = 0;
size_t getBytes()
@ -12,6 +13,9 @@ size_t getBytes()
return bytes;
}
#undef malloc
#undef calloc
void* smalloc(size_t size)
{
bytes += size;
@ -27,6 +31,7 @@ void* scalloc(size_t size, size_t count)
#define malloc(x) smalloc(x)
#define calloc(x, y) scalloc(x, y)
#endif
/**
* Returns the length of a given list Object
@ -313,7 +318,7 @@ char* stringObj(const Object* obj, size_t* length)
};
s.allocPoint[0] = '\0';
stringNObj(&s, obj);
*length = s.cursor - s.allocPoint;
*length += s.cursor - s.allocPoint;
return s.allocPoint;
}

View File

@ -134,6 +134,11 @@ struct string {
size_t capacity;
};
/**
* Returns a dynamically-sized string representation of the given object.
*
* _Adds_ the length of the new string to the `length` parameter.
*/
char* stringObj(const Object* obj, size_t* length);
void printType(const Object* obj);
@ -240,4 +245,11 @@ int getAllocations();
size_t getBytes();
void* scalloc(size_t size, size_t count);
void* smalloc(size_t size);
#define malloc(x) smalloc(x)
#define calloc(x, y) scalloc(x, y)
#endif

View File

@ -224,46 +224,28 @@ Object parseEvalO(Object* params, int length, struct Environment* env)
}
}
#ifdef STANDALONE
#define CAT_MAX 1024
#else
#define CAT_MAX 64
#endif
Object _catObjects(Object obj1, Object obj2, struct Environment* env)
{
Object evalObj1 = eval(&obj1, env);
Object evalObj2 = eval(&obj2, env);
size_t length1;
size_t length2;
char* str1 = stringObj(&evalObj1, &length1);
char* str2 = stringObj(&evalObj2, &length2);
cleanObject(&evalObj1);
cleanObject(&evalObj2);
Object o = withLen(length1 + length2, TYPE_STRING);
sprintf(o.string, "%s%s", str1, str2);
free(str1);
free(str2);
return o;
}
Object catObjects(Object* params, int length, struct Environment* env)
{
checkTypes(catObjects)
Object string = stringFromSlice("", 0);
if (length == 0) {
return string;
return stringFromSlice("", 0);
}
char* strings[length];
size_t totalLength = 0;
for (int i = 0; i < length; i++) {
Object newString = _catObjects(string, params[i], env);
cleanObject(&string);
string = newString;
strings[i] = stringObj(&params[i], &totalLength);
}
Object string = withLen(totalLength, TYPE_STRING);
string.string[0] = '\0';
for (int i = 0; i < length; i++) {
strcat(string.string, strings[i]);
free(strings[i]);
}
return string;
}
@ -332,14 +314,14 @@ Object equ(Object* params, int length, struct Environment* env)
if (length < 2) {
return errorObject(NOT_ENOUGH_ARGUMENTS);
}
int bool = 1;
for (int i = 0; i < length - 1; i++) {
if (!areEqual(&params[i], &params[i + 1])) {
bool = 0;
break;
return falseObject();
}
}
return boolObject(bool);
return trueObject();
}
Object or(Object* params, int length, struct Environment* env)
@ -347,14 +329,13 @@ Object or(Object* params, int length, struct Environment* env)
if (length < 2) {
return errorObject(NOT_ENOUGH_ARGUMENTS);
}
int bool = 0;
for (int i = 0; i < length - 1; i++) {
if (params[i].number || params[i + 1].number) {
bool = 1;
break;
return trueObject();
}
}
return boolObject(bool);
return falseObject();
}
#define BASIC_COMPARISON(NAME, OP)\
@ -363,14 +344,13 @@ Object NAME(Object* params, int length, struct Environment* env) \
if (length < 2) { \
return errorObject(NOT_ENOUGH_ARGUMENTS); \
} \
int bool = 1; \
\
for (int i = 0; i < length - 1; i++) { \
if (!(params[i].number OP params[i + 1].number)) { \
bool = 0; \
break; \
return falseObject(); \
} \
} \
return boolObject(bool); \
return trueObject(); \
}
BASIC_COMPARISON(greaterThan, >)