#ifndef LIBGROUND_H #define LIBGROUND_H /* * groundvm.h * Provides an interface for external programs wanting to run Ground code. * */ #include #include #include #include #include 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, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD } GroundInstType; typedef enum GroundValueType { INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, STRUCTVAL, CUSTOM, ERROR, NONE } GroundValueType; typedef enum GroundArgType { VALUE, VALREF, DIRREF, LINEREF, LABEL, FNREF, TYPEREF } GroundArgType; typedef enum ListAccessStatus { LIST_OKAY, LIST_OUT_OF_BOUNDS, LIST_FIXME } ListAccessStatus; struct GroundValue; struct GroundFunction; struct List; /* * Custom data type that stores Ground values. */ typedef struct List { size_t size; 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 __attribute__((packed)) GroundValue { GroundValueType type; struct GroundStruct* customType; union { int64_t intVal; double doubleVal; char* stringVal; char charVal; bool boolVal; List listVal; GroundError errorVal; struct GroundFunction* fnVal; struct GroundStruct* structVal; struct GroundObject* customVal; } data; } GroundValue; /* * Indicates status when accessing a list. */ typedef struct ListAccess { ListAccessStatus status; GroundValue* value; } ListAccess; /* * Stores arguments for the GroundInstruction struct. */ typedef struct GroundArg { GroundArgType type; union { GroundValue value; char* refName; } value; } GroundArg; /* * Represents a Ground instruction. */ typedef struct GroundInstruction { GroundInstType type; struct { GroundArg* args; size_t length; } args; } GroundInstruction; /* * Represents a Ground program or function. */ typedef struct GroundProgram { GroundInstruction* instructions; size_t size; } GroundProgram; /* * Represents the argument typing for a GroundFunction. */ typedef struct GroundFunctionArgs { GroundValueType type; char* name; } GroundFunctionArgs; /* * Represents a Ground function. */ typedef struct GroundFunction { GroundFunctionArgs* args; size_t argSize; GroundValueType returnType; GroundProgram program; 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 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); void groundAddReferenceToInstruction(GroundInstruction* inst, GroundArg value); GroundArg groundCreateReference(GroundArgType type, char* ref); GroundValue groundCreateValue(GroundValueType type, ...); GroundProgram groundParseFile(const char* code); GroundStruct groundCreateStruct(); void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field); #ifdef __cplusplus } #endif #endif