Skip to content

Commit

Permalink
test: Enable inp_include.c test + feat: ifndef, fixes #60
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Nov 28, 2024
1 parent d849085 commit 200dda4
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
60 changes: 60 additions & 0 deletions preprocess/macro_ifndef.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdlib.h>
#include <stdio.h>
#include "macro_ifndef.h"
#include "preprocess.h"
#include <string.h>

seg_ifndef *parse_ifndef(prep_ctx *ctx, typed_token **tkns_ptr)
{
typed_token *tkn = *tkns_ptr;
if (tkn->type_id == TKN_DIRECTIVE)
{
typed_token *inner_tkn = (typed_token *)tkn->data;
tkn = tkn->next;
if (inner_tkn->type_id == TKN_ID)
{
if (strcmp((char *)inner_tkn->data, "ifndef") == 0)
{
inner_tkn = inner_tkn->next;
if (inner_tkn->type_id == TKN_ID)
{
char *def = (char *)inner_tkn->data;
inner_tkn = inner_tkn->next;

if (inner_tkn->type_id == TKN_EOF)
{
*tkns_ptr = tkn;
seg_ifndef *ret = malloc(sizeof(seg_ifndef));
ret->def = def;
return ret;
}
else
{
return NULL;
}
}
}
}
}
return NULL;
}

int is_endif(typed_token *tkn)
{
if (tkn->type_id == TKN_DIRECTIVE)
{
typed_token *inner_tkn = (typed_token *)tkn->data;
if (inner_tkn->type_id == TKN_ID)
{
if (strcmp((char *)inner_tkn->data, "endif") == 0)
{
inner_tkn = inner_tkn->next;
if (inner_tkn->type_id == TKN_EOF)
{
return 1;
}
}
}
}
return 0;
}
15 changes: 15 additions & 0 deletions preprocess/macro_ifndef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef MACRO_IFNDEF_H
#define MACRO_IFNDEF_H

#include "../linked_list.h"
#include "preprocess.h"

typedef struct
{
char *def;
} seg_ifndef;

seg_ifndef *parse_ifndef(prep_ctx *ctx, typed_token **tkns_ptr);
int is_endif(typed_token *tkn);

#endif
35 changes: 35 additions & 0 deletions preprocess/preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "macro_define.h"
#include "macro_call.h"
#include "macro_include.h"
#include "macro_ifndef.h"
#include "token.h"

typed_token *chain_tokens(linked_list *tkns)
Expand Down Expand Up @@ -37,6 +38,40 @@ typed_token *preprocess(prep_ctx *ctx, char *path)

while (tkn->type_id != TKN_EOF)
{
if (is_endif(tkn))
{
tkn = tkn->next;
continue;
}

seg_ifndef *ifndef = parse_ifndef(ctx, &tkn);
if (ifndef)
{
if (find_define(ctx, ifndef->def))
{
int endif_found = 0;
while (tkn->type_id != TKN_EOF)
{
if (is_endif(tkn))
{
tkn = tkn->next;
endif_found = 1;
break;
}
tkn = tkn->next;
}
if (!endif_found)
{
fprintf(stderr, "Couldn't find #endif!\n");
exit(1);
}
else
{
continue;
}
}
}

seg *s = NULL;
if (!s)
s = parse_token(ctx, &tkn);
Expand Down
2 changes: 1 addition & 1 deletion scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"./examples/inp_linked_list.c": [],
"./examples/inp_unary_op.c": [],
"./examples/lib_usage.c": [],
#"./examples/inp_include.c": [],
"./examples/inp_include.c": [],
"./examples/inp_func_ptrs.c": [],
"./examples/inp_goto.c": [],
"./examples/inp_break.c": [],
Expand Down

0 comments on commit 200dda4

Please sign in to comment.