40 lines
953 B
C
40 lines
953 B
C
//
|
|
// Created by Natuie on 2025/3/22.
|
|
//
|
|
|
|
#include <malloc.h>
|
|
#include <stdio.h>
|
|
#include "ast.h"
|
|
|
|
// 创建一个节点
|
|
ASTNode *create_node(NodeType type) {
|
|
ASTNode *node = malloc(sizeof(ASTNode));
|
|
node->type = type;
|
|
node->value = NULL;
|
|
node->line = 0;
|
|
node->column = 0;
|
|
node->children = NULL;
|
|
node->children_count = 0;
|
|
return node;
|
|
}
|
|
|
|
// 添加子节点
|
|
ASTNode add_child(ASTNode *parent, ASTNode *child) {
|
|
parent->children = realloc(parent->children, (parent->children_count + 1) * sizeof(ASTNode *));
|
|
if (parent->children == NULL) {
|
|
fprintf(stderr, "Memory allocation error\n");
|
|
exit(1);
|
|
}
|
|
parent->children[parent->children_count] = child;
|
|
parent->children_count++;
|
|
return *parent;
|
|
}
|
|
|
|
void set_node_position(ASTNode *node, int line, int column) {
|
|
node->line = line;
|
|
node->column = column;
|
|
}
|
|
|
|
void set_node_value(ASTNode *node, char *value) {
|
|
node->value = value;
|
|
} |