Skip to content

Commit

Permalink
Creating native definitions - Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fedi-nabli committed Apr 5, 2024
1 parent 511a293 commit 5b091db
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
OBJECTS = ./build/compiler.o ./build/cprocess.o ./build/lexer.o ./build/lex_process.o ./build/token.o ./build/parser.o ./build/node.o ./build/expressionable.o ./build/datatype.o ./build/scope.o ./build/symresolver.o ./build/array.o ./build/fixup.o ./build/codegen.o ./build/stackframe.o ./build/resolver.o ./build/rdefault.o ./build/helper.o ./build/preprocessor.o ./build/helpers/buffer.o ./build/helpers/vector.o
OBJECTS = ./build/compiler.o ./build/cprocess.o ./build/lexer.o ./build/lex_process.o ./build/token.o ./build/parser.o ./build/node.o ./build/expressionable.o ./build/datatype.o ./build/scope.o ./build/symresolver.o ./build/array.o ./build/fixup.o ./build/codegen.o ./build/stackframe.o ./build/resolver.o ./build/rdefault.o ./build/helper.o ./build/preprocessor.o ./build/native.o ./build/helpers/buffer.o ./build/helpers/vector.o
INCLUDES = -I./

all: ${OBJECTS}
Expand Down Expand Up @@ -61,6 +61,9 @@ all: ${OBJECTS}
./build/preprocessor.o: ./preprocessor/preprocessor.c
gcc ./preprocessor/preprocessor.c ${INCLUDES} -g -c -o ./build/preprocessor.o

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

./build/helpers/buffer.o: ./helpers/buffer.c
gcc ./helpers/buffer.c -o ./build/helpers/buffer.o -g -c

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

int preprocessor_line_macro_evaluate(struct preprocessor_definition* definition, struct preprocessor_function_arguments* arguments)
{
struct preprocessor* preprocessor = definition->preprocessor;
struct compile_process* compiler = preprocessor->compiler;

if (arguments)
{
compiler_error(compiler, "__LINE__ macro expects no arguments");
}

struct token* previous_token = preprocessor_previous_token(compiler);
return previous_token->pos.line;
}

struct vector* preprocessor_line_macro_value(struct preprocessor_definition* definition, struct preprocessor_function_arguments* arguments)
{
struct preprocessor* preprocessor = definition->preprocessor;
struct compile_process* compiler = preprocessor->compiler;

if (arguments)
{
compiler_error(compiler, "__LINE__ macro expects no arguments");
}

struct token* previous_token = preprocessor_previous_token(compiler);
return preprocessor_build_value_vector_for_integer(previous_token->pos.line);
}

void preprocessor_create_definitions(struct preprocessor* preprocessor)
{
preprocessor_definition_create_native(preprocessor, "__LINE__", preprocessor_line_macro_evaluate, preprocessor_line_macro_value);
}
14 changes: 13 additions & 1 deletion preprocessor/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void preprocessor_initialize(struct preprocessor* preprocessor)
memset(preprocessor, 0, sizeof(struct preprocessor));
preprocessor->definitions = vector_create(sizeof(struct preprocessor_definition*));
preprocessor->includes = vector_create(sizeof(struct preprocessor_included_file*));
#warning "Create preprocessor default definitions"
preprocessor_create_definitions(preprocessor);
}

struct preprocessor* preprocessor_create(struct compile_process* compiler)
Expand Down Expand Up @@ -710,6 +710,18 @@ struct preprocessor_definition* preprocessor_get_definition(struct preprocessor*
return definition;
}

struct preprocessor_definition* preprocessor_definition_create_native(struct preprocessor* preprocessor, const char* name, PREPROCESSOR_DEFINITION_NATIVE_CALL_EVALUATION evaluate, PREPROCESSOR_DEFINITION_NATIVE_CALL_VALUE value)
{
struct preprocessor_definition* definition = calloc(1, sizeof(struct preprocessor_definition));
definition->type = PREPROCESSOR_DEFINITION_NATIVE_CALLBACK;
definition->name = name;
definition->native.evaluate = evaluate;
definition->native.value = value;
definition->preprocessor = preprocessor;
vector_push(preprocessor->definitions, &definition);
return definition;
}

struct preprocessor_definition* preprocessor_definition_create_typedef(struct preprocessor* preprocessor, const char* name, struct vector* value_vec)
{
struct preprocessor_definition* definition = calloc(1, sizeof(struct preprocessor_definition));
Expand Down
8 changes: 8 additions & 0 deletions preprocessor/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,12 @@ struct preprocessor

// > Preprocessor structures end

// Preprocessor native funtions
void preprocessor_create_definitions(struct preprocessor* preprocessor);

// Preprocessor global functions
struct token* preprocessor_previous_token(struct compile_process* compiler);
struct vector* preprocessor_build_value_vector_for_integer(int value);
struct preprocessor_definition* preprocessor_definition_create_native(struct preprocessor* preprocessor, const char* name, PREPROCESSOR_DEFINITION_NATIVE_CALL_EVALUATION evaluate, PREPROCESSOR_DEFINITION_NATIVE_CALL_VALUE value);

#endif

0 comments on commit 5b091db

Please sign in to comment.