Files
cground/include/groundvm.h

193 lines
4.4 KiB
C
Raw Normal View History

#ifndef LIBGROUND_H
#define LIBGROUND_H
/*
* groundvm.h
* Provides an interface for external programs wanting to run Ground code.
*
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdarg.h>
2026-01-20 21:17:08 +11:00
#include <uthash.h>
typedef enum GroundInstType {
2026-01-21 18:59:24 +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, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD
} GroundInstType;
typedef enum GroundValueType {
2026-01-20 20:17:26 +11:00
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 List;
/*
* Custom data type that stores Ground values.
*/
typedef struct List {
size_t size;
struct GroundValue* values;
} List;
2026-01-20 21:17:08 +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;
bool hasLine;
} GroundError;
/*
* Stores literal values created in a Ground program.
*/
2026-01-23 14:32:27 +11:00
typedef struct __attribute__((packed)) GroundValue {
GroundValueType type;
2026-01-22 10:50:48 +11:00
struct GroundStruct* customType;
union {
int64_t intVal;
double doubleVal;
char* stringVal;
char charVal;
bool boolVal;
List listVal;
2026-01-20 21:17:08 +11:00
GroundError errorVal;
struct GroundFunction* fnVal;
2026-01-20 21:17:08 +11:00
struct GroundStruct* structVal;
struct GroundObject* customVal;
} data;
} GroundValue;
/*
* Indicates status when accessing a list.
*/
typedef struct ListAccess {
ListAccessStatus status;
GroundValue* value;
} ListAccess;
/*
* Stores arguments for the GroundInstruction struct.
*/
typedef struct GroundArg {
GroundArgType type;
union {
GroundValue value;
char* refName;
} value;
} GroundArg;
/*
* Represents a Ground instruction.
*/
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;
char* name;
} GroundFunctionArgs;
/*
* Represents a Ground function.
*/
typedef struct GroundFunction {
GroundFunctionArgs* args;
size_t argSize;
GroundValueType returnType;
GroundProgram program;
size_t startLine;
} GroundFunction;
2026-01-20 21:17:08 +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-13 13:20:55 +11:00
#ifdef __cplusplus
extern "C" {
#endif
GroundProgram groundCreateProgram();
void groundAddInstructionToProgram(GroundProgram* program, GroundInstruction instruction);
GroundValue groundRunProgram(GroundProgram* program);
void groundPrintProgram(GroundProgram* program);
2026-01-21 15:54:18 +11:00
char* groundCompileProgram(GroundProgram* program);
GroundInstruction groundCreateInstruction(GroundInstType type);
void groundAddValueToInstruction(GroundInstruction* inst, GroundValue value);
void groundAddReferenceToInstruction(GroundInstruction* inst, GroundArg value);
GroundArg groundCreateReference(GroundArgType type, char* ref);
GroundValue groundCreateValue(GroundValueType type, ...);
2025-12-29 19:13:36 +11:00
GroundProgram groundParseFile(const char* code);
2026-01-20 21:17:08 +11:00
GroundStruct groundCreateStruct();
void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field);
2025-12-13 13:20:55 +11:00
#ifdef __cplusplus
}
#endif
#endif