diff --git a/src/ast.c b/src/ast.c index 24c0cc4..361371f 100644 --- a/src/ast.c +++ b/src/ast.c @@ -25,10 +25,10 @@ void ast_destroy(AST* ast) { if (!ast) return; 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; - case AST_TYPE_VDEF: ast_vdef_data_destroy(ast->data); break; + 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; + case AST_TYPE_VDEF: ast_vdef_data_destroy(ast->data); break; case AST_TYPE_BLOCK: ast_block_data_destroy(ast->data); break; default: log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX); @@ -77,6 +77,10 @@ void ast_num_print(ASTNumData* data, int i) { INDENT_END; } +ASTExcData* ast_exc_data_init(char* msg) { + return (ASTExcData*) msg; +} + ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) { talloc(ASTCallData, call); diff --git a/src/exec.c b/src/exec.c index 881c28f..4809ad3 100644 --- a/src/exec.c +++ b/src/exec.c @@ -10,45 +10,30 @@ extern AST* root; -ASTNumData exec_start(AST* ast) { +AST* exec_start(AST* ast) { scope = stack_init(); - // Global scope. Just dummy values for testing. HTab* global = htab_init(); - // n = 42.02 - char* name = malloc(2); // TODO: Write a macro for this pattern. - strcpy(name, "n"); - AST* n = ast_init( - AST_TYPE_VDEF, - ast_vdef_data_init( - name, ast_init(AST_TYPE_NUM, ast_num_data_init(42.02)) - ) - ); - - htab_ins( - global, ((ASTVDefData*)n->data)->name, ((ASTVDefData*)n->data)->val - ); - // Push global namespace to `scope`. stack_push(scope, global); return exec_exp(ast); } -ASTNumData exec_exp(AST* ast) { +AST* exec_exp(AST* ast) { log_dbg("Started execution."); switch (ast->type) { case AST_TYPE_BLOCK: return exec_block(ast); case AST_TYPE_CALL: return exec_call(ast); - case AST_TYPE_NUM: return *(ASTNumData*)ast->data; + case AST_TYPE_NUM: return ast; case AST_TYPE_VREF: return exec_vref(ast); case AST_TYPE_VDEF: return exec_vdef(ast); default: printf("what\n"); exit(1); } } -ASTNumData exec_block(AST* ast) { +AST* exec_block(AST* ast) { ASTBlockData* block = (ASTBlockData*)ast->data; HTab* local = htab_init(); @@ -56,7 +41,7 @@ ASTNumData exec_block(AST* ast) { // Loop through all but last ast. for (int i = 0; i < block->ln - 1; i++) exec_exp(block->inside[i]); - ASTNumData last = exec_exp(block->inside[block->ln - 1]); + AST* last = exec_exp(block->inside[block->ln - 1]); stack_pop(scope); htab_destroy(local); @@ -64,19 +49,28 @@ ASTNumData exec_block(AST* ast) { return last; } -ASTNumData exec_call(AST* ast) { +AST* exec_call(AST* ast) { log_dbg("Started call execution."); fflush(stdout); ASTCallData* calldata = (ASTCallData*)ast->data; if (calldata->argc >= 1) { if (!strcmp(calldata->to, "sum")) { - double total = exec_exp(calldata->argv[0]); + AST* total = ast_init(AST_TYPE_NUM, ast_num_data_init(0)); - for (size_t i = 1; i < calldata->argc; - total += exec_exp(calldata->argv[i++])); + for (size_t i = 0; i < calldata->argc; i++) { + AST* arg = exec_exp(calldata->argv[i]); + + if (arg->type != AST_TYPE_NUM) { + return ast_init( + AST_TYPE_EXC, ast_exc_data_init("Wrong type, fool.") + ); + } + + *((ASTNumData*) total->data) += *((ASTNumData*)arg->data); + } return total; - } else if (!strcmp(calldata->to, "sub")) { + } /*else if (!strcmp(calldata->to, "sub")) { double total = exec_exp(calldata->argv[0]); for (size_t i = 1; i < calldata->argc; @@ -97,9 +91,9 @@ ASTNumData exec_call(AST* ast) { total /= exec_exp(calldata->argv[i++])); return total; - } + }*/ } - return -1000; + return ast_init(AST_TYPE_EXC, ast_exc_data_init("No such function found.")); } AST* exec_find(char* name) { @@ -115,7 +109,7 @@ AST* exec_find(char* name) { exit(1); } -ASTNumData exec_vdef(AST* ast) { +AST* exec_vdef(AST* ast) { ASTVDefData* data = (ASTVDefData*)ast->data; AST* val = data->val; char* key = data->name; @@ -123,7 +117,7 @@ ASTNumData exec_vdef(AST* ast) { return exec_exp(val); } -ASTNumData exec_vref(AST* ast) { +AST* exec_vref(AST* ast) { log_dbg("attempting to reference var"); ASTVrefData* vref = (ASTVrefData*)ast->data; diff --git a/src/include/ast.h b/src/include/ast.h index cbe6fd0..41f932e 100644 --- a/src/include/ast.h +++ b/src/include/ast.h @@ -5,14 +5,15 @@ typedef enum { // Primitive types. - AST_TYPE_NUM, // A number (float). - AST_TYPE_STR, // A string - AST_TYPE_INT, // An integer. - AST_TYPE_SYM, // A symbol. + AST_TYPE_NUM, // A number (float). + AST_TYPE_STR, // A string + AST_TYPE_INT, // An integer. + AST_TYPE_SYM, // A symbol. + AST_TYPE_EXC, // Exception. - // Complex types: - AST_TYPE_VEC, // A vector (fixed size, fixed type). - AST_TYPE_LIST, // A list (variable size, variable type). + // Collection types: + AST_TYPE_VEC, // A vector (fixed size, fixed type). + AST_TYPE_LIST, // A list (variable size, variable type). // Misc. types. AST_TYPE_CALL, // A function call. @@ -38,6 +39,11 @@ ASTNumData* ast_num_data_init(double val); void ast_num_data_destroy(ASTNumData* num); void ast_num_print(ASTNumData*, int i); +// An exception. +typedef char* ASTExcData; +ASTExcData* ast_exc_data_init(char* msg); +void ast_exc_data_destroy(ASTExcData* exc); + typedef struct { char* to; // What the call's to. size_t argc; // Argument count. diff --git a/src/include/exec.h b/src/include/exec.h index 7867c07..a6b0c81 100644 --- a/src/include/exec.h +++ b/src/include/exec.h @@ -8,12 +8,12 @@ extern Stack* scope; // Start executing at the root of the AST. Initialize the `scope`. -ASTNumData exec_start(AST* ast); -ASTNumData exec_exp(AST* ast); -ASTNumData exec_block(AST* ast); -ASTNumData exec_call(AST* ast); -ASTNumData exec_vref(AST* ast); -ASTNumData exec_vdef(AST* ast); +AST* exec_start(AST* ast); +AST* exec_exp(AST* ast); +AST* exec_block(AST* ast); +AST* exec_call(AST* ast); +AST* exec_vref(AST* ast); +AST* exec_vdef(AST* ast); void exec_print(double n); #endif diff --git a/src/main.c b/src/main.c index 665d37e..4c29420 100644 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,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_start(root)); + ast_print(exec_start(root)); HTab* global = stack_pop(scope); htab_destroy(global); stack_destroy(scope); @@ -62,7 +62,7 @@ int main(int argc, char** argv) { ast_print(root); #endif - exec_print(exec_start(root)); + ast_print(exec_start(root)); HTab* global = stack_pop(scope); htab_destroy(global); stack_destroy(scope); diff --git a/test/val/test.bats b/test/val/test.bats index e5b6f95..c12a72c 100644 --- a/test/val/test.bats +++ b/test/val/test.bats @@ -113,7 +113,19 @@ bin() { ./scl.out $1 | tail -n1; } # [ "$output" = "= 4.000000" ] #} -@test "integer arithmetic" { +@test "type stuff" { run bin "x:int=1" [ "$output" = "= 1" ] + + run bin "x=1.5; type(x)" + [ "$output" = "= num"] + + run bin "x:int=1.5; type(x)" + [ "$output" = "= int" ] + + run bin "x:int=1.5" + [ "$output" = "= 1" ] + + run bin "print(\"Hello, world!\")" + [ "$output" = "= Hello, world!" ] }