-
Notifications
You must be signed in to change notification settings - Fork 6
/
ast.c
120 lines (92 loc) · 2.55 KB
/
ast.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Autor: Adriano Melo <[email protected]>
* Modificado por:
*
* ast.c : implementa as funções necessárias para criação da AST.
**/
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
/**
* Aloca memória para um program_t e zera os seus atributos
*/
program_t* new_program()
{
program_t* programa = (program_t*) malloc (sizeof(program_t));
programa->node_type = type_program;
programa->head = 0;
programa->tail = 0;
return programa;
}
/**
* Adiciona um statement a um programa.
*/
void add_statement(program_t* program, statement_list_t* stmt)
{
printf("[add_statement] statement adicionado\n");
if (program->head == 0){
program->head = stmt;
program->tail = stmt;
} else if (stmt != 0) {
program->tail->next = stmt;
program->tail = stmt;
}
}
statement_list_t* new_statement_list(ast_t* stmt)
{
statement_list_t* stmt_list = (statement_list_t*) malloc (sizeof(statement_list_t));
stmt_list->node_type = type_statement_list;
stmt_list->stmt = stmt;
stmt_list->next = 0;
return stmt_list;
}
attribution_t* new_attribution(identifier_t* identifier, ast_t* expression)
{
attribution_t* att = (attribution_t*) malloc (sizeof(attribution_t));
att->node_type = type_attribution;
att->identifier = identifier;
att->value = expression;
return att;
}
expression_t* new_expression(ast_t* left, ast_t* operation, ast_t* right)
{
expression_t* exp = (expression_t*) malloc (sizeof(expression_t));
exp->node_type = type_expression;
exp->left = left;
exp->operation = operation;
exp->right = right;
return exp;
}
operation_t* new_operation(char* operation)
{
operation_t* op = (operation_t*) malloc (sizeof(operation_t));
op->operation = operation;
return op;
}
identifier_t* new_identifier(char* identifier)
{
identifier_t* id = (identifier_t*) malloc (sizeof(identifier_t));
id->node_type = type_identifier;
id->identifier = identifier;
return id;
}
type_t* new_type(char* name)
{
type_t* tipo = (type_t*) malloc (sizeof(type_t));
tipo->name = name;
if (strcmp(name, "int") == 0){
tipo->node_type = type_int;
} else if (strcmp(name, "double") == 0){
tipo->node_type = type_double;
} else if (strcmp(name, "char") == 0) {
tipo->node_type = type_char;
}
return tipo;
}
number_t* new_number(double value)
{
number_t* num = (number_t*) malloc (sizeof(number_t));
num->node_type = type_number;
num->value = value;
return num;
}