Compare commits
	
		
			2 Commits
		
	
	
		
			1e11b5921d
			...
			4be71317b0
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 4be71317b0 | |||
| 49642553e1 | 
							
								
								
									
										26
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								Makefile
									
									
									
									
									
								
							@@ -16,6 +16,8 @@ LINK = clang
 | 
			
		||||
CFLAGS = -Wall -DDBG -ggdb
 | 
			
		||||
LDFLAGS = -lm
 | 
			
		||||
BATS = bats
 | 
			
		||||
BISON = bison
 | 
			
		||||
PRINT = $(PRINT)
 | 
			
		||||
 | 
			
		||||
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
 | 
			
		||||
OBJ_FILES = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC_FILES))
 | 
			
		||||
@@ -47,56 +49,56 @@ run: $(TARGET)
 | 
			
		||||
# Generate grammars with bison.
 | 
			
		||||
$(GRAM_FILES): $(SRC_DIR)/grammar.y
 | 
			
		||||
	@ mkdir -p $(GRAM_DIR)
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR)"
 | 
			
		||||
	bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR)"
 | 
			
		||||
	$(BISON) $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
 | 
			
		||||
 | 
			
		||||
# Compile grammars.
 | 
			
		||||
$(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Compiling grammars...$(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Compiling grammars...$(RESETCOLOR)"
 | 
			
		||||
	$(CC) $(CFLAGS) -c $< -o $@
 | 
			
		||||
 | 
			
		||||
# Lexer depends on grammars.
 | 
			
		||||
$(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c $(GRAM_FILES)
 | 
			
		||||
	@ mkdir -p $(OBJ_DIR)
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
 | 
			
		||||
	$(CC) $(CFLAGS) -c $< -o $@
 | 
			
		||||
 | 
			
		||||
# Compile project source objects.
 | 
			
		||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h
 | 
			
		||||
	@ mkdir -p $(OBJ_DIR)
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
 | 
			
		||||
	$(CC) $(CFLAGS) -c $< -o $@
 | 
			
		||||
 | 
			
		||||
# Link to final binary.
 | 
			
		||||
$(TARGET): $(OBJ_DIR)/grammar.o $(OBJ_FILES)
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Linking $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
 | 
			
		||||
	$(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS)
 | 
			
		||||
 | 
			
		||||
# Compile Unity object.
 | 
			
		||||
$(UNITY_OBJ): $(UNITY_C) $(UNITY_H)
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Compiling Unity...$(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Compiling Unity...$(RESETCOLOR)"
 | 
			
		||||
	$(CC) $(CFLAGS) -D UNITY_OUTPUT_COLOR -c $< -o $@
 | 
			
		||||
 | 
			
		||||
# Compile test object.
 | 
			
		||||
$(TEST_OBJ_DIR)/test_%.o: $(TEST_DIR)/test_%.c
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Compiling test object $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Compiling test object $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
 | 
			
		||||
	$(CC) $(CFLAGS) -c $< -o $@
 | 
			
		||||
 | 
			
		||||
# Link final test binary.
 | 
			
		||||
$(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/%.o $(UNITY_OBJ)
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Linking test binary $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Linking test binary $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
 | 
			
		||||
	$(LINK) -o $@ $? $(LDFLAGS)
 | 
			
		||||
 | 
			
		||||
# Run the test files.
 | 
			
		||||
test: $(TARGET) $(TEST_BIN_FILES)
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Running unit tests...$(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Running unit tests...$(RESETCOLOR)"
 | 
			
		||||
	for test in $(TEST_BIN_FILES); do ./$${test}; done
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Running validation tests...$(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Running validation tests...$(RESETCOLOR)"
 | 
			
		||||
	$(BATS) $(TEST_VAL_DIR)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	@ echo -e "$(WHITE_BOLD)Cleaning up...$(RESETCOLOR)"
 | 
			
		||||
	@ $(PRINT) "$(WHITE_BOLD)Cleaning up...$(RESETCOLOR)"
 | 
			
		||||
	rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) $(GRAM_DIR)/* $(UNITY_OBJ)
 | 
			
		||||
 | 
			
		||||
.PHONY: all clean test nocolor release run
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										18
									
								
								src/ast.c
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								src/ast.c
									
									
									
									
									
								
							@@ -30,6 +30,8 @@ void ast_destroy(AST* ast) {
 | 
			
		||||
        default:
 | 
			
		||||
            log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    free(ast);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ast_print(AST* ast) { ast_print_i(ast, 0); }
 | 
			
		||||
@@ -55,16 +57,12 @@ void ast_print_i(AST* ast, int i) {
 | 
			
		||||
ASTNumData* ast_num_data_init(double val) {
 | 
			
		||||
    talloc(ASTNumData, num);
 | 
			
		||||
 | 
			
		||||
    log_dbgf("val: %lf", val);
 | 
			
		||||
 | 
			
		||||
    *num = val;
 | 
			
		||||
 | 
			
		||||
    return num;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ast_num_data_destroy(ASTNumData* num) {
 | 
			
		||||
    if (!num) return free(num);
 | 
			
		||||
}
 | 
			
		||||
void ast_num_data_destroy(ASTNumData* num) { free(num); }
 | 
			
		||||
 | 
			
		||||
void ast_num_print(ASTNumData* data, int i) {
 | 
			
		||||
    INDENT_BEGIN(i);
 | 
			
		||||
@@ -87,8 +85,10 @@ ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ast_call_data_destroy(ASTCallData* call) {
 | 
			
		||||
    if (!call) return free(call->to);
 | 
			
		||||
    for (size_t i = 0; i < call->argc; i++) free(call->argv[i]);
 | 
			
		||||
    if (!call) return;
 | 
			
		||||
    free(call->to);
 | 
			
		||||
    for (size_t i = 0; i < call->argc; i++) ast_destroy(call->argv[i]);
 | 
			
		||||
    free(call->argv);
 | 
			
		||||
    free(call);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -103,9 +103,7 @@ void ast_call_print(ASTCallData* data, int i) {
 | 
			
		||||
    INDENT_END;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ASTVrefData* ast_vref_data_init(char* to) {
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
ASTVrefData* ast_vref_data_init(char* to) {}
 | 
			
		||||
 | 
			
		||||
void ast_vref_data_destroy(ASTVrefData* vref) {}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -21,6 +21,8 @@ void dstr_destroy(Dstr* dstr) {
 | 
			
		||||
    free(dstr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void dstr_destroypsv(Dstr* dstr) { free(dstr); }
 | 
			
		||||
 | 
			
		||||
// Check whether the buffer is overflowing and resize it if necessary.
 | 
			
		||||
void check_resz(Dstr* dstr, size_t ln) {
 | 
			
		||||
    while (dstr->ln + ln + 1 > dstr->sz) {
 | 
			
		||||
@@ -28,8 +30,7 @@ void check_resz(Dstr* dstr, size_t ln) {
 | 
			
		||||
        dstr->sz *= 2;
 | 
			
		||||
        dstr->buf = realloc(dstr->buf, dstr->sz);
 | 
			
		||||
        log_dbgf(
 | 
			
		||||
            "dstr @ %p doubled from %ld to %ld", dstr, dstr->sz / 2,
 | 
			
		||||
            dstr->sz
 | 
			
		||||
            "dstr @ %p doubled from %ld to %ld", dstr, dstr->sz / 2, dstr->sz
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
%{
 | 
			
		||||
    #include <string.h>
 | 
			
		||||
    #include <stdio.h>
 | 
			
		||||
    #include "../../src/include/ast.h"
 | 
			
		||||
    #include "../../src/include/lexer.h"
 | 
			
		||||
@@ -75,7 +76,9 @@ exp:
 | 
			
		||||
        AST** argv = calloc(2, sizeof(AST*));
 | 
			
		||||
        argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1));
 | 
			
		||||
        argv[1] = $2;
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("mul", 2, argv));
 | 
			
		||||
        char* to = malloc(4);
 | 
			
		||||
        strcpy(to, "mul");
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    | LGROUP exp RGROUP { $$ = $2; }
 | 
			
		||||
@@ -84,34 +87,45 @@ exp:
 | 
			
		||||
    //| WORD
 | 
			
		||||
 | 
			
		||||
    | WORD LGROUP arg RGROUP {
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, $3->ln, $3->buf));
 | 
			
		||||
        size_t argc = $3->ln;
 | 
			
		||||
        AST** argv = $3->buf;
 | 
			
		||||
        argarr_destroypsv($3);
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, argc, argv));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    | exp PLUS exp {
 | 
			
		||||
        AST** argv = calloc(2, sizeof(AST*));
 | 
			
		||||
        argv[0] = $1;
 | 
			
		||||
        argv[1] = $3;
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("sum", 2, argv));
 | 
			
		||||
        char* to = malloc(4);
 | 
			
		||||
        strcpy(to, "sum");
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    | exp SUB exp {
 | 
			
		||||
        AST** argv = calloc(2, sizeof(AST*));
 | 
			
		||||
        argv[0] = $1;
 | 
			
		||||
        argv[1] = $3;
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("sub", 2, argv));
 | 
			
		||||
        char* to = malloc(4);
 | 
			
		||||
        strcpy(to, "sub");
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    | exp MULT exp {
 | 
			
		||||
        AST** argv = calloc(2, sizeof(AST*));
 | 
			
		||||
        argv[0] = $1;
 | 
			
		||||
        argv[1] = $3;
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("mul", 2, argv));
 | 
			
		||||
        char* to = malloc(4);
 | 
			
		||||
        strcpy(to, "mul");
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    | exp DIV exp {
 | 
			
		||||
        AST** argv = calloc(2, sizeof(AST*));
 | 
			
		||||
        argv[0] = $1;
 | 
			
		||||
        argv[1] = $3;
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init("div", 2, argv));
 | 
			
		||||
        char* to = malloc(4);
 | 
			
		||||
        strcpy(to, "div");
 | 
			
		||||
        $$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
 | 
			
		||||
    }
 | 
			
		||||
%%
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
typedef enum {
 | 
			
		||||
    AST_TYPE_NUM, // A number.
 | 
			
		||||
    AST_TYPE_NUM,  // A number.
 | 
			
		||||
    AST_TYPE_CALL, // A function call.
 | 
			
		||||
    AST_TYPE_VREF, // A variable reference.
 | 
			
		||||
    AST_TYPE_MAX = AST_TYPE_CALL
 | 
			
		||||
@@ -27,9 +27,9 @@ void ast_num_data_destroy(ASTNumData* num);
 | 
			
		||||
void ast_num_print(ASTNumData*, int i);
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    char* to;       // What the call's to.
 | 
			
		||||
    size_t argc;    // Argument count.
 | 
			
		||||
    AST** argv;     // Argument vector.
 | 
			
		||||
    char* to;    // What the call's to.
 | 
			
		||||
    size_t argc; // Argument count.
 | 
			
		||||
    AST** argv;  // Argument vector.
 | 
			
		||||
} ASTCallData;
 | 
			
		||||
 | 
			
		||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
 | 
			
		||||
@@ -37,7 +37,7 @@ void ast_call_data_destroy(ASTCallData* call);
 | 
			
		||||
void ast_call_print(ASTCallData*, int i);
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    char* to;       // What the reference's to.
 | 
			
		||||
    char* to; // What the reference's to.
 | 
			
		||||
} ASTVrefData;
 | 
			
		||||
 | 
			
		||||
ASTVrefData* ast_vref_data_init(char* to);
 | 
			
		||||
 
 | 
			
		||||
@@ -3,16 +3,18 @@
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
#define DSTR_INITSZ 128
 | 
			
		||||
#define DSTR_INITSZ 2
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    char* buf;    // The buffer containing the string.
 | 
			
		||||
    char* buf; // The buffer containing the string.
 | 
			
		||||
    size_t sz; // The size of the buffer.
 | 
			
		||||
    size_t ln;    // The number of characters in the buffer.
 | 
			
		||||
    size_t ln; // The number of characters in the buffer.
 | 
			
		||||
} Dstr;
 | 
			
		||||
 | 
			
		||||
Dstr* dstr_init(void);
 | 
			
		||||
void dstr_destroy(Dstr* dstr);
 | 
			
		||||
// Destroy Dstr structure but preserve ->buf.
 | 
			
		||||
void dstr_destroypsv(Dstr* dstr);
 | 
			
		||||
 | 
			
		||||
// Append ln characters of src to dest.
 | 
			
		||||
void dstr_append(Dstr* dest, char* src, size_t ln);
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,8 @@ typedef struct {
 | 
			
		||||
 | 
			
		||||
ArgArr* argarr_init();
 | 
			
		||||
void argarr_destroy(ArgArr* argarr);
 | 
			
		||||
// Destroy ArgArr structure but preserve -> buf.
 | 
			
		||||
void argarr_destroypsv(ArgArr* argarr);
 | 
			
		||||
void argarr_add(ArgArr* argarr, AST* arg);
 | 
			
		||||
 | 
			
		||||
#include "../../build/grammars/grammar.tab.h"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								src/lexer.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/lexer.c
									
									
									
									
									
								
							@@ -5,8 +5,8 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#include "include/dstr.h"
 | 
			
		||||
#include "include/util.h"
 | 
			
		||||
#include "include/lexer.h"
 | 
			
		||||
#include "include/util.h"
 | 
			
		||||
 | 
			
		||||
ArgArr* argarr_init() {
 | 
			
		||||
    ArgArr* argarr = malloc(sizeof(ArgArr));
 | 
			
		||||
@@ -23,14 +23,14 @@ void argarr_destroy(ArgArr* argarr) {
 | 
			
		||||
    free(argarr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void argarr_destroypsv(ArgArr* argarr) { free(argarr); }
 | 
			
		||||
 | 
			
		||||
void argarr_add(ArgArr* argarr, AST* arg) {
 | 
			
		||||
    if ((argarr->ln + 1) * argarr->sz > argarr->sz) {
 | 
			
		||||
        argarr->sz *= 2;
 | 
			
		||||
        argarr->buf = realloc(argarr->buf, argarr->sz);
 | 
			
		||||
        log_dbgf(
 | 
			
		||||
            "ArgArr @ %p doubled from %ld to %ld",
 | 
			
		||||
            argarr,
 | 
			
		||||
            argarr->sz/2,
 | 
			
		||||
            "ArgArr @ %p doubled from %ld to %ld", argarr, argarr->sz / 2,
 | 
			
		||||
            argarr->sz
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
@@ -78,14 +78,16 @@ char* acc_word(int c) {
 | 
			
		||||
    } while (isalpha(*inp));
 | 
			
		||||
    dstr_appendch(val, *(inp - 1));
 | 
			
		||||
 | 
			
		||||
    return val->buf;
 | 
			
		||||
    char* ret = val->buf;
 | 
			
		||||
    dstr_destroypsv(val);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int yylex() {
 | 
			
		||||
    if (*inp == '\0') return YYEOF;
 | 
			
		||||
 | 
			
		||||
    // Skip all whitespace.
 | 
			
		||||
    while (*inp == ' ' || *inp == '\t') { inp++; }
 | 
			
		||||
    while (*inp == ' ' || *inp == '\t') inp++;
 | 
			
		||||
 | 
			
		||||
    // Assign & consume current character.
 | 
			
		||||
    int c = *inp++;
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,7 @@ int main(int argc, char** argv) {
 | 
			
		||||
    if (argc - 1 && strlen(argv[1]) > 0 && (inp = argv[1]) && !yyparse()) {
 | 
			
		||||
        log_dbg("Parsed successfully!\n");
 | 
			
		||||
        exec_print(exec_exp(root));
 | 
			
		||||
        ast_destroy(root);
 | 
			
		||||
        exit(0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -56,9 +57,12 @@ int main(int argc, char** argv) {
 | 
			
		||||
#ifdef DBG
 | 
			
		||||
            ast_print(root);
 | 
			
		||||
#endif
 | 
			
		||||
            ast_destroy(root);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        dstr_destroy(ln);
 | 
			
		||||
    }
 | 
			
		||||
lnskip:;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user