Start work on structs and objects

This commit is contained in:
2026-01-15 12:44:09 +11:00
parent 6ffd01d2c3
commit 694722591e
2 changed files with 137 additions and 3 deletions

View File

@@ -6,13 +6,14 @@
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#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
} GroundInstType;
typedef enum GroundValueType {
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, CUSTOM, NONE
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, STRUCTVAL, CUSTOM, NONE
} GroundValueType;
typedef enum GroundArgType {
@@ -26,6 +27,8 @@ typedef enum ListAccessStatus {
struct GroundValue;
struct GroundFunction;
struct GroundScope;
struct GroundStruct;
struct GroundObject;
struct List;
@@ -55,7 +58,8 @@ typedef struct GroundValue {
bool boolVal;
List listVal;
struct GroundFunction* fnVal;
void* customVal;
struct GroundStruct* structVal;
struct GroundObject* customVal;
} data;
} GroundValue;
@@ -129,6 +133,38 @@ typedef struct GroundFunction {
NativeGroundFunction nativeFn;
} 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;
// Creates a GroundValue containing (in), with type INT.
GroundValue createIntGroundValue(int64_t in);
@@ -203,4 +239,22 @@ ListAccess getListAt(List* list, int idx);
// Sets an item in list (list) at index (idx) to GroundValue (value).
ListAccessStatus setListAt(List* list, int idx, GroundValue value);
// Creates a Ground struct
GroundStruct createStruct();
// Adds a field (field) to Ground struct (gstruct)
void addFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field);
// Frees a Ground struct
void freeGroundStruct(GroundStruct* gstruct);
// Creates a Ground object from GroundStruct (gstruct)
GroundObject createObject(GroundStruct gstruct);
// Finds a field in an object for modification or copying out.
GroundObjectField* findField(GroundObject head, const char *id);
// Frees a GroundObject
void freeGroundObject(GroundObject* object);
#endif