compilers-lab1/node.c

202 lines
3.9 KiB
C
Raw Normal View History

2025-03-24 18:40:16 +08:00
#include "node.h"
#include <stdio.h>
#include <stdlib.h>
pNode newNode(NodeType typ, intptr_t line, intptr_t size, Node **children,
ValueUnion value) {
pNode node = (pNode)calloc(sizeof(Node), 1);
Node **new_children = (Node **)calloc(sizeof(pNode), size);
for (int i = 0; i < size; i++) {
new_children[i] = children[i];
}
node->type = typ;
node->line = line;
node->size = size;
node->children = new_children;
node->value = value;
return node;
}
ValueUnion nullValue() {
ValueUnion value = {.null = NULL};
return value;
}
void displayNode(pNode);
void DFS(pNode node, int level) {
if (node == NULL) {
return;
};
if (node->line != -1) {
if (node->size == 0) {
for (int i = 0; i < level; i++)
printf(" ");
displayNode(node);
putchar('\n');
} else {
for (int i = 0; i < level; i++)
printf(" ");
displayNode(node);
putchar('\n');
}
}
for (int i = 0; i < node->size; i++)
DFS(node->children[i], level + 1);
}
void displayNode(pNode node) {
switch (node->type) {
case Type_int:
printf("TYPE: int");
break;
case Type_float:
printf("TYPE: float");
break;
case Ident:
printf("ID: %s", node->value.id);
break;
case Integer:
printf("INT: %ld", node->value.integer);
break;
case Float:
printf("FLOAT: %lf", node->value.real);
break;
case Semi:
printf("SEMI");
break;
case Comma:
printf("COMMA");
break;
case Assignop:
printf("ASSIGNOP");
break;
case Plus:
printf("PLUS");
break;
case Minus:
printf("MINUS");
break;
case Times:
printf("TIMES");
break;
case Div:
printf("DIV");
break;
case And:
printf("AND");
break;
case Or:
printf("OR");
break;
case Dot:
printf("DOT");
break;
case Not:
printf("NOT");
break;
case Lp:
printf("LP");
break;
case Rp:
printf("RP");
break;
case Lb:
printf("LB");
break;
case Rb:
printf("RB");
break;
case Lc:
printf("LC");
break;
case Rc:
printf("RC");
break;
case Struct:
printf("STRUCT");
break;
case Return:
printf("RETURN");
break;
case If:
printf("IF");
break;
case Else:
printf("ELSE");
break;
case While:
printf("WHILE");
break;
case Line_comment:
printf("LINE_COMMENT");
break;
case Block_comment:
printf("BLOCK_COMMENT");
break;
case Program:
printf("Program (%ld)", node->line);
break;
case ExtDefList:
printf("ExtDefList (%ld)", node->line);
break;
case ExtDef:
printf("ExtDef (%ld)", node->line);
break;
case ExtDecList:
printf("ExtDecList (%ld)", node->line);
break;
case Specifier:
printf("Specifier (%ld)", node->line);
break;
case StructSpecifier:
printf("StructSpecifier (%ld)", node->line);
break;
case OptTag:
printf("OptTag (%ld)", node->line);
break;
case Tag:
printf("Tag (%ld)", node->line);
break;
case VarDec:
printf("VarDec (%ld)", node->line);
break;
case FunDec:
printf("FunDec (%ld)", node->line);
break;
case VarList:
printf("VarList (%ld)", node->line);
break;
case ParamDec:
printf("ParamDec (%ld)", node->line);
break;
case CompSt:
printf("CompSt (%ld)", node->line);
break;
case StmtList:
printf("StmtList (%ld)", node->line);
break;
case Stmt:
printf("Stmt (%ld)", node->line);
break;
case DefList:
printf("DefList (%ld)", node->line);
break;
case Def:
printf("Def (%ld)", node->line);
break;
case DecList:
printf("DecList (%ld)", node->line);
break;
case Dec:
printf("Dec (%ld)", node->line);
break;
case Exp:
printf("Exp (%ld)", node->line);
break;
case Args:
printf("Args (%ld)", node->line);
break;
default:
printf("%d (%ld)", node->type, node->line);
}
}