Files
cground/src/types.h

199 lines
5.3 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>
typedef enum GroundInstType {
2025-11-23 16:32:54 +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
2025-11-23 13:37:08 +11:00
} GroundInstType;
typedef enum GroundValueType {
2025-12-06 11:50:42 +11:00
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, CUSTOM, 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;
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;
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;
union {
int64_t intVal;
double doubleVal;
char* stringVal;
char charVal;
bool boolVal;
2025-12-01 10:15:37 +11:00
List listVal;
2025-12-06 11:50:42 +11:00
struct GroundFunction* fnVal;
2025-11-23 13:37:08 +11:00
void* customVal;
} 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;
char* name;
} GroundFunctionArgs;
/*
* 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;
2025-12-06 11:50:42 +11:00
} GroundFunction;
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);
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, int idx);
// Sets an item in list (list) at index (idx) to GroundValue (value).
ListAccessStatus setListAt(List* list, int idx, GroundValue value);
2025-12-02 09:00:21 +11:00
#endif