diff options
Diffstat (limited to 'platform/elf')
| -rw-r--r-- | platform/elf/main.c | 72 | ||||
| -rw-r--r-- | platform/elf/osal.c | 57 |
2 files changed, 129 insertions, 0 deletions
diff --git a/platform/elf/main.c b/platform/elf/main.c new file mode 100644 index 0000000..b2ebb34 --- /dev/null +++ b/platform/elf/main.c @@ -0,0 +1,72 @@ +#include <stdio.h> + +#include <lex.h> +#include <lib/vector.h> + +#include <platform/osal.h> + +#include <stdlib.h> + +int main(int argc, char **argv) +{ + printf("Platform set to 'linux-elf'.\n"); + + if (argc == 1) { + printf("You must specify a file to compile!\n"); + exit(1); + } + + struct file fl = open_file(argv[1], FILE_READ); + + struct vector token_storage = {0}; + if (vector_init(&token_storage)) { + printf("error: vector_init()"); + exit(1); + } + + struct lex lex = {0}; + lex.line = 0; + lex.buffer = fl.buffer; + lex.size = fl.size; + lex.pos = 0; + + struct token *tkn = NULL; + + size_t size = 0; + + do { + tkn = mem_alloc(sizeof(struct token)); // Change mem alloc with arena alloc + + if (tkn == NULL) { + printf("error: token alloc"); + exit(1); + } + + *tkn = lex_next(&lex); + + // printf("Tokenized: %.*s, with length: %d.\n", (int)tkn->length, tkn->lexeme, (int)tkn->type); + + if (vector_emplace_back(&token_storage, tkn) == -1) { + printf("error: vector_emplace_back()"); + exit(1); + } + + } while (tkn != NULL && + tkn->type != TOKEN_INVALID && + tkn->type != TOKEN_EOP); + + for (int i = 0; i < token_storage.size; i++) { + if (token_storage.buffer[i] == NULL) + break; + + printf("Tokenized '%.*s' with type: %d\n", //, on line: %d at position: %d\n", + (int)((struct token*) token_storage.buffer[i])->length, + ((struct token*)token_storage.buffer[i])->lexeme, + (int)((struct token*)token_storage.buffer[i])->type +// ((struct token*)token_storage.buffer[i])->line, +// ((struct token*)token_storage.buffer[i])->pos + ); + } + + return 0; +} diff --git a/platform/elf/osal.c b/platform/elf/osal.c new file mode 100644 index 0000000..414cd80 --- /dev/null +++ b/platform/elf/osal.c @@ -0,0 +1,57 @@ +#include <platform/osal.h> + +#include <stdlib.h> +#include <stdio.h> + +#include <stdarg.h> + +#include <assert.h> + +void *mem_alloc(size_t s) +{ + return malloc(s); +} + +void mem_free(void *p) +{ + free(p); +} + +struct file open_file(const char *path, int flags) +{ + assert(path != NULL); + + FILE *file; + + if (flags & FILE_READ) { + file = fopen(path, "rb"); + } else { + printf("Read option not specified for file read!"); + exit(1); + } + + fseek(file, 0, SEEK_END); + long fsize = ftell(file); + fseek(file, 0, SEEK_SET); + + char *content = malloc(fsize + 1); + fread(content, fsize, 1, file); + fclose(file); + + content[fsize] = 0; + + return (struct file) {.path=path, .buffer=content, .size=fsize}; +} + +void print(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); +} + +void exitc(int c) +{ + exit(c); +} |
