diff --git a/CMakeLists.txt b/CMakeLists.txt index 54c3be4..7b260ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # Source files file(GLOB LIB_SOURCES "src/lib/*.c") +file(GLOB AS_SOURCES "src/as/*.c") file(GLOB EMU_SOURCES "src/emu/*.c") # Library for shared code @@ -13,8 +14,11 @@ add_library(lc3tools ${LIB_SOURCES}) target_include_directories(lc3tools PUBLIC include) # Executables +add_executable(lc3as ${AS_SOURCES}) add_executable(lc3emu ${EMU_SOURCES}) +target_include_directories(lc3as PRIVATE include/as) target_include_directories(lc3emu PRIVATE include/emu) # Link shared code and executables +target_link_libraries(lc3as lc3tools) target_link_libraries(lc3emu lc3tools) diff --git a/include/as/lc3as.h b/include/as/lc3as.h new file mode 100644 index 0000000..38d0e26 --- /dev/null +++ b/include/as/lc3as.h @@ -0,0 +1,6 @@ +#ifndef __LC3AS_H +#define __LC3AS_H + +#define LC3AS_TEST 6969 + +#endif /* __LC3AS_H */ diff --git a/include/lc3tools.h b/include/lc3tools.h index 69c8cd2..09e068d 100644 --- a/include/lc3tools.h +++ b/include/lc3tools.h @@ -28,6 +28,8 @@ */ #define DEBUG 1 +#define TEST_DIR "../test" + /* * File separator character. */ diff --git a/src/as/main.c b/src/as/main.c new file mode 100644 index 0000000..bf119b5 --- /dev/null +++ b/src/as/main.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include + +#include +#include + +#define LINE_BUFFER_SIZE 256 + +struct source_file +{ + FILE *file; + char line[LINE_BUFFER_SIZE]; + size_t line_len; + size_t line_num; + size_t line_pos; +}; + +int read_line(struct source_file *src) +{ + src->line_num++; + if (feof(src->file) || fgets(src->line, LINE_BUFFER_SIZE, src->file) == 0) + { + return -1; + } + + src->line_len = strlen(src->line); + src->line_pos = 0; + + return src->line_len; +} + +int read_token(struct source_file *src, char **tok, char delim) +{ + size_t i; + // char *tok; + char *tok_head; + char *tok_tail; + char c; + int char_seen; + + if (src->line_pos >= src->line_len) + { + return 0; + } + + tok_head = &src->line[src->line_pos]; + tok_tail = tok_head; + i = 0; + char_seen = 0; + + while ((c = tok_head[i]) != '\0') + { + src->line_pos++; + if (isspace(c)) + { + if (!char_seen) + { + tok_head++; + tok_tail++; + continue; + } + else if (c == '\n') + { + *tok_tail = '\0'; + *tok = tok_head; + return 1; + } + else if (!isspace(delim)) + { + i++; + continue; + } + } + + char_seen = 1; + if (c == delim) + { + *tok_tail = '\0'; + *tok = tok_head; + return 1; + } + i++; + tok_tail++; + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + struct source_file src; + char *tok; + + memset(&src, 0, sizeof(struct source_file)); + src.file = fopen(TEST_DIR"/astest2.asm", "rb"); + if (src.file == NULL) + { + return 2; + } + + while (read_line(&src) != -1) + { + read_token(&src, &tok, ' '); + // read_token(&src, &tok, '\t'); + + printf("tok: %s\n", tok); + if (strcasecmp(tok, "ADD") == 0) + { + printf("ADD\n"); + while ((read_token(&src, &tok, ',')) != 0) + { + printf("'%s'\n", tok); + } + } + } + + printf("Hit end of file! (line %zu)\n", src.line_num); + fclose(src.file); + + return 0; +} \ No newline at end of file