Compare commits

42 Commits

Author SHA1 Message Date
017d0eb273 Merge pull request 'Update unstable with master' (#18) from master into unstable
Reviewed-on: #18
2026-01-23 14:42:36 +11:00
e8c49508a0 Band-aid fix for structs, use more memory :\ 2026-01-23 13:48:18 +11:00
68182d916e Satisfy Leo's thirst for structs and extlibs 2026-01-22 20:58:49 +11:00
3f684dad3f I'm tired of ragebaiting Leo 2026-01-22 20:40:27 +11:00
03aab86413 Fix the fix? 2026-01-22 20:36:19 +11:00
f9352cf59f Start work on floats 2026-01-22 19:09:26 +11:00
3afb59929a Add int, char, and bool to compiler 2026-01-22 14:22:12 +11:00
0c2abbae2f Start working on allowing more types in compiler 2026-01-22 14:10:26 +11:00
955e75addb Merge pull request 'Pull stuff in again' (#17) from master into unstable
Reviewed-on: #17
2026-01-22 14:07:37 +11:00
c382ecd867 Fix the header again 2026-01-22 10:50:48 +11:00
228d2ac762 I forgot the header again lmao 2026-01-21 18:59:24 +11:00
b289448f56 Add struct field access 2026-01-21 18:41:24 +11:00
a3ca979133 Add not to compiler 2026-01-21 16:02:13 +11:00
3e9ce0dfc0 Expose compiler in groundvm.h 2026-01-21 15:54:18 +11:00
063e85d24a Merge pull request 'Add compiler' (#14) from unstable into master
Reviewed-on: #14
2026-01-21 15:53:20 +11:00
07474d4317 Add print and println to compiler 2026-01-21 15:20:00 +11:00
51639e904d Add control flow 2026-01-21 14:35:48 +11:00
c728801bc3 Add comparisons 2026-01-21 14:10:34 +11:00
e3c8a2f453 Add multiply and subtract 2026-01-21 14:02:05 +11:00
bf68f1500c Add add instruction to compiler 2026-01-21 13:50:54 +11:00
31577fcc62 Add set to compiler 2026-01-21 13:25:13 +11:00
925077d55e Merge branch 'master' into unstable 2026-01-21 12:20:51 +11:00
32d6a029dd trying to fix stuff 2026-01-21 11:55:42 +11:00
dac983b684 Update compiler 2026-01-21 11:38:37 +11:00
d3c03b4987 Better cmdline args, start work on compiler 2026-01-21 11:17:19 +11:00
0f155c80be ground library version 1.1.5 2026-01-21 07:23:24 +11:00
4680597065 update to math library (v1.1.0) 2026-01-20 21:29:42 +11:00
c6762a7966 extlibs can now add variables and structs! 2026-01-20 21:26:40 +11:00
792aed13ae Balright time to break master again 2026-01-20 21:17:08 +11:00
08b1edd7a7 Update include header 2026-01-20 20:17:26 +11:00
8eef78c310 Merge pull request 'master' (#11) from master into unstable
Reviewed-on: #11
2026-01-20 20:15:00 +11:00
ec23d55f9d Fix bug about passing objs to functions 2026-01-20 19:57:49 +11:00
724162f42e Refactor fn arg checking 2026-01-20 19:56:14 +11:00
6d0dd99406 Merge pull request 'Error function' (#10) from DiamondNether90/cground:testing into master
Reviewed-on: #10
2026-01-19 21:21:25 +11:00
7717a40574 Error function 2026-01-19 21:14:48 +11:00
bbf2277b6f Merge pull request 'Add math library' (#9) from unstable into master
Reviewed-on: #9
2026-01-19 20:44:56 +11:00
fd6fbbaed5 Merge pull request 'math library' (#8) from math-branch into unstable
Reviewed-on: #8
2026-01-19 20:44:04 +11:00
f2968f74b3 added math library 2026-01-19 20:32:29 +11:00
86096d6113 Merge pull request 'Better library stuff' (#7) from unstable into master
Reviewed-on: #7
2026-01-19 20:01:56 +11:00
054af1631c I forgot to push header file 2026-01-19 19:29:14 +11:00
754b63fb75 Update error handling inside extlibs 2026-01-18 21:37:46 +11:00
72162a7410 Start work on error handling 2026-01-18 20:49:50 +11:00
18 changed files with 1719 additions and 123 deletions

View File

@@ -82,7 +82,7 @@ build
- [x] Return values (type checked)
- [x] Arguments for functions
- [x] Jumping within functions
- [ ] Custom data structures
- [x] Custom data structures
- [ ] Working with external libraries
## Debugger
@@ -98,3 +98,26 @@ Commands:
* eval (code): Runs Ground code in the current scope
* help: Shows a help message
## Compiler
CGround now includes an experimental Ground -> x86_64 Linux ASM compiler. You can try it by adding the `-c` flag when running Ground. This will print the generated ASM to the console.
At present only the `int`, `char`, and `bool` data types are supported.
Supported instructions so far:
* set
* add
* subtract
* multiply
* equal
* inequal
* greater
* lesser
* not
* jump
* if
* @ (label creation)
* print
* println
* end

View File

@@ -7,10 +7,19 @@
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;
// 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

@@ -11,13 +11,14 @@
#include <stdbool.h>
#include <stdlib.h>
#include <stdarg.h>
#include <uthash.h>
typedef enum GroundInstType {
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD
} GroundInstType;
typedef enum GroundValueType {
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, CUSTOM, NONE
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, STRUCTVAL, CUSTOM, ERROR, NONE
} GroundValueType;
typedef enum GroundArgType {
@@ -30,6 +31,7 @@ typedef enum ListAccessStatus {
struct GroundValue;
struct GroundFunction;
struct GroundStruct;
struct List;
@@ -41,20 +43,34 @@ 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.
*/
typedef struct GroundValue {
GroundValueType type;
union {
struct GroundStruct* customType;
struct {
int64_t intVal;
double doubleVal;
char* stringVal;
char charVal;
bool boolVal;
List listVal;
GroundError errorVal;
struct GroundFunction* fnVal;
void* customVal;
struct GroundStruct* structVal;
struct GroundObject* customVal;
} data;
} GroundValue;
@@ -115,6 +131,39 @@ typedef struct GroundFunction {
size_t startLine;
} GroundFunction;
/*
* Field for a GroundStruct
*/
typedef struct GroundStructField {
char id[64];
GroundValue value;
UT_hash_handle hh;
} GroundStructField;
/*
* Represents a Ground struct.
*/
typedef struct GroundStruct {
GroundStructField* fields;
size_t size;
} GroundStruct;
/*
* Field for a GroundObject
*/
typedef struct GroundObjectField {
char id[64];
GroundValue value;
UT_hash_handle hh;
} GroundObjectField;
/*
* Represents an initialised Ground struct.
*/
typedef struct GroundObject {
GroundObjectField* fields;
} GroundObject;
#ifdef __cplusplus
extern "C" {
#endif
@@ -123,6 +172,7 @@ GroundProgram groundCreateProgram();
void groundAddInstructionToProgram(GroundProgram* program, GroundInstruction instruction);
GroundValue groundRunProgram(GroundProgram* program);
void groundPrintProgram(GroundProgram* program);
char* groundCompileProgram(GroundProgram* program);
GroundInstruction groundCreateInstruction(GroundInstType type);
void groundAddValueToInstruction(GroundInstruction* inst, GroundValue value);
@@ -133,6 +183,9 @@ GroundValue groundCreateValue(GroundValueType type, ...);
GroundProgram groundParseFile(const char* code);
GroundStruct groundCreateStruct();
void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field);
#ifdef __cplusplus
}
#endif

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);

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

@@ -0,0 +1,213 @@
#include <groundext.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <float.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) {
return groundCreateValue(
DOUBLE,
args.values[0].data.doubleVal * (180.0 / M_PI)
);
}
GroundValue deg_to_rad(GroundScope* scope, List args) {
return groundCreateValue(
DOUBLE,
args.values[0].data.doubleVal * (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)
);
}
GroundValue ground_abs(GroundScope* scope, List args) {
return groundCreateValue(
DOUBLE,
abs(args.values[0].data.doubleVal)
);
}
GroundValue ground_min(GroundScope* scope, List args) {
double a = args.values[0].data.doubleVal;
double b = args.values[1].data.doubleVal;
return groundCreateValue(
DOUBLE,
(a < b) ? a : b
);
}
GroundValue ground_max(GroundScope* scope, List args) {
double a = args.values[0].data.doubleVal;
double b = args.values[1].data.doubleVal;
return groundCreateValue(
DOUBLE,
(a > b) ? a : b
);
}
GroundValue ground_clamp(GroundScope* scope, List args) {
double number = args.values[0].data.doubleVal;
double min = args.values[1].data.doubleVal;
double max = args.values[2].data.doubleVal;
if (number < min) number = min;
if (number > max) number = max;
return groundCreateValue(
DOUBLE,
number
);
}
GroundValue ground_round(GroundScope* scope, List args) {
double x = args.values[0].data.doubleVal;
if (x >= 0.0)
return groundCreateValue(
INT,
(long long)(x + 0.5)
);
else
return groundCreateValue(
INT,
(long long)(x - 0.5)
);
}
GroundValue ground_floor(GroundScope* scope, List args) {
double x = args.values[0].data.doubleVal;
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
long long i = (long long)x; // truncates toward zero
if (x < 0 && x != (double)i) {
return groundCreateValue(
INT,
i-1
);
}
return groundCreateValue(
INT,
i
);
}
GroundValue ground_ceil(GroundScope* scope, List args) {
double x = args.values[0].data.doubleVal;
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
long long i = (long long)x; // truncates toward zero
if (x > 0 && x != (double)i) {
return groundCreateValue(
INT,
i+1
);
}
return groundCreateValue(
INT,
i
);
}
GroundValue ground_random(GroundScope* scope, List args) {
int64_t min = args.values[0].data.intVal;
int64_t max = args.values[1].data.intVal;
return groundCreateValue(
INT,
min + rand() % (max - min)
);
}
GroundValue ground_random_double(GroundScope* scope, List args) {
double min = args.values[0].data.doubleVal;
double max = args.values[1].data.doubleVal;
return groundCreateValue(
DOUBLE,
min + (double)rand() / RAND_MAX * (max - min)
);
}
GroundValue ground_random_set_seed(GroundScope* scope, List args) {
srand(args.values[0].data.intVal);
return groundCreateValue(
INT,
0
);
}
void ground_init(GroundScope* scope) {
srand((unsigned)time(NULL) ^ (unsigned)clock());
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");
groundAddNativeFunction(scope, "math_Abs", ground_abs, DOUBLE, 1, DOUBLE, "number");
groundAddNativeFunction(scope, "math_Min", ground_min, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
groundAddNativeFunction(scope, "math_Max", ground_max, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
groundAddNativeFunction(scope, "math_Clamp", ground_clamp, DOUBLE, 2, DOUBLE, "number", DOUBLE, "min", DOUBLE, "max");
groundAddNativeFunction(scope, "math_Round", ground_round, INT, 1, DOUBLE, "number");
groundAddNativeFunction(scope, "math_Floor", ground_floor, INT, 1, DOUBLE, "number");
groundAddNativeFunction(scope, "math_Ceil", ground_ceil, INT, 1, DOUBLE, "number");
groundAddNativeFunction(scope, "math_Random", ground_random, INT, 2, INT, "min", INT, "max");
groundAddNativeFunction(scope, "math_RandomDouble", ground_random_double, DOUBLE, 2, DOUBLE, "min", DOUBLE, "max");
groundAddNativeFunction(scope, "math_RandomSetSeed", ground_random_set_seed, INT, 1, INT, "seed");
}

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);

922
src/compiler.c Normal file
View File

@@ -0,0 +1,922 @@
#include "compiler.h"
#include "interpreter.h"
#include "types.h"
#include "include/estr.h"
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
VariableTable createVariableTable() {
VariableTable vt;
vt.capacity = 16;
vt.count = 0;
vt.vars = malloc(sizeof(VariableInfo) * vt.capacity);
return vt;
}
void addVtVariable(VariableTable* vt, const char* name, GroundValueType type) {
// check if it already exists
for (size_t i = 0; i < vt->count; i++) {
if (strcmp(vt->vars[i].name, name) == 0) {
return;
}
}
// increase capacity
if (vt->count >= vt->capacity) {
vt->capacity *= 2;
vt->vars = realloc(vt->vars, sizeof(VariableInfo) * vt->capacity);
}
// add the variable
strcpy(vt->vars[vt->count].name, name);
vt->vars[vt->count].type = type;
vt->vars[vt->count].offset = vt->count * 8;
vt->count++;
}
VariableInfo* getVariable(VariableTable* vt, const char* name) {
for (size_t i = 0; i < vt->count; i++) {
if (strcmp(vt->vars[i].name, name) == 0) {
return &vt->vars[i];
}
}
return NULL;
}
int getVariablePos(VariableTable* vt, const char* name) {
for (size_t i = 0; i < vt->count; i++) {
if (strcmp(vt->vars[i].name, name) == 0) {
return vt->vars[i].offset;
}
}
return -1;
}
DoubleTable createDoubleTable() {
DoubleTable dt;
dt.count = 0;
dt.capacity = 16;
dt.constants = malloc(sizeof(DoubleConstant) * 16);
return dt;
}
char* addDoubleConstant(DoubleTable* dt, Estr* rodata, double value, int* counter) {
for (size_t i = 0; i < dt->count; i++) {
if (dt->constants[i].value == value) {
return dt->constants[i].label;
}
}
if (dt->count >= dt->capacity) {
dt->capacity = dt->capacity == 0 ? 16 : dt->capacity * 2;
dt->constants = realloc(dt->constants, sizeof(DoubleConstant) * dt->capacity);
}
char label[64];
snprintf(label, sizeof(label), "float_const_%d", (*counter)++);
strcpy(dt->constants[dt->count].label, label);
dt->constants[dt->count].value = value;
dt->count++;
char line[128];
snprintf(line, 128, "%s: dq %.17g ; double constant\n", label, value);
APPEND_ESTR((*rodata), line);
return dt->constants[dt->count - 1].label;
}
GroundValueType getInstructionReturnType(GroundInstruction* gi, VariableTable* table) {
switch (gi->type) {
case JUMP:
case PRINT:
case PRINTLN:
case END:
case IF:
case RETURN:
case ENDFUN:
case PUSHARG:
case CALL: // FIXME do this when functions are added
case USE:
case EXTERN:
case CREATELABEL:
case PAUSE:
case DROP:
case ERRORCMD:
case ENDSTRUCT:
case INIT: // FIXME do this when objects are added
case GETFIELD: // FIXME do this when objects are added
case SETFIELD: // FIXME do this when objects are added
case DIVIDE: // FIXME do this when division is added
case SETLISTAT:
case GETLISTAT: // FIXME do this when lists are getting implemented
case LISTAPPEND: {
return NONE;
}
case TOSTRING:
case GETTYPE:
case INPUT: {
return STRING;
}
case STOI:
case GETLISTSIZE:
case GETSTRSIZE: {
return INT;
}
case STOD: {
return DOUBLE;
}
case EQUAL:
case INEQUAL:
case GREATER:
case LESSER:
case NOT:
case EXISTS: {
return BOOL;
}
case GETSTRCHARAT: {
return CHAR;
}
case SETLIST: {
return LIST;
}
case FUN: {
return FUNCTION;
}
case STRUCT: {
return STRUCTVAL;
}
case SET: {
if (gi->args.length == 2) {
if (gi->args.args[1].type == VALUE) {
return gi->args.args[1].value.value.type;
} else if (gi->args.args[1].type == VALREF) {
VariableInfo* var = getVariable(table, gi->args.args[1].value.refName);
if (var == NULL) {
return NONE;
}
return var->type;
} else {
return NONE;
}
} else {
return NONE;
}
}
case ADD: {
if (gi->args.length == 3) {
GroundValueType leftType;
GroundValueType rightType;
if (gi->args.args[0].type == VALUE) {
leftType = gi->args.args[0].value.value.type;
} else if (gi->args.args[0].type == VALREF) {
VariableInfo* var = getVariable(table, gi->args.args[0].value.refName);
if (var == NULL) {
return NONE;
}
leftType = var->type;
} else {
return NONE;
}
if (gi->args.args[1].type == VALUE) {
rightType = gi->args.args[1].value.value.type;
} else if (gi->args.args[1].type == VALREF) {
VariableInfo* var = getVariable(table, gi->args.args[1].value.refName);
if (var == NULL) {
return NONE;
}
rightType = var->type;
} else {
return NONE;
}
if (leftType == STRING) {
if (rightType == STRING) {
return STRING;
} else {
return NONE;
}
}
if (leftType == DOUBLE) {
if (rightType == DOUBLE || rightType == INT) {
return DOUBLE;
} else {
return NONE;
}
}
if (rightType == DOUBLE) {
if (leftType == DOUBLE || leftType == INT) {
return DOUBLE;
} else {
return NONE;
}
}
if (rightType == INT && leftType == INT) {
return INT;
}
return NONE;
} else {
return NONE;
}
}
case SUBTRACT: {
if (gi->args.length == 3) {
GroundValueType leftType;
GroundValueType rightType;
if (gi->args.args[0].type == VALUE) {
leftType = gi->args.args[0].value.value.type;
} else if (gi->args.args[0].type == VALREF) {
VariableInfo* var = getVariable(table, gi->args.args[0].value.refName);
if (var == NULL) {
return NONE;
}
leftType = var->type;
} else {
return NONE;
}
if (gi->args.args[1].type == VALUE) {
rightType = gi->args.args[1].value.value.type;
} else if (gi->args.args[1].type == VALREF) {
VariableInfo* var = getVariable(table, gi->args.args[1].value.refName);
if (var == NULL) {
return NONE;
}
rightType = var->type;
} else {
return NONE;
}
if (leftType == DOUBLE) {
if (rightType == DOUBLE || rightType == INT) {
return DOUBLE;
} else {
return NONE;
}
}
if (rightType == DOUBLE) {
if (leftType == DOUBLE || leftType == INT) {
return DOUBLE;
} else {
return NONE;
}
}
if (rightType == INT && leftType == INT) {
return INT;
}
return NONE;
} else {
return NONE;
}
}
case MULTIPLY: {
if (gi->args.length == 3) {
GroundValueType leftType;
GroundValueType rightType;
if (gi->args.args[0].type == VALUE) {
leftType = gi->args.args[0].value.value.type;
} else if (gi->args.args[0].type == VALREF) {
VariableInfo* var = getVariable(table, gi->args.args[0].value.refName);
if (var == NULL) {
return NONE;
}
leftType = var->type;
} else {
return NONE;
}
if (gi->args.args[1].type == VALUE) {
rightType = gi->args.args[1].value.value.type;
} else if (gi->args.args[1].type == VALREF) {
VariableInfo* var = getVariable(table, gi->args.args[1].value.refName);
if (var == NULL) {
return NONE;
}
rightType = var->type;
} else {
return NONE;
}
if (leftType == DOUBLE) {
if (rightType == DOUBLE || rightType == INT) {
return DOUBLE;
} else {
return NONE;
}
}
if (rightType == DOUBLE) {
if (leftType == DOUBLE || leftType == INT) {
return DOUBLE;
} else {
return NONE;
}
}
if (rightType == INT && leftType == INT) {
return INT;
}
return NONE;
} else {
return NONE;
}
}
}
return INT;
}
char* processValueString(GroundArg arg, VariableTable* vt, GroundValueType* outType, DoubleTable* dt, Estr* rodata, int* doubleCounter) {
if (arg.type == VALREF) {
VariableInfo* var = getVariable(vt, arg.value.refName);
if (var && outType) *outType = var->type;
char* buf = malloc(260);
snprintf(buf, 260, "[%s]", arg.value.refName);
return buf;
}
if (arg.type == VALUE) {
if (outType) *outType = arg.value.value.type;
switch(arg.value.value.type) {
case INT: {
char* buf = malloc(64);
snprintf(buf, 64, "%" PRId64, arg.value.value.data.intVal);
return buf;
break;
}
case DOUBLE: {
char* label = addDoubleConstant(dt, rodata, arg.value.value.data.doubleVal, doubleCounter);
char* buf = malloc(128);
snprintf(buf, 128, "[rel %s]", label);
return buf;
break;
}
case BOOL:
return arg.value.value.data.boolVal ? "1" : "0";
break;
case CHAR: {
char* buf = malloc(8);
snprintf(buf, 8, "%d", (int)arg.value.value.data.charVal);
return buf;
break;
}
default: {
printf("For now, only int, bool, and char are supported data types in the compiler.");
exit(1);
break;
}
}
}
return NULL;
}
/*
char* processValueString(GroundArg arg) {
if (arg.type == VALREF) {
char* buf = malloc(sizeof(char) * 260);
snprintf(buf, sizeof(char) * 260, "[%s]", arg.value.refName);
return buf;
}
if (arg.type == VALUE) {
switch (arg.value.value.type) {
case INT: {
char* buf = malloc(sizeof(char) * 64);
snprintf(buf, sizeof(char) * 260, "%" PRId64, arg.value.value.data.intVal);
return buf;
break;
}
case BOOL: {
return arg.value.value.data.boolVal ? "1" : "0";
break;
}
case CHAR: {
char* buf = malloc(8);
snprintf(buf, 8, "%d", (int) arg.value.value.data.charVal);
return buf;
break;
}
default: {
printf("For now, only int, bool, and char are supported data types in the compiler.");
exit(1);
break;
}
}
}
return NULL;
}
*/
char* compileGroundProgram(GroundProgram* program) {
Estr start = CREATE_ESTR("global _start\nsection .text\n_start:\n");
Estr data = CREATE_ESTR("section .bss\n");
Estr helpers = CREATE_ESTR("");
Estr rodata = CREATE_ESTR("section .rodata\n");
APPEND_ESTR(rodata, " newline_char: db 10\n");
APPEND_ESTR(rodata, " space_char: db 32\n");
DoubleTable doubleTable = createDoubleTable();
int doubleConstCounter = 0;
APPEND_ESTR(helpers, "\n; Helper: Print integer in rax\n");
APPEND_ESTR(helpers, "print_int:\n");
APPEND_ESTR(helpers, " push rbp\n");
APPEND_ESTR(helpers, " mov rbp, rsp\n");
APPEND_ESTR(helpers, " sub rsp, 32 ; Allocate buffer on stack\n");
APPEND_ESTR(helpers, " mov rdi, rsp ; RDI = buffer pointer\n");
APPEND_ESTR(helpers, " add rdi, 31 ; Point to end of buffer\n");
APPEND_ESTR(helpers, " mov byte [rdi], 0 ; Null terminator\n");
APPEND_ESTR(helpers, " dec rdi\n");
APPEND_ESTR(helpers, " mov rbx, 10 ; Divisor\n");
APPEND_ESTR(helpers, " test rax, rax\n");
APPEND_ESTR(helpers, " jns .positive\n");
APPEND_ESTR(helpers, " neg rax ; Make positive\n");
APPEND_ESTR(helpers, " push rax\n");
APPEND_ESTR(helpers, " mov byte [rdi], '-'\n");
APPEND_ESTR(helpers, " dec rdi\n");
APPEND_ESTR(helpers, " pop rax\n");
APPEND_ESTR(helpers, ".positive:\n");
APPEND_ESTR(helpers, " xor rcx, rcx ; Digit counter\n");
APPEND_ESTR(helpers, ".convert_loop:\n");
APPEND_ESTR(helpers, " xor rdx, rdx\n");
APPEND_ESTR(helpers, " div rbx ; rax = rax/10, rdx = remainder\n");
APPEND_ESTR(helpers, " add dl, '0' ; Convert to ASCII\n");
APPEND_ESTR(helpers, " mov [rdi], dl\n");
APPEND_ESTR(helpers, " dec rdi\n");
APPEND_ESTR(helpers, " inc rcx\n");
APPEND_ESTR(helpers, " test rax, rax\n");
APPEND_ESTR(helpers, " jnz .convert_loop\n");
APPEND_ESTR(helpers, " inc rdi ; Point back to first digit\n");
APPEND_ESTR(helpers, " ; Now print the string\n");
APPEND_ESTR(helpers, " mov rax, 1 ; sys_write\n");
APPEND_ESTR(helpers, " mov rsi, rdi ; Buffer\n");
APPEND_ESTR(helpers, " mov rdx, rcx ; Length\n");
APPEND_ESTR(helpers, " mov rdi, 1 ; stdout\n");
APPEND_ESTR(helpers, " syscall\n");
APPEND_ESTR(helpers, " mov rsp, rbp\n");
APPEND_ESTR(helpers, " pop rbp\n");
APPEND_ESTR(helpers, " ret\n");
APPEND_ESTR(helpers, "\n; Helper: Print string at address in rax, length in rbx\n");
APPEND_ESTR(helpers, "print_string:\n");
APPEND_ESTR(helpers, " push rax\n");
APPEND_ESTR(helpers, " push rbx\n");
APPEND_ESTR(helpers, " mov rdi, 1 ; stdout\n");
APPEND_ESTR(helpers, " mov rsi, rax ; String address\n");
APPEND_ESTR(helpers, " mov rdx, rbx ; Length\n");
APPEND_ESTR(helpers, " mov rax, 1 ; sys_write\n");
APPEND_ESTR(helpers, " syscall\n");
APPEND_ESTR(helpers, " pop rbx\n");
APPEND_ESTR(helpers, " pop rax\n");
APPEND_ESTR(helpers, " ret\n");
VariableTable varTable = createVariableTable();
// Allocate a spot for all direct references
for (size_t i = 0; i < program->size; i++) {
GroundInstruction gi = program->instructions[i];
for (size_t j = 0; j < gi.args.length; j++) {
if (gi.args.args[j].type == DIRREF) {
addVtVariable(&varTable, gi.args.args[j].value.refName, getInstructionReturnType(&gi, &varTable));
}
}
}
// Create data section
for (size_t i = 0; i < varTable.count; i++) {
if (strcmp(varTable.vars[i].name, "") == 0) {
continue;
}
APPEND_ESTR(data, " ");
APPEND_ESTR(data, varTable.vars[i].name);
APPEND_ESTR(data, " resq 1\n");
}
// Generate assembly
for (size_t i = 0; i < program->size; i++) {
GroundInstruction gi = program->instructions[i];
switch (gi.type) {
case CREATELABEL: {
APPEND_ESTR(start, gi.args.args[0].value.refName);
APPEND_ESTR(start, ":\n");
break;
}
case SET: {
if (gi.args.length < 2) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", &gi, i);
}
if (gi.args.length > 2) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", &gi, i);
}
if (gi.args.args[0].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 1", &gi, i);
}
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value for arg 2", &gi, i);
}
char* varName= gi.args.args[0].value.refName;
APPEND_ESTR(start, " ; set\n")
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[1], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " mov [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], rax\n");
break;
}
case JUMP: {
if (gi.args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", &gi, i);
}
if (gi.args.length > 1) {
runtimeError(TOO_MANY_ARGS, "Expecting 1 arg", &gi, i);
}
if (gi.args.args[0].type != LINEREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a LineRef", &gi, i);
}
char* labelName = gi.args.args[0].value.refName;
APPEND_ESTR(start, " ; jump\n");
APPEND_ESTR(start, " jmp ");
APPEND_ESTR(start, labelName);
APPEND_ESTR(start, "\n");
break;
}
case IF: {
if (gi.args.length < 2) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", &gi, i);
}
if (gi.args.length > 2) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", &gi, i);
}
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int (for now) for arg 1", &gi, i);
}
if (gi.args.args[1].type != LINEREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a LineRef", &gi, i);
}
char* labelName = gi.args.args[1].value.refName;
APPEND_ESTR(start, " ; if\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " test rax, rax\n");
APPEND_ESTR(start, " jnz ");
APPEND_ESTR(start, labelName);
APPEND_ESTR(start, "\n");
break;
}
case NOT: {
if (gi.args.length < 2) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", &gi, i);
}
if (gi.args.length > 2) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", &gi, i);
}
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int (for now) for arg 1", &gi, i);
}
if (gi.args.args[1].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef", &gi, i);
}
char* varName = gi.args.args[1].value.refName;
APPEND_ESTR(start, " ; not\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " xor rax, 1\n");
APPEND_ESTR(start, " mov [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], rax\n");
break;
}
case ADD: {
if (gi.args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for add instruction", &gi, i);
}
if (gi.args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for add instruction", &gi, i);
}
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
}
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
}
if (gi.args.args[2].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
}
GroundValueType leftType, rightType;
char* left = processValueString(gi.args.args[0], &varTable, &leftType, &doubleTable, &rodata, &doubleConstCounter);
char* right = processValueString(gi.args.args[1], &varTable, &rightType, &doubleTable, &rodata, &doubleConstCounter);
char* varName = gi.args.args[2].value.refName;
bool isDouble = (leftType == DOUBLE || rightType == DOUBLE);
if (isDouble) {
APPEND_ESTR(start, " ; add (double)\n");
if (leftType == INT) {
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, left);
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " cvtsi2sd xmm0, rax ; Convert int to double\n");
} else {
APPEND_ESTR(start, " movsd xmm0, ");
APPEND_ESTR(start, left);
APPEND_ESTR(start, "\n");
}
if (rightType == INT) {
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, right);
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " cvtsi2sd xmm1, rax ; Convert int to double\n");
APPEND_ESTR(start, " addsd xmm0, xmm1\n");
} else {
APPEND_ESTR(start, " addsd xmm0, ");
APPEND_ESTR(start, right);
APPEND_ESTR(start, "\n");
}
APPEND_ESTR(start, " movsd [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], xmm0\n");
} else {
APPEND_ESTR(start, " ; add\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " add rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[1], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " mov [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], rax\n");
}
break;
}
case SUBTRACT: {
if (gi.args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for subtract instruction", &gi, i);
}
if (gi.args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for subtract instruction", &gi, i);
}
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
}
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
}
if (gi.args.args[2].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
}
char* varName= gi.args.args[2].value.refName;
APPEND_ESTR(start, " ; subtract\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " sub rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[1], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " mov [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], rax\n");
break;
}
case MULTIPLY: {
if (gi.args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for multiply instruction", &gi, i);
}
if (gi.args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for multiply instruction", &gi, i);
}
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
}
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
}
if (gi.args.args[2].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
}
char* varName= gi.args.args[2].value.refName;
APPEND_ESTR(start, " ; multiply\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " imul rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[1], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " mov [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], rax\n");
break;
}
case EQUAL: {
if (gi.args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for equal instruction", &gi, i);
}
if (gi.args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for equal instruction", &gi, i);
}
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
}
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
}
if (gi.args.args[2].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
}
char* varName= gi.args.args[2].value.refName;
APPEND_ESTR(start, " ; equal\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " cmp rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[1], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " sete al\n");
APPEND_ESTR(start, " movzx rax, al\n");
APPEND_ESTR(start, " mov [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], rax\n");
break;
}
case INEQUAL: {
if (gi.args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for inequal instruction", &gi, i);
}
if (gi.args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for inequal instruction", &gi, i);
}
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
}
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
}
if (gi.args.args[2].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
}
char* varName= gi.args.args[2].value.refName;
APPEND_ESTR(start, " ; inequal\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " cmp rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[1], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " setne al\n");
APPEND_ESTR(start, " movzx rax, al\n");
APPEND_ESTR(start, " mov [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], rax\n");
break;
}
case GREATER: {
if (gi.args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for greater instruction", &gi, i);
}
if (gi.args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for greater instruction", &gi, i);
}
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
}
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
}
if (gi.args.args[2].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
}
char* varName= gi.args.args[2].value.refName;
APPEND_ESTR(start, " ; greater\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " cmp rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[1], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " setg al\n");
APPEND_ESTR(start, " movzx rax, al\n");
APPEND_ESTR(start, " mov [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], rax\n");
break;
}
case LESSER: {
if (gi.args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args for lesser instruction", &gi, i);
}
if (gi.args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args for lesser instruction", &gi, i);
}
if (gi.args.args[0].type != VALUE && gi.args.args[0].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", &gi, i);
}
if (gi.args.args[1].type != VALUE && gi.args.args[1].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", &gi, i);
}
if (gi.args.args[2].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", &gi, i);
}
char* varName= gi.args.args[2].value.refName;
APPEND_ESTR(start, " ; lesser\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " cmp rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[1], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " setl al\n");
APPEND_ESTR(start, " movzx rax, al\n");
APPEND_ESTR(start, " mov [");
APPEND_ESTR(start, varName);
APPEND_ESTR(start, "], rax\n");
break;
}
case PRINT:
case PRINTLN: {
if (gi.args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 or more args", &gi, i);
}
for (size_t j = 0; j < gi.args.length; j++) {
if (gi.args.args[j].type != VALUE && gi.args.args[j].type != VALREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", &gi, i);
}
if (j > 0) {
// Print space between arguments
APPEND_ESTR(start, " ; print space\n");
APPEND_ESTR(start, " mov rax, 1\n");
APPEND_ESTR(start, " mov rdi, 1\n");
APPEND_ESTR(start, " lea rsi, [rel space_char]\n");
APPEND_ESTR(start, " mov rdx, 1\n");
APPEND_ESTR(start, " syscall\n");
}
APPEND_ESTR(start, " ; print int\n");
APPEND_ESTR(start, " mov rax, ");
APPEND_ESTR(start, processValueString(gi.args.args[j], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " call print_int\n");
}
if (gi.type == PRINTLN) {
APPEND_ESTR(start, " ; print newline\n");
APPEND_ESTR(start, " mov rax, 1\n");
APPEND_ESTR(start, " mov rdi, 1\n");
APPEND_ESTR(start, " lea rsi, [rel newline_char]\n");
APPEND_ESTR(start, " mov rdx, 1\n");
APPEND_ESTR(start, " syscall\n");
}
break;
}
case END: {
if (gi.args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg for end instruction", &gi, i);
}
if (gi.args.length > 1) {
runtimeError(TOO_MANY_ARGS, "Expecting 1 arg for end instruction", &gi, i);
}
APPEND_ESTR(start, " ; end\n");
APPEND_ESTR(start, " mov rax, 60\n");
APPEND_ESTR(start, " mov rdi, ");
APPEND_ESTR(start, processValueString(gi.args.args[0], &varTable, NULL, &doubleTable, &rodata, &doubleConstCounter));
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " syscall\n");
break;
}
case DROP: {
// Drop does nothing in ground->asm as we use the stack
break;
}
default: {
printf("no\n");
exit(1);
}
}
}
Estr complete = CREATE_ESTR("");
APPEND_ESTR(complete, start.str);
APPEND_ESTR(complete, " ; End of program\n mov rax, 60\n mov rdi, 0\n syscall\n");
APPEND_ESTR(complete, helpers.str);
APPEND_ESTR(complete, data.str);
APPEND_ESTR(complete, rodata.str);
return complete.str;
}

28
src/compiler.h Normal file
View File

@@ -0,0 +1,28 @@
#include "types.h"
#include "interpreter.h"
char* compileGroundProgram(GroundProgram* program);
[[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine);
typedef struct VariableInfo {
char name[256];
int offset;
GroundValueType type;
} VariableInfo;
typedef struct VariableTable {
VariableInfo* vars;
size_t count;
size_t capacity;
} VariableTable;
typedef struct DoubleConstant {
double value;
char label[64];
} DoubleConstant;
typedef struct DoubleTable {
DoubleConstant* constants;
size_t count;
size_t capacity;
} DoubleTable;

52
src/include/estr.h Normal file
View File

@@ -0,0 +1,52 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef ESTR_H
#define ESTR_H
/*
estr.h - Easy string manipulation
This library has macros to allow easier manipulation of strings. No longer shall
you have to malloc and realloc away to keep adding to your strings.
Usage:
Estr myString = CREATE_ESTR("my awesome string");
APPEND_ESTR(myString, " is so cool");
printf("%s\n", myString.str);
*/
#define CREATE_ESTR(instr) \
(Estr) { \
.str = instr,\
.size = strlen(instr),\
.shouldBeFreed = 0, \
.destroyed = 0 \
}
#define APPEND_ESTR(estr, instr) { \
estr.size = estr.size + strlen(instr); \
char* tmp_ptr = malloc(estr.size + 1); \
if (tmp_ptr == NULL) printf("WARNING: Could not realloc estr " #estr "\n"); \
else { \
snprintf(tmp_ptr, estr.size + 1, "%s%s", estr.str, instr); \
if (estr.shouldBeFreed > 0) free(estr.str); \
estr.shouldBeFreed = 1; \
estr.str = tmp_ptr; \
} \
}
#define DESTROY_ESTR(estr) if (estr.shouldBeFreed > 0 && estr.destroyed < 1) free(estr.str);
typedef struct Estr {
char* str;
size_t size;
int8_t shouldBeFreed;
int8_t destroyed;
} Estr;
#endif // ESTR_H

View File

@@ -1,6 +1,6 @@
#include "lexer.h"
#include "parser.h"
#include "interpreter.h"
#include "compiler.h"
#include "types.h"
#include <stdlib.h>
@@ -68,11 +68,30 @@ GroundValue groundCreateValue(GroundValueType type, ...) {
return createListGroundValue(va_arg(args, List));
break;
}
default: {
case FUNCTION: {
return createFunctionGroundValue(va_arg(args, GroundFunction*));
break;
}
case STRUCTVAL: {
GroundValue gv;
gv.type = STRUCTVAL;
gv.data.structVal = malloc(sizeof(GroundStruct));
*gv.data.structVal = va_arg(args, GroundStruct);
return gv;
}
case NONE: {
return createNoneGroundValue();
break;
}
case ERROR:
case CUSTOM: {
// FIXME
break;
}
}
return createNoneGroundValue();
va_end(args);
}
@@ -90,3 +109,22 @@ void groundPrintProgram(GroundProgram* program) {
GroundProgram groundParseFile(const char* code) {
return parseFile(code);
}
GroundStruct groundCreateStruct() {
GroundStruct gs;
gs.size = 0;
gs.fields = malloc(sizeof(GroundStructField));
return gs;
}
void groundAddValueToScope(GroundScope* gs, const char* name, GroundValue value) {
addVariable(gs->variables, name, value);
}
void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field) {
addFieldToStruct(gstruct, name, field);
}
char* groundCompileProgram(GroundProgram* program) {
return compileGroundProgram(program);
}

View File

@@ -11,6 +11,25 @@
int currentInstruction = 0;
bool isMainScopeGlobal = true;
[[noreturn]] void customError(GroundArg type, GroundArg what, GroundInstruction* where, int whereLine, int exitCode) {
printf("Ground runtime error:\n ErrorType: ");
printGroundArg(&type);
printf("\n ErrorContext: ");
printGroundArg(&what);
printf("\n");
if (where != NULL) {
printf(" ErrorInstruction: ");
printGroundInstruction(where);
printf("\n");
}
if (whereLine > -1) {
printf(" ErrorLine: %d\n", whereLine + 1);
}
exit(exitCode);
}
[[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine) {
printf("Ground runtime error:\n ErrorType: ");
switch (error) {
@@ -78,6 +97,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);
@@ -317,7 +352,7 @@ GroundStruct parseStruct(GroundProgram* in, GroundScope* scope, size_t errorOffs
GroundValue gv;
switch (stringToValueType(in->instructions[i].args.args[0].value.refName)) {
switch (stringToValueType(in->instructions[i].args.args[1].value.refName)) {
case INT: {
gv = createIntGroundValue(0);
break;
@@ -364,6 +399,7 @@ GroundStruct parseStruct(GroundProgram* in, GroundScope* scope, size_t errorOffs
gv.type = CUSTOM;
gv.data.customVal = malloc(sizeof(GroundObject));
*gv.data.customVal = createObject(*gstruct);
gv.customType = gstruct;
break;
}
case NONE: {
@@ -438,6 +474,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) {
@@ -793,7 +831,19 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
}
break;
}
case ERRORCMD: {
if (in->args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
} else if (in->args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 3 args", in, currentInstruction);
}
if (in->args.args[2].value.value.type != INT ) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an int for arg 2", in, currentInstruction);
}
customError(in->args.args[0], in->args.args[1], in, currentInstruction, in->args.args[2].value.value.data.intVal);
break;
}
/*
* VARIABLES AND LISTS
* These instructions are for initializing variables and lists.
@@ -872,6 +922,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;
@@ -1729,7 +1783,8 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
if (in->args.args[i + 1].type != VALUE) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction);
}
if (in->args.args[i + 1].value.value.type != function->args[i].type) {
//if (in->args.args[i + 1].value.value.type != function->args[i].type) {
if (!checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) {
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
}
appendToList(&argsList, in->args.args[i + 1].value.value);
@@ -1737,6 +1792,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);
}
@@ -1748,7 +1812,8 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
if (in->args.args[i + 1].type != VALUE) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction);
}
if (in->args.args[i + 1].value.value.type != function->args[i].type) {
//if (in->args.args[i + 1].value.value.type != function->args[i].type) {
if (!checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) {
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
}
addVariable(newScope.variables, function->args[i].name, in->args.args[i + 1].value.value);
@@ -1756,6 +1821,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);
}
@@ -1943,6 +2011,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
gv.type = CUSTOM;
gv.data.customVal = malloc(sizeof(GroundObject));
*gv.data.customVal = createObject(*gstruct);
gv.customType = gstruct;
break;
}
case NONE: {
@@ -1959,6 +2028,77 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
break;
}
/*
* OBJECTS
* Allows manipulation of Ground objects.
*/
// getfield $obj &field &result
case GETFIELD: {
if (in->args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
}
if (in->args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 3 args", in, currentInstruction);
}
if (in->args.args[0].type != VALUE) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Object for arg 1", in, currentInstruction);
}
if (in->args.args[0].value.value.type != CUSTOM) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Object for arg 1", in, currentInstruction);
}
if (in->args.args[1].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
}
if (in->args.args[2].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", in, currentInstruction);
}
GroundObjectField* field = findField(*in->args.args[0].value.value.data.customVal, in->args.args[1].value.refName);
if (field == NULL) {
runtimeError(UNKNOWN_VARIABLE, "Struct does not contain that field", in, currentInstruction);
}
addVariable(scope->variables, in->args.args[2].value.refName, field->value);
break;
}
// setfield &obj &field $value
case SETFIELD: {
if (in->args.length < 3) {
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
}
if (in->args.length > 3) {
runtimeError(TOO_MANY_ARGS, "Expecting 3 args", in, currentInstruction);
}
if (in->args.args[0].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef to an Object for arg 1", in, currentInstruction);
}
if (in->args.args[1].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
}
if (in->args.args[2].type != VALUE) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value for arg 3", in, currentInstruction);
}
GroundVariable* var = findVariable(*scope->variables, in->args.args[0].value.refName);
if (var == NULL) {
runtimeError(UNKNOWN_VARIABLE, "Could not find that object", in, currentInstruction);
}
if (var->value.type != CUSTOM) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef to an Object for arg 1", in, currentInstruction);
}
GroundObjectField* field = findField(*var->value.data.customVal, in->args.args[1].value.refName);
if (field == NULL) {
runtimeError(UNKNOWN_VARIABLE, "Struct does not contain that field", in, currentInstruction);
}
if (field->value.type != in->args.args[2].value.value.type) {
runtimeError(ARG_TYPE_MISMATCH, "Field type and provided type do not match", in, currentInstruction);
}
field->value = copyGroundValue(&in->args.args[2].value.value);
break;
}
case DROP: {
if (in->args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", in, currentInstruction);

View File

@@ -29,6 +29,7 @@ typedef struct GroundVariable {
typedef struct GroundScope {
GroundLabel** labels;
GroundVariable** variables;
bool isMainScope;
} GroundScope;
typedef struct GroundDebugInstruction {
@@ -41,6 +42,7 @@ GroundFunction* parseFunction(GroundProgram* in, size_t errorOffset);
GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope);
GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scope);
void addVariable(GroundVariable **head, const char *id, GroundValue data);

View File

@@ -1,5 +1,6 @@
#include "parser.h"
#include "interpreter.h"
#include "compiler.h"
#include <stdio.h>
char* getFileContents(const char* filename) {
@@ -39,6 +40,7 @@ char* getFileContents(const char* filename) {
}
int main(int argc, char** argv) {
/*
if (argc < 2) {
printf("Usage: ground [file]\n");
exit(1);
@@ -46,5 +48,49 @@ int main(int argc, char** argv) {
char* file = getFileContents(argv[1]);
GroundProgram program = parseFile(file);
free(file);
char* compiled = compileGroundProgram(&program);
printf("%s\n", compiled);
interpretGroundProgram(&program, NULL);
*/
if (argc < 2) {
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
exit(1);
}
bool compile = false;
char* fileName = NULL;
for (int i = 1; i < argc; i++) {
if (strcmp("--compile", argv[i]) == 0 || strcmp("-c", argv[i]) == 0) {
compile = true;
}
else if (strcmp("--help", argv[i]) == 0 || strcmp("-h", argv[i]) == 0) {
printf("GroundVM help\n");
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
printf("Options:\n");
printf(" -c or --compile: Outputs Linux x86_64 assembly instead of interpreting (WIP)\n");
printf(" -h or --help: Shows this help message\n");
exit(0);
} else {
fileName = argv[i];
}
}
if (fileName == NULL) {
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
printf("Error: No file name provided\n");
exit(1);
}
char* file = getFileContents(fileName);
GroundProgram program = parseFile(file);
free(file);
if (compile) {
char* compiled = compileGroundProgram(&program);
printf("%s\n", compiled);
} else {
interpretGroundProgram(&program, NULL);
}
}

View File

@@ -137,6 +137,7 @@ static GroundInstType getInstructionType(const char* inst) {
if (strcmp(inst, "input") == 0 || strcmp(inst, "stdin") == 0) return INPUT;
if (strcmp(inst, "print") == 0 || strcmp(inst, "stdout") == 0) return PRINT;
if (strcmp(inst, "println") == 0 || strcmp(inst, "stdlnout") == 0) return PRINTLN;
if (strcmp(inst, "error") == 0) return ERRORCMD;
if (strcmp(inst, "set") == 0) return SET;
if (strcmp(inst, "gettype") == 0) return GETTYPE;
if (strcmp(inst, "exists") == 0) return EXISTS;
@@ -167,6 +168,8 @@ static GroundInstType getInstructionType(const char* inst) {
if (strcmp(inst, "struct") == 0) return STRUCT;
if (strcmp(inst, "endstruct") == 0) return ENDSTRUCT;
if (strcmp(inst, "init") == 0) return INIT;
if (strcmp(inst, "getfield") == 0) return GETFIELD;
if (strcmp(inst, "setfield") == 0) return SETFIELD;
if (strcmp(inst, "use") == 0) return USE;
if (strcmp(inst, "extern") == 0) return EXTERN;
if (strcmp(inst, "drop") == 0) return DROP;

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) {
@@ -393,6 +412,9 @@ void printGroundInstruction(GroundInstruction* gi) {
break;
case CREATELABEL:
break;
case ERRORCMD:
printf("error");
break;
default:
printf("FIXME");
break;
@@ -529,3 +551,77 @@ 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;
}
bool checkFnTypes(GroundValue* left, GroundFunctionArgs* right) {
if (left->type != right->type) {
return false;
}
if (left->type == CUSTOM) {
if (left->customType->size != right->customType->size) {
return false;
}
for (size_t i = 0; i < left->customType->size; i++) {
if (strcmp(left->customType->fields[i].id, right->customType->fields[i].id) != 0) {
return false;
}
if (!checkTypes(&left->customType->fields[i].value, &right->customType->fields[i].value)) {
return false;
}
}
}
return true;
}
bool checkTypes(GroundValue* left, GroundValue* right) {
if (left->type != right->type) {
return false;
}
if (left->type == CUSTOM) {
if (left->customType->size != right->customType->size) {
return false;
}
for (size_t i = 0; i < left->customType->size; i++) {
if (strcmp(left->customType->fields[i].id, right->customType->fields[i].id) != 0) {
return false;
}
if (!checkTypes(&left->customType->fields[i].value, &right->customType->fields[i].value)) {
return false;
}
}
}
return true;
}

View File

@@ -9,11 +9,11 @@
#include "include/uthash.h"
typedef enum GroundInstType {
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD
} 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:
@@ -50,13 +62,15 @@ typedef struct List {
*/
typedef struct GroundValue {
GroundValueType type;
union {
struct GroundStruct* customType;
struct {
int64_t intVal;
double doubleVal;
char* stringVal;
char charVal;
bool boolVal;
List listVal;
GroundError errorVal;
struct GroundFunction* fnVal;
struct GroundStruct* structVal;
struct GroundObject* customVal;
@@ -112,6 +126,7 @@ typedef struct GroundProgram {
*/
typedef struct GroundFunctionArgs {
GroundValueType type;
struct GroundStruct* customType;
char* name;
} GroundFunctionArgs;
@@ -187,6 +202,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 +275,14 @@ 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);
// Compares types of a value and function args.
bool checkFnTypes(GroundValue* left, GroundFunctionArgs* arg);
// Compares types of two values.
bool checkTypes(GroundValue* left, GroundValue* right);
#endif

2
tests/error.grnd Normal file
View File

@@ -0,0 +1,2 @@
setlist &dat 1 2 3 "Hi!"
error "Hello" $dat 1

17
tests/struct.grnd Normal file
View File

@@ -0,0 +1,17 @@
struct -point
init &x -int
init &y -int
endstruct
init &mypoint -point
setfield &mypoint &x 53
setfield &mypoint &y 32
getfield $mypoint &x &value
println $value
getfield $mypoint &y &value
println $value