Skip to content

Commit

Permalink
feat: Implement ifndef, fixes #60
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Nov 28, 2024
1 parent 2fdc411 commit 0bb9be5
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 208 deletions.
23 changes: 4 additions & 19 deletions examples/inp_preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@ void printf(char *, ...);

#define DEBUG 1

#ifdef DEBUG
int function() {
return 69;
}
#endif
int main()
{

#ifdef RELEASE
int function() {
return 13;
}
#ifndef DEBUG
printf("Debug not enabled!");
#endif

#ifndef RELEASE
int function2() {
return 99;
}
#endif

int main() {
printf("function(): %d\n", function());
printf("function2(): %d\n", function2());
return 0;
}
82 changes: 82 additions & 0 deletions preprocess/macro_ifndef.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <stdlib.h>
#include <stdio.h>
#include "macro_ifndef.h"
#include "preprocess.h"
#include "macro_define.h"
#include <string.h>

linked_list *seg_ifndef_gen(seg *self, prep_ctx *ctx)
{
seg_ifndef *ifndef = (seg_ifndef *)self->data;
if (find_define(ctx, ifndef->def))
return ifndef->body;
else
return new_linked_list();
}

seg *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)
{
int found_endif = 0;
linked_list *body = new_linked_list();
while (tkn)
{
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, "endif") == 0)
{
found_endif = 1;
break;
}
}
}
add_to_list(body, tkn);
tkn = tkn->next;
}

if (found_endif)
{
*tkns_ptr = tkn;
seg *ret = (seg *)malloc(sizeof(seg));
ret->data = malloc(sizeof(seg_ifndef));
ret->to_code = seg_ifndef_gen;
ret->stringified = NULL;
((seg_ifndef *)ret->data)->def = def;
((seg_ifndef *)ret->data)->body = body;
return ret;
}
else
{
return NULL;
}
}
else
{
return NULL;
}
}
}
}
}
return NULL;
}
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;
linked_list *body;
} seg_ifndef;

seg *parse_ifndef(prep_ctx *ctx, typed_token **tkns_ptr);

#endif
3 changes: 3 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 @@ -46,6 +47,8 @@ typed_token *preprocess(prep_ctx *ctx, char *path)
s = parse_include(ctx, &tkn);
if (!s)
s = parse_macro_call(ctx, &tkn);
if (!s)
s = parse_ifndef(ctx, &tkn);

if (s)
{
Expand Down
4 changes: 2 additions & 2 deletions scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"./examples/inp_func_ptrs.c": [],
"./examples/inp_goto.c": [],
"./examples/inp_break.c": [],
#"./examples/inp_preprocess.c": [],
"./examples/inp_preprocess.c": [],
"./examples/inp_pointer.c": [],
"./examples/switch.c": [],
"./examples/inp_loop.c": [],
Expand Down Expand Up @@ -120,7 +120,7 @@ def main():
diff_count = 0
failed = False
for test_file in TEST_FILES.keys():
for mode in ['lex', 'prep', 'tree', 'asm']:
for mode in ["lex", "prep", "tree", "asm"]:
extension = "txt"
if mode == "asm":
extension = "asm"
Expand Down
59 changes: 4 additions & 55 deletions tests/output/inp_preprocess.c_asm_output.asm
Original file line number Diff line number Diff line change
@@ -1,70 +1,19 @@
section .data
__printf_size: equ 16
__function_size: equ 16
__function2_size: equ 16
__temp_str_3 db `function(): %d\n`, 0
__temp_str_4 db `function2(): %d\n`, 0
__main_size: equ 96
__temp_str_0 db `Debug not enabled!`, 0
__main_size: equ 32
section .text
extern printf
global function
function:
push rbp
mov rbp, rsp
sub rsp, __function_size
mov rax, 69
mov rsp, rbp
pop rbp
ret
mov rsp, rbp
pop rbp
ret
global function2
function2:
push rbp
mov rbp, rsp
sub rsp, __function2_size
mov rax, 99
mov rsp, rbp
pop rbp
ret
mov rsp, rbp
pop rbp
ret
global main
main:
push rbp
mov rbp, rsp
sub rsp, __main_size
mov rax, __temp_str_3
mov rax, __temp_str_0
mov [rsp+0], rax
mov rax, rsp
add rax, 0
mov [rsp+8], rax
call function
mov [rsp+16], rax
mov rax, [rsp+16]
mov [rsp+24], rax
mov rdi, [rsp+0]
mov rsi, [rsp+24]
mov rax, rsp
add rax, 0
mov [rsp+32], rax
call printf
mov rax, __temp_str_4
mov [rsp+40], rax
mov rax, rsp
add rax, 0
mov [rsp+48], rax
call function2
mov [rsp+56], rax
mov rax, [rsp+56]
mov [rsp+64], rax
mov rdi, [rsp+40]
mov rsi, [rsp+64]
mov rax, rsp
add rax, 0
mov [rsp+72], rax
mov [rsp+8], rax
call printf
mov rax, 0
mov rsp, rbp
Expand Down
64 changes: 4 additions & 60 deletions tests/output/inp_preprocess.c_lex_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,79 +12,23 @@ TKN_DIRECTIVE:
TKN_ID(DEBUG)
TKN_LIT_INT(1)
TKN_EOF
TKN_DIRECTIVE:
TKN_ID(ifdef)
TKN_ID(DEBUG)
TKN_EOF
TKN_INT
TKN_ID(function)
TKN_L_PAREN
TKN_R_PAREN
TKN_L_BRACE
TKN_RETURN
TKN_LIT_INT(69)
TKN_SEMICOLON
TKN_R_BRACE
TKN_DIRECTIVE:
TKN_ID(endif)
TKN_EOF
TKN_DIRECTIVE:
TKN_ID(ifdef)
TKN_ID(RELEASE)
TKN_EOF
TKN_INT
TKN_ID(function)
TKN_ID(main)
TKN_L_PAREN
TKN_R_PAREN
TKN_L_BRACE
TKN_RETURN
TKN_LIT_INT(13)
TKN_SEMICOLON
TKN_R_BRACE
TKN_DIRECTIVE:
TKN_ID(endif)
TKN_EOF
TKN_DIRECTIVE:
TKN_ID(ifndef)
TKN_ID(RELEASE)
TKN_ID(DEBUG)
TKN_EOF
TKN_INT
TKN_ID(function2)
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(Debug not enabled!)
TKN_R_PAREN
TKN_L_BRACE
TKN_RETURN
TKN_LIT_INT(99)
TKN_SEMICOLON
TKN_R_BRACE
TKN_DIRECTIVE:
TKN_ID(endif)
TKN_EOF
TKN_INT
TKN_ID(main)
TKN_L_PAREN
TKN_R_PAREN
TKN_L_BRACE
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(function(): %d
)
TKN_COMMA
TKN_ID(function)
TKN_L_PAREN
TKN_R_PAREN
TKN_R_PAREN
TKN_SEMICOLON
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(function2(): %d
)
TKN_COMMA
TKN_ID(function2)
TKN_L_PAREN
TKN_R_PAREN
TKN_R_PAREN
TKN_SEMICOLON
TKN_RETURN
TKN_LIT_INT(0)
TKN_SEMICOLON
Expand Down
35 changes: 1 addition & 34 deletions tests/output/inp_preprocess.c_prep_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,13 @@ TKN_DOTS
TKN_R_PAREN
TKN_SEMICOLON
TKN_INT
TKN_ID(function)
TKN_L_PAREN
TKN_R_PAREN
TKN_L_BRACE
TKN_RETURN
TKN_LIT_INT(69)
TKN_SEMICOLON
TKN_R_BRACE
TKN_INT
TKN_ID(function2)
TKN_L_PAREN
TKN_R_PAREN
TKN_L_BRACE
TKN_RETURN
TKN_LIT_INT(99)
TKN_SEMICOLON
TKN_R_BRACE
TKN_INT
TKN_ID(main)
TKN_L_PAREN
TKN_R_PAREN
TKN_L_BRACE
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(function(): %d
)
TKN_COMMA
TKN_ID(function)
TKN_L_PAREN
TKN_R_PAREN
TKN_R_PAREN
TKN_SEMICOLON
TKN_ID(printf)
TKN_L_PAREN
TKN_LIT_STR(function2(): %d
)
TKN_COMMA
TKN_ID(function2)
TKN_L_PAREN
TKN_R_PAREN
TKN_LIT_STR(Debug not enabled!)
TKN_R_PAREN
TKN_SEMICOLON
TKN_RETURN
Expand Down
Loading

0 comments on commit 0bb9be5

Please sign in to comment.