Skip to content

Commit

Permalink
feat: Allow macros with arguments + 30cc can now compile its lexer.c! (
Browse files Browse the repository at this point in the history
…#50)

* macro args

* Accumulate preprocessor result in a linkedlist

* Stringify macro

* Testing preprocessed outputs

* Refactor

* next version of preprocessor?

* Fix

* Assume newline in begin

* Remove extern/size when declared func is defined

* Organize preprocessor

* Organize preprocessor

* Compare according to size

* fix: Lexer worksgit statusgit status

* More stuff

* Remove preprocessor tests
  • Loading branch information
keyvank authored Nov 24, 2024
1 parent b1ecfe0 commit a5470ce
Show file tree
Hide file tree
Showing 62 changed files with 6,233 additions and 1,304 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ CFLAGS := -std=gnu99 -Og -ggdb

BIN := a.out

$(BIN): *.c parser/*.c parser/expr/*.c codegen/*.c
$(CC) $(CFLAGS) *.c parser/*.c codegen/*.c parser/expr/*.c
$(BIN): *.h *.c parser/*.c parser/*.h parser/expr/*.c parser/expr/*.h codegen/*.c codegen/*.h preprocess/*.c preprocess/*.h
$(CC) $(CFLAGS) *.c parser/*.c codegen/*.c parser/expr/*.c preprocess/*.c

run: $(BIN)
@./a.out $(program) --asm > out.asm
Expand Down
27 changes: 21 additions & 6 deletions codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ void add_text(context *ctx, char *fmt, ...)
add_to_list(ctx->text, txt);
}

void replace_text(context *ctx, char *a, char *b)
{
list_node *curr = ctx->text->first;
while (curr)
{
if (strcmp(curr->value, a) == 0)
{
curr->value = b;
}
curr = curr->next;
}
}

symbol *find_symbol(context *ctx, char *name)
{
list_node *curr = ctx->symbol_table->last;
Expand Down Expand Up @@ -92,14 +105,15 @@ char *new_loop_end_label(context *ctx)
{
char *name = new_label(ctx);
int *label_id = malloc(sizeof(int));
*label_id = ctx->label_counter-1;
*label_id = ctx->label_counter - 1;
add_to_list(ctx->loop_end_labels, label_id);
return name;
}

char* get_current_loop_end_label_counter(context *ctx, char *name)
char *get_current_loop_end_label_counter(context *ctx, char *name)
{
if (ctx->loop_end_labels->count == 0) {
if (ctx->loop_end_labels->count == 0)
{
fprintf(stderr, "Not inside loop\n");
return NULL;
}
Expand All @@ -112,16 +126,17 @@ char *new_loop_start_label(context *ctx)
{
char *name = new_label(ctx);
int *label_id = malloc(sizeof(int));
*label_id = ctx->label_counter-1;
*label_id = ctx->label_counter - 1;
add_to_list(ctx->loop_start_labels, label_id);

add_text(ctx, "; enter loop");
return name;
}

char* get_current_loop_start_label_counter(context *ctx, char *name)
char *get_current_loop_start_label_counter(context *ctx, char *name)
{
if (ctx->loop_start_labels->count == 0) {
if (ctx->loop_start_labels->count == 0)
{
fprintf(stderr, "Not inside loop\n");
return NULL;
}
Expand Down
1 change: 1 addition & 0 deletions codegen/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int types_equal(general_type *a, general_type *b, context *ctx);
context new_context();
void add_text(context *ctx, char *fmt, ...);
void add_data(context *ctx, char *fmt, ...);
void replace_text(context *ctx, char *a, char *b);

typedef struct
{
Expand Down
12 changes: 12 additions & 0 deletions examples/inp_arg_macro.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
void printf(char *, ...);

#define ADD(x, y) (20 + 2 * x + 3 * y)
#define STR(x) #x

int main()
{
int a = 10;
int cde = ADD(a, 20);
char *s = STR(cde);
printf("%s: %d\n", s, cde);
}
Loading

0 comments on commit a5470ce

Please sign in to comment.