diff options
author | kugwa <kugwa2000@gmail.com> | 2015-11-11 19:06:01 +0800 |
---|---|---|
committer | kugwa <kugwa2000@gmail.com> | 2015-11-11 19:06:01 +0800 |
commit | 62d9753dac503b34bb27635faf96a395d465f54d (patch) | |
tree | 62c06eaa6e0a06fdad263b27fc0843bd2af7d46d /src/ast.c | |
parent | 97c3aabffdc7ed612d322dd839f9ae4ab76efc6a (diff) | |
download | compiler2015-62d9753dac503b34bb27635faf96a395d465f54d.tar.gz compiler2015-62d9753dac503b34bb27635faf96a395d465f54d.tar.zst compiler2015-62d9753dac503b34bb27635faf96a395d465f54d.zip |
Merge TA's codes to ours
Delete tmp funtion used in HW2.
Move AST functions into ast.c.
CONST_INT, CONST_FLOAT, and CONST_STRING all return CONST.
Diffstat (limited to 'src/ast.c')
-rw-r--r-- | src/ast.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/ast.c b/src/ast.c new file mode 100644 index 0000000..7c35744 --- /dev/null +++ b/src/ast.c @@ -0,0 +1,114 @@ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include "ast.h" + +extern int line_number; + +AST_NODE *Allocate(AST_TYPE type) +{ + AST_NODE *temp; + temp = (AST_NODE*)malloc(sizeof(struct AST_NODE)); + temp->nodeType = type; + temp->dataType = NONE_TYPE; + temp->child = NULL; + temp->rightSibling = NULL; + temp->parent = NULL; + // Notice that leftmostSibling is not initialized as NULL + temp->leftmostSibling = temp; + temp->linenumber = line_number; + return temp; +} + +AST_NODE* makeSibling(AST_NODE *a, AST_NODE *b) +{ + while (a->rightSibling) { + a = a->rightSibling; + } + if (b == NULL) { + return a; + } + b = b->leftmostSibling; + a->rightSibling = b; + + b->leftmostSibling = a->leftmostSibling; + b->parent = a->parent; + while (b->rightSibling) { + b = b->rightSibling; + b->leftmostSibling = a->leftmostSibling; + b->parent = a->parent; + } + return b; +} + +AST_NODE* makeChild(AST_NODE *parent, AST_NODE *child) +{ + if (child == NULL) { + return parent; + } + if (parent->child) { + makeSibling(parent->child, child); + } else { + child = child->leftmostSibling; + parent->child = child; + while (child) { + child->parent = parent; + child = child->rightSibling; + } + } + return parent; +} + +AST_NODE* makeFamily(AST_NODE *parent, int childrenCount, ...) +{ + va_list childrenList; + va_start(childrenList, childrenCount); + AST_NODE* child = va_arg(childrenList, AST_NODE*); + makeChild(parent, child); + AST_NODE* tmp = child; + int index = 1; + for (index = 1; index < childrenCount; ++index) { + child = va_arg(childrenList, AST_NODE*); + tmp = makeSibling(tmp, child); + } + va_end(childrenList); + return parent; +} + +AST_NODE* makeIDNode(char *lexeme, IDENTIFIER_KIND idKind) +{ + AST_NODE* identifier = Allocate(IDENTIFIER_NODE); + identifier->semantic_value.identifierSemanticValue.identifierName = lexeme; + identifier->semantic_value.identifierSemanticValue.kind = idKind; + identifier->semantic_value.identifierSemanticValue.symbolTableEntry = NULL; + return identifier; +} + +AST_NODE* makeStmtNode(STMT_KIND stmtKind) +{ + AST_NODE* stmtNode = Allocate(STMT_NODE); + stmtNode->semantic_value.stmtSemanticValue.kind = stmtKind; + return stmtNode; +} + +AST_NODE* makeDeclNode(DECL_KIND declKind) +{ + AST_NODE* declNode = Allocate(DECLARATION_NODE); + declNode->semantic_value.declSemanticValue.kind = declKind; + return declNode; +} + +AST_NODE* makeExprNode(EXPR_KIND exprKind, int operationEnumValue) +{ + AST_NODE* exprNode = Allocate(EXPR_NODE); + exprNode->semantic_value.exprSemanticValue.isConstEval = 0; + exprNode->semantic_value.exprSemanticValue.kind = exprKind; + if (exprKind == BINARY_OPERATION) { + exprNode->semantic_value.exprSemanticValue.op.binaryOp = operationEnumValue; + } else if (exprKind == UNARY_OPERATION) { + exprNode->semantic_value.exprSemanticValue.op.unaryOp = operationEnumValue; + } else { + printf("Error in AST_NODE* makeExprNode(EXPR_KIND exprKind, int operationEnumValue)\n"); + } + return exprNode; +} |