From 139d6fcb2215dab36ec0b56d3e4ae44149ed3e78 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Sat, 9 Nov 2024 11:09:57 -0500 Subject: [PATCH] Things. --- Makefile | 17 +++++++++++------ src/ast.c | 12 +++++++----- src/grammar.y | 15 +++++++++------ src/include/ast.h | 4 ++-- src/include/lexer.h | 1 + src/lexer.c | 1 + 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index cafa0f1..dce9357 100644 --- a/Makefile +++ b/Makefile @@ -44,13 +44,14 @@ grammars: $(SRC_DIR)/grammar.y @ echo -e "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR) bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h" @ bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h -# Link to final binary. -$(TARGET): $(OBJ_FILES) - @ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)" - @ $(LINK) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS) + +# Compile grammars. +$(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o + $(CC) $(CFLAGS) -c $< -o $@ # Lexer depends on grammars. -$(OBJ_DIR)/lexer.o: grammars +$(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c + $(CC) $(CFLAGS) -c $< -o $@ # Compile project sources. $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h @@ -58,6 +59,11 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h @ echo -e "$(WHITE_BOLD)Compiling $(WHITE)$<$(WHITE_BOLD)... $(RESETCOLOR)$(CC) $(CFLAGS) -c $< -o $@" @ $(CC) $(CFLAGS) -c $< -o $@ +# Link to final binary. +$(TARGET): $(OBJ_FILES) $(OBJ_DIR)/grammar.o + @ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)" + @ $(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS) + # Compile test sources. $(TEST_OBJ_DIR)/%.o: $(TEST_DIR)/%.c @ mkdir -p $(TEST_OBJ_DIR) @@ -76,4 +82,3 @@ clean: @ rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) .PHONY: all clean test nocolor release run - diff --git a/src/ast.c b/src/ast.c index ec0aa1b..9f79725 100644 --- a/src/ast.c +++ b/src/ast.c @@ -10,7 +10,7 @@ static char* asttype_names[] = { }; #endif -ASTTypeNum* ast_type_num_init(int val) { +ASTTypeNum* ast_type_num_init(AST* val) { talloc(ASTTypeNum, num); num->val = val; @@ -19,9 +19,10 @@ ASTTypeNum* ast_type_num_init(int val) { } void ast_type_num_destroy(ASTTypeNum* num) { - if (!num) return + if (!num) + return - free(num); + free(num); } ASTTypeCall* ast_type_call_init(char* to, size_t argc, AST** argv) { @@ -35,9 +36,10 @@ ASTTypeCall* ast_type_call_init(char* to, size_t argc, AST** argv) { } void ast_type_call_destroy(ASTTypeCall* call) { - if (!call) return + if (!call) + return - free(call->to); + free(call->to); for (size_t i = 0; i < call->argc; i++) free(call->argv[i]); free(call); } diff --git a/src/grammar.y b/src/grammar.y index 35c9db9..8a191ff 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -1,11 +1,14 @@ +%{ +#include "../../src/include/ast.h" +#include "../../src/include/lexer.h" +int yylex (void); +void yyerror (char const *); +%} + %code requires { #include "../../src/include/ast.h" } -%{ -#include "../../src/include/ast.h" -%} - %union { int intval; char* strval; @@ -20,8 +23,8 @@ %% exp: - NUM {} - | exp PLUS exp { $$ = ast_type_call_init("+", 2, [ast_type_num_init($1), ast_type_num_init(int val)]} + NUM { $$ = ast_type_num_init($1); } + | NUM PLUS NUM { AST* argv[2] = {ast_type_num_init($1), ast_type_num_init($1)}; $$ = ast_type_call_init("+", 2, argv);} ; %% diff --git a/src/include/ast.h b/src/include/ast.h index f1051c9..538be3c 100644 --- a/src/include/ast.h +++ b/src/include/ast.h @@ -18,10 +18,10 @@ AST* ast_init(ASTType type, void* data); void ast_destroy(AST* ast); typedef struct { - int val; + AST* val; } ASTTypeNum; -ASTTypeNum* ast_type_num_init(int val); +ASTTypeNum* ast_type_num_init(AST* val); void ast_type_num_destroy(ASTTypeNum* num); typedef struct { diff --git a/src/include/lexer.h b/src/include/lexer.h index 37c05f8..f11b9c0 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -72,5 +72,6 @@ void lexerstate_print_raw(); void lexer_set_global(const char* str); int yylex(); +void yyerror(char const* s); #endif diff --git a/src/lexer.c b/src/lexer.c index 14d1eab..c7cc28b 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -172,3 +172,4 @@ int yylex() { return 0; } +void yyerror(char const* s) { fprintf(stderr, "%s\n", s); }