summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-09-17 01:24:46 +0200
committermetamuffin <metamuffin@disroot.org>2024-09-17 01:24:46 +0200
commit84e75a2757c6faa0710887f23b4bc2a0c5328548 (patch)
treeb788b22b039f8cec30bd0ec7820a41674c670aff
parent45aba31a75b4fe732567f0b091f919ad1a5e6c15 (diff)
downloadattocc-84e75a2757c6faa0710887f23b4bc2a0c5328548.tar
attocc-84e75a2757c6faa0710887f23b4bc2a0c5328548.tar.bz2
attocc-84e75a2757c6faa0710887f23b4bc2a0c5328548.tar.zst
enum parse works 50%
-rw-r--r--attocc.c83
-rw-r--r--readme.txt5
2 files changed, 53 insertions, 35 deletions
diff --git a/attocc.c b/attocc.c
index 3bf3506..4d74a0c 100644
--- a/attocc.c
+++ b/attocc.c
@@ -20,7 +20,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#define DEBUG
const int NUM_KEYWORDS = 32;
static char *KEYWORDS[] = {
@@ -253,7 +252,15 @@ char map_escape(char c) {
void *realloc_failsafe(void *old, unsigned long size) {
void *p = realloc(old, size);
if (!p) {
- fprintf(stderr, "malloc failed\n");
+ fprintf(stderr, "malloc failed for size %lu\n", size);
+ exit(2);
+ }
+ return p;
+}
+char *strdup_failsafe(char *s) {
+ char *p = strdup(s);
+ if (!p) {
+ fprintf(stderr, "strdup failed\n");
exit(2);
}
return p;
@@ -581,8 +588,8 @@ char parse_enum_decl(struct token *tokens, struct node **node) {
printf("3 %i %i\n", tokens[p].kind, tokens[p].data.seperator);
char is_decl = 0;
- struct enum_member *members;
- unsigned long num_members;
+ struct enum_member *members = NULL;
+ unsigned long num_members = 0;
if (tokens[p].kind == TOK_SEPERATOR &&
tokens[p].data.seperator == SEP_LCURLY) {
@@ -592,31 +599,43 @@ char parse_enum_decl(struct token *tokens, struct node **node) {
int value = 0;
- while (tokens[p].kind != TOK_SEPERATOR &&
- tokens[p].data.seperator != SEP_RCURLY) {
+ while (1) {
+ if (tokens[p].kind == TOK_SEPERATOR &&
+ tokens[p].data.seperator == SEP_RCURLY)
+ break;
+
if (tokens[p].kind == TOK_END)
return 1;
if (tokens[p].kind != TOK_IDENTIFIER)
return 1;
- }
- return 0;
- char *m_ident = tokens[p].data.identifier;
- p++;
+ char *m_ident = tokens[p].data.identifier;
+
+ if (tokens[p].kind == TOK_OPERATOR &&
+ tokens[p].data.operator== OP_ASSIGN) {
+ p++;
+
+ if (tokens[p].kind == TOK_END)
+ return 1;
+
+ if (tokens[p].kind != TOK_CONSTANT ||
+ tokens[p].data.constant_kind != CONST_INT)
+ return 1;
+
+ value = tokens[p].data.constant_int_value;
+ p++;
+ }
+
+ num_members++;
+ members =
+ realloc_failsafe(NULL, sizeof(struct enum_member) * num_members);
+ struct enum_member *new_member = &members[num_members - 1];
+ new_member->value = value++;
+ new_member->name = strdup_failsafe(m_ident);
- if (tokens[p].kind == TOK_OPERATOR && tokens[p].data.operator== OP_ASSIGN) {
p++;
}
- if (tokens[p].kind != TOK_SEPERATOR ||
- tokens[p].data.seperator == SEP_COMMA)
- return 1;
-
- num_members++;
- members = realloc_failsafe(NULL, sizeof(struct enum_member) * num_members);
- struct enum_member *new_member = &members[num_members - 1];
- new_member->value = value++;
- new_member->name = strdup(m_ident);
} else if (tokens[p].kind == TOK_SEPERATOR &&
tokens[p].data.seperator == SEP_SEMICOLON) {
@@ -625,14 +644,10 @@ char parse_enum_decl(struct token *tokens, struct node **node) {
return 1;
}
- *node = malloc(sizeof(struct node));
- if (!*node) {
- fprintf(stderr, "malloc failed\n");
- return 1;
- }
+ *node = realloc_failsafe(NULL, sizeof(struct node));
(**node).kind = NO_ENUM;
(**node).data.enum_.is_decl = is_decl;
- (**node).data.enum_.name = strdup(ident);
+ (**node).data.enum_.name = strdup_failsafe(ident);
(**node).data.enum_.num_members = num_members;
(**node).data.enum_.members = members;
@@ -650,7 +665,14 @@ void debug_node(struct node *node) {
printf("NO_ENUM\n");
printf(" name=%s\n", node->data.enum_.name);
printf(" is_decl=%i\n", node->data.enum_.is_decl);
- printf(" num_members=%li\n", node->data.enum_.num_members);
+ if (node->data.enum_.is_decl) {
+ printf(" num_members=%li\n", node->data.enum_.num_members);
+ printf(" members\n");
+ for (int i = 0; i < node->data.enum_.num_members; i++) {
+ printf(" %s=%i\n", node->data.enum_.members[i].name,
+ node->data.enum_.members[i].value);
+ }
+ }
break;
case NO_BINOP:
printf("NO_BINOP\n");
@@ -714,12 +736,7 @@ int main(int argc, char **argv) {
}
source_len += size;
- source = realloc(source, source_len + 1);
- if (!source) {
- fprintf(stderr, "malloc failed\n");
- return 1;
- }
-
+ source = realloc_failsafe(source, source_len + 1);
for (int i = 0; i < size; i++)
source[source_len - size + i] = buffer[i];
}
diff --git a/readme.txt b/readme.txt
index 98388a7..4549e80 100644
--- a/readme.txt
+++ b/readme.txt
@@ -9,12 +9,13 @@ Currently it can:
What it should do:
- Preprocess
- Parse
- - Compile
+ - Compile simple programs
- Compile itself
+ - Compile everything
Compiling
---------
-Run `make attocc`.
+Run `cc attocc.c -o attocc` or `make attocc`.
Licence
-------