-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.cpp
88 lines (85 loc) · 2.22 KB
/
format.cpp
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
#include "format.h"
#include "lexer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern FILE *fp;
extern char token_text[MAXLEN];
void format()
{
int tabs = 0; // 控制缩进
int i;
int flag = 0; // 用于标记无大括号但需要缩进的情况
token *root, *tail, *p;
while (1)
{
root = readline();
if (root == NULL)
break;
tail = root;
while (tail->next != NULL)
tail = tail->next;
if ((root == tail) && (strcmp(tail->str, "}") == 0))
tabs--;
if ((root != tail) && (strcmp(root->str, "}") == 0))
tabs--;
for (i = 0; i < tabs; i++) // 输出缩进
printf("\t");
if (flag == 1)
{
tabs--;
flag = 0;
}
p = root;
while (p != NULL)
{
printf("%s ", p->str);
p = p->next;
}
printf("\n");
if (strcmp(tail->str, "{") == 0)
tabs++;
p = root;
while (p != NULL)
{
if ((strcmp(p->str, "if") == 0) || (strcmp(p->str, "for") == 0) || (strcmp(p->str, "while") == 0))
{
if (strcmp(tail->str, "{") != 0 && strcmp(tail->str, ";") != 0)
{
tabs++;
flag = 1;
}
}
p = p->next;
}
}
}
token *readline()
{
char c;
int w;
token *root = NULL;
token *tail = root; //完成初始化
memset(token_text, 0, sizeof(token_text));
w = gettoken(fp);
if (w == EndOfFile)
return NULL;
root = (token *)malloc(sizeof(token));
root->str = (char *)malloc(MAXLEN*sizeof(char));
strcpy(root->str, token_text);
root->next = NULL;
tail = root;
/* 每识别一个单词判断单词后面是不是换行符 */
while (((c = fgetc(fp)) != '\n') && (c != EOF))
{
ungetc(c, fp);
memset(token_text, 0, sizeof(token_text));
gettoken(fp);
tail->next = (token *)malloc(sizeof(token));
tail = tail->next;
tail->str = (char *)malloc(MAXLEN*sizeof(char));
strcpy(tail->str, token_text);
tail->next = NULL;
}
return root;
}