Setup the assembler to use the new file reading code.
This commit is contained in:
+3
-1
@@ -1 +1,3 @@
|
||||
assm
|
||||
assm
|
||||
obj/
|
||||
bin/
|
||||
@@ -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
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <string.h>
|
||||
#include "list.h"
|
||||
|
||||
List* GetTokens(const char *);
|
||||
char* GetToken(char*);
|
||||
char* PeekNextToken(void);
|
||||
|
||||
|
||||
+3
-4
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
+15
-27
@@ -1,43 +1,31 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sysexits.h>
|
||||
#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 <file1.asm>\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);
|
||||
}
|
||||
Reference in New Issue
Block a user