Skip to content

Commit

Permalink
impl native definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 9, 2024
1 parent 464b451 commit e60d87b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
OBJECTS= ./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/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/native.o: ./preprocessor/native.c
gcc ./preprocessor/native.c ${INCLUDES} -o ./build/native.o -g -c

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

Expand Down
7 changes: 7 additions & 0 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1579,5 +1579,12 @@ void expressionable_parse(struct expressionable *expressionable);

struct preprocessor *preprocessor_create(struct compile_process *compiler);
int preprocessor_run(struct compile_process *compiler);
struct token *preprocessor_prev_token(struct compile_process *compiler);
struct vector *preprocessor_build_value_vector_for_integer(int value);
struct preprocessor_definition *preprocessor_definition_create_native(
const char *name, PREPROCESSOR_DEFINITION_NATIVE_CALL_EVALUATE evaluate,
PREPROCESSOR_DEFINITION_NATIVE_CALL_VALUE value,
struct preprocessor *preprocessor);
void preprocessor_create_defs(struct preprocessor *preprocessor);

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

int preprocessor_line_macro_evaluate(struct preprocessor_definition *def,
struct preprocessor_function_args *args) {
struct preprocessor *preprocessor = def->preprocessor;
struct compile_process *compiler = preprocessor->compiler;
if (args) {
compiler_error(compiler, "line macro does not accept arguments");
}

struct token *prev_token = preprocessor_prev_token(compiler);
return prev_token->pos.line;
}

struct vector *
preprocessor_line_macro_value(struct preprocessor_definition *def,
struct preprocessor_function_args *args) {
struct preprocessor *preprocessor = def->preprocessor;
struct compile_process *compiler = preprocessor->compiler;
if (args) {
compiler_error(compiler, "line macro does not accept arguments");
}

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

void preprocessor_create_defs(struct preprocessor *preprocessor) {
preprocessor_definition_create_native(
"__LINE__", preprocessor_line_macro_evaluate,
preprocessor_line_macro_value, preprocessor);
}
35 changes: 30 additions & 5 deletions preprocessor/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void preprocessor_init(struct preprocessor *preprocessor) {
preprocessor->defs = vector_create(sizeof(struct preprocessor_definition *));
preprocessor->includes =
vector_create(sizeof(struct preprocessor_included_file *));
#warning "create proprocessor default definitions"
preprocessor_create_defs(preprocessor);
}

struct preprocessor *preprocessor_create(struct compile_process *compiler) {
Expand Down Expand Up @@ -649,6 +649,21 @@ preprocessor_definition_create(const char *name, struct vector *value,
return def;
}

struct preprocessor_definition *preprocessor_definition_create_native(
const char *name, PREPROCESSOR_DEFINITION_NATIVE_CALL_EVALUATE evaluate,
PREPROCESSOR_DEFINITION_NATIVE_CALL_VALUE value,
struct preprocessor *preprocessor) {
struct preprocessor_definition *def =
malloc(sizeof(struct preprocessor_definition));
def->type = PREPROCESSOR_DEFINITION_NATIVE_CALLBACK;
def->name = name;
def->native.evaluate = evaluate;
def->native.value = value;
def->preprocessor = preprocessor;
vector_push(preprocessor->defs, &def);
return def;
}

struct preprocessor_definition *
preprocessor_definition_create_typedef(const char *name,
struct vector *value_vec,
Expand Down Expand Up @@ -698,12 +713,17 @@ preprocessor_definition_value_for_typedef(struct preprocessor_definition *def) {
return preprocessor_definition_value_for_typedef_or_other(def);
}

struct vector *preprocessor_definition_value_for_native(
struct preprocessor_definition *def,
struct preprocessor_function_args *args) {
return def->native.value(def, args);
}

struct vector *preprocessor_definition_value_with_arguments(
struct preprocessor_definition *def,
struct preprocessor_function_args *args) {
if (def->type == PREPROCESSOR_DEFINITION_NATIVE_CALLBACK) {
#warning "impl definition value for native"
return NULL;
return preprocessor_definition_value_for_native(def, args);
} else if (def->type == PREPROCESSOR_DEFINITION_TYPEDEF) {
return preprocessor_definition_value_for_typedef(def);
}
Expand Down Expand Up @@ -739,14 +759,19 @@ int preprocessor_definition_evaluated_value_for_standard(
return token->llnum;
}

int preprocess_evaluated_value_for_native(
struct preprocessor_definition *def,
struct preprocessor_function_args *args) {
return def->native.evaluate(def, args);
}

int preprocessor_definition_evaluated_value(
struct preprocessor_definition *def,
struct preprocessor_function_args *args) {
if (def->type == PREPROCESSOR_DEFINITION_STANDARD) {
return preprocessor_definition_evaluated_value_for_standard(def);
} else if (def->type == PREPROCESSOR_DEFINITION_NATIVE_CALLBACK) {
#warning "impl native callback"
return -1;
return preprocess_evaluated_value_for_native(def, args);
}

compiler_error(def->preprocessor->compiler, "Cannot evaluate to number");
Expand Down
10 changes: 7 additions & 3 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#define TEST(a, b) a##b
/*
int printf(const char *fmt, ...);
int main() { printf("%i", TEST(20, 30)); }
*/

int x = __LINE__;

int main() { return __LINE__; }

0 comments on commit e60d87b

Please sign in to comment.