Files
cground/include/groundvm.h

138 lines
3.1 KiB
C

#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>
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, USE, EXTERN, CREATELABEL, PAUSE
} GroundInstType;
typedef enum GroundValueType {
INT, DOUBLE, STRING, CHAR, BOOL, LIST, FUNCTION, CUSTOM, 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;
/*
* Stores literal values created in a Ground program.
*/
typedef struct GroundValue {
GroundValueType type;
union {
int64_t intVal;
double doubleVal;
char* stringVal;
char charVal;
bool boolVal;
List listVal;
struct GroundFunction* fnVal;
void* 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;
#ifdef __cplusplus
extern "C" {
#endif
GroundProgram groundCreateProgram();
void groundAddInstructionToProgram(GroundProgram* program, GroundInstruction instruction);
GroundValue groundRunProgram(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, ...);
#ifdef __cplusplus
}
#endif
#endif