diff --git a/.gitignore b/.gitignore index 5aa86cd..72692b6 100644 --- a/.gitignore +++ b/.gitignore @@ -43,8 +43,62 @@ flycheck_*.el # directory configuration .dir-locals.el +.ccls-cache/ # network security /network-security.data -.ccls-cache/ \ No newline at end of file +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +zlc \ No newline at end of file diff --git a/docs/project-example/language_ztl.lua b/docs/project-syntax-example/language_ztl.lua similarity index 100% rename from docs/project-example/language_ztl.lua rename to docs/project-syntax-example/language_ztl.lua diff --git a/docs/project-example/project.ztl b/docs/project-syntax-example/project.ztl similarity index 64% rename from docs/project-example/project.ztl rename to docs/project-syntax-example/project.ztl index da230b7..b492b8d 100644 --- a/docs/project-example/project.ztl +++ b/docs/project-syntax-example/project.ztl @@ -4,15 +4,15 @@ fn build(ProjectConfig c) { c.client([ LanguageSettings( - "c", ! lang - "src/client.ztl", ! file - "client/", ! out path - [ ! ffi settings + "c", // lang + "src/client.ztl", // file + "client/", // out path + [ // ffi settings FFISetting ( - "raylib", ! libary name - "$RAYLIB_PATH/libraylib.a", ! path - "./", ! local path - "make build", ! build command + "raylib", // libary name + "$RAYLIB_PATH/libraylib.a", // path + "./", // local path + "make build", // build command ) ] ) diff --git a/docs/project-example/src/client.ztl b/docs/project-syntax-example/src/client.ztl similarity index 100% rename from docs/project-example/src/client.ztl rename to docs/project-syntax-example/src/client.ztl diff --git a/docs/project-example/src/common.ztl b/docs/project-syntax-example/src/common.ztl similarity index 100% rename from docs/project-example/src/common.ztl rename to docs/project-syntax-example/src/common.ztl diff --git a/docs/project-example/src/server.ztl b/docs/project-syntax-example/src/server.ztl similarity index 100% rename from docs/project-example/src/server.ztl rename to docs/project-syntax-example/src/server.ztl diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..4f11d13 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,39 @@ +# Compiler and flags +CC = gcc +CFLAGS += -std=c11 -g -Wall -Wextra -Werror -Wno-unused-parameter +LDFLAGS += +LDLIBS += + +# Source and build configuration +SRC = $(wildcard *.c) +OBJ = $(SRC:.c=.o) +EXEC = zlc + +# Phony targets +.PHONY: all clean test install + +# Default target +all: $(EXEC) + +# Main build rule +$(EXEC): $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) $(LDLIBS) -o $@ + +# Pattern rule for object files +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +# Test target with dependency +test: $(EXEC) + @echo "Running tests..." + ./$(EXEC) + @echo "Tests completed successfully." + +# Clean build artifacts +clean: + $(RM) $(OBJ) $(EXEC) + +# Install target (example) +install: $(EXEC) + install -d $(DESTDIR)/usr/local/bin + install $(EXEC) $(DESTDIR)/usr/local/bin/ diff --git a/src/chunk.c b/src/chunk.c new file mode 100644 index 0000000..e36ddd4 --- /dev/null +++ b/src/chunk.c @@ -0,0 +1,22 @@ +#include + +#include "chunk.h" + +void newChunk(Chunk* chunk) { + chunk->count = 0; + chunk->capacity = 0; + chunk->code = NULL; +} + +void writeChunk(Chunk *chunk, uint8_t byte) { + if (chunk->capacity < chunk->count + 1) { + int oldCapacity = chunk->capacity; + chunk->capacity = GROW_CAPACITY(oldCapacity); + chunk->code = GROW_ARRAY(uint8_t, chunk->code, + oldCapacity, chunk->capacity); + } + + chunk->code[chunk->count] = byte; + chunk->count++; + +} diff --git a/src/chunk.h b/src/chunk.h new file mode 100644 index 0000000..6f75384 --- /dev/null +++ b/src/chunk.h @@ -0,0 +1,21 @@ +#ifndef ztl_chunk_h +#define ztl_chunk_h + +#include "common.h" +#include "memory.h" + +typedef enum { + OP_RETURN, +} OpCode; + +typedef struct Chunk { + int count; + int capacity; + uint8_t *code; +} Chunk; + +void newChunk(Chunk *chunk); +void freeChunk(Chunk *chunk); +void writeChunk(Chunk *chunk, uint8_t byte); + +#endif diff --git a/src/common.h b/src/common.h index e69de29..845c554 100644 --- a/src/common.h +++ b/src/common.h @@ -0,0 +1,8 @@ +#ifndef ztl_common_h +#define ztl_common_h + +#include +#include +#include + +#endif diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000..4eb7eba --- /dev/null +++ b/src/debug.c @@ -0,0 +1,29 @@ +#include + +#include "debug.h" + +void disassembleChunk(Chunk* chunk, const char* name) { + printf("== %s ==\n", name); + + for (int offset = 0; offset < chunk->count;) { + offset = disassembleInstruction(chunk, offset); + } +} + +static int simpleInstruction(const char* name, int offset) { + printf("%s\n", name); + return offset + 1; +} + +int disassembleInstruction(Chunk* chunk, int offset) { + printf("%04d ", offset); + + uint8_t instruction = chunk->code[offset]; + switch (instruction) { + case OP_RETURN: + return simpleInstruction("OP_RETURN", offset); + default: + printf("Unknown opcode %d\n", instruction); + return offset + 1; + } +} diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..0495cda --- /dev/null +++ b/src/debug.h @@ -0,0 +1,9 @@ +#ifndef ztl_debug_h +#define ztl_debug_h + +#include "chunk.h" + +void disassembleChunk(Chunk* chunk, const char* name); +int disassembleInstruction(Chunk* chunk, int offset); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..245eb31 --- /dev/null +++ b/src/main.c @@ -0,0 +1,14 @@ +#include "common.h" +#include "chunk.h" +#include "debug.h" + +int main(int argc, const char** argv) { + Chunk chunk; + newChunk(&chunk); + writeChunk(&chunk, OP_RETURN); + + disassembleChunk(&chunk, "test chunk"); + freeChunk(&chunk); + return 0; +} + diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..c93a6b3 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,21 @@ +#include + +#include "chunk.h" +#include "memory.h" + +void* reallocate(void *pointer, size_t oldSize, size_t newSize) { + if (newSize == 0) { + free(pointer); + return NULL; + } + + void *result = realloc(pointer, newSize); + if (result == NULL) exit(1); + return result; +} + +void freeChunk(Chunk *chunk) { + FREE_ARRAY(uint8_t, chunk->code, chunk->capacity); + newChunk(chunk); +} + diff --git a/src/memory.h b/src/memory.h new file mode 100644 index 0000000..d5b805d --- /dev/null +++ b/src/memory.h @@ -0,0 +1,21 @@ +#ifndef ztl_memory_h +#define ztl_memory_h + +#include "common.h" + +#define GROW_CAPACITY(capacity) \ + ((capacity) < 8 ? 8 : (capacity) * 2) + + +#define GROW_ARRAY(type, pointer, oldCount, newCount) \ + (type*)reallocate(pointer, sizeof(type) * (oldCount), \ + sizeof(type) * (newCount)) + +#define FREE_ARRAY(type, pointer, oldCount) \ + reallocate(pointer, sizeof(type) * (oldCount), 0) + +void* reallocate(void* pointer, size_t oldSize, size_t newSize); + + +#endif + diff --git a/test/add.ztl b/test/add.ztl new file mode 100644 index 0000000..0e92774 --- /dev/null +++ b/test/add.ztl @@ -0,0 +1 @@ +1 + 2;