From f59f5913e5624dd7770a69c31e6917c2825a5437 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 24 Jun 2021 16:25:42 -0400 Subject: [PATCH] created ast.c --- cerise/inc/cerise.h | 62 ++++++------------------------------ cerise/src/ast.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 53 deletions(-) create mode 100644 cerise/src/ast.c diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 9b13ab8..246b261 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -295,56 +295,12 @@ typedef struct { } val; } AstValue; -static AstNode* ast_new(int type, AstNode* l0, AstNode* l1, AstNode* l2) -{ - AstNode* node = calloc(1, sizeof(AstNode)); - node->hdr.code = type; - node->links[0] = l0; - node->links[1] = l1; - node->links[2] = l2; - return node; -} - -static AstNode* ast_int(long long val) -{ - AstValue* node = (AstValue*)ast_new(INT, NULL, NULL, NULL); - node->val.i = val; - return (AstNode*)node; -} - -static AstNode* ast_real(double val) -{ - AstValue* node = (AstValue*)ast_new(REAL, NULL, NULL, NULL); - node->val.i = val; - return (AstNode*)node; -} - -static AstNode* ast_binop(int op, AstNode* left, AstNode* right) -{ - return ast_new(op, left, right, NULL); -} - -static AstNode* ast_unop(int op, AstNode* operand) -{ - return ast_new(op, operand, NULL, NULL); -} - -static AstNode* ast_block(void) -{ - return ast_new(BEGIN, NULL, NULL, NULL); -} - -static AstNode* ast_call(AstNode* func) -{ - return ast_new(CALL, func, NULL, NULL); -} - -static AstNode* ast_if(AstNode* cond, AstNode* br1, AstNode* br2) -{ - return ast_new(IF, cond, br1, br2); -} - -static AstNode* ast_return(AstNode* expr) -{ - return ast_new(RETURN, expr, NULL, NULL); -} +AstNode* ast_new(int type, AstNode* l0, AstNode* l1, AstNode* l2); +AstNode* ast_int(long long val); +AstNode* ast_real(double val); +AstNode* ast_binop(int op, AstNode* left, AstNode* right); +AstNode* ast_unop(int op, AstNode* operand); +AstNode* ast_block(void); +AstNode* ast_call(AstNode* func); +AstNode* ast_if(AstNode* cond, AstNode* br1, AstNode* br2); +AstNode* ast_return(AstNode* expr); diff --git a/cerise/src/ast.c b/cerise/src/ast.c new file mode 100644 index 0000000..a2f7ba7 --- /dev/null +++ b/cerise/src/ast.c @@ -0,0 +1,77 @@ +#include + +AstNode* ast_new(int type, AstNode* l0, AstNode* l1, AstNode* l2) +{ + AstNode* node = calloc(1, sizeof(AstNode)); + node->hdr.code = type; + node->links[0] = l0; + node->links[1] = l1; + node->links[2] = l2; + return node; +} + +AstNode* ast_ident(long long index) +{ + AstValue* node = (AstValue*)ast_new(IDENT, NULL, NULL, NULL); + node->val.i = index; + return (AstNode*)node; +} + +AstNode* ast_int(long long val) +{ + AstValue* node = (AstValue*)ast_new(INT, NULL, NULL, NULL); + node->val.i = val; + return (AstNode*)node; +} + +AstNode* ast_real(double val) +{ + AstValue* node = (AstValue*)ast_new(REAL, NULL, NULL, NULL); + node->val.i = val; + return (AstNode*)node; +} + +AstNode* ast_binop(int op, AstNode* left, AstNode* right) +{ + return ast_new(op, left, right, NULL); +} + +AstNode* ast_unop(int op, AstNode* operand) +{ + return ast_new(op, operand, NULL, NULL); +} + +AstNode* ast_block(void) +{ + return ast_new(BEGIN, NULL, NULL, NULL); +} + +void ast_block_add(AstNode* func) +{ + /* TODO: append to linked list */ +} + +AstNode* ast_call(AstNode* func) +{ + return ast_new(CALL, func, NULL, NULL); +} + +void ast_call_add(AstNode* func) +{ + /* TODO: append to linked list */ +} + +AstNode* ast_if(AstNode* cond, AstNode* br1, AstNode* br2) +{ + return ast_new(IF, cond, br1, br2); +} + +AstNode* ast_return(AstNode* expr) +{ + return ast_new(RETURN, expr, NULL, NULL); +} + +void ast_print(AstNode* node) +{ + /* TODO: append to linked list */ +} -- 2.49.0