diff options
-rw-r--r-- | attocc.c | 50 | ||||
-rw-r--r-- | makefile | 4 |
2 files changed, 51 insertions, 3 deletions
@@ -18,7 +18,6 @@ #include <fcntl.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <unistd.h> const int NUM_KEYWORDS = 32; @@ -257,6 +256,38 @@ void *realloc_failsafe(void *old, unsigned long size) { } return p; } + +void *memcpy(void *dest, const void *src, unsigned long size) { + char *dest_ = (char *)dest; + char *src_ = (char *)src; + while (size--) + *dest_++ = *src_++; + return dest; +} + +// not to specification but this program only needs to check equality +int strncmp(const char *a, const char *b, unsigned long len) { + while (len--) + if (*a++ != *b++) + return 1; + return 0; +} + +unsigned long strlen(const char *s) { + int len = 0; + while (s[len++] != 0) + ; + return len - 1; +} +char *strdup(const char *s) { + int len = strlen(s); + char *new_s = realloc(NULL, len + 1); + if (!new_s) + return NULL; + memcpy(new_s, s, len + 1); + return new_s; +} + char *strdup_failsafe(char *s) { char *p = strdup(s); if (!p) { @@ -279,12 +310,15 @@ struct token *tokenize(char *source) { struct token *tokens = NULL; while (*p) { unsigned long remaining = end - p; + printf("%li\n", p - source); + printf("a\n"); //* whitespace if (p[0] == ' ' || p[0] == '\t' || p[0] == '\n') { p++; continue; } + printf("b\n"); //* comments if (remaining >= 2) { @@ -305,6 +339,7 @@ struct token *tokenize(char *source) { continue; } } + printf("c\n"); //* seperators char match = 0; @@ -323,10 +358,14 @@ struct token *tokenize(char *source) { if (match) continue; + printf("ca\n"); //* operators match = 0; for (int i = 0; i < NUM_OPERATORS; i++) { + printf("caa\n"); char *op = OPERATORS[i]; + printf("cab\n"); + printf("%li %s\n", strlen(op), op); if (remaining >= strlen(op)) { if (strncmp(op, p, strlen(op)) == 0) { struct token *new_token = token_push(&tokens, &num_tokens); @@ -341,8 +380,10 @@ struct token *tokenize(char *source) { } } } + printf("cb\n"); if (match) continue; + printf("d\n"); //* keyword match = 0; @@ -396,6 +437,7 @@ struct token *tokenize(char *source) { new_token->data.constant_int_value = value; continue; } + printf("e\n"); //* string constant if (p[0] == '"') { @@ -441,6 +483,7 @@ struct token *tokenize(char *source) { new_token->data.constant_char_value = chr; continue; } + printf("f\n"); //* identifier if (is_ident(p[0]) && !is_numeric(p[0])) { @@ -712,18 +755,21 @@ int main(int argc, char **argv) { char *input = argv[1]; char *output = argv[2]; + printf("open input\n"); int input_fd = open(input, O_RDONLY | O_CLOEXEC); if (input_fd < 0) { perror("cannot open input"); return 1; } + printf("open output\n"); int output_fd = open(output, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0640); if (output_fd < 0) { perror("cannot open input"); return 1; } + printf("read input\n"); int source_len = 0; char *source = NULL; int size; @@ -742,6 +788,7 @@ int main(int argc, char **argv) { } source[source_len] = '\0'; + printf("tokenize\n"); struct token *tokens = tokenize(source); if (!tokens) return 1; @@ -750,6 +797,7 @@ int main(int argc, char **argv) { debug_tokens(tokens); #endif + printf("parse\n"); struct node *nodes = NULL; if (parse(tokens, &nodes)) { printf("parse failed\n"); @@ -5,11 +5,11 @@ clean: rm $(ALL) attocc: attocc.c - cc -O3 -o $@ $< + cc -DDEBUG -O3 -o $@ $< attocc-debug: attocc.c cc -DDEBUG -Wall -Wextra -Wpedantic -o $@ $< attocc-small: attocc.c - cc -Wl,--gc-sections -flto -ffunction-sections -fdata-sections -fPIC -s -Os -o $@ $< + cc -DDEBUG -Wl,--gc-sections -flto -ffunction-sections -fdata-sections -fPIC -s -Os -o $@ $< strip -x $@ |