Compare commits

...

5 Commits

8 changed files with 242 additions and 110 deletions

View File

@@ -7,10 +7,30 @@
extern "C" {
#endif
// Macro for easily returning errors
#define ERROR(what, type) return createErrorGroundValue(createGroundError(what, type, NULL, NULL))
// Forward declaration of the scope structure used in interpreter
struct GroundScope;
typedef struct GroundScope GroundScope;
/*
* Stores data associated with an error thrown during Ground execution.
*/
typedef struct GroundError {
char* what;
char* type;
struct GroundInstruction* where;
size_t line;
bool hasLine;
} GroundError;
// Creates a GroundValue containing (in), with type ERROR.
GroundValue createErrorGroundValue(GroundError in);
// Creates a GroundError.
GroundError createGroundError(char* what, char* type, GroundInstruction* where, size_t* line);
/*
* Function pointer type for native functions.
* scope: The current execution scope (opaque).

View File

@@ -10,7 +10,7 @@ GroundValue native_file_read(GroundScope* scope, List args) {
char* path = args.values[0].data.stringVal;
FILE* f = fopen(path, "r");
if (!f) {
return groundCreateValue(NONE);
ERROR("Failed to open file for reading", "FileError");
}
fseek(f, 0, SEEK_END);
@@ -20,7 +20,7 @@ GroundValue native_file_read(GroundScope* scope, List args) {
char* content = malloc(fsize + 1);
if (!content) {
fclose(f);
return groundCreateValue(NONE);
ERROR("Failed to allocate memory for file reading", "FileError");
}
fread(content, 1, fsize, f);
@@ -42,7 +42,7 @@ GroundValue native_file_write(GroundScope* scope, List args) {
FILE* f = fopen(path, "w");
if (!f) {
return groundCreateValue(BOOL, 0);
ERROR("Failed to open file for writing", "FileError");
}
fprintf(f, "%s", content);

77
libs/math/math.c Normal file
View File

@@ -0,0 +1,77 @@
#include <groundext.h>
#include <math.h>
GroundValue ground_sin(GroundScope* scope, List args) {
return groundCreateValue(
DOUBLE,
sin(args.values[0].data.doubleVal)
);
}
GroundValue ground_cos(GroundScope* scope, List args) {
return groundCreateValue(
DOUBLE,
cos(args.values[0].data.doubleVal)
);
}
GroundValue ground_tan(GroundScope* scope, List args) {
return groundCreateValue(
DOUBLE,
tan(args.values[0].data.doubleVal)
);
}
GroundValue rad_to_deg(GroundScope* scope, List args) {
double radians = args.values[0].data.doubleVal;
return groundCreateValue(
DOUBLE,
radians * (180.0 / M_PI)
);
}
GroundValue deg_to_rad(GroundScope* scope, List args) {
double deg = args.values[0].data.doubleVal;
return groundCreateValue(
DOUBLE,
deg * (M_PI / 180.0)
);
}
GroundValue ground_modulos(GroundScope* scope, List args) {
return groundCreateValue(
DOUBLE,
fmod(args.values[0].data.doubleVal, args.values[1].data.doubleVal)
);
}
GroundValue ground_pow(GroundScope* scope, List args) {
return groundCreateValue(
DOUBLE,
pow(args.values[0].data.doubleVal, args.values[1].data.doubleVal)
);
}
GroundValue ground_sqrt(GroundScope* scope, List args) {
double value = args.values[0].data.doubleVal;
if (value < 0) {
ERROR("Can't get square root of number less than 0.", "InvalidMath");
}
return groundCreateValue(
DOUBLE,
sqrt(value)
);
}
void ground_init(GroundScope* scope) {
groundAddNativeFunction(scope, "math_Sin", ground_sin, DOUBLE, 1, DOUBLE, "radians");
groundAddNativeFunction(scope, "math_Cos", ground_cos, DOUBLE, 1, DOUBLE, "radians");
groundAddNativeFunction(scope, "math_Tan", ground_tan, DOUBLE, 1, DOUBLE, "radians");
groundAddNativeFunction(scope, "math_DegreesToRadians", deg_to_rad, DOUBLE, 1, DOUBLE, "degrees");
groundAddNativeFunction(scope, "math_RadiansToDegrees", rad_to_deg, DOUBLE, 1, DOUBLE, "radians");
groundAddNativeFunction(scope, "math_Modulos", ground_modulos, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
groundAddNativeFunction(scope, "math_Pow", ground_pow, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
groundAddNativeFunction(scope, "math_Sqrt", ground_sqrt, DOUBLE, 1, DOUBLE, "number");
}

View File

@@ -50,21 +50,11 @@ size_t write_callback(void* ptr, size_t size, size_t nmemb, void* userdata) {
// GET request
GroundValue get_request(GroundScope* scope, List args) {
if (args.size < 1) {
printf("Requires a URL argument\n");
return groundCreateValue(NONE);
}
if (args.values[0].type != STRING) {
printf("Arg 1 needs to be a string (URL)\n");
return groundCreateValue(NONE);
}
curl_setup();
CURL* handle = curl_easy_init();
if (!handle) {
printf("Curl failed to init\n");
return groundCreateValue(NONE);
ERROR("CURL failed to init", "GenericRequestError");
}
ResponseBuffer buffer = {0};
@@ -81,10 +71,9 @@ GroundValue get_request(GroundScope* scope, List args) {
CURLcode result = curl_easy_perform(handle);
if (result != CURLE_OK) {
printf("Curl request failed: %s\n", curl_easy_strerror(result));
free(buffer.data);
curl_easy_cleanup(handle);
return groundCreateValue(NONE);
char buf[256];
snprintf(buf, sizeof(buf) - 1, "Curl request failed: %s\n", curl_easy_strerror(result));
ERROR(buf, "ActionRequestError");
}
curl_easy_cleanup(handle);
@@ -95,21 +84,11 @@ GroundValue get_request(GroundScope* scope, List args) {
// POST request
GroundValue post_request(GroundScope* scope, List args) {
if (args.size < 2) {
printf("Requires URL and POST data arguments\n");
return groundCreateValue(NONE);
}
if (args.values[0].type != STRING || args.values[1].type != STRING) {
printf("Both arguments need to be strings (URL, POST data)\n");
return groundCreateValue(NONE);
}
curl_setup();
CURL* handle = curl_easy_init();
if (!handle) {
printf("Curl failed to init\n");
return groundCreateValue(NONE);
ERROR("CURL failed to init", "GenericRequestError");
}
ResponseBuffer buffer = {0};
@@ -127,10 +106,11 @@ GroundValue post_request(GroundScope* scope, List args) {
CURLcode result = curl_easy_perform(handle);
if (result != CURLE_OK) {
printf("Curl POST request failed: %s\n", curl_easy_strerror(result));
free(buffer.data);
char buf[256];
snprintf(buf, sizeof(buf) - 1, "Curl request failed: %s\n", curl_easy_strerror(result));
curl_easy_cleanup(handle);
return groundCreateValue(NONE);
ERROR(buf, "ActionRequestError");
}
curl_easy_cleanup(handle);
@@ -153,27 +133,16 @@ int find_ws_slot() {
// WebSocket Connect - returns connection ID as INT
GroundValue ws_connect(GroundScope* scope, List args) {
if (args.size < 1) {
printf("Requires a WebSocket URL argument (ws:// or wss://)\n");
return groundCreateValue(NONE);
}
if (args.values[0].type != STRING) {
printf("Arg 1 needs to be a string (WebSocket URL)\n");
return groundCreateValue(NONE);
}
curl_setup();
int slot = find_ws_slot();
if (slot == -1) {
printf("Maximum WebSocket connections reached\n");
return groundCreateValue(NONE);
ERROR("Maximum WebSocket connections reached", "OverflowError");
}
CURL* handle = curl_easy_init();
if (!handle) {
printf("Curl failed to init\n");
return groundCreateValue(NONE);
ERROR("CURL failed to init", "GenericWSError");
}
curl_easy_setopt(handle, CURLOPT_URL, args.values[0].data.stringVal);
@@ -183,9 +152,10 @@ GroundValue ws_connect(GroundScope* scope, List args) {
CURLcode result = curl_easy_perform(handle);
if (result != CURLE_OK) {
printf("WebSocket connection failed: %s\n", curl_easy_strerror(result));
char buf[256];
snprintf(buf, sizeof(buf) - 1, "Curl WebSocket failed: %s\n", curl_easy_strerror(result));
curl_easy_cleanup(handle);
return groundCreateValue(NONE);
ERROR(buf, "ActionWSError");
}
ws_connections[slot].handle = handle;
@@ -197,19 +167,9 @@ GroundValue ws_connect(GroundScope* scope, List args) {
// WebSocket Send - sends a text message, returns bytes sent as INT
GroundValue ws_send(GroundScope* scope, List args) {
if (args.size < 2) {
printf("Requires connection ID and message\n");
return groundCreateValue(NONE);
}
if (args.values[0].type != INT || args.values[1].type != STRING) {
printf("Args need to be (INT connection ID, STRING message)\n");
return groundCreateValue(NONE);
}
int conn_id = (int)args.values[0].data.intVal;
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
printf("Invalid or closed WebSocket connection\n");
return groundCreateValue(NONE);
ERROR("Invalid WebSocket connection ID", "GenericWSError");
}
CURL* handle = ws_connections[conn_id].handle;
@@ -219,8 +179,9 @@ GroundValue ws_send(GroundScope* scope, List args) {
CURLcode result = curl_ws_send(handle, message, strlen(message), &sent, 0, CURLWS_TEXT);
if (result != CURLE_OK) {
printf("WebSocket send failed: %s\n", curl_easy_strerror(result));
return groundCreateValue(NONE);
char buf[256];
snprintf(buf, sizeof(buf) - 1, "WebSocket send failed: %s\n", curl_easy_strerror(result));
ERROR(buf, "ActionWSError");
}
return groundCreateValue(INT, (int64_t)sent);
@@ -228,19 +189,9 @@ GroundValue ws_send(GroundScope* scope, List args) {
// WebSocket Receive - receives a message (blocking)
GroundValue ws_receive(GroundScope* scope, List args) {
if (args.size < 1) {
printf("Requires connection ID\n");
return groundCreateValue(NONE);
}
if (args.values[0].type != INT) {
printf("Arg 1 needs to be an INT (connection ID)\n");
return groundCreateValue(NONE);
}
int conn_id = (int)args.values[0].data.intVal;
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
printf("Invalid or closed WebSocket connection\n");
return groundCreateValue(NONE);
ERROR("Invalid WebSocket connection ID", "GenericWSError");
}
CURL* handle = ws_connections[conn_id].handle;
@@ -255,8 +206,9 @@ GroundValue ws_receive(GroundScope* scope, List args) {
// No data available right now
return groundCreateValue(STRING, "");
}
printf("WebSocket receive failed: %s\n", curl_easy_strerror(result));
return groundCreateValue(NONE);
char buf[256];
snprintf(buf, sizeof(buf) - 1, "WebSocket receive failed: %s\n", curl_easy_strerror(result));
ERROR(buf, "ActionWSError");
}
buffer[received] = '\0';
@@ -283,21 +235,11 @@ GroundValue ws_receive(GroundScope* scope, List args) {
// WebSocket Receive with timeout (non-blocking version)
GroundValue ws_receive_timeout(GroundScope* scope, List args) {
if (args.size < 2) {
printf("Requires connection ID and timeout in milliseconds\n");
return groundCreateValue(NONE);
}
if (args.values[0].type != INT || args.values[1].type != INT) {
printf("Both arguments need to be INT (connection ID, timeout_ms)\n");
return groundCreateValue(NONE);
}
int conn_id = (int)args.values[0].data.intVal;
long timeout_ms = (long)args.values[1].data.intVal;
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
printf("Invalid or closed WebSocket connection\n");
return groundCreateValue(NONE);
ERROR("Invalid WebSocket connection ID", "GenericWSError");
}
CURL* handle = ws_connections[conn_id].handle;
@@ -316,8 +258,9 @@ GroundValue ws_receive_timeout(GroundScope* scope, List args) {
}
if (result != CURLE_OK) {
printf("WebSocket receive failed: %s\n", curl_easy_strerror(result));
return groundCreateValue(NONE);
char buf[256];
snprintf(buf, sizeof(buf) - 1, "WebSocket receive failed: %s\n", curl_easy_strerror(result));
ERROR(buf, "ActionWSError");
}
buffer[received] = '\0';
@@ -341,19 +284,9 @@ GroundValue ws_receive_timeout(GroundScope* scope, List args) {
// WebSocket Close - properly close a connection, returns BOOL success
GroundValue ws_close(GroundScope* scope, List args) {
if (args.size < 1) {
printf("Requires connection ID\n");
return groundCreateValue(NONE);
}
if (args.values[0].type != INT) {
printf("Arg 1 needs to be an INT (connection ID)\n");
return groundCreateValue(NONE);
}
int conn_id = (int)args.values[0].data.intVal;
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
printf("Invalid or already closed WebSocket connection\n");
return groundCreateValue(BOOL, false);
ERROR("Invalid or already connected websocket", "GenericWSError");
}
CURL* handle = ws_connections[conn_id].handle;
@@ -374,19 +307,9 @@ GroundValue ws_close(GroundScope* scope, List args) {
// WebSocket Send Binary data, returns bytes sent as INT
GroundValue ws_send_binary(GroundScope* scope, List args) {
if (args.size < 2) {
printf("Requires connection ID and binary data\n");
return groundCreateValue(NONE);
}
if (args.values[0].type != INT || args.values[1].type != STRING) {
printf("Args need to be (INT connection ID, STRING binary data)\n");
return groundCreateValue(NONE);
}
int conn_id = (int)args.values[0].data.intVal;
if (conn_id < 0 || conn_id >= MAX_WS_CONNECTIONS || !ws_connections[conn_id].connected) {
printf("Invalid or closed WebSocket connection\n");
return groundCreateValue(NONE);
ERROR("Invalid WebSocket connection ID", "GenericWSError");
}
CURL* handle = ws_connections[conn_id].handle;
@@ -396,8 +319,9 @@ GroundValue ws_send_binary(GroundScope* scope, List args) {
CURLcode result = curl_ws_send(handle, data, strlen(data), &sent, 0, CURLWS_BINARY);
if (result != CURLE_OK) {
printf("WebSocket binary send failed: %s\n", curl_easy_strerror(result));
return groundCreateValue(NONE);
char buf[256];
snprintf(buf, sizeof(buf) - 1, "WebSocket binary send failed: %s\n", curl_easy_strerror(result));
ERROR(buf, "ActionWSError");
}
return groundCreateValue(INT, (int64_t)sent);

View File

@@ -11,6 +11,8 @@
int currentInstruction = 0;
bool isMainScopeGlobal = true;
[[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine) {
printf("Ground runtime error:\n ErrorType: ");
switch (error) {
@@ -78,6 +80,22 @@ int currentInstruction = 0;
exit(1);
}
[[noreturn]] void throwError(GroundError* error) {
printf("Uncaught Ground runtime error:\n ErrorType: %s\n", error->type);
if (error->what != NULL) {
printf(" ErrorContext: %s\n", error->what);
}
if (error->where != NULL) {
printf(" ErrorInstruction: ");
printGroundInstruction(error->where);
printf("\n");
}
if (error->hasLine) {
printf(" ErrorLine: %zu\n", error->line + 1);
}
exit(1);
}
GroundLabel* findLabel(GroundLabel* head, const char *id) {
GroundLabel *item;
HASH_FIND_STR(head, id, item);
@@ -438,6 +456,8 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
scope.labels = &labels;
scope.variables = &variables;
}
scope.isMainScope = isMainScopeGlobal;
isMainScopeGlobal = false;
// Preprocess all labels, structs and functions
for (size_t i = 0; i < in->size; i++) {
if (in->instructions[i].type == CREATELABEL) {
@@ -872,6 +892,10 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("none"));
break;
}
case ERROR: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("error"));
break;
}
}
break;
@@ -1737,6 +1761,15 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
if (function->nativeFn) {
GroundValue returnValue = function->nativeFn(scope, argsList);
if (returnValue.type == ERROR) {
returnValue.data.errorVal.line = currentInstruction;
returnValue.data.errorVal.hasLine = true;
returnValue.data.errorVal.where = in;
if (scope->isMainScope) {
throwError(&returnValue.data.errorVal);
}
return returnValue;
}
if (returnValue.type != function->returnType) {
runtimeError(RETURN_TYPE_MISMATCH, "Unexpected return value type from native function", in, currentInstruction);
}
@@ -1756,6 +1789,9 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
size_t currentCurrentInstruction = currentInstruction;
currentInstruction = function->startLine;
GroundValue returnValue = interpretGroundProgram(&function->program, &newScope);
if (returnValue.type == ERROR) {
return returnValue;
}
if (returnValue.type != function->returnType) {
runtimeError(RETURN_TYPE_MISMATCH, "Unexpected return value type from function", in, currentInstruction);
}

View File

@@ -29,6 +29,7 @@ typedef struct GroundVariable {
typedef struct GroundScope {
GroundLabel** labels;
GroundVariable** variables;
bool isMainScope;
} GroundScope;
typedef struct GroundDebugInstruction {

View File

@@ -51,6 +51,13 @@ GroundValue createFunctionGroundValue(GroundFunction* in) {
return gv;
}
GroundValue createErrorGroundValue(GroundError in) {
GroundValue gv;
gv.data.errorVal = in;
gv.type = ERROR;
return gv;
}
GroundValue createNoneGroundValue() {
GroundValue gv;
gv.type = NONE;
@@ -168,6 +175,18 @@ void freeGroundValue(GroundValue* gv) {
freeGroundStruct(gstruct);
free(gstruct);
}
if (gv->type == ERROR) {
GroundError* error = &gv->data.errorVal;
if (error->type != NULL) {
free(error->type);
}
if (error->what != NULL) {
free(error->what);
}
if (error->where != NULL) {
freeGroundInstruction(error->where);
}
}
}
GroundArg createValueGroundArg(GroundValue value) {
@@ -529,3 +548,37 @@ void freeGroundObject(GroundObject* object) {
}
object->fields = NULL;
}
GroundError createGroundError(char* what, char* type, GroundInstruction* where, size_t* line) {
GroundError ge;
if (what != NULL) {
ge.what = malloc(strlen(what) + 1);
strcpy(ge.what, what);
} else {
ge.what = NULL;
}
if (type != NULL) {
ge.type = malloc(strlen(type) + 1);
strcpy(ge.type, type);
} else {
ge.type = malloc(strlen("GenericError") + 1);
strcpy(ge.type, "GenericError");
}
if (where != NULL) {
ge.where = malloc(sizeof(GroundInstruction));
*ge.where = copyGroundInstruction(where);
} else {
ge.where = NULL;
}
if (line != NULL) {
ge.line = *line;
} else {
ge.line = 0;
ge.hasLine = false;
}
return ge;
}

View File

@@ -13,7 +13,7 @@ typedef enum GroundInstType {
} GroundInstType;
typedef enum GroundValueType {
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, STRUCTVAL, CUSTOM, NONE
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, STRUCTVAL, CUSTOM, ERROR, NONE
} GroundValueType;
typedef enum GroundArgType {
@@ -29,6 +29,7 @@ struct GroundFunction;
struct GroundScope;
struct GroundStruct;
struct GroundObject;
struct GroundInstruction;
struct List;
@@ -42,6 +43,17 @@ typedef struct List {
struct GroundValue* values;
} List;
/*
* Stores data associated with an error thrown during Ground execution.
*/
typedef struct GroundError {
char* what;
char* type;
struct GroundInstruction* where;
size_t line;
bool hasLine;
} GroundError;
/*
* Stores literal values created in a Ground program.
* Associated functions:
@@ -57,6 +69,7 @@ typedef struct GroundValue {
char charVal;
bool boolVal;
List listVal;
GroundError errorVal;
struct GroundFunction* fnVal;
struct GroundStruct* structVal;
struct GroundObject* customVal;
@@ -187,6 +200,9 @@ GroundValue createListGroundValue(List in);
// Creates a GroundValue conatining (in), with type FUNCTION.
GroundValue createFunctionGroundValue(GroundFunction* in);
// Creates a GroundValue containing (in), with type ERROR.
GroundValue createErrorGroundValue(GroundError in);
// Creates a Groundvalue with type NONE.
GroundValue createNoneGroundValue();
@@ -257,4 +273,9 @@ GroundObjectField* findField(GroundObject head, const char *id);
// Frees a GroundObject
void freeGroundObject(GroundObject* object);
// Creates a GroundError.
GroundError createGroundError(char* what, char* type, GroundInstruction* where, size_t* line);
#endif