Files
ground-rewrite/include/ground.h

567 lines
15 KiB
C
Raw Normal View History

2026-06-13 19:45:36 +10:00
#ifndef GROUND_H
#define GROUND_H
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <uthash.h>
2026-07-04 15:33:40 +10:00
#ifdef _WIN32
// TODO: check if this works
#include <minwindef.h>
#else
#include <limits.h>
#endif
2026-06-13 19:45:36 +10:00
//
// 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;
2026-07-02 11:35:56 +10:00
typedef struct GroundBytecodeValue GroundBytecodeValue;
typedef struct GroundBytecodeList GroundBytecodeList;
typedef struct GroundBytecodeFunction GroundBytecodeFunction;
typedef struct GroundBytecodeStruct GroundBytecodeStruct;
typedef struct GroundBytecodeObject GroundBytecodeObject;
typedef struct GroundBytecodeError GroundBytecodeError;
2026-06-20 19:45:52 +10:00
typedef struct GroundBytecodeProgram GroundBytecodeProgram;
typedef struct GroundBytecodeInstruction GroundBytecodeInstruction;
typedef struct GroundBytecodeHeap GroundBytecodeHeap;
2026-06-21 14:35:48 +10:00
typedef struct GroundBytecode GroundBytecode;
2026-06-20 19:45:52 +10:00
2026-06-13 19:45:36 +10:00
// --- 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 ---
2026-06-17 10:11:15 +10:00
typedef struct GroundIdentifier GroundIdentifier;
2026-06-13 19:45:36 +10:00
typedef struct GroundFunctionArg GroundFunctionArg;
typedef struct GroundObjectField GroundObjectField;
typedef struct GroundVariable GroundVariable;
typedef struct GroundLabel GroundLabel;
typedef struct GroundCatch GroundCatch;
2026-06-20 19:45:52 +10:00
typedef uint64_t GroundSize;
2026-06-13 19:45:36 +10:00
// --- Complex types definitions ---
struct GroundString {
char* cstr;
2026-06-20 19:45:52 +10:00
GroundSize len;
2026-06-13 19:45:36 +10:00
};
struct GroundList {
2026-06-20 19:45:52 +10:00
GroundSize capacity;
GroundSize count;
2026-06-13 19:45:36 +10:00
GroundValue* at;
};
struct GroundFunction {
struct {
2026-06-20 19:45:52 +10:00
GroundSize capacity;
GroundSize count;
2026-06-13 19:45:36 +10:00
GroundFunctionArg* at;
} args;
bool isNativeFunction;
union {
void* native;
GroundProgram* ground;
} program;
GroundState* closure;
2026-06-15 20:27:50 +10:00
2026-06-20 19:45:52 +10:00
GroundSize _requiredSize;
2026-06-13 19:45:36 +10:00
};
struct GroundStruct {
GroundObjectField* fields;
};
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,
GroundType_CoreType,
2026-06-13 19:45:36 +10:00
};
struct GroundType {
enum GroundTypeType type;
GroundStruct complexType;
};
struct GroundFunctionArg {
union {
2026-06-17 10:11:15 +10:00
GroundIdentifier* id;
2026-06-13 19:45:36 +10:00
GroundType type;
} as;
};
struct GroundValue {
union {
GroundInt Int;
GroundDouble Double;
GroundChar Char;
GroundBool Bool;
GroundString String;
GroundList List;
GroundFunction Function;
GroundStruct Struct;
GroundObject Object;
enum GroundTypeType CoreType;
2026-06-13 19:45:36 +10:00
} as;
GroundType type;
};
2026-07-05 18:01:09 +10:00
struct GroundObjectField {
char name[2048];
GroundValue value;
GroundSize offset;
UT_hash_handle hh;
};
2026-06-13 19:45:36 +10:00
enum GroundArgType {
GroundArg_Value, GroundArg_ValueRef, GroundArg_DirectRef,
GroundArg_LineRef, GroundArg_Label,
GroundArg_FunctionRef, GroundArg_TypeRef
};
struct GroundArg {
enum GroundArgType type;
union {
2026-06-17 10:11:15 +10:00
GroundIdentifier* ref;
2026-06-13 19:45:36 +10:00
GroundValue value;
} as;
2026-06-15 20:27:50 +10:00
2026-06-20 19:45:52 +10:00
GroundSize _offset;
2026-06-13 19:45:36 +10:00
};
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 {
2026-06-20 19:45:52 +10:00
GroundSize len;
GroundSize capacity;
2026-06-13 19:45:36 +10:00
GroundArg* at;
} args;
};
struct GroundProgram {
2026-06-20 19:45:52 +10:00
GroundSize len;
GroundSize capacity;
2026-06-13 19:45:36 +10:00
GroundInstruction* at;
};
struct GroundVariable {
char name[2048];
GroundValue value;
UT_hash_handle hh;
2026-06-15 20:27:50 +10:00
2026-06-20 19:45:52 +10:00
GroundSize _offset;
2026-06-13 19:45:36 +10:00
};
struct GroundLabel {
char name[2048];
2026-06-20 19:45:52 +10:00
GroundSize lineNum;
2026-06-13 19:45:36 +10:00
UT_hash_handle hh;
};
struct GroundCatch {
char name[2048];
GroundType type;
UT_hash_handle hh;
};
struct GroundState {
GroundVariable* variables;
GroundLabel* labels;
GroundCatch* catches;
2026-06-15 20:27:50 +10:00
2026-06-20 19:45:52 +10:00
GroundSize _size;
2026-06-13 19:45:36 +10:00
};
2026-06-17 10:11:15 +10:00
struct GroundIdentifier {
char* string;
2026-06-20 19:45:52 +10:00
GroundSize referenceCount;
};
2026-07-02 11:35:56 +10:00
struct GroundBytecodeList {
GroundSize capacity;
GroundSize count;
GroundBytecodeValue* at;
};
struct GroundBytecodeFunction {
struct {
GroundSize capacity;
GroundSize count;
GroundFunctionArg* at;
} args;
bool isNativeFunction;
union {
void* native;
GroundBytecodeProgram* ground;
} program;
GroundBytecodeHeap* closure;
GroundSize _requiredSize;
2026-07-04 19:50:52 +10:00
// Only for native function
GroundType* returnType;
2026-07-02 11:35:56 +10:00
};
2026-07-05 18:25:49 +10:00
struct GroundBytecodeStruct {
GroundBytecodeValue* values;
size_t size;
size_t capacity;
};
struct GroundBytecodeObject {
GroundBytecodeValue* values;
size_t size;
size_t capacity;
};
2026-07-02 11:35:56 +10:00
struct GroundBytecodeValue {
union {
GroundInt Int;
GroundDouble Double;
GroundChar Char;
GroundBool Bool;
GroundString String;
GroundBytecodeList List;
GroundBytecodeFunction Function;
GroundBytecodeStruct Struct;
GroundBytecodeObject Object;
// For core types
enum GroundTypeType CoreType;
2026-07-02 11:35:56 +10:00
} as;
GroundType type;
};
2026-06-20 19:45:52 +10:00
struct GroundBytecodeProgram {
GroundBytecodeInstruction* at;
GroundSize capacity;
GroundSize len;
};
struct GroundBytecodeInstruction {
2026-06-21 14:35:48 +10:00
enum GroundInstructionType type;
2026-06-20 19:45:52 +10:00
struct {
GroundSize* at;
GroundSize capacity;
GroundSize len;
} args;
};
struct GroundBytecodeHeap {
2026-07-02 11:35:56 +10:00
GroundBytecodeValue* heap;
2026-06-20 19:45:52 +10:00
GroundSize capacity;
2026-06-21 14:35:48 +10:00
GroundSize len;
2026-06-17 10:11:15 +10:00
};
2026-06-21 14:35:48 +10:00
struct GroundBytecode {
GroundBytecodeProgram program;
GroundBytecodeHeap heap;
};
2026-06-20 19:45:52 +10:00
// --- Internal Ground components, avoid use outside of Ground ---
enum GroundExecutionResultType {
_GER_CONTINUE, _GER_JUMP, _GER_RETURN, _GER_END
};
struct GroundExecutionResult {
enum GroundExecutionResultType type;
union {
GroundBytecodeValue value;
GroundSize line;
GroundInt end;
} as;
};
2026-07-04 15:33:40 +10:00
struct GroundOpenSharedObjects {
char path[PATH_MAX];
void* handle;
UT_hash_handle hh;
};
2026-06-13 19:45:36 +10:00
//
// 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);
2026-06-17 10:11:15 +10:00
GroundArg (*ValueRef) (GroundIdentifier* ref);
GroundArg (*DirectRef) (GroundIdentifier* ref);
GroundArg (*LineRef) (GroundIdentifier* ref);
GroundArg (*LabelRef) (GroundIdentifier* ref);
GroundArg (*FunctionRef) (GroundIdentifier* ref);
GroundArg (*TypeRef) (GroundIdentifier* ref);
2026-06-13 19:45:36 +10:00
} Arg;
GroundList (*List) ();
GroundString (*String) (const char* in);
GroundFunction (*Function) (GroundState* state);
GroundStruct (*Struct) ();
GroundObject (*Object) (GroundStruct* in);
GroundError (*Error) (GroundValue* in);
2026-06-20 19:45:52 +10:00
GroundFunction (*NativeFunction) (void* function, GroundSize argc, ...);
2026-06-13 19:45:36 +10:00
GroundInstruction (*Instruction) (enum GroundInstructionType type);
GroundProgram (*Program) ();
GroundType (*Type) (enum GroundTypeType type, ...);
2026-06-17 10:11:15 +10:00
GroundIdentifier* (*Identifier) (const char* id);
2026-07-02 11:35:56 +10:00
GroundBytecode (*Bytecode) (GroundProgram* program, GroundState* state);
GroundBytecodeValue (*BytecodeValue) (GroundValue value);
2026-06-21 14:35:48 +10:00
2026-06-13 19:45:36 +10:00
} 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);
2026-06-17 10:11:15 +10:00
void (*Identifier) (GroundIdentifier* identifier);
2026-07-02 11:35:56 +10:00
void (*BytecodeValue) (GroundBytecodeValue* value);
2026-06-13 19:45:36 +10:00
} 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);
2026-06-17 10:11:15 +10:00
GroundIdentifier* (*Identifier) (GroundIdentifier* in);
GroundBytecodeValue (*BytecodeValue) (GroundBytecodeValue* in);
2026-06-13 19:45:36 +10:00
} Copy;
struct {
void (*append) (GroundList* list, GroundValue value);
} List;
struct {
void (*append) (GroundInstruction* instruction, GroundArg arg);
2026-06-16 19:19:37 +10:00
int64_t (*execute) (GroundInstruction* instruction, GroundValue* heap);
2026-06-13 19:45:36 +10:00
} Instruction;
struct {
2026-07-02 11:35:56 +10:00
void (*append) (GroundProgram* program, GroundInstruction instruction);
void (*execute) (GroundProgram* program, GroundState* state);
GroundProgram (*preprocess) (GroundProgram* program, GroundState* state);
2026-06-13 19:45:36 +10:00
} 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;
2026-06-15 20:27:50 +10:00
struct {
GroundValue* (*findVariable) (GroundState* state, const char* id);
2026-06-20 19:45:52 +10:00
GroundSize* (*findLabel) (GroundState* state, const char* id);
2026-06-15 20:27:50 +10:00
GroundCatch* (*findCatch) (GroundState* state, const char* id);
} State;
struct {
void (*run)(GroundProgram* program, GroundState* state);
void (*giveUpAndCry)();
} Internal;
struct {
2026-06-21 14:35:48 +10:00
void (*Error) (const char* message);
2026-06-15 20:27:50 +10:00
void (*Warning) (const char* message);
void (*printErrors)();
char* errors[4096];
2026-06-20 19:45:52 +10:00
GroundSize errorCount;
2026-06-15 20:27:50 +10:00
char* warnings[4096];
2026-06-20 19:45:52 +10:00
GroundSize warningCount;
2026-06-15 20:27:50 +10:00
} Log;
2026-06-20 19:45:52 +10:00
struct {
2026-06-21 16:13:00 +10:00
void (*save) (GroundBytecode* bytecode, const char* path);
GroundBytecode (*load) (const char* path);
2026-06-20 19:45:52 +10:00
struct {
GroundBytecodeValue (*execute) (GroundBytecodeProgram* program, GroundBytecodeHeap* heap);
void (*optimise) (GroundBytecodeProgram* program);
2026-06-20 19:45:52 +10:00
} Program;
2026-06-21 14:35:48 +10:00
struct {
struct GroundExecutionResult (*execute) (GroundBytecodeInstruction* instruction, GroundBytecodeHeap* heap);
2026-06-21 14:35:48 +10:00
} Instruction;
struct {
2026-07-04 19:59:00 +10:00
void (*set) (GroundBytecodeHeap* heap, GroundSize idx, GroundBytecodeValue value);
2026-06-21 14:35:48 +10:00
GroundValue* (*get) (GroundBytecodeHeap* heap, GroundSize idx);
} Heap;
2026-06-20 19:45:52 +10:00
} Bytecode;
2026-06-25 18:57:53 +10:00
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);
2026-07-02 11:35:56 +10:00
char* (*BytecodeValue)(GroundBytecodeValue* value);
2026-06-25 18:57:53 +10:00
} Stringify;
2026-07-04 15:33:40 +10:00
struct {
struct GroundOpenSharedObjects* objects;
void* (*openSharedObject)(char* id);
void* (*getFunction)(void* handle, char* id);
} FFI;
2026-06-13 19:45:36 +10:00
};
extern struct _Ground Ground;
#endif