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:
parent
32e21bf758
commit
d2a0abeff3
18
src/Makefile
18
src/Makefile
|
@ -12,21 +12,21 @@ mkfile_dir := $(dir $(mkfile_path))
|
||||||
GCC_ARGS ?= -g -Wall -o $(exe) -D WEBSERVER -D STANDALONE -DSCRIPTDIR=\"$(SCRIPTDIR)\"
|
GCC_ARGS ?= -g -Wall -o $(exe) -D WEBSERVER -D STANDALONE -DSCRIPTDIR=\"$(SCRIPTDIR)\"
|
||||||
GCC_COM ?= gcc -O0 $(GCC_ARGS)
|
GCC_COM ?= gcc -O0 $(GCC_ARGS)
|
||||||
|
|
||||||
release:
|
all: notest
|
||||||
gcc -O3 $(GCC_ARGS) $(file_libs) && strip ./$(exe) && echo && ./tests.sh
|
echo && ./tests.sh
|
||||||
|
|
||||||
notest:
|
notest:
|
||||||
$(GCC_COM) $(file_libs)
|
$(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
|
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:
|
debug:
|
||||||
$(GCC_COM) -D DEBUG $(file_libs)
|
$(GCC_COM) -D DEBUG $(file_libs)
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef STANDALONE
|
||||||
size_t bytes = 0;
|
size_t bytes = 0;
|
||||||
|
|
||||||
size_t getBytes()
|
size_t getBytes()
|
||||||
|
@ -12,6 +13,9 @@ size_t getBytes()
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#undef malloc
|
||||||
|
#undef calloc
|
||||||
|
|
||||||
void* smalloc(size_t size)
|
void* smalloc(size_t size)
|
||||||
{
|
{
|
||||||
bytes += size;
|
bytes += size;
|
||||||
|
@ -27,6 +31,7 @@ void* scalloc(size_t size, size_t count)
|
||||||
|
|
||||||
#define malloc(x) smalloc(x)
|
#define malloc(x) smalloc(x)
|
||||||
#define calloc(x, y) scalloc(x, y)
|
#define calloc(x, y) scalloc(x, y)
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the length of a given list Object
|
* Returns the length of a given list Object
|
||||||
|
@ -313,7 +318,7 @@ char* stringObj(const Object* obj, size_t* length)
|
||||||
};
|
};
|
||||||
s.allocPoint[0] = '\0';
|
s.allocPoint[0] = '\0';
|
||||||
stringNObj(&s, obj);
|
stringNObj(&s, obj);
|
||||||
*length = s.cursor - s.allocPoint;
|
*length += s.cursor - s.allocPoint;
|
||||||
return s.allocPoint;
|
return s.allocPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
src/object.h
12
src/object.h
|
@ -134,6 +134,11 @@ struct string {
|
||||||
size_t capacity;
|
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);
|
char* stringObj(const Object* obj, size_t* length);
|
||||||
|
|
||||||
void printType(const Object* obj);
|
void printType(const Object* obj);
|
||||||
|
@ -240,4 +245,11 @@ int getAllocations();
|
||||||
|
|
||||||
size_t getBytes();
|
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
|
#endif
|
||||||
|
|
68
src/plfunc.c
68
src/plfunc.c
|
@ -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)
|
Object catObjects(Object* params, int length, struct Environment* env)
|
||||||
{
|
{
|
||||||
checkTypes(catObjects)
|
checkTypes(catObjects)
|
||||||
|
|
||||||
Object string = stringFromSlice("", 0);
|
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return string;
|
return stringFromSlice("", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* strings[length];
|
||||||
|
size_t totalLength = 0;
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
Object newString = _catObjects(string, params[i], env);
|
strings[i] = stringObj(¶ms[i], &totalLength);
|
||||||
cleanObject(&string);
|
|
||||||
string = newString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,14 +314,14 @@ Object equ(Object* params, int length, struct Environment* env)
|
||||||
if (length < 2) {
|
if (length < 2) {
|
||||||
return errorObject(NOT_ENOUGH_ARGUMENTS);
|
return errorObject(NOT_ENOUGH_ARGUMENTS);
|
||||||
}
|
}
|
||||||
int bool = 1;
|
|
||||||
for (int i = 0; i < length - 1; i++) {
|
for (int i = 0; i < length - 1; i++) {
|
||||||
if (!areEqual(¶ms[i], ¶ms[i + 1])) {
|
if (!areEqual(¶ms[i], ¶ms[i + 1])) {
|
||||||
bool = 0;
|
return falseObject();
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return boolObject(bool);
|
|
||||||
|
return trueObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
Object or(Object* params, int length, struct Environment* env)
|
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) {
|
if (length < 2) {
|
||||||
return errorObject(NOT_ENOUGH_ARGUMENTS);
|
return errorObject(NOT_ENOUGH_ARGUMENTS);
|
||||||
}
|
}
|
||||||
int bool = 0;
|
|
||||||
for (int i = 0; i < length - 1; i++) {
|
for (int i = 0; i < length - 1; i++) {
|
||||||
if (params[i].number || params[i + 1].number) {
|
if (params[i].number || params[i + 1].number) {
|
||||||
bool = 1;
|
return trueObject();
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return boolObject(bool);
|
return falseObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BASIC_COMPARISON(NAME, OP)\
|
#define BASIC_COMPARISON(NAME, OP)\
|
||||||
|
@ -363,14 +344,13 @@ Object NAME(Object* params, int length, struct Environment* env) \
|
||||||
if (length < 2) { \
|
if (length < 2) { \
|
||||||
return errorObject(NOT_ENOUGH_ARGUMENTS); \
|
return errorObject(NOT_ENOUGH_ARGUMENTS); \
|
||||||
} \
|
} \
|
||||||
int bool = 1; \
|
\
|
||||||
for (int i = 0; i < length - 1; i++) { \
|
for (int i = 0; i < length - 1; i++) { \
|
||||||
if (!(params[i].number OP params[i + 1].number)) { \
|
if (!(params[i].number OP params[i + 1].number)) { \
|
||||||
bool = 0; \
|
return falseObject(); \
|
||||||
break; \
|
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
return boolObject(bool); \
|
return trueObject(); \
|
||||||
}
|
}
|
||||||
|
|
||||||
BASIC_COMPARISON(greaterThan, >)
|
BASIC_COMPARISON(greaterThan, >)
|
||||||
|
|
Loading…
Reference in New Issue