-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.h
81 lines (66 loc) · 2.32 KB
/
ast.h
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
#ifndef ast_h
#define ast_h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdbool.h>
#define AST_GLOBALS
#define MAX_VARS 100 // Maximum number of Varblies
#define MAX_DEFS 100 // Maximum number of Keywords
// #define MAX_BUFF_COLS 40 // Maximum number of Characters every line
// #define MAX_BUFF_ROWS 40 // Maximum number of lines
// #define PARSE_DEBUG // Whether to print the debugging information
/*************************** Define the type of syntax tree ***************************/
/* Enumerated type of the tree node */
typedef enum {TYPE_CONST, TYPE_VAR, TYPR_FUN, TYPE_ARRAY, TYPE_STRUCT, TYPE_TERMINAL, TYPE_UNTREMINAL} NodeEnum;
typedef enum {TYPE_NUM = -2, TYPE_FLOAT} Type;
/* Nonterminal */
typedef struct{
char name[32]; // Name
int num; // Number of Nonterminals
struct NodeTag **node;
} VN_Node;
typedef struct NodeTag{
NodeEnum kind; // Enumerated type
Type type; // Variable type
int numI; // Int
float numF; // Float
int index; // Index
int level; // Level of the variable
int deminsion; // 0 while it is not an array
VN_Node vn; // Nonterminal Struct
}Node;
struct VarIndex
{
int val;
char mark[32];
};
struct VarDefine
{
int index;
char name[32];
};
extern Node *treeHead;
struct VarIndex G_Var[MAX_VARS]; // Variable index array
extern struct VarDefine G_Def[MAX_DEFS]; // Variable array
extern int G_iVarMaxIndex; // Current total number of variables
extern int G_iVarCurIndex; // Current variable index
FILE *treeFile;
extern int yylineno; // Line number
extern char* yytext;
extern bool error;
void yyerror(char *s); // Refactoring the error function
/*************************** Build the synax tree ***************************/
/* Attribute type related functions */
Node *set_vn(char* name, int num, ...);
Node *set_var(int value);
Node *set_int(int value);
Node *set_float(float value);
Node *set_terminal(int value);
/* Tree node related functions */
void NodeFree(Node *p);
int NodeExecute(FILE* target_file, Node *p, int num);
void add_var(char *);
#endif