Initial commit
This commit is contained in:
351
include/ground.h
Normal file
351
include/ground.h
Normal file
@@ -0,0 +1,351 @@
|
||||
#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;
|
||||
|
||||
// --- 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 GroundFunctionArg GroundFunctionArg;
|
||||
|
||||
typedef struct GroundObjectField GroundObjectField;
|
||||
|
||||
typedef struct GroundVariable GroundVariable;
|
||||
typedef struct GroundLabel GroundLabel;
|
||||
typedef struct GroundCatch GroundCatch;
|
||||
|
||||
// --- Complex types definitions ---
|
||||
|
||||
struct GroundString {
|
||||
char* cstr;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
struct GroundList {
|
||||
size_t capacity;
|
||||
size_t count;
|
||||
GroundValue* at;
|
||||
};
|
||||
|
||||
struct GroundFunction {
|
||||
struct {
|
||||
size_t capacity;
|
||||
size_t count;
|
||||
GroundFunctionArg* at;
|
||||
} args;
|
||||
|
||||
bool isNativeFunction;
|
||||
|
||||
union {
|
||||
void* native;
|
||||
GroundProgram* ground;
|
||||
} program;
|
||||
|
||||
GroundState* closure;
|
||||
};
|
||||
|
||||
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 {
|
||||
char* 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 {
|
||||
const char* ref;
|
||||
GroundValue value;
|
||||
} as;
|
||||
};
|
||||
|
||||
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 {
|
||||
size_t len;
|
||||
size_t capacity;
|
||||
GroundArg* at;
|
||||
} args;
|
||||
};
|
||||
|
||||
struct GroundProgram {
|
||||
size_t len;
|
||||
size_t capacity;
|
||||
GroundInstruction* at;
|
||||
};
|
||||
|
||||
struct GroundVariable {
|
||||
char name[2048];
|
||||
GroundValue value;
|
||||
UT_hash_handle hh;
|
||||
};
|
||||
|
||||
struct GroundLabel {
|
||||
char name[2048];
|
||||
size_t lineNum;
|
||||
UT_hash_handle hh;
|
||||
};
|
||||
|
||||
struct GroundCatch {
|
||||
char name[2048];
|
||||
GroundType type;
|
||||
UT_hash_handle hh;
|
||||
};
|
||||
|
||||
struct GroundState {
|
||||
GroundVariable* variables;
|
||||
GroundLabel* labels;
|
||||
GroundCatch* catches;
|
||||
};
|
||||
|
||||
//
|
||||
// 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) (const char* ref);
|
||||
GroundArg (*DirectRef) (const char* ref);
|
||||
GroundArg (*LineRef) (const char* ref);
|
||||
GroundArg (*LabelRef) (const char* ref);
|
||||
GroundArg (*FunctionRef) (const char* ref);
|
||||
GroundArg (*TypeRef) (const char* 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, size_t argc, ...);
|
||||
|
||||
GroundInstruction (*Instruction) (enum GroundInstructionType type);
|
||||
GroundProgram (*Program) ();
|
||||
GroundType (*Type) (enum GroundTypeType type, ...);
|
||||
|
||||
} 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);
|
||||
} 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);
|
||||
} Copy;
|
||||
|
||||
struct {
|
||||
void (*append) (GroundList* list, GroundValue value);
|
||||
} List;
|
||||
|
||||
struct {
|
||||
void (*append) (GroundInstruction* instruction, GroundArg arg);
|
||||
void (*execute) (GroundInstruction* instruction, GroundState* state);
|
||||
} 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;
|
||||
};
|
||||
|
||||
extern struct _Ground Ground;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user