Files
cground/src/types.h

289 lines
7.6 KiB
C

#ifndef TYPES_H
#define TYPES_H
#include <stdlib.h>
#include <stdio.h>
#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, 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 GroundScope;
struct GroundStruct;
struct GroundObject;
struct GroundInstruction;
struct List;
/*
* Custom data type that stores Ground values.
* Associated functions:
* createList(), appendToList(), getListAt(), setListAt()
*/
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.
* Associated functions:
* createIntGroundValue(), createDoubleGroundValue(), createStringGroundvalue(),
* createCharGroundValue(), createBoolGroundValue(), freeGroundValue()
*/
typedef struct GroundValue {
GroundValueType type;
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;
} data;
} GroundValue;
/*
* Indicates status when accessing a list.
* Associated functions:
* getListAt()
*/
typedef struct ListAccess {
ListAccessStatus status;
GroundValue* value;
} ListAccess;
/*
* 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;
/*
* 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;
struct GroundStruct* customType;
char* name;
} GroundFunctionArgs;
/*
* Native function pointer type.
*/
typedef GroundValue (*NativeGroundFunction)(struct GroundScope* scope, List args);
/*
* Represents a Ground function.
*/
typedef struct GroundFunction {
GroundFunctionArgs* args;
size_t argSize;
GroundValueType returnType;
GroundProgram program;
size_t startLine;
bool isNative;
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);
// Creates a GroundValue containing (in), with type DOUBLE.
GroundValue createDoubleGroundValue(double in);
// 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);
// Creates a GroundValue containing (in), with type LIST.
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();
// Creates a deep copy of a GroundValue
GroundValue copyGroundValue(const GroundValue* gv);
// If (gv) contains any data stored on the heap, frees it.
void freeGroundValue(GroundValue* gv);
// Prints out a GroundValue.
void printGroundValue(GroundValue* gv);
// 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);
// Prints out a GroundArg.
void printGroundArg(GroundArg* ga);
// 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);
// Creates a deep copy of a GroundInstruction.
GroundInstruction copyGroundInstruction(const GroundInstruction* gi);
// Adds arg (arg) to the GroundInstruction (gi).
void addArgToInstruction(GroundInstruction* gi, GroundArg arg);
// Prints out a GroundInstruction.
void printGroundInstruction(GroundInstruction* gi);
// 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);
// Sets an item in list (list) at index (idx) to GroundValue (value).
ListAccessStatus setListAt(List* list, size_t 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);
// 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