Files
ground-rewrite/include/ground.h
2026-06-25 18:57:53 +10:00

456 lines
12 KiB
C

#ifndef GROUND_H
#define GROUND_H
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <uthash.h>
//
// TYPES
//
// --- Program structure types ---
typedef struct GroundType GroundType;
typedef struct GroundValue GroundValue;
typedef struct GroundArg GroundArg;
typedef struct GroundInstruction GroundInstruction;
typedef struct GroundProgram GroundProgram;
typedef struct GroundState GroundState;
typedef struct GroundBytecodeProgram GroundBytecodeProgram;
typedef struct GroundBytecodeInstruction GroundBytecodeInstruction;
typedef struct GroundBytecodeHeap GroundBytecodeHeap;
typedef struct GroundBytecode GroundBytecode;
// --- Literal types ---
typedef int64_t GroundInt;
typedef double GroundDouble;
typedef char GroundChar;
typedef bool GroundBool;
// --- Complex types ---
typedef struct GroundList GroundList;
typedef struct GroundString GroundString;
typedef struct GroundFunction GroundFunction;
typedef struct GroundStruct GroundStruct;
typedef struct GroundObject GroundObject;
typedef struct GroundError GroundError;
// --- Helper types ---
typedef struct GroundIdentifier GroundIdentifier;
typedef struct GroundFunctionArg GroundFunctionArg;
typedef struct GroundObjectField GroundObjectField;
typedef struct GroundVariable GroundVariable;
typedef struct GroundLabel GroundLabel;
typedef struct GroundCatch GroundCatch;
typedef uint64_t GroundSize;
// --- Complex types definitions ---
struct GroundString {
char* cstr;
GroundSize len;
};
struct GroundList {
GroundSize capacity;
GroundSize count;
GroundValue* at;
};
struct GroundFunction {
struct {
GroundSize capacity;
GroundSize count;
GroundFunctionArg* at;
} args;
bool isNativeFunction;
union {
void* native;
GroundProgram* ground;
} program;
GroundState* closure;
GroundSize _requiredSize;
};
struct GroundStruct {
GroundObjectField* fields;
};
struct GroundObjectField {
char name[2048];
GroundValue* value;
UT_hash_handle hh;
};
struct GroundObject {
GroundObjectField* fields;
GroundStruct* type;
};
struct GroundError {
GroundValue* value;
};
// --- Program structure types definitions ---
enum GroundTypeType {
GroundType_Int, GroundType_Double, GroundType_Char, GroundType_Bool,
GroundType_String, GroundType_List,
GroundType_Function, GroundType_Struct, GroundType_Object
};
struct GroundType {
enum GroundTypeType type;
GroundStruct complexType;
};
struct GroundFunctionArg {
union {
GroundIdentifier* id;
GroundType type;
} as;
};
struct GroundValue {
union {
GroundInt Int;
GroundDouble Double;
GroundChar Char;
GroundBool Bool;
GroundString String;
GroundList List;
GroundFunction Function;
GroundStruct Struct;
GroundObject Object;
} as;
GroundType type;
};
enum GroundArgType {
GroundArg_Value, GroundArg_ValueRef, GroundArg_DirectRef,
GroundArg_LineRef, GroundArg_Label,
GroundArg_FunctionRef, GroundArg_TypeRef
};
struct GroundArg {
enum GroundArgType type;
union {
GroundIdentifier* ref;
GroundValue value;
} as;
GroundSize _offset;
};
enum GroundInstructionType {
// Control flow
GroundInstruction_IF, GroundInstruction_JUMP, GroundInstruction_END,
// I/O
GroundInstruction_INPUT, GroundInstruction_PRINT, GroundInstruction_PRINTLN,
// Variable manipulation
GroundInstruction_SET, GroundInstruction_GETTYPE, GroundInstruction_EXISTS,
// List manipulation
GroundInstruction_SETLIST, GroundInstruction_SETLISTAT, GroundInstruction_GETLISTAT,
GroundInstruction_GETLISTSIZE, GroundInstruction_LISTAPPEND,
// String manipulation
GroundInstruction_GETSTRSIZE, GroundInstruction_GETSTRCHARAT,
// Math
GroundInstruction_ADD, GroundInstruction_SUBTRACT, GroundInstruction_MULTIPLY, GroundInstruction_DIVIDE,
// Comparison
GroundInstruction_EQUAL, GroundInstruction_INEQUAL, GroundInstruction_NOT,
GroundInstruction_GREATER, GroundInstruction_LESSER,
// Bitwise operations
GroundInstruction_AND, GroundInstruction_OR, GroundInstruction_XOR,
GroundInstruction_NEG, GroundInstruction_SHIFT,
// Conversions
GroundInstruction_STOI, GroundInstruction_STOD, GroundInstruction_ITOC, GroundInstruction_CTOI, GroundInstruction_TOSTRING,
// Functions
GroundInstruction_FUN, GroundInstruction_RETURN, GroundInstruction_ENDFUN,
// Calling functions
GroundInstruction_CALL, GroundInstruction_CALLMETHOD,
// Structs
GroundInstruction_STRUCT, GroundInstruction_ENDSTRUCT, GroundInstruction_INIT,
GroundInstruction_GETFIELD, GroundInstruction_SETFIELD,
// Libraries
GroundInstruction_USE, GroundInstruction_EXTERN,
// Create labels
GroundInstruction_CREATELABEL,
// Utility
GroundInstruction_PAUSE, GroundInstruction_DROP, GroundInstruction_LICENSE,
// Error
GroundInstruction_ERROR, GroundInstruction_THROW, GroundInstruction_CATCH
};
struct GroundInstruction {
enum GroundInstructionType type;
struct {
GroundSize len;
GroundSize capacity;
GroundArg* at;
} args;
};
struct GroundProgram {
GroundSize len;
GroundSize capacity;
GroundInstruction* at;
};
struct GroundVariable {
char name[2048];
GroundValue value;
UT_hash_handle hh;
GroundSize _offset;
};
struct GroundLabel {
char name[2048];
GroundSize lineNum;
UT_hash_handle hh;
};
struct GroundCatch {
char name[2048];
GroundType type;
UT_hash_handle hh;
};
struct GroundState {
GroundVariable* variables;
GroundLabel* labels;
GroundCatch* catches;
GroundSize _size;
};
struct GroundIdentifier {
char* string;
GroundSize referenceCount;
};
struct GroundBytecodeProgram {
GroundBytecodeInstruction* at;
GroundSize capacity;
GroundSize len;
};
struct GroundBytecodeInstruction {
enum GroundInstructionType type;
struct {
GroundSize* at;
GroundSize capacity;
GroundSize len;
} args;
};
struct GroundBytecodeHeap {
GroundValue* heap;
GroundSize capacity;
GroundSize len;
};
struct GroundBytecode {
GroundBytecodeProgram program;
GroundBytecodeHeap heap;
};
//
// INTERFACE
//
struct _Ground {
struct {
bool error;
} Flags;
struct {
// Creates a new value of the specified type.
// Returns a GroundValue
struct {
GroundValue (*Int) (GroundInt in);
GroundValue (*Double) (GroundDouble in);
GroundValue (*Char) (GroundChar in);
GroundValue (*Bool) (GroundBool in);
GroundValue (*List) (GroundList in);
GroundValue (*String) (GroundString in);
GroundValue (*Function) (GroundFunction in);
GroundValue (*Struct) (GroundStruct in);
GroundValue (*Object) (GroundObject in);
} Value;
struct {
GroundArg (*Value) (GroundValue value);
GroundArg (*ValueRef) (GroundIdentifier* ref);
GroundArg (*DirectRef) (GroundIdentifier* ref);
GroundArg (*LineRef) (GroundIdentifier* ref);
GroundArg (*LabelRef) (GroundIdentifier* ref);
GroundArg (*FunctionRef) (GroundIdentifier* ref);
GroundArg (*TypeRef) (GroundIdentifier* ref);
} Arg;
GroundList (*List) ();
GroundString (*String) (const char* in);
GroundFunction (*Function) (GroundState* state);
GroundStruct (*Struct) ();
GroundObject (*Object) (GroundStruct* in);
GroundError (*Error) (GroundValue* in);
GroundFunction (*NativeFunction) (void* function, GroundSize argc, ...);
GroundInstruction (*Instruction) (enum GroundInstructionType type);
GroundProgram (*Program) ();
GroundType (*Type) (enum GroundTypeType type, ...);
GroundIdentifier* (*Identifier) (const char* id);
GroundBytecode (*Bytecode) (GroundProgram* program, GroundState* state);
} New;
// Frees the memory held by the specified struct
struct {
void (*Value) (GroundValue* in);
void (*List) (GroundList* in);
void (*String) (GroundString* in);
void (*Function) (GroundFunction* in);
void (*Struct) (GroundStruct* in);
void (*Object) (GroundObject* in);
void (*Arg) (GroundArg* in);
void (*Instruction) (GroundInstruction* in);
void (*Program) (GroundProgram* in);
void (*State) (GroundState* state);
void (*Identifier) (GroundIdentifier* identifier);
} Free;
// Creates a copy of the memory held by the specified struct
struct {
GroundValue (*Value) (GroundValue* in);
GroundList (*List) (GroundList* in);
GroundString (*String) (GroundString* in);
GroundFunction (*Function) (GroundFunction* in);
GroundStruct (*Struct) (GroundStruct* in);
GroundObject (*Object) (GroundObject* in);
GroundArg (*Arg) (GroundArg* in);
GroundInstruction (*Instruction) (GroundInstruction* in);
GroundProgram (*Program) (GroundProgram* in);
GroundState (*State) (GroundState* in);
GroundIdentifier* (*Identifier) (GroundIdentifier* in);
} Copy;
struct {
void (*append) (GroundList* list, GroundValue value);
} List;
struct {
void (*append) (GroundInstruction* instruction, GroundArg arg);
int64_t (*execute) (GroundInstruction* instruction, GroundValue* heap);
} Instruction;
struct {
void (*append) (GroundProgram* program, GroundInstruction instruction);
void (*execute) (GroundProgram* program, GroundState* state);
} Program;
struct {
void (*appendInstruction) (GroundFunction* function, GroundInstruction instruction);
void (*appendArg) (GroundFunction* function, const char* argName);
} Function;
struct {
void (*addField) (GroundStruct* gs, const char* id, GroundValue value);
} Struct;
struct {
GroundValue* (*findVariable) (GroundState* state, const char* id);
GroundSize* (*findLabel) (GroundState* state, const char* id);
GroundCatch* (*findCatch) (GroundState* state, const char* id);
} State;
struct {
void (*run)(GroundProgram* program, GroundState* state);
void (*giveUpAndCry)();
} Internal;
struct {
void (*Error) (const char* message);
void (*Warning) (const char* message);
void (*printErrors)();
char* errors[4096];
GroundSize errorCount;
char* warnings[4096];
GroundSize warningCount;
} Log;
struct {
void (*save) (GroundBytecode* bytecode, const char* path);
GroundBytecode (*load) (const char* path);
struct {
void (*execute) (GroundBytecodeProgram* program, GroundBytecodeHeap* heap);
void (*optimise) (GroundBytecodeProgram* program);
} Program;
struct {
int64_t (*execute) (GroundBytecodeInstruction* instruction, GroundBytecodeHeap* heap);
} Instruction;
struct {
void (*set) (GroundBytecodeHeap* heap, GroundSize idx, GroundValue value);
GroundValue* (*get) (GroundBytecodeHeap* heap, GroundSize idx);
} Heap;
} Bytecode;
struct {
char* (*Arg)(GroundArg* arg);
char* (*Heap)(GroundBytecodeHeap* heap);
char* (*Instruction)(GroundInstruction* instruction);
char* (*Program)(GroundProgram* program);
char* (*String)(GroundString* string);
char* (*Value)(GroundValue* value);
} Stringify;
};
extern struct _Ground Ground;
#endif