-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpprint.c
125 lines (101 loc) · 4.4 KB
/
pprint.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
/****************************************************************/
/* */
/* Program 8-1: Pascal Pretty Printer */
/* */
/* Read and check the syntax of a Pascal program, */
/* and then print it out in a nicely-indented format. */
/* */
/* FILE: pprint.c */
/* */
/* REQUIRES: Modules parser, symbol table, scanner, */
/* error */
/* */
/* Files ppdecl.c, ppstmt.c */
/* */
/* FLAGS: Macro flag "analyze" must be defined */
/* */
/* USAGE: pprint sourcefile */
/* */
/* sourcefile name of source file containing */
/* the program to be pretty-printed */
/* */
/* Copyright (c) 1991 by Ronald Mak */
/* For instructional purposes only. No warranties. */
/* */
/****************************************************************/
#include <stdio.h>
#include "common.h"
#include "error.h"
#include "scanner.h"
#include "parser.h"
#include "pprint.h"
/*--------------------------------------------------------------*/
/* Externals */
/*--------------------------------------------------------------*/
extern BOOLEAN print_flag;
/*--------------------------------------------------------------*/
/* Globals */
/*--------------------------------------------------------------*/
char *code_buffer; /* code buffer */
char *code_bufferp; /* code buffer ptr */
char *code_segmentp; /* code segment ptr */
char *code_segment_limit; /* end of code segment */
TOKEN_CODE ctoken; /* token from code segment */
char pprint_buffer[MAX_PRINT_LINE_LENGTH]; /* print buffer */
int left_margin = 0; /* margin in buffer */
/*--------------------------------------------------------------*/
/* main Initialize the scanner and call */
/* routine program. */
/*--------------------------------------------------------------*/
main(argc, argv)
int argc;
char *argv[];
{
/*
-- Initialize the scanner.
*/
print_flag = FALSE;
init_scanner(argv[1]);
/*
-- Process a program.
*/
get_token();
program();
quit_scanner();
}
/*--------------------------------------------------------------*/
/* emit Emit a string to the print buffer. */
/*--------------------------------------------------------------*/
emit(string)
char *string;
{
int buffer_length = strlen(pprint_buffer);
int string_length = strlen(string);
if (buffer_length + string_length >= MAX_PRINT_LINE_LENGTH - 1) {
flush();
indent();
}
strcat(pprint_buffer, string);
}
/*--------------------------------------------------------------*/
/* indent Indent left_margin spaces in the print */
/* buffer. */
/*--------------------------------------------------------------*/
indent()
{
if (left_margin > 0)
sprintf(pprint_buffer, "%*s", left_margin, " ");
else
pprint_buffer[0] = '\0';
}
/*--------------------------------------------------------------*/
/* flush Print the print buffer if there is */
/* anything in it. */
/*--------------------------------------------------------------*/
flush()
{
if (pprint_buffer[0] != '\0') {
printf("%s\n", pprint_buffer);
pprint_buffer[0] = '\0';
}
}