libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
ast.h File Reference
#include <stdio.h>
#include "types/ast.h"
Include dependency graph for ast.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void ast_node_init (ast_node_t *node, ast_node_t *parent, node_type_t type, token_t *token)
 Initializes an AST node.
 
void ast_close (ast_node_t *root)
 Closes the given AST root node and all children.
 
ast_node_tast_add_children (ast_node_t *parent, node_type_t type, token_t *token)
 Add a new children for the given AST node.
 
ast_node_tast_insert_children (ast_node_t *parent, ast_node_t *child)
 Insert a exitent AST node as children of the given parent node.
 
void ast_copy (ast_node_t *destiny, ast_node_t *source)
 Copies the content of source to destinty AST nodes.
 
void ast_to_json (ast_node_t *root, FILE *file)
 Reads the AST and converts it to JSON, writting on the given file stream.
 

Function Documentation

◆ ast_add_children()

ast_node_t * ast_add_children ( ast_node_t * parent,
node_type_t type,
token_t * token )

Add a new children for the given AST node.

Parameters
parentThe node parent where the new children will be added.
typeType of the node.
tokenToken to set for the new children node.
Returns
Pointer for the new children on parent.

Definition at line 40 of file ast.c.

41{
42 _ast_ensure_children_size(parent);
43
44 ast_node_t* child = &parent->children[parent->children_count++];
45 ast_node_init(child, parent, type, token);
46
47 return child;
48}
void ast_node_init(ast_node_t *node, ast_node_t *parent, node_type_t type, token_t *token)
Initializes an AST node.
Definition ast.c:17
unsigned int children_count
Definition ast.h:28
struct ast_node * children
Definition ast.h:27
Struct for a Mya token.
Definition token.h:32
struct ast_node ast_node_t

◆ ast_close()

void ast_close ( ast_node_t * root)

Closes the given AST root node and all children.

Parameters
rootThe AST's root node.

Definition at line 29 of file ast.c.

30{
31 for (int i = 0; i < root->children_count; i++) {
32 ast_close(&root->children[i]);
33 }
34
35 free(root->children);
36 root->children = NULL;
37}
void ast_close(ast_node_t *root)
Closes the given AST root node and all children.
Definition ast.c:29

◆ ast_copy()

void ast_copy ( ast_node_t * destiny,
ast_node_t * source )

Copies the content of source to destinty AST nodes.

Parameters
destinyWhere to copy the node data.
sourceThe node data that will be copied.

Definition at line 64 of file ast.c.

65{
66 memcpy(destiny, source, sizeof(ast_node_t));
67}

◆ ast_insert_children()

ast_node_t * ast_insert_children ( ast_node_t * parent,
ast_node_t * child )

Insert a exitent AST node as children of the given parent node.

It's copies the content of the given child for the new children on parent node.

Parameters
parentThe parent where to insert the child.
childThe child to insert as parent's children.
Returns
Pointer for the new children on parent.

Definition at line 51 of file ast.c.

52{
53 _ast_ensure_children_size(parent);
54
55 ast_node_t* new_child = &parent->children[parent->children_count++];
56
57 ast_copy(new_child, child);
58 new_child->parent = parent;
59
60 return new_child;
61}
void ast_copy(ast_node_t *destiny, ast_node_t *source)
Copies the content of source to destinty AST nodes.
Definition ast.c:64
struct ast_node * parent
Definition ast.h:24

◆ ast_node_init()

void ast_node_init ( ast_node_t * node,
ast_node_t * parent,
node_type_t type,
token_t * token )

Initializes an AST node.

Parameters
nodeThe node to be initialized.
parentParent node of the given node.
typeType of the node.
tokenToken of the given node.

Definition at line 17 of file ast.c.

18{
19 node->parent = parent;
20 node->type = type;
21 node->token = token;
22
23 node->children = malloc(sizeof(ast_node_t) * AST_INITIAL_CHILDREN_LENGTH);
24 node->children_count = 0;
26}
token_t * token
Definition ast.h:26
unsigned int _children_length
Definition ast.h:29
node_type_t type
Definition ast.h:25
#define AST_INITIAL_CHILDREN_LENGTH
Definition ast.h:5

◆ ast_to_json()

void ast_to_json ( ast_node_t * root,
FILE * file )

Reads the AST and converts it to JSON, writting on the given file stream.

Parameters
rootThe root of the AST.
fileThe file where to write the JSON.

Definition at line 70 of file ast.c.

71{
72 _ast_to_json_aux(root, file, 0);
73 fputc('\n', file);
74}