-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
131 lines (116 loc) · 3.17 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
120
121
122
123
124
125
126
127
128
129
130
131
//
// Created by JakoError on 2021/11/29.
//
#include "ast.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void printAst(Bean bean, int layer) {
if (bean == NULL) return;
printf("%s\n", toString(bean));
if (bean->i > 0 && bean->beans != NULL) {
for (int i = 0; i < bean->i; ++i) {
for (int j = 0; j < layer; ++j)
printf(" | ");
printf(" |-");
printAst(bean->beans[i], layer + 1);
}
}
}
Bean newBean() {
Bean bean = (Bean) malloc(sizeof(struct bean));
if (bean == NULL) {
printf("error in malloc bean\n");
exit(0);
}
memset(bean, 0, sizeof(struct bean));
return bean;
}
Bean beanInfo(String type, String value) {
Bean bean = newBean();
bean->type = type;
bean->value = value;
return bean;
}
void tobType(Bean bean, String type) {
if (bean == NULL) return;
bean->type = type;
}
void tobValue(Bean bean, String value) {
if (bean == NULL) return;
bean->value = value;
}
Bean *newBeans() {
Bean *beans = (Bean *) malloc(sizeof(Bean));
if (beans == NULL) {
printf("error in malloc bean\n");
exit(0);
}
memset(beans, 0, sizeof(Bean));
return beans;
}
void addBean(Bean bean, Bean child) {
if (child == NULL) return;
if (bean->beans == NULL) {
bean->i = 0;
bean->beans = newBeans();
} else {
bean->beans = realloc(bean->beans, sizeof(Bean) * (bean->i + 1));
if (bean->beans == NULL) {
printf("fail to realloc bean");
exit(0);
}
}
bean->beans[bean->i++] = child;
}
void addBeans(Bean bean, Bean beans) {
if (bean == NULL || beans == NULL || beans->beans == NULL) return;
for (int i = 0; i < beans->i; ++i)
addBean(bean, beans->beans[i]);
free(beans);
}
String toString(Bean bean) {
if (bean->value == NULL || strlen(bean->value) == 0)
return bean->type;
String result = (String) malloc(sizeof(char) * (strlen(bean->type) + strlen(bean->value) + 3));
sprintf(result, "%s(%s)", bean->type, bean->value);
return result;
}
int o_atoi(String str) {
if (str == NULL || strlen(str) < 1 || str[0] != '0') return 0;
int len = strlen(str);
int result = 0;
int factor = 1;
for (int i = 0; i < len - 1; ++i) {
char c = str[len - i - 1];
if (c < '0' || c > '7')
return 0;
result += (c - '0') * factor;
factor *= 8;
}
return result;
}
int h_atoi(String str) {
if (str == NULL || strlen(str) < 2 || (str[0] != '0' || (str[1] != 'x' && str[1] != 'X'))) return 0;
int len = strlen(str);
int result = 0;
int factor = 1;
for (int i = 0; i < len - 2; ++i) {
char c = str[len - i - 1];
if (c >= '0' && c <= '9')
result += (c - '0') * factor;
else if (c >= 'a' && c <= 'f')
result += (c - 'a' + 10) * factor;
else if (c >= 'A' && c <= 'F')
result += (c - 'A' + 10) * factor;
else
return 0;
factor *= 16;
}
return result;
}
String itos(int i) {
String str = (String) malloc(sizeof(char) * 12);
sprintf(str, "%d", i);
return str;
}