diff --git a/.gitignore b/.gitignore index fe4a28d..c8cc4d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -assm \ No newline at end of file +assm +obj/ +bin/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5c4960a --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +CC = gcc +CFLAGS=-g -Wall -DDEBUG -Wpedantic +SRCDIR=src +OBJDIR=obj +SRCS=$(wildcard $(SRCDIR)/*.c) +# Substitute all .c with .o from SRCS +OBJS=$(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SRCS)) + +BINDIR=bin +BIN=$(BINDIR)/assm + +all: $(BIN) + +release: CFLAGS=-Wall -Wpedantic -O2 +release: clean +release: $(BIN) + +$(BIN): $(OBJS) $(BINDIR) + $(CC) $(CFLAGS) $(OBJS) -o $@ + +$(OBJDIR)/%.o: $(SRCDIR)/%.c $(OBJDIR) + $(CC) $(CFLAGS) -c $< -o $@ + +$(BINDIR): + mkdir $@ + +$(OBJDIR): + mkdir $@ + +.PHONY: clean +.PHONY: test + +clean: + rm -rf $(BINDIR)/* $(OBJDIR)/* + +test: + $(BIN) misc/test.asm \ No newline at end of file diff --git a/includes/tokenizer.h b/includes/tokenizer.h index a074cc6..a853fac 100644 --- a/includes/tokenizer.h +++ b/includes/tokenizer.h @@ -6,6 +6,7 @@ #include #include "list.h" +List* GetTokens(const char *); char* GetToken(char*); char* PeekNextToken(void); diff --git a/src/futil.c b/src/futil.c index ee18f6c..01b5d6d 100644 --- a/src/futil.c +++ b/src/futil.c @@ -5,16 +5,15 @@ int ReadAllString(const char* path, char** content_ptr, size_t* bytes_read) { if (!path || !content_ptr || !bytes_read) return 0; - FILE *file; - file = fopen(path, "rb"); + FILE *file = fopen(path, "rb"); if (!file) { fprintf(stderr, "Failed to open '%s'. %s.\n", path, strerror(errno)); return 0; } - char *content = NULL, *temp; - size_t used, capacity, read; + char *content = NULL, *temp = NULL; + size_t used = 0, capacity = 0, read = 0; while(1) { if (used + FUTIL_READ_SIZE + 1 > capacity) { diff --git a/src/list.c b/src/list.c index 3dcfdbe..c87acc9 100644 --- a/src/list.c +++ b/src/list.c @@ -25,7 +25,7 @@ List* CreateList() { int AddListItem(const void *value, size_t size, List* list) { if (!list) return -1; if (!value) return -1; - if (size == 0) return - 1; + if (size == 0) return -1; if (list->capacity < list->size + 1) { void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2); diff --git a/src/main.c b/src/main.c index 04148b2..370cc5d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,43 +1,31 @@ #include #include #include +#include #include "../includes/list.h" #include "../includes/lexer.h" #include "../includes/parser.h" - -typedef struct { - char *opcode; - unsigned char count; - char** parameters; -} Instruction; +#include "../includes/futil.h" List* get_strings(const char*); int main(int argc, char* args[]) { if (argc == 1) { - printf("Input files required.\n"); - return -1; + printf("Usage: assm \n"); + return EX_USAGE; } - FILE *file; - int line_count = 1; - char* line = NULL; - size_t len = 0; - ssize_t bytes_read; + char* source_code; + size_t bytes_read; + + if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR; + + char* token = strtok(source_code, "\n"); int line_number = 1; - file = fopen(args[1], "r"); + while(token != NULL) { - if (!file) { - printf("Failed to open '%s' for reading.\n", args[1]); - return 1; - } - - //getline() uses realloc() for the line parameter, so there shouldn't - //be any issue with memory leaks as long as the last time this function - //is called, line gets free()ed. - while((bytes_read = getline(&line, &len, file)) != -1) { - List* tokens = GetTokensFromLine(line); + List* tokens = GetTokensFromLine(token); if (line_number == 1 || line_number == 2) { ParseTokens(tokens); @@ -53,10 +41,10 @@ int main(int argc, char* args[]) { DestroyList(tokens); + token = strtok(NULL, "\n"); + line_number++; } - fclose(file); - - if (line) free(line); + free(source_code); } \ No newline at end of file