Compare commits

..

1 Commits

Author SHA1 Message Date
8b01407374 Added initial support for variables.
They are now parsed correctly, though they cannot be defined manually
and all have a value of 42.42.
2025-01-20 14:47:58 -05:00
10 changed files with 29 additions and 75 deletions

View File

@ -35,7 +35,6 @@ TEST_VAL_DIR = $(TEST_DIR)/validation
RESETCOLOR = \033[0m
WHITE = $(RESETCOLOR)\033[37m
WHITE_BOLD = $(RESETCOLOR)\033[37;1m
RED_BOLD = $(RESETCOLOR)\033[31;1m
all: $(TARGET)
@ -87,7 +86,7 @@ $(TEST_OBJ_DIR)/test_%.o: $(TEST_DIR)/test_%.c
$(CC) $(CFLAGS) -c $< -o $@
# Link final test binary.
$(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/grammar.o $(OBJ_FILES_NOMAIN) $(UNITY_OBJ)
$(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/%.o $(UNITY_OBJ)
@ mkdir -p $(TEST_BUILD_DIR)
@ $(PRINT) "$(WHITE_BOLD)Linking test binary $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
$(LINK) -o $@ $? $(LDFLAGS)
@ -95,7 +94,7 @@ $(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/grammar.o $(OB
# Run the test files.
test: $(TARGET) $(TEST_BIN_FILES)
@ $(PRINT) "$(WHITE_BOLD)Running unit tests...$(RESETCOLOR)"
for test in $(TEST_BIN_FILES); do ./$${test} || echo -e "$(RED_BOLD) BAD EXIT ON $${test} $(RESETCOLOR)"; done
for test in $(TEST_BIN_FILES); do ./$${test}; done
@ $(PRINT) "$(WHITE_BOLD)Running validation tests...$(RESETCOLOR)"
$(BATS) $(TEST_VAL_DIR)

View File

@ -27,6 +27,7 @@ void ast_destroy(AST* ast) {
switch (ast->type) {
case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break;
case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break;
case AST_TYPE_VREF: ast_vref_data_destroy(ast->data); break;
default:
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
}
@ -103,9 +104,18 @@ void ast_call_print(ASTCallData* data, int i) {
INDENT_END;
}
ASTVrefData* ast_vref_data_init(char* to) {}
ASTVrefData* ast_vref_data_init(char* to) {
talloc(ASTVrefData, vref);
void ast_vref_data_destroy(ASTVrefData* vref) {}
vref->to = to;
return vref;
}
void ast_vref_data_destroy(ASTVrefData* vref) {
free(vref->to);
free(vref);
}
void ast_vref_print(ASTVrefData* data, int i) {
INDENT_BEGIN(i);

View File

@ -13,6 +13,7 @@ ASTNumData exec_exp(AST* ast) {
switch (ast->type) {
case AST_TYPE_CALL: return exec_call(ast);
case AST_TYPE_NUM: return *(ASTNumData*)ast->data;
case AST_TYPE_VREF: return exec_vref(ast);
default: printf("what\n");
exit(1);
}
@ -67,4 +68,8 @@ ASTNumData exec_call(AST* ast) {
return -1000;
}
ASTNumData exec_vref(AST* ast) {
return *ast_num_data_init(42.42);
}
void exec_print(double n) { printf("= %lf\n", n); }

View File

@ -1,6 +0,0 @@
#include "include/global.h"
#include <stdlib.h>
// Global input text.
char* inp = NULL;

View File

@ -84,7 +84,9 @@ exp:
| LGROUP exp RGROUP { $$ = $2; }
// Variable reference.
//| WORD
| WORD {
$$ = ast_init(AST_TYPE_VREF, ast_vref_data_init($1));
}
| WORD LGROUP arg RGROUP {
size_t argc = $3->ln;

View File

@ -5,6 +5,7 @@
ASTNumData exec_exp(AST* ast);
ASTNumData exec_call(AST* ast);
ASTNumData exec_vref(AST* ast);
void exec_print(double n);
#endif

View File

@ -1,4 +0,0 @@
#ifndef GLOBAL_H
#define GLOBAL_H
#endif

View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include <string.h>
#include "include/global.h"
#include "include/ast.h"
#include "include/dstr.h"
#include "include/exec.h"
@ -12,7 +11,10 @@
// Global Abstract Syntax Tree.
extern AST* root;
extern char* inp;
// Global input text.
char* inp = NULL;
extern int yyparse();
int main(int argc, char** argv) {

View File

@ -1,56 +0,0 @@
#include "../src/include/ast.h"
#include "Unity/src/unity.h"
#include "Unity/src/unity_internals.h"
#include <string.h>
void setUp() {}
void tearDown() {}
void test_ast_num() {
ASTNumData* num = ast_num_data_init(12.0);
AST* ast = ast_init(AST_TYPE_NUM, num);
TEST_ASSERT_EQUAL(AST_TYPE_NUM, ast->type);
TEST_ASSERT_EQUAL(12.0, *(ASTNumData*)ast->data);
ast_destroy(ast);
}
void test_ast_vref() {
char* s = malloc(2);
strcpy(s, "x");
ASTVrefData* vref = ast_vref_data_init(s);
AST* ast = ast_init(AST_TYPE_VREF, vref);
TEST_ASSERT_EQUAL(AST_TYPE_VREF, ast->type);
ASTVrefData data = *(ASTVrefData*)ast->data;
TEST_ASSERT_EQUAL_STRING("x", data.to);
//ast_destroy(ast);
}
void test_ast_call() {
AST** argv = malloc(2*sizeof(AST*));
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(1.0));
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init(2.0));
ASTCallData* call = ast_call_data_init("f", 2, argv);
AST* ast = ast_init(AST_TYPE_CALL, call);
TEST_ASSERT_EQUAL(AST_TYPE_CALL, ast->type);
TEST_ASSERT_EQUAL("f", ((ASTCallData*)ast->data)->to);
TEST_ASSERT_EQUAL(2, ((ASTCallData*)ast->data)->argc);
TEST_ASSERT_EQUAL(1.0, ((ASTCallData*)ast->data)->argv[0]);
TEST_ASSERT_EQUAL(2.0, ((ASTCallData*)ast->data)->argv[1]);
ast_destroy(ast);
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_ast_num);
RUN_TEST(test_ast_vref);
RUN_TEST(test_ast_call);
return UNITY_END();
}

View File

@ -69,7 +69,8 @@ void test_dstr_appendch() {
TEST_ASSERT_EQUAL(DSTR_INITSZ * 2, dstr->sz);
}
int main() {
// not needed when using generate_test_runner.rb
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_dstr_init);
RUN_TEST(test_dstr_append);