Files
cground/src/types.h

289 lines
7.6 KiB
C
Raw Normal View History

2025-11-23 13:37:08 +11:00
#ifndef TYPES_H
#define TYPES_H
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
2026-01-15 12:44:09 +11:00
#include "include/uthash.h"
2025-11-23 13:37:08 +11:00
typedef enum GroundInstType {
2026-01-19 21:14:48 +11:00
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, ERRORCMD
2025-11-23 13:37:08 +11:00
} GroundInstType;
typedef enum GroundValueType {
2026-01-18 20:49:50 +11:00
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, STRUCTVAL, CUSTOM, ERROR, NONE
2025-11-23 13:37:08 +11:00
} GroundValueType;
typedef enum GroundArgType {
2025-11-23 16:32:54 +11:00
VALUE, VALREF, DIRREF, LINEREF, LABEL, FNREF, TYPEREF
2025-11-23 13:37:08 +11:00
} GroundArgType;
2025-11-28 09:23:43 +11:00
typedef enum ListAccessStatus {
LIST_OKAY, LIST_OUT_OF_BOUNDS, LIST_FIXME
} ListAccessStatus;
2025-11-23 13:37:08 +11:00
struct GroundValue;
2025-12-06 11:50:42 +11:00
struct GroundFunction;
struct GroundScope;
2026-01-15 12:44:09 +11:00
struct GroundStruct;
struct GroundObject;
2026-01-18 20:49:50 +11:00
struct GroundInstruction;
2025-11-23 13:37:08 +11:00
struct List;
2025-12-01 10:15:37 +11:00
/*
* Custom data type that stores Ground values.
* Associated functions:
* createList(), appendToList(), getListAt(), setListAt()
*/
typedef struct List {
size_t size;
struct GroundValue* values;
} List;
2026-01-18 20:49:50 +11:00
/*
* Stores data associated with an error thrown during Ground execution.
*/
typedef struct GroundError {
char* what;
char* type;
struct GroundInstruction* where;
size_t line;
2026-01-18 21:37:46 +11:00
bool hasLine;
2026-01-18 20:49:50 +11:00
} GroundError;
2025-11-23 13:37:08 +11:00
/*
* Stores literal values created in a Ground program.
* Associated functions:
* createIntGroundValue(), createDoubleGroundValue(), createStringGroundvalue(),
* createCharGroundValue(), createBoolGroundValue(), freeGroundValue()
*/
typedef struct GroundValue {
GroundValueType type;
2026-01-20 19:55:38 +11:00
struct GroundStruct* customType;
2025-11-23 13:37:08 +11:00
union {
int64_t intVal;
double doubleVal;
char* stringVal;
char charVal;
bool boolVal;
2025-12-01 10:15:37 +11:00
List listVal;
2026-01-18 20:49:50 +11:00
GroundError errorVal;
2025-12-06 11:50:42 +11:00
struct GroundFunction* fnVal;
2026-01-15 12:44:09 +11:00
struct GroundStruct* structVal;
struct GroundObject* customVal;
2025-11-23 13:37:08 +11:00
} data;
} GroundValue;
2025-11-28 09:23:43 +11:00
/*
* Indicates status when accessing a list.
* Associated functions:
* getListAt()
*/
typedef struct ListAccess {
ListAccessStatus status;
GroundValue* value;
} ListAccess;
2025-11-23 13:37:08 +11:00
/*
* Stores arguments for the GroundInstruction struct.
* Associated functions:
* createValueGroundArg(), createRefGroundArg(), freeGroundArg()
*/
typedef struct GroundArg {
GroundArgType type;
union {
GroundValue value;
char* refName;
} value;
} GroundArg;
/*
* Represents a Ground instruction.
* Associated functions:
* createGroundInstruction(), freeGroundInstruction(), addArgToInstruction()
*/
typedef struct GroundInstruction {
GroundInstType type;
struct {
GroundArg* args;
size_t length;
} args;
} GroundInstruction;
2025-12-06 11:50:42 +11:00
/*
* 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;
2026-01-20 19:55:38 +11:00
struct GroundStruct* customType;
2025-12-06 11:50:42 +11:00
char* name;
} GroundFunctionArgs;
/*
* Native function pointer type.
*/
typedef GroundValue (*NativeGroundFunction)(struct GroundScope* scope, List args);
2025-12-06 11:50:42 +11:00
/*
* Represents a Ground function.
*/
typedef struct GroundFunction {
GroundFunctionArgs* args;
size_t argSize;
GroundValueType returnType;
GroundProgram program;
2025-12-06 14:35:13 +11:00
size_t startLine;
bool isNative;
NativeGroundFunction nativeFn;
2025-12-06 11:50:42 +11:00
} GroundFunction;
2026-01-15 12:44:09 +11:00
/*
* 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;
2025-12-06 11:50:42 +11:00
2025-11-23 13:37:08 +11:00
// Creates a GroundValue containing (in), with type INT.
GroundValue createIntGroundValue(int64_t in);
// Creates a GroundValue containing (in), with type DOUBLE.
GroundValue createDoubleGroundValue(double in);
2025-11-23 18:34:30 +11:00
2025-11-23 13:37:08 +11:00
// Creates a GroundValue containing (in), with type STRING.
GroundValue createStringGroundValue(const char* in);
// Creates a GroundValue containing (in), with type CHAR.
GroundValue createCharGroundValue(char in);
// Creates a GroundValue containing (in), with type BOOl.
GroundValue createBoolGroundValue(bool in);
2025-12-01 10:15:37 +11:00
// Creates a GroundValue containing (in), with type LIST.
GroundValue createListGroundValue(List in);
2025-12-06 11:50:42 +11:00
// Creates a GroundValue conatining (in), with type FUNCTION.
GroundValue createFunctionGroundValue(GroundFunction* in);
2026-01-18 20:49:50 +11:00
// Creates a GroundValue containing (in), with type ERROR.
GroundValue createErrorGroundValue(GroundError in);
2025-12-06 14:35:13 +11:00
// Creates a Groundvalue with type NONE.
GroundValue createNoneGroundValue();
2025-12-01 10:36:09 +11:00
// Creates a deep copy of a GroundValue
GroundValue copyGroundValue(const GroundValue* gv);
2025-11-23 13:37:08 +11:00
// If (gv) contains any data stored on the heap, frees it.
void freeGroundValue(GroundValue* gv);
2025-11-23 18:34:30 +11:00
// Prints out a GroundValue.
void printGroundValue(GroundValue* gv);
2025-11-23 13:37:08 +11:00
// Initializes a GroundArg with type VALUE
GroundArg createValueGroundArg(GroundValue value);
// Initializes a GroundArg with type (type), and refname (refname).
GroundArg createRefGroundArg(GroundArgType type, const char* refname);
// Frees all data stored on the heap in a GroundArg.
void freeGroundArg(GroundArg* ga);
2025-11-23 18:34:30 +11:00
// Prints out a GroundArg.
void printGroundArg(GroundArg* ga);
2025-11-23 13:37:08 +11:00
// Initializes a GroundInstruction, with inst type (type).
GroundInstruction createGroundInstruction(GroundInstType type);
// Frees all data stored on the heap in a GroundInstruction.
void freeGroundInstruction(GroundInstruction* gi);
2025-11-24 10:15:53 +11:00
// Creates a deep copy of a GroundInstruction.
GroundInstruction copyGroundInstruction(const GroundInstruction* gi);
2025-11-23 13:37:08 +11:00
// Adds arg (arg) to the GroundInstruction (gi).
void addArgToInstruction(GroundInstruction* gi, GroundArg arg);
2025-11-23 18:34:30 +11:00
// Prints out a GroundInstruction.
void printGroundInstruction(GroundInstruction* gi);
2025-11-28 09:23:43 +11:00
// Creates a Ground list
List createList();
// Add item (value) to List (list)
void appendToList(List* list, GroundValue value);
// Gets item at index (idx) from list (list). If there is an error, it
// will be indicated in the status field.
ListAccess getListAt(List* list, size_t idx);
2025-11-28 09:23:43 +11:00
// Sets an item in list (list) at index (idx) to GroundValue (value).
ListAccessStatus setListAt(List* list, size_t idx, GroundValue value);
2025-11-28 09:23:43 +11:00
2026-01-15 12:44:09 +11:00
// 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);
2026-01-18 20:49:50 +11:00
// Creates a GroundError.
GroundError createGroundError(char* what, char* type, GroundInstruction* where, size_t* line);
2026-01-20 19:55:38 +11:00
// 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);
2026-01-18 20:49:50 +11:00
2025-12-02 09:00:21 +11:00
#endif