boilerplate hopefully down
This commit is contained in:
4
Makefile
4
Makefile
@@ -2,7 +2,7 @@ SRC=src
|
||||
OBJ=obj
|
||||
|
||||
CC=gcc
|
||||
CFLAGS=-Wall -Wextra -pedantic -std=c99 -pedantic-errors
|
||||
CFLAGS=-Wall -Wextra -O3
|
||||
|
||||
TARGET=libgravity.so
|
||||
TEST_TARGET=testapp
|
||||
@@ -17,7 +17,7 @@ $(OBJ)/%.o: $(SRC)/%.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(TEST_TARGET): test.c $(TARGET)
|
||||
$(CC) test.c -lgravity -L. -o $(TEST_TARGET)
|
||||
$(CC) $(CFLAGS) test.c -lgravity -L. -o $(TEST_TARGET)
|
||||
|
||||
test: $(TEST_TARGET)
|
||||
|
||||
|
||||
@@ -1,12 +1,35 @@
|
||||
#ifndef __GRAVITY_CORE_H
|
||||
#define __GRAVITY_CORE_H
|
||||
#ifndef __GRAVITY__CORE_H
|
||||
#define __GRAVITY__CORE_H
|
||||
|
||||
// === STRUCT DEFINITIONS === //
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// === TYPE DEFINITIONS === //
|
||||
typedef struct GravityOpaqueModule* GravityModule;
|
||||
|
||||
typedef struct GravityOpaqueValue* GravityValue;
|
||||
|
||||
typedef struct GravityOpaqueBuilder* GravityBuilder;
|
||||
|
||||
typedef struct GravityOpaqueFunction* GravityFunction;
|
||||
|
||||
typedef struct GravityOpaqueType* GravityType;
|
||||
typedef enum {
|
||||
GravityIntTypeKind = 0,
|
||||
GravityFloatTypeKind,
|
||||
GravityPointerTypeKind,
|
||||
GravityFunctionTypeKind,
|
||||
} GravityTypeKind;
|
||||
// ========================== //
|
||||
|
||||
// === FUNCTION DEFINITIONS === //
|
||||
GravityModule gravityCreateModule();
|
||||
GravityModule gravityCreateModule(void);
|
||||
GravityBuilder gravityCreateBuilder(void);
|
||||
|
||||
GravityFunction gravityNewFunction(GravityModule module, const char* name, GravityType functionType);
|
||||
|
||||
GravityType gravityCreateFunctionType(int numArgs, GravityType* argTypes, GravityType returnType);
|
||||
GravityType gravityCreateIntegerType(uint8_t width, bool isSigned);
|
||||
// ============================ //
|
||||
|
||||
#endif
|
||||
13
src/block.h
Normal file
13
src/block.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef __GRAVITY__BLOCK_H
|
||||
#define __GRAVITY__BLOCK_H
|
||||
|
||||
#include "list.h"
|
||||
#include "instruction.h"
|
||||
|
||||
UseList(GravityInstruction);
|
||||
|
||||
typedef struct {
|
||||
List(GravityInstruction) instructions;
|
||||
} GravityBlock;
|
||||
|
||||
#endif
|
||||
8
src/builder.c
Normal file
8
src/builder.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "builder.h"
|
||||
|
||||
GravityBuilder* gravityCreateBuilder(void) {
|
||||
GravityBuilder* newBuilder = malloc(sizeof(GravityBuilder));
|
||||
if (newBuilder == NULL) return newBuilder;
|
||||
|
||||
return newBuilder;
|
||||
}
|
||||
15
src/builder.h
Normal file
15
src/builder.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef __GRAVITY__BUILDER_H
|
||||
#define __GRAVITY__BUILDER_H
|
||||
|
||||
#include "block.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct GravityOpaqueBuilder* GravityBuilder;
|
||||
|
||||
struct {
|
||||
GravityBlock currentBlock;
|
||||
} GravityOpaqueBuilder;
|
||||
|
||||
GravityBuilder gravityCreateBuilder(void);
|
||||
|
||||
#endif
|
||||
@@ -1,8 +1,18 @@
|
||||
#ifndef __GRAVITY_FUNCTION_H
|
||||
#define __GRAVITY_FUNCTION_H
|
||||
#ifndef __GRAVITY__FUNCTION_H
|
||||
#define __GRAVITY__FUNCTION_H
|
||||
|
||||
typedef struct {
|
||||
#include "type.h"
|
||||
#include "block.h"
|
||||
#include "list.h"
|
||||
#include <assert.h>
|
||||
|
||||
} GravityIRFunction;
|
||||
UseList(GravityBlock);
|
||||
|
||||
typedef struct GravityOpaqueFunction* GravityFunction;
|
||||
|
||||
struct GravityOpaqueFunction {
|
||||
GravityFunctionType functionType;
|
||||
List(GravityBlock) blocks;
|
||||
};
|
||||
|
||||
#endif
|
||||
17
src/instruction.h
Normal file
17
src/instruction.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef __GRAVITY__INSTRUCTION_H
|
||||
#define __GRAVITY__INSTRUCTION_H
|
||||
|
||||
#include "value.h"
|
||||
#include <stddef.h>
|
||||
|
||||
typedef enum {
|
||||
GravityOpcodeAdd = 0
|
||||
} GravityOpcode;
|
||||
|
||||
typedef struct {
|
||||
GravityOpcode opcode;
|
||||
GravityValue* operands;
|
||||
size_t numOperands;
|
||||
} GravityInstruction;
|
||||
|
||||
#endif
|
||||
71
src/list.h
Normal file
71
src/list.h
Normal file
@@ -0,0 +1,71 @@
|
||||
// list.h - create a list of any kind in C
|
||||
// append, pop, safely retrieve
|
||||
// licenced to you under the MIT license
|
||||
// example:
|
||||
//
|
||||
// #include "list.h"
|
||||
//
|
||||
// UseList(int);
|
||||
//
|
||||
// int main() {
|
||||
// List(int) myList = newList(int);
|
||||
// append(myList, 32);
|
||||
// printf("%d\n", *get(myList, 0));
|
||||
// pop(myList);
|
||||
// destroy(myList);
|
||||
// }
|
||||
//
|
||||
#ifndef LIST_H
|
||||
#define LIST_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LIST_H_INIT_CAPACITY 32
|
||||
|
||||
#define UseList(type) struct __List_##type { type* pointer; size_t count; size_t capacity; size_t itemsize; }
|
||||
#define List(type) struct __List_##type
|
||||
|
||||
#define newList(type) ({ \
|
||||
List(type) __list_tmp = { .pointer = malloc(sizeof(type) * LIST_H_INIT_CAPACITY), .count = 0, .capacity = LIST_H_INIT_CAPACITY, .itemsize = sizeof(type) };\
|
||||
if (__list_tmp.pointer == NULL) { \
|
||||
printf("list.h:32 (newList(type)) - failed to allocate memory"); exit(1);\
|
||||
}\
|
||||
__list_tmp;\
|
||||
})
|
||||
|
||||
#define append(list, item) {\
|
||||
if (list.pointer == NULL) {\
|
||||
printf("list.h:39 (append(list, item)) - list pointer is null (perhaps you accidently destroyed the list?)"); exit(1);\
|
||||
}\
|
||||
if (list.capacity < list.count + 1) {\
|
||||
list.capacity *= 2;\
|
||||
void* __tmp_ptr = realloc(list.pointer, list.capacity * list.itemsize);\
|
||||
if (__tmp_ptr == NULL) {\
|
||||
printf("list.h:45 (append(list, item)) - failed to allocate memory"); exit(1);\
|
||||
}\
|
||||
list.pointer = __tmp_ptr;\
|
||||
}\
|
||||
list.pointer[list.count] = item;\
|
||||
list.count++;\
|
||||
}
|
||||
|
||||
#define get(list, idx) ({\
|
||||
if (list.pointer == NULL) {\
|
||||
printf("list.h:55 (get(list, idx)) - list pointer is null (perhaps you accidently destroyed the list?)"); exit(1);\
|
||||
}\
|
||||
(idx >= list.count) ? NULL : &list.pointer[idx];\
|
||||
})
|
||||
|
||||
#define pop(list) {\
|
||||
if (list.pointer == NULL) {\
|
||||
printf("list.h:65 (pop(list)) - list pointer is null (perhaps you accidently destroyed the list?)"); exit(1);\
|
||||
}\
|
||||
if (list.count > 0) list.count--;\
|
||||
}
|
||||
|
||||
#define destroy(list) if (list.pointer != NULL) {\
|
||||
free(list.pointer); list.pointer = NULL; \
|
||||
}
|
||||
|
||||
#endif
|
||||
20
src/module.c
20
src/module.c
@@ -1,10 +1,24 @@
|
||||
#include "module.h"
|
||||
|
||||
GravityOpaqueModule* gravityCreateModule() {
|
||||
GravityOpaqueModule* newModule = malloc(sizeof(GravityOpaqueModule));
|
||||
GravityModule gravityCreateModule(void) {
|
||||
GravityModule newModule = malloc(sizeof(struct GravityOpaqueModule));
|
||||
if (newModule == NULL) return NULL;
|
||||
|
||||
|
||||
newModule->functions = newList(GravityFunction);
|
||||
|
||||
return newModule;
|
||||
}
|
||||
|
||||
GravityFunction gravityNewFunction(GravityModule module, const char* name, GravityType functionType) {
|
||||
assert(functionType->typeKind != GravityFunctionTypeKind);
|
||||
|
||||
GravityFunction newFunc = malloc(sizeof(struct GravityOpaqueFunction));
|
||||
if (newFunc == NULL) return newFunc;
|
||||
|
||||
newFunc->blocks = newList(GravityBlock);
|
||||
newFunc->functionType = functionType->function;
|
||||
|
||||
append(module->functions, newFunc);
|
||||
|
||||
return newFunc;
|
||||
}
|
||||
20
src/module.h
20
src/module.h
@@ -1,12 +1,20 @@
|
||||
#ifndef __GRAVITY_MODULE_H
|
||||
#define __GRAVITY_MODULE_H
|
||||
#ifndef __GRAVITY__MODULE_H
|
||||
#define __GRAVITY__MODULE_H
|
||||
|
||||
#include "function.h"
|
||||
#include "list.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
int hi;
|
||||
} GravityOpaqueModule;
|
||||
typedef struct GravityOpaqueModule* GravityModule;
|
||||
|
||||
GravityOpaqueModule* gravityCreateModule();
|
||||
UseList(GravityFunction);
|
||||
|
||||
struct GravityOpaqueModule {
|
||||
List(GravityFunction) functions;
|
||||
};
|
||||
|
||||
GravityModule gravityCreateModule(void);
|
||||
|
||||
GravityFunction gravityNewFunction(GravityModule module, const char* name, GravityType functionType);
|
||||
|
||||
#endif
|
||||
27
src/type.c
Normal file
27
src/type.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "type.h"
|
||||
|
||||
GravityType gravityCreateType(void) {
|
||||
GravityType type = malloc(sizeof(struct GravityOpaqueType));
|
||||
return type;
|
||||
}
|
||||
|
||||
GravityType gravityCreateFunctionType(int numArgs, GravityType* argTypes, GravityType returnType) {
|
||||
GravityType type = gravityCreateType();
|
||||
if (type == NULL) return type;
|
||||
|
||||
type->function.numArgs = numArgs;
|
||||
type->function.argTypes = argTypes;
|
||||
type->function.returnType = returnType;
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
GravityType gravityCreateIntegerType(uint8_t width, bool isSigned) {
|
||||
GravityType type = gravityCreateType();
|
||||
if (type == NULL) return type;
|
||||
|
||||
type->integer.width = width;
|
||||
type->integer.isSigned = isSigned;
|
||||
|
||||
return type;
|
||||
}
|
||||
41
src/type.h
Normal file
41
src/type.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef __GRAVITY__TYPE_H
|
||||
#define __GRAVITY__TYPE_H
|
||||
|
||||
#include "list.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct GravityOpaqueType* GravityType;
|
||||
|
||||
typedef enum {
|
||||
GravityIntTypeKind = 0,
|
||||
GravityFloatTypeKind,
|
||||
GravityPointerTypeKind,
|
||||
GravityFunctionTypeKind,
|
||||
} GravityTypeKind;
|
||||
|
||||
typedef struct {
|
||||
GravityType* argTypes;
|
||||
size_t numArgs;
|
||||
GravityType returnType;
|
||||
} GravityFunctionType;
|
||||
|
||||
typedef struct {
|
||||
uint8_t width;
|
||||
bool isSigned;
|
||||
} GravityIntType;
|
||||
|
||||
struct GravityOpaqueType {
|
||||
GravityTypeKind typeKind;
|
||||
union {
|
||||
GravityFunctionType function;
|
||||
GravityIntType integer;
|
||||
};
|
||||
};
|
||||
|
||||
GravityType gravityCreateFunctionType(int numArgs, GravityType* argTypes, GravityType returnType);
|
||||
GravityType gravityCreateIntegerType(uint8_t width, bool isSigned);
|
||||
GravityType gravityCreateType(void);
|
||||
|
||||
#endif
|
||||
11
src/value.h
Normal file
11
src/value.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef __GRAVITY__VALUE_H
|
||||
#define __GRAVITY__VALUE_H
|
||||
|
||||
#include "type.h"
|
||||
|
||||
typedef struct {
|
||||
GravityType type;
|
||||
const char* name;
|
||||
} GravityValue;
|
||||
|
||||
#endif
|
||||
9
test.c
9
test.c
@@ -1,9 +1,14 @@
|
||||
#include "include/gravity/core.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int main(void) {
|
||||
GravityModule module = gravityCreateModule();
|
||||
printf("module: %p\n", module);
|
||||
GravityBuilder builder = gravityCreateBuilder();
|
||||
|
||||
GravityType charType = gravityCreateIntegerType(1, false);
|
||||
|
||||
GravityType mainFuncType = gravityCreateFunctionType(0, NULL, charType);
|
||||
GravityFunction mainFunc = gravityNewFunction(module, "main", mainFuncType);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user