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 <string.h>
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
|
List* GetTokens(const char *);
|
||||||
char* GetToken(char*);
|
char* GetToken(char*);
|
||||||
char* PeekNextToken(void);
|
char* PeekNextToken(void);
|
||||||
|
|
||||||
|
|||||||
+3
-4
@@ -5,16 +5,15 @@
|
|||||||
int ReadAllString(const char* path, char** content_ptr, size_t* bytes_read) {
|
int ReadAllString(const char* path, char** content_ptr, size_t* bytes_read) {
|
||||||
if (!path || !content_ptr || !bytes_read) return 0;
|
if (!path || !content_ptr || !bytes_read) return 0;
|
||||||
|
|
||||||
FILE *file;
|
FILE *file = fopen(path, "rb");
|
||||||
file = fopen(path, "rb");
|
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
fprintf(stderr, "Failed to open '%s'. %s.\n", path, strerror(errno));
|
fprintf(stderr, "Failed to open '%s'. %s.\n", path, strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *content = NULL, *temp;
|
char *content = NULL, *temp = NULL;
|
||||||
size_t used, capacity, read;
|
size_t used = 0, capacity = 0, read = 0;
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
if (used + FUTIL_READ_SIZE + 1 > capacity) {
|
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) {
|
int AddListItem(const void *value, size_t size, List* list) {
|
||||||
if (!list) return -1;
|
if (!list) return -1;
|
||||||
if (!value) return -1;
|
if (!value) return -1;
|
||||||
if (size == 0) return - 1;
|
if (size == 0) return -1;
|
||||||
|
|
||||||
if (list->capacity < list->size + 1) {
|
if (list->capacity < list->size + 1) {
|
||||||
void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2);
|
void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2);
|
||||||
|
|||||||
+15
-27
@@ -1,43 +1,31 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sysexits.h>
|
||||||
#include "../includes/list.h"
|
#include "../includes/list.h"
|
||||||
#include "../includes/lexer.h"
|
#include "../includes/lexer.h"
|
||||||
#include "../includes/parser.h"
|
#include "../includes/parser.h"
|
||||||
|
#include "../includes/futil.h"
|
||||||
typedef struct {
|
|
||||||
char *opcode;
|
|
||||||
unsigned char count;
|
|
||||||
char** parameters;
|
|
||||||
} Instruction;
|
|
||||||
|
|
||||||
List* get_strings(const char*);
|
List* get_strings(const char*);
|
||||||
|
|
||||||
int main(int argc, char* args[]) {
|
int main(int argc, char* args[]) {
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
printf("Input files required.\n");
|
printf("Usage: assm <file1.asm>\n");
|
||||||
return -1;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *file;
|
char* source_code;
|
||||||
int line_count = 1;
|
size_t bytes_read;
|
||||||
char* line = NULL;
|
|
||||||
size_t len = 0;
|
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
|
||||||
ssize_t bytes_read;
|
|
||||||
|
char* token = strtok(source_code, "\n");
|
||||||
int line_number = 1;
|
int line_number = 1;
|
||||||
|
|
||||||
file = fopen(args[1], "r");
|
while(token != NULL) {
|
||||||
|
|
||||||
if (!file) {
|
List* tokens = GetTokensFromLine(token);
|
||||||
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);
|
|
||||||
|
|
||||||
if (line_number == 1 || line_number == 2) {
|
if (line_number == 1 || line_number == 2) {
|
||||||
ParseTokens(tokens);
|
ParseTokens(tokens);
|
||||||
@@ -53,10 +41,10 @@ int main(int argc, char* args[]) {
|
|||||||
|
|
||||||
DestroyList(tokens);
|
DestroyList(tokens);
|
||||||
|
|
||||||
|
token = strtok(NULL, "\n");
|
||||||
|
|
||||||
line_number++;
|
line_number++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
free(source_code);
|
||||||
|
|
||||||
if (line) free(line);
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user