Skip to content

Commit

Permalink
impl includes
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 9, 2024
1 parent e60d87b commit d9e61e8
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 11 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
OBJECTS= ./build/native.o ./build/preprocessor.o ./build/compiler.o ./build/codegen.o ./build/resolver.o ./build/rdefault.o ./build/stackframe.o ./build/array.o ./build/fixup.o ./build/helper.o ./build/scope.o ./build/symresolver.o ./build/cprocess.o ./build/datatype.o ./build/expressionable.o ./build/lexer.o ./build/token.o ./build/lex_process.o ./build/parser.o ./build/node.o ./build/helpers/buffer.o ./build/helpers/vector.o
OBJECTS= ./build/stddef.o ./build/stdarg.o ./build/static_include.o ./build/native.o ./build/preprocessor.o ./build/compiler.o ./build/codegen.o ./build/resolver.o ./build/rdefault.o ./build/stackframe.o ./build/array.o ./build/fixup.o ./build/helper.o ./build/scope.o ./build/symresolver.o ./build/cprocess.o ./build/datatype.o ./build/expressionable.o ./build/lexer.o ./build/token.o ./build/lex_process.o ./build/parser.o ./build/node.o ./build/helpers/buffer.o ./build/helpers/vector.o
INCLUDES= -I./

all: ${OBJECTS}
gcc main.c ${INCLUDES} ${OBJECTS} -g -o ./main

./build/stddef.o: ./preprocessor/static_includes/stddef.c
gcc ./preprocessor/static_includes/stddef.c ${INCLUDES} -o ./build/stddef.o -g -c

./build/stdarg.o: ./preprocessor/static_includes/stdarg.c
gcc ./preprocessor/static_includes/stdarg.c ${INCLUDES} -o ./build/stdarg.o -g -c

./build/static_include.o: ./preprocessor/static_include.c
gcc ./preprocessor/static_include.c ${INCLUDES} -o ./build/static_include.o -g -c

./build/native.o: ./preprocessor/native.c
gcc ./preprocessor/native.c ${INCLUDES} -o ./build/native.o -g -c

Expand Down
48 changes: 48 additions & 0 deletions compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,54 @@ void compiler_warning(struct compile_process *compiler, const char *msg, ...) {
compiler->pos.col, compiler->pos.filename);
}

struct compile_process *
compile_include_for_include_dir(const char *include_dir, const char *filename,
struct compile_process *parent_process) {
char tmp_filename[512];
sprintf(tmp_filename, "%s/%s", include_dir, filename);
if (file_exists(tmp_filename)) {
filename = tmp_filename;
}

struct compile_process *new_process = compile_process_create(
filename, NULL, parent_process->flags, parent_process);
if (!new_process) {
return NULL;
}

struct lex_process *lex_process =
lex_process_create(new_process, &compiler_lex_functions, NULL);
if (!lex_process) {
return NULL;
}

if (lex(lex_process) != LEXICAL_ANALYSIS_ALL_OK) {
return NULL;
}

new_process->token_vec_original = lex_process_tokens(lex_process);

if (preprocessor_run(new_process) != PREPROCESS_ALL_OK) {
return NULL;
}

return new_process;
}

// Compile include file with only lexing and preprocessing
struct compile_process *
compile_include(const char *filename, struct compile_process *parent_process) {
struct compile_process *new_process = NULL;
const char *include_dir = compiler_include_dir_begin(parent_process);
while (include_dir && !new_process) {
new_process =
compile_include_for_include_dir(include_dir, filename, parent_process);
include_dir = compiler_include_dir_next(parent_process);
}

return new_process;
}

int compile_file(const char *filename, const char *out_filename, int flags) {
struct compile_process *process =
compile_process_create(filename, out_filename, flags, NULL);
Expand Down
13 changes: 13 additions & 0 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ struct preprocessor_included_file {
char filename[PATH_MAX];
};

typedef void (*PREPROCESSOR_STATIC_INCLUDE_HANDLER_POST_CREATION)(
struct preprocessor *preprocessor,
struct preprocessor_included_file *included_file);
PREPROCESSOR_STATIC_INCLUDE_HANDLER_POST_CREATION
preprocessor_static_include_handler_for(const char *filename);

struct preprocessor {
// vector of struct_definition*
struct vector *defs;
Expand Down Expand Up @@ -1123,6 +1129,12 @@ char compile_process_next_char(struct lex_process *lex_process);
char compile_process_peek_char(struct lex_process *lex_process);
void compile_process_push_char(struct lex_process *lex_process, char c);

const char *compiler_include_dir_begin(struct compile_process *process);
const char *compiler_include_dir_next(struct compile_process *process);
void compiler_setup_default_include_dir(struct vector *include_dirs);
struct compile_process *compile_include(const char *filename,
struct compile_process *parent_process);

void compiler_error(struct compile_process *compiler, const char *msg, ...);
void compiler_warning(struct compile_process *compiler, const char *msg, ...);

Expand Down Expand Up @@ -1176,6 +1188,7 @@ bool unary_operand_compatible(struct token *token);
void datatype_decrement_pointer(struct datatype *dtype);
long arithmetic(struct compile_process *compiler, long left, long right,
const char *op, bool *success);
bool file_exists(const char *filename);

size_t datatype_size_for_array_access(struct datatype *dtype);
size_t datatype_element_size(struct datatype *dtype);
Expand Down
27 changes: 26 additions & 1 deletion cprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
#include <stdio.h>
#include <stdlib.h>

const char *default_include_dirs[] = {"./rc_includes", "../rc_includes",
"/usr/include/rosebud_includes",
"/usr/include"};

const char *compiler_include_dir_begin(struct compile_process *process) {
vector_set_peek_pointer(process->include_dirs, 0);
const char *dir = vector_peek_ptr(process->include_dirs);
return dir;
}

const char *compiler_include_dir_next(struct compile_process *process) {
const char *dir = vector_peek_ptr(process->include_dirs);
return dir;
}

void compiler_setup_default_include_dir(struct vector *include_dirs) {
size_t total = sizeof(default_include_dirs) / sizeof(const char *);
for (size_t i = 0; i < total; i++) {
const char *dir = default_include_dirs[i];
vector_push(include_dirs, &dir);
}
}

struct compile_process *
compile_process_create(const char *filename, const char *filename_out,
int flags, struct compile_process *parent_process) {
Expand Down Expand Up @@ -40,7 +63,9 @@ compile_process_create(const char *filename, const char *filename_out,
} else {
process->preprocessor = preprocessor_create(process);
process->include_dirs = vector_create(sizeof(const char *));
// setup default include directories

// laod default include dirs
compiler_setup_default_include_dir(process->include_dirs);
}

return process;
Expand Down
10 changes: 10 additions & 0 deletions helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,13 @@ long arithmetic(struct compile_process *compiler, long left, long right,

return res;
}

bool file_exists(const char *filename) {
FILE *file = fopen(filename, "r");
if (file) {
fclose(file);
return true;
}

return false;
}
45 changes: 45 additions & 0 deletions preprocessor/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,14 @@ bool preprocessor_token_is_typedef(struct token *token) {
return S_EQ(token->sval, "typedef");
}

bool preprocessor_token_is_include(struct token *token) {
if (!preprocessor_token_is_preprocessor_keyword(token)) {
return false;
}

return S_EQ(token->sval, "include");
}

struct buffer *
preprocessor_multi_value_string(struct compile_process *compiler) {
struct buffer *str_buf = buffer_create();
Expand Down Expand Up @@ -1582,6 +1590,40 @@ void preprocessor_handle_ifndef_token(struct compile_process *compiler) {
preprocessor_read_to_endif(compiler, def == NULL);
}

struct token *
preprocessor_next_token_skip_nl(struct compile_process *compiler) {
struct token *token = preprocessor_next_token(compiler);
while (token && token->type == TOKEN_TYPE_NEWLINE) {
token = preprocessor_next_token(compiler);
}

return token;
}

void preprocessor_handle_include_token(struct compile_process *compiler) {
struct token *file_path_token = preprocessor_next_token_skip_nl(compiler);
if (!file_path_token) {
compiler_error(compiler, "expected file path");
}

struct compile_process *new_compile_process =
compile_include(file_path_token->sval, compiler);
if (!new_compile_process) {
PREPROCESSOR_STATIC_INCLUDE_HANDLER_POST_CREATION handler =
preprocessor_static_include_handler_for(file_path_token->sval);
if (handler) {
// handle static include
preprocessor_create_static_include(compiler->preprocessor,
file_path_token->sval, handler);
return;
}

compiler_error(compiler, "failed to include file");
}

preprocessor_token_vec_push_src(compiler, new_compile_process->token_vec);
}

int preprocessor_handle_hashtag_token(struct compile_process *compiler,
struct token *token) {
bool is_preprocessed = false;
Expand All @@ -1607,6 +1649,9 @@ int preprocessor_handle_hashtag_token(struct compile_process *compiler,
} else if (preprocessor_token_is_if(next_token)) {
preprocessor_handle_if_token(compiler);
is_preprocessed = true;
} else if (preprocessor_token_is_include(next_token)) {
preprocessor_handle_include_token(compiler);
is_preprocessed = true;
}

return is_preprocessed;
Expand Down
19 changes: 19 additions & 0 deletions preprocessor/static_include.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "compiler.h"

void preprocessor_stddef_include(
struct preprocessor *preprocessor,
struct preprocessor_included_file *included_file);
void preprocessor_stdarg_include(
struct preprocessor *preprocessor,
struct preprocessor_included_file *included_file);

PREPROCESSOR_STATIC_INCLUDE_HANDLER_POST_CREATION
preprocessor_static_include_handler_for(const char *filename) {
if (S_EQ(filename, "stddef_internal.h")) {
return preprocessor_stddef_include;
} else if (filename, "stdarg_internal.h") {
return preprocessor_stdarg_include;
}

return NULL;
}
Empty file.
7 changes: 7 additions & 0 deletions preprocessor/static_includes/stdarg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "compiler.h"

void preprocessor_stdarg_include(
struct preprocessor *preprocessor,
struct preprocessor_included_file *included_file) {
#warning "create va_list"
}
7 changes: 7 additions & 0 deletions preprocessor/static_includes/stddef.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "compiler.h"

void preprocessor_stddef_include(
struct preprocessor *preprocessor,
struct preprocessor_included_file *included_file) {
#warning "implement stddef.h"
}
Empty file added rc_includes/.keep
Empty file.
1 change: 1 addition & 0 deletions rc_includes/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define ABC 10
10 changes: 1 addition & 9 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
/*
*/

int x = __LINE__;

int main() { return __LINE__; }
#include <stdarg_internal.h>

0 comments on commit d9e61e8

Please sign in to comment.