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"
@ 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

View File

@ -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);
}

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 {
#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);}
;
%%

View File

@ -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 {

View File

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

View File

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