This commit is contained in:
Jacob Signorovitch 2024-11-09 11:09:57 -05:00
parent 92d9da14c2
commit 139d6fcb22
6 changed files with 31 additions and 19 deletions

View File

@ -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" @ 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 @ bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
# Link to final binary.
$(TARGET): $(OBJ_FILES) # Compile grammars.
@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)" $(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o
@ $(LINK) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS) $(CC) $(CFLAGS) -c $< -o $@
# Lexer depends on grammars. # Lexer depends on grammars.
$(OBJ_DIR)/lexer.o: grammars $(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c
$(CC) $(CFLAGS) -c $< -o $@
# Compile project sources. # Compile project sources.
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h $(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 $@" @ echo -e "$(WHITE_BOLD)Compiling $(WHITE)$<$(WHITE_BOLD)... $(RESETCOLOR)$(CC) $(CFLAGS) -c $< -o $@"
@ $(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. # Compile test sources.
$(TEST_OBJ_DIR)/%.o: $(TEST_DIR)/%.c $(TEST_OBJ_DIR)/%.o: $(TEST_DIR)/%.c
@ mkdir -p $(TEST_OBJ_DIR) @ 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) @ rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET)
.PHONY: all clean test nocolor release run .PHONY: all clean test nocolor release run

View File

@ -10,7 +10,7 @@ static char* asttype_names[] = {
}; };
#endif #endif
ASTTypeNum* ast_type_num_init(int val) { ASTTypeNum* ast_type_num_init(AST* val) {
talloc(ASTTypeNum, num); talloc(ASTTypeNum, num);
num->val = val; num->val = val;
@ -19,7 +19,8 @@ ASTTypeNum* ast_type_num_init(int val) {
} }
void ast_type_num_destroy(ASTTypeNum* num) { void ast_type_num_destroy(ASTTypeNum* num) {
if (!num) return if (!num)
return
free(num); free(num);
} }
@ -35,7 +36,8 @@ ASTTypeCall* ast_type_call_init(char* to, size_t argc, AST** argv) {
} }
void ast_type_call_destroy(ASTTypeCall* call) { 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]); for (size_t i = 0; i < call->argc; i++) free(call->argv[i]);

View File

@ -1,11 +1,14 @@
%{
#include "../../src/include/ast.h"
#include "../../src/include/lexer.h"
int yylex (void);
void yyerror (char const *);
%}
%code requires { %code requires {
#include "../../src/include/ast.h" #include "../../src/include/ast.h"
} }
%{
#include "../../src/include/ast.h"
%}
%union { %union {
int intval; int intval;
char* strval; char* strval;
@ -20,8 +23,8 @@
%% %%
exp: exp:
NUM {} NUM { $$ = ast_type_num_init($1); }
| exp PLUS exp { $$ = ast_type_call_init("+", 2, [ast_type_num_init($1), ast_type_num_init(int val)]} | NUM PLUS NUM { AST* argv[2] = {ast_type_num_init($1), ast_type_num_init($1)}; $$ = ast_type_call_init("+", 2, argv);}
; ;
%% %%

View File

@ -18,10 +18,10 @@ AST* ast_init(ASTType type, void* data);
void ast_destroy(AST* ast); void ast_destroy(AST* ast);
typedef struct { typedef struct {
int val; AST* val;
} ASTTypeNum; } ASTTypeNum;
ASTTypeNum* ast_type_num_init(int val); ASTTypeNum* ast_type_num_init(AST* val);
void ast_type_num_destroy(ASTTypeNum* num); void ast_type_num_destroy(ASTTypeNum* num);
typedef struct { typedef struct {

View File

@ -72,5 +72,6 @@ void lexerstate_print_raw();
void lexer_set_global(const char* str); void lexer_set_global(const char* str);
int yylex(); int yylex();
void yyerror(char const* s);
#endif #endif

View File

@ -172,3 +172,4 @@ int yylex() {
return 0; return 0;
} }
void yyerror(char const* s) { fprintf(stderr, "%s\n", s); }