Skip to content

Commit

Permalink
feat: Put all of the external functions in a single header file (#64)
Browse files Browse the repository at this point in the history
* feat: Put all of the external functions in a single header file

* feat: Define _30CC by default to allow recog the compiler

* fix: Make lexer.c compilable by 30cc

* Add realloc() and memcpy()
  • Loading branch information
keyvank authored Dec 3, 2024
1 parent 5e8d310 commit 7f43a3a
Show file tree
Hide file tree
Showing 43 changed files with 150 additions and 148 deletions.
8 changes: 2 additions & 6 deletions codegen/codegen.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "../libc.h"
#include "codegen.h"
#include "../linked_list.h"
#include "stdarg.h"
#define UNUSED(x) (void)(x)

context new_context()
Expand Down
8 changes: 8 additions & 0 deletions examples/inp_preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ void printf(char *, ...);

int main()
{
#ifdef _30CC
printf("30CC is compiling this!\n");
#endif

#ifndef _30CC
printf("GCC is compiling this!\n");
#endif

#ifndef D
#ifdef A
#ifdef B
Expand Down
44 changes: 15 additions & 29 deletions lexer.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "libc.h"
#include "lexer.h"

char is_num(char c)
Expand Down Expand Up @@ -48,7 +43,7 @@ void str_tkn_debug(typed_token *tkn)
}
void directive_tkn_debug(typed_token *tkn)
{
typed_token *curr = tkn->data;
typed_token *curr = (typed_token *)tkn->data;
printf("%s:\n", STR(TKN_DIRECTIVE));
while (curr)
{
Expand Down Expand Up @@ -79,7 +74,7 @@ typed_token *next_keyword_or_identifier(char **inp_ptr)
char *val_ptr = val;
*val_ptr = c;
val_ptr++;
while (*inp && (c = is_letter_or_num(*inp)))
while ((*inp != '\0') && ((c = is_letter_or_num(*inp)) != '\0'))
{
*val_ptr = c;
inp++;
Expand Down Expand Up @@ -142,13 +137,13 @@ typed_token *next_op(char **inp_ptr, int is_newline)
char *inp = *inp_ptr;
if (is_num(*inp))
{
int a = *inp - 48;
int a = (int)*inp - 48;
inp++;
while (is_num(*inp))
{

a = a * 10;
a += (*inp - 48);
a += ((int)*inp - 48);
inp++;
}
*inp_ptr = inp;
Expand Down Expand Up @@ -297,7 +292,7 @@ typed_token *next_op(char **inp_ptr, int is_newline)
{
*inp_ptr += 4;
char e = *(inp + 2);
char *ch = malloc(1);
char *ch = (char *)malloc(1);
if (e == 'n')
{
*ch = '\n';
Expand All @@ -316,7 +311,7 @@ typed_token *next_op(char **inp_ptr, int is_newline)
else if (*(inp + 1) != '\0' && *(inp + 2) == '\'')
{
*inp_ptr += 3;
char *ch = malloc(1);
char *ch = (char *)malloc(1);
*ch = *(inp + 1);
return new_tkn(TKN_LIT_CHAR, (void *)ch, char_tkn_debug);
}
Expand Down Expand Up @@ -532,7 +527,7 @@ int skip_whitespaces(char **inp_ptr, int is_start)
{
char *inp = *inp_ptr;
int is_newline = is_start || (*inp == '\n');
while (*inp != 0 && (*inp == ' ' || *inp == '\n' || *inp == '\t'))
while (*inp != '\0' && (*inp == ' ' || *inp == '\n' || *inp == '\t'))
{
is_newline = (*inp == '\n');
inp++;
Expand Down Expand Up @@ -561,7 +556,7 @@ typed_token *next_token(char **inp_ptr, int *is_start)
}
else
{
fprintf(stderr, "Unexpected character '%c': %s", **inp_ptr, strerror(errno));
fprintf(stderr, "Unexpected character '%c'!\n", **inp_ptr);
exit(0);
}
}
Expand Down Expand Up @@ -596,7 +591,7 @@ typed_token *tokenize(char *inp)

typed_token *tokenize_file(char *path)
{
FILE *fp = NULL;
void *fp = NULL;

fp = fopen(path, "rb");
if (fp == NULL)
Expand All @@ -606,28 +601,19 @@ typed_token *tokenize_file(char *path)
}

char *data = NULL;
struct stat st;

if (fstat(fileno(fp), &st) == -1)
goto ret;

data = calloc(st.st_size + 1, sizeof(char));
data = (char *)calloc(32768, sizeof(char));
if (!data)
goto ret;

int rd = fread(data, sizeof(char), st.st_size, fp);
if (rd != st.st_size)
{
data = NULL;
goto ret;
}
data[st.st_size] = '\0';
int rd = fread(data, sizeof(char), 32768, fp);
data[rd] = '\0';

return tokenize(data);
ret:
return NULL;
}

typed_token *eof_token() {
typed_token *eof_token()
{
return new_simp_tkn(TKN_EOF);
}
2 changes: 1 addition & 1 deletion lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

typedef struct typed_token_
{
unsigned char type_id;
int type_id;
void *data;
void (*debug)(struct typed_token_ *);
struct typed_token_ *next;
Expand Down
31 changes: 31 additions & 0 deletions libc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef LIBC_H
#define LIBC_H

#ifndef _30CC
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#endif

#ifdef _30CC
#define NULL 0
void printf(char *, ...);
int strcmp(char *, char *);
void strcat(char *, char *);
void strcpy(char *, char *);
void *malloc(int);
void *calloc(int, int);
void *realloc(void *, int);
void free(void *);
void sprintf(char *, char *, ...);
void fprintf(void *, char *, ...);
extern void *stdout;
extern void *stderr;
void *fopen(char *, char *);
int fclose(void *);
int fread(void *, int, int, void *);
void exit(int);
void memcpy(void *, void *, int);
#endif

#endif
2 changes: 1 addition & 1 deletion linked_list.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <stdlib.h>
#include "libc.h"
#include "linked_list.h"

linked_list *new_linked_list()
Expand Down
14 changes: 9 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "libc.h"
#include "lexer.h"
#include "parser/program.h"
#include "linked_list.h"
#include "preprocess/preprocess.h"
#include "preprocess/macro_define.h"

typed_token *process(char *filename, int log_lex, int log_prep)
{
prep_ctx *ctx = (prep_ctx *)malloc(sizeof(prep_ctx));
ctx->curr_path = NULL;
ctx->defs = new_linked_list();

seg_define *_30cc_define = (seg_define *)malloc(sizeof(seg_define));
_30cc_define->arg_names=new_linked_list();
_30cc_define->id = "_30CC";
_30cc_define->replace = new_linked_list();
add_to_list(ctx->defs, _30cc_define);

typed_token *lexed = tokenize_file(filename);
typed_token *prep = preprocess(ctx, filename);
if (log_lex)
{
typed_token *t = lexed;
Expand All @@ -25,6 +28,7 @@ typed_token *process(char *filename, int log_lex, int log_prep)
}
}

typed_token *prep = preprocess(ctx, filename);
if (log_prep)
{
typed_token *t = prep;
Expand Down
4 changes: 1 addition & 3 deletions parser/asm.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include <stdlib.h>

#include "../libc.h"
#include "../lexer.h"
#include "parser.h"
#include "asm.h"
Expand Down
4 changes: 1 addition & 3 deletions parser/break.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "break.h"
#include <stdlib.h>
#include <string.h>

#include "../libc.h"
#include "parser.h"

void break_debug(int depth, parser_node *node)
Expand Down
4 changes: 1 addition & 3 deletions parser/expr/access.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@


#include <stdlib.h>
#include <string.h>

#include "../../libc.h"
#include "../../lexer.h"
#include "../parser.h"
#include "access.h"
Expand Down
4 changes: 1 addition & 3 deletions parser/expr/deref.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@


#include <stdlib.h>

#include "../../libc.h"
#include "../../lexer.h"
#include "../parser.h"
#include "deref.h"
Expand Down
5 changes: 1 addition & 4 deletions parser/expr/expr.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

#include <stdio.h>
#include <stdlib.h>

#include "../../libc.h"
#include "../../lexer.h"
#include "../parser.h"
#include "expr.h"
Expand Down
6 changes: 1 addition & 5 deletions parser/expr/func_call.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@


#include <stdlib.h>
#include <string.h>

#include "../../libc.h"
#include "../../lexer.h"
#include "../parser.h"
#include "func_call.h"
Expand Down
6 changes: 1 addition & 5 deletions parser/expr/index.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@


#include <stdlib.h>
#include <string.h>

#include "../../libc.h"
#include "../../lexer.h"
#include "../parser.h"
#include "index.h"
Expand Down
5 changes: 1 addition & 4 deletions parser/expr/literal.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@


#include <stdlib.h>
#include <string.h>

#include "../../libc.h"
#include "../../lexer.h"
#include "../parser.h"
#include "literal.h"
Expand Down
5 changes: 1 addition & 4 deletions parser/expr/ref.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@


#include <stdlib.h>

#include "../../libc.h"
#include "../../lexer.h"
#include "../parser.h"
#include "ref.h"
Expand Down
4 changes: 1 addition & 3 deletions parser/expr/var.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <stdlib.h>
#include <string.h>

#include "../../libc.h"
#include "../../lexer.h"
#include "../parser.h"
#include "var.h"
Expand Down
3 changes: 1 addition & 2 deletions parser/extern.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "extern.h"
#include <stdlib.h>
#include <string.h>
#include "../libc.h"
#include "../codegen/codegen.h"

#include "parser.h"
Expand Down
4 changes: 1 addition & 3 deletions parser/for.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include <stdlib.h>

#include "../libc.h"
#include "../lexer.h"
#include "parser.h"
#include "for.h"
Expand Down
6 changes: 1 addition & 5 deletions parser/func.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../libc.h"
#include "../vec.h"
#include "../lexer.h"
#include "parser.h"
Expand Down
4 changes: 1 addition & 3 deletions parser/goto.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <stdlib.h>
#include <string.h>

#include "../libc.h"
#include "goto.h"

void label_debug(int depth, parser_node *node)
Expand Down
4 changes: 1 addition & 3 deletions parser/if.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include <stdlib.h>

#include "../libc.h"
#include "../lexer.h"
#include "parser.h"
#include "if.h"
Expand Down
3 changes: 1 addition & 2 deletions parser/parser.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdlib.h>

#include "../libc.h"
#include "parser.h"

void printtabs(int depth)
Expand Down
1 change: 0 additions & 1 deletion parser/parser.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

#ifndef PARSER_H
#define PARSER_H
#include <stdio.h>
#include "../codegen/codegen.h"

void printtabs(int depth);
Expand Down
5 changes: 1 addition & 4 deletions parser/program.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@


#include <stdlib.h>
#include <string.h>

#include "../libc.h"
#include "../lexer.h"
#include "../vec.h"
#include "parser.h"
Expand Down
Loading

0 comments on commit 7f43a3a

Please sign in to comment.