diff --git a/src/ast.c b/src/ast.c index e8eba2f..019417d 100644 --- a/src/ast.c +++ b/src/ast.c @@ -2,26 +2,12 @@ #include #include "include/ast.h" -#include "include/dstr.h" #include "include/gc.h" #include "include/scope.h" #include "include/util.h" extern AST* root; -static char* asttype_names[] = { - [AST_TYPE_CALL] = "FUNC CALL", - [AST_TYPE_NUM] = "NUMBER", - [AST_TYPE_VREF] = "VAR REFERENCE", - [AST_TYPE_VDEF] = "VAR DEFINITION", - [AST_TYPE_BLOCK] = "BLOCK", - [AST_TYPE_EXC] = "EXCEPTION", - [AST_TYPE_FDEF] = "FUNCTION DEFINITION", - [AST_TYPE_ARG] = "DEFINITION ARGUMENT", - [AST_TYPE_LAMBDA] = "LAMBDA EXPRESSION" - -}; - AST* ast_init(ASTType type, void* data) { AST* ast = gc_alloc(sizeof(AST), GC_TYPE_AST); @@ -92,36 +78,6 @@ void ast_destroy_psv(AST* ast) { free(ast); } -void ast_print(AST* ast) { - if (!ast) return; - ast_print_i(ast, 0); -} - -void ast_print_i(AST* ast, int i) { - INDENT_BEGIN(i); - - INDENT_TITLE("AST", ast); - INDENT_FIELD("type", "%s", asttype_names[ast->type]); - INDENT_FIELD("scope", "%p", ast->scope); - INDENT_FIELD_EXT_NONL_START("data"); - switch (ast->type) { - case AST_TYPE_NUM: - printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data); - break; - case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break; - case AST_TYPE_EXC: ast_exc_print(ast->data, i + 2); break; - case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break; - case AST_TYPE_VDEF: ast_vdef_print(ast->data, i + 2); break; - case AST_TYPE_BLOCK: ast_block_print(ast->data, i + 2); break; - case AST_TYPE_FDEF: ast_fdef_print(ast->data, i + 2); break; - case AST_TYPE_ARG: ast_arg_print(ast->data, i + 2); break; - case AST_TYPE_LAMBDA: ast_lambda_print(ast->data, i + 2); break; - default: exit(1); - } - INDENT_FIELD_NONL_END; - INDENT_END; -} - ASTNumData* ast_num_data_init(double val) { talloc(ASTNumData, num); @@ -132,14 +88,6 @@ ASTNumData* ast_num_data_init(double val) { void ast_num_data_destroy(ASTNumData* num) { free(num); } -void ast_num_print(ASTNumData* data, int i) { - INDENT_BEGIN(i); - - INDENT_FIELD("data", "%lf", *data); - - INDENT_END; -} - ASTExcData* ast_exc_data_init(const char* msg, AST* trace) { ASTExcData* data = malloc(sizeof(ASTExcData)); data->msg = msg; @@ -152,21 +100,6 @@ void ast_exc_data_destroy(ASTExcData* exc) { free(exc); } -void ast_exc_print(ASTExcData* data, int i) { - INDENT_BEGIN(i); - - INDENT_TITLE("ASTExcData", data); - INDENT_FIELD("msg", "\"%s\"", data->msg); - if (data->trace == NULL) { - INDENT_FIELD("trace", "%p", NULL) - } else { - INDENT_FIELD_EXT_NONL_START("trace"); - ast_print_i(data->trace, i + 1); - INDENT_FIELD_NONL_END; - } - INDENT_END; -} - ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*)) { return (ASTBIFData*)fn; } @@ -202,17 +135,6 @@ void ast_call_data_destroy_psv(ASTCallData* call) { free(call); } -void ast_call_print(ASTCallData* data, int i) { - INDENT_BEGIN(i); - - INDENT_TITLE("ASTCallData", data); - INDENT_FIELD("to", "%s", data->to); - INDENT_FIELD("argc", "%ld", data->argc); - INDENT_FIELD_LIST("argv", data->argv, data->argc, ast_print_i); - - INDENT_END; -} - ASTVDefData* ast_vdef_data_init(char* name, AST* val) { talloc(ASTVDefData, vdef); @@ -233,18 +155,6 @@ void ast_vdef_data_destroy_psv(ASTVDefData* vdef) { free(vdef); } -void ast_vdef_print(ASTVDefData* vdef, int depth) { - INDENT_BEGIN(depth); - - INDENT_TITLE("ASTVDefData", vdef); - INDENT_FIELD("name", "%s", vdef->name); - INDENT_FIELD_EXT_NONL_START("val"); - ast_print_i(vdef->val, depth + 2); // 2 because already indented. - INDENT_FIELD_NONL_END; - - INDENT_END; -} - ASTVrefData* ast_vref_data_init(char* to) { talloc(ASTVrefData, vref); @@ -258,15 +168,6 @@ void ast_vref_data_destroy(ASTVrefData* vref) { free(vref); } -void ast_vref_print(ASTVrefData* data, int i) { - INDENT_BEGIN(i); - - INDENT_TITLE("ASTVrefData", data); - INDENT_FIELD("to", "%s", data->to); - - INDENT_END; -} - ASTBlockData* ast_block_data_init(AST** inside, size_t ln) { ASTBlockData* block = malloc(sizeof(ASTBlockData)); @@ -288,16 +189,6 @@ void ast_block_data_destroy_psv(ASTBlockData* block) { free(block); } -void ast_block_print(ASTBlockData* data, int depth) { - INDENT_BEGIN(depth); - - INDENT_TITLE("ASTBlockData", data); - INDENT_FIELD("ln", "%ld", data->ln); - INDENT_FIELD_LIST("inside", data->inside, data->ln, ast_print_i); - - INDENT_END; -} - ASTFDefData* ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body) { ASTFDefData* fdef = malloc(sizeof(ASTFDefData)); @@ -322,18 +213,6 @@ void ast_fdef_data_destroy_psv(ASTFDefData* fdef) { free(fdef); } -void ast_fdef_print(ASTFDefData* fdef, int i) { - INDENT_BEGIN(i) - INDENT_TITLE("ASTFDefData", fdef); - INDENT_FIELD("name", "%s", fdef->name); - INDENT_FIELD("argc", "%ld", fdef->argc); - INDENT_FIELD_LIST("argv", fdef->argv, fdef->argc, ast_print_i); - INDENT_FIELD_EXT_NONL_START("body"); - ast_print_i(fdef->body, i + 2); - INDENT_FIELD_NONL_END; - INDENT_END; -} - ASTArgData* ast_arg_data_init(char* name) { ASTArgData* arg = malloc(sizeof(ASTArgData)); arg->name = name; @@ -342,13 +221,6 @@ ASTArgData* ast_arg_data_init(char* name) { void ast_arg_data_destroy(ASTArgData* arg) { free(arg->name); } -void ast_arg_print(ASTArgData* arg, int i) { - INDENT_BEGIN(i); - INDENT_TITLE("ASTArgData", arg); - INDENT_FIELD("name", "%s", arg->name); - INDENT_END; -} - ASTLambdaData* ast_lambda_data_init(size_t argc, AST** argv, AST* body) { talloc(ASTLambdaData, lambda); @@ -364,17 +236,6 @@ void ast_lambda_data_destroy(ASTLambdaData* lambda) { free(lambda); } -void ast_lambda_print(ASTLambdaData* lambda, int i) { - INDENT_BEGIN(i) - INDENT_TITLE("ASTLambdaData", lambda); - INDENT_FIELD("argc", "%ld", lambda->argc); - INDENT_FIELD_LIST("argv", lambda->argv, lambda->argc, ast_print_i); - INDENT_FIELD_EXT_NONL_START("body"); - ast_print_i(lambda->body, i + 2); - INDENT_FIELD_NONL_END; - INDENT_END; -} - AST* ast_find(Scope* scope, char* name) { while (scope) { AST* gotten = htab_get(scope->here, name); diff --git a/src/exec.c b/src/exec.c index ce1c5de..ba64b71 100644 --- a/src/exec.c +++ b/src/exec.c @@ -4,7 +4,6 @@ #include "include/ast.h" #include "include/builtin.h" -#include "include/dlist.h" #include "include/exec.h" #include "include/htab.h" #include "include/scope.h" diff --git a/src/include/ast.h b/src/include/ast.h index ea6de51..05d76d1 100644 --- a/src/include/ast.h +++ b/src/include/ast.h @@ -44,10 +44,6 @@ AST* ast_init_scope(ASTType type, void* data, Scope* scope); void ast_destroy(AST* ast); // Destroy an `AST`. void ast_destroy_psv(AST* ast); -// Print an `AST`, recursively. -void ast_print(AST* ast); -// Helper function to `ast_print()`, where `i` is indentation level. -void ast_print_i(AST* ast, int i); // A number. typedef double ASTNumData; @@ -56,8 +52,6 @@ typedef double ASTNumData; ASTNumData* ast_num_data_init(double val); // Destroy an `ASTNumData`. void ast_num_data_destroy(ASTNumData* num); -// Print an `ASTNumData`. -void ast_num_print(ASTNumData*, int i); // An exception. typedef struct ASTEXCDATA { @@ -68,16 +62,30 @@ typedef struct ASTEXCDATA { ASTExcData* ast_exc_data_init(const char* msg, AST* trace); // Destroy an `ASTExecData`. void ast_exc_data_destroy(ASTExcData* exc); -// Print an `ASTExecData`. -void ast_exc_print(ASTExcData*, int i); -// arguments list as anonymous struct +// A variable; associates a name with an expression. +typedef struct { + char* name; + AST* val; +} ASTVar; + +#define ARG(VAL) (ASTVar){NULL, VAL} +#define PAR(NAME) (ASTVar){NAME, NULL} + +// Argument list as anonymous struct. #define ARGS \ struct { \ size_t argc; \ AST** argv; \ } +// Parameter list as anonymous struct. +#define PARS \ + struct { \ + size_t parc; \ + AST** parv; \ + } + // A built-in function. typedef AST* (*ASTBIFData)(size_t argc, AST** argv, Scope* scope); @@ -98,8 +106,6 @@ ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv); void ast_call_data_destroy(ASTCallData* call); // Destroy an `ASTCallData`. void ast_call_data_destroy_psv(ASTCallData* call); -// Print an `ASTCallData`. -void ast_call_print(ASTCallData*, int i); // A variable definition's data. typedef struct { @@ -113,8 +119,6 @@ ASTVDefData* ast_vdef_data_init(char* name, AST* val); void ast_vdef_data_destroy(ASTVDefData* vdef); // Destroy an `ASTVDefData`. void ast_vdef_data_destroy_psv(ASTVDefData* vdef); -// Print an `ASTVDefData`. -void ast_vdef_print(ASTVDefData*, int depth); // A variable reference's data. typedef struct { @@ -125,8 +129,6 @@ typedef struct { ASTVrefData* ast_vref_data_init(char* to); // Destroy an `ASTVRefData`. void ast_vref_data_destroy(ASTVrefData* call); -// Print an `ASTVRefData`. -void ast_vref_print(ASTVrefData*, int i); // A code block. typedef struct { @@ -140,8 +142,6 @@ ASTBlockData* ast_block_data_init(AST** inside, size_t ln); void ast_block_data_destroy(ASTBlockData* block); // Destroy an `ASTBlockData`. void ast_block_data_destroy_psv(ASTBlockData* block); -// Print an `ASTBlockData`. -void ast_block_print(ASTBlockData*, int i); typedef struct { char* name; // Function name. @@ -155,8 +155,6 @@ ASTFDefData* ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body); void ast_fdef_data_destroy(ASTFDefData* fdef); // Destroy an `ASTFDefData`. void ast_fdef_data_destroy_psv(ASTFDefData* fdef); -// Print an `ASTFDefData`. -void ast_fdef_print(ASTFDefData* fdef, int i); typedef struct { char* name; // Argument name. @@ -166,8 +164,6 @@ typedef struct { ASTArgData* ast_arg_data_init(char* name); // Destroy an `ASTArgData`. void ast_arg_data_destroy(ASTArgData* arg); -// Print an `ASTArgData`. -void ast_arg_print(ASTArgData* arg, int i); // Represents a function as data. typedef struct { @@ -179,8 +175,6 @@ typedef struct { ASTLambdaData* ast_lambda_data_init(size_t argc, AST** argv, AST* body); // Destroy an `ASTLambdaData`. void ast_lambda_data_destroy(ASTLambdaData*); -// Print an `ASTLambdaData`. -void ast_lambda_print(ASTLambdaData* arg, int i); // Find a name in the scope. AST* ast_find(Scope* scope, char* name); diff --git a/src/main.c b/src/main.c index 0cd57dc..6c84b76 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include #include "include/ast.h" +#include "include/ast_print.h" #include "include/dstr.h" #include "include/exec.h" #include "include/gc.h"