#ifdef HAVE_CONFIG_H # include "config.h" #endif #include #include #include #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; }