Skip to content

Commit

Permalink
chore: set up project with scanner, disassembler
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsahebgiri committed Jun 1, 2023
0 parents commit 5d0ae8d
Show file tree
Hide file tree
Showing 16 changed files with 841 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Directories
build/

test.cpp
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"stdio.h": "c",
"common.h": "c"
}
}
60 changes: 60 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
CC = gcc
CFLAGS = -fPIC
DEBUG_CFLAGS = -D DEBUG -g3 -Og
RELEASE_CFLAGS = -g -O3
LDFLAGS = -lm

TARGET_EXEC = byte
BUILD_DIR = ./build/

SRC_DIRS = ./src/core/
INC_DIRS = ./src/include/

BIN_DIR = bin/
OBJ_DIR = obj/

SRCS := $(foreach DIR,$(SRC_DIRS),$(wildcard $(DIR)*.c))
OBJS := $(SRCS:.c=.o)
DEPS := $(OBJS:.o=.d)

INC_FLAGS := $(addprefix -I,$(INC_DIRS))
DEP_FLAGS = -MMD -MP
CC_FLAGS = $(INC_FLAGS) $(DEP_FLAGS) $(CFLAGS)

DEBUG_DIR = $(BUILD_DIR)Debug/
DEBUG_TARGET = $(DEBUG_DIR)$(BIN_DIR)$(TARGET_EXEC)
DEBUG_OBJS := $(addprefix $(DEBUG_DIR)$(OBJ_DIR), $(OBJS))

RELEASE_DIR = $(BUILD_DIR)Release/
RELEASE_TARGET = $(RELEASE_DIR)$(BIN_DIR)$(TARGET_EXEC)
RELEASE_OBJS := $(addprefix $(RELEASE_DIR)$(OBJ_DIR), $(OBJS))

.PHONY: debug release all clean

# default; target if run as `make`
debug: $(DEBUG_TARGET)

$(DEBUG_TARGET): $(DEBUG_OBJS)
@mkdir -p $(dir $@)
$(CC) $^ -o $@ $(LDFLAGS)

$(DEBUG_DIR)$(OBJ_DIR)%.o: %.c
@mkdir -p $(dir $@)
$(CC) $(CC_FLAGS) $(DEBUG_CFLAGS) -c $< -o $@

release: $(RELEASE_TARGET)

$(RELEASE_TARGET): $(RELEASE_OBJS)
@mkdir -p $(dir $@)
$(CC) $^ -o $@ $(LDFLAGS)

$(RELEASE_DIR)$(OBJ_DIR)%.o: %.c
@mkdir -p $(dir $@)
$(CC) $(CC_FLAGS) $(RELEASE_CFLAGS) -c $< -o $@

all: debug release

clean:
rm -rf $(BUILD_DIR)

-include $(DEPS)
37 changes: 37 additions & 0 deletions src/core/chunk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "chunk.h"
#include "common.h"
#include "memory.h"

void initChunk(Chunk* chunk) {
chunk->count = 0;
chunk->capacity = 0;
chunk->code = NULL;
chunk->lines = NULL;
initValueArray(&chunk->constants);
}

void writeChunk(Chunk* chunk, uint8_t byte, int line) {
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->lines = GROW_ARRAY(int, chunk->lines, oldCapacity, chunk->capacity);
}

chunk->code[chunk->count] = byte;
chunk->lines[chunk->count] = line;
chunk->count++;
}

void freeChunk(Chunk* chunk) {
FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
FREE_ARRAY(int, chunk->lines, chunk->capacity);
freeValueArray(&chunk->constants);
initChunk(chunk);
}

int addConstant(Chunk* chunk, Value value) {
writeValueArray(&chunk->constants, value);
return chunk->constants.count - 1;
}
22 changes: 22 additions & 0 deletions src/core/chunk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef BYTE_CHUNK_H
#define BYTE_CHUNK_H

#include "common.h"
#include "value.h"

typedef enum { OP_CONSTANT, OP_RETURN } OpCode;

typedef struct {
int count;
int capacity;
uint8_t* code;
int* lines;
ValueArray constants;
} Chunk;

void initChunk(Chunk* chunk);
void freeChunk(Chunk* chunk);
void writeChunk(Chunk* chunk, uint8_t byte, int line);
int addConstant(Chunk* chunk, Value value);

#endif
19 changes: 19 additions & 0 deletions src/core/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef BYTE_COMMON_H
#define BYTE_COMMON_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>


#define MAX_INTERPOLATION_NESTING 8

// Fixes vasprintf usage
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif


#define BYTE_COPYRIGHT "Copyright (c) 2023 Saheb Giri"

#endif
Empty file added src/core/compiler.c
Empty file.
43 changes: 43 additions & 0 deletions src/core/debug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>

#include "debug.h"

static int simpleInstruction(const char* name, int offset) {
printf("%s\n", name);
return offset + 1;
}

static int constantInstruction(const char* name, Chunk* chunk, int offset) {
uint8_t constant = chunk->code[offset + 1];
printf("%-16s %4d '", name, constant);
printValue(chunk->constants.values[constant]);
printf("'\n");
return offset + 2;
}

int disassembleInstruction(Chunk* chunk, int offset) {
printf("%04d ", offset);
if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) {
printf(" | ");
} else {
printf("%4d ", chunk->lines[offset]);
}
uint8_t instruction = chunk->code[offset];
switch (instruction) {
case OP_CONSTANT:
return constantInstruction("OP_CONSTANT", chunk, offset);
case OP_RETURN:
return simpleInstruction("OP_RETURN", offset);
default:
printf("Unknown opcode %d\n", instruction);
return offset + 1;
}
}

void disassembleChunk(Chunk* chunk, const char* name) {
printf("== %s ==\n", name);

for (int offset = 0; offset < chunk->count;) {
offset = disassembleInstruction(chunk, offset);
}
}
9 changes: 9 additions & 0 deletions src/core/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef BYTE_DEBUG_H
#define BYTE_DEBUG_H

#include "chunk.h"

void disassembleChunk(Chunk* chunk, const char* name);
int disassembleInstruction(Chunk* chunk, int offset);

#endif
98 changes: 98 additions & 0 deletions src/core/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "chunk.h"
#include "common.h"
#include "debug.h"
#include "scanner.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void repl() {
printf("Byte v.0.1 \n");

char line[1024];
for (;;) {
printf(">> ");

if (!fgets(line, sizeof(line), stdin)) {
printf("\n");
break;
}

if (memcmp(line, "exit", 4) == 0) {
break;
}

if (memcmp(line, "help", 4) == 0) {
printf("exit - Exit the program\n");
}

interpret(line);
}
}

static char* readFile(const char* path) {
FILE* file = fopen(path, "rb");

if (file == NULL) {
fprintf(stderr, "Could not open file \"%s\".\n", path);
exit(74);
}

fseek(file, 0L, SEEK_END);
size_t fileSize = ftell(file);
rewind(file);

char* buffer = (char*)malloc(fileSize + 1);

if (buffer == NULL) {
fprintf(stderr, "Not enough memory to read \"%s\".\n", path);
exit(74);
}

size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
if (bytesRead < fileSize) {
fprintf(stderr, "Could not read file \"%s\".\n", path);
exit(74);
}

buffer[bytesRead] = '\0';

fclose(file);
return buffer;
}

// static void runFile(const char* path) {
// char* source = readFile(path);
// InterpretResult result = interpret(source);
// free(source); // [owner]

// if (result == INTERPRET_COMPILE_ERROR)
// exit(65);
// if (result == INTERPRET_RUNTIME_ERROR)
// exit(70);
// }

int main(int argc, const char* argv[]) {
Chunk chunk;
initChunk(&chunk);
writeChunk(&chunk, OP_RETURN, 12);
writeChunk(&chunk, OP_RETURN, 12);
writeChunk(&chunk, OP_RETURN, 12);
writeChunk(&chunk, OP_RETURN, 12);
int constant = addConstant(&chunk, 1.2);
writeChunk(&chunk, OP_CONSTANT, 123);
writeChunk(&chunk, constant, 123);
disassembleChunk(&chunk, "TEST");
freeChunk(&chunk);

// if (argc == 1) {
// repl();
// } else if (argc == 2) {
// runFile(argv[1]);
// } else {
// fprintf(stderr, "Usage: byte <path>\n");
// exit(64);
// }
return 0;
}
12 changes: 12 additions & 0 deletions src/core/memory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "memory.h"
#include <stdlib.h>

void* reallocate(void* pointer, size_t oldSize, size_t newSize) {
if (newSize == 0) {
free(pointer);
return NULL;
}

void* result = realloc(pointer, newSize);
return result;
}
21 changes: 21 additions & 0 deletions src/core/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef BYTE_MEMORY_H
#define BYTE_MEMORY_H

#include "common.h"

#define ALLOCATE(type, count) (type*)reallocate(NULL, 0, sizeof(type) * (count))

#define FREE(type, pointer) reallocate(pointer, sizeof(type), 0)

#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
Loading

0 comments on commit 5d0ae8d

Please sign in to comment.