add initial compiler, add tests

This commit is contained in:
zongor 2025-05-03 21:07:19 -04:00
parent 281211d150
commit 58f7e30357
16 changed files with 248 additions and 9 deletions

56
.gitignore vendored
View File

@ -43,8 +43,62 @@ flycheck_*.el
# directory configuration # directory configuration
.dir-locals.el .dir-locals.el
.ccls-cache/
# network security # network security
/network-security.data /network-security.data
.ccls-cache/ # 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

View File

@ -4,15 +4,15 @@ fn build(ProjectConfig c) {
c.client([ c.client([
LanguageSettings( LanguageSettings(
"c", ! lang "c", // lang
"src/client.ztl", ! file "src/client.ztl", // file
"client/", ! out path "client/", // out path
[ ! ffi settings [ // ffi settings
FFISetting ( FFISetting (
"raylib", ! libary name "raylib", // libary name
"$RAYLIB_PATH/libraylib.a", ! path "$RAYLIB_PATH/libraylib.a", // path
"./", ! local path "./", // local path
"make build", ! build command "make build", // build command
) )
] ]
) )

39
src/Makefile Normal file
View File

@ -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/

22
src/chunk.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdlib.h>
#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++;
}

21
src/chunk.h Normal file
View File

@ -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

View File

@ -0,0 +1,8 @@
#ifndef ztl_common_h
#define ztl_common_h
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#endif

29
src/debug.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#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;
}
}

9
src/debug.h Normal file
View File

@ -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

14
src/main.c Normal file
View File

@ -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;
}

21
src/memory.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdlib.h>
#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);
}

21
src/memory.h Normal file
View File

@ -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

1
test/add.ztl Normal file
View File

@ -0,0 +1 @@
1 + 2;