-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreprocessor.h
More file actions
146 lines (110 loc) · 3.22 KB
/
preprocessor.h
File metadata and controls
146 lines (110 loc) · 3.22 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifdef LINE_DIRECTIVE
#line 2 "preprocessor.h"
#endif
// ---------------------------------------------------------------------------
char file_name[Str_size];
int line_no;
int line_pos;
char line[Str_size];
int line_length;
char source_buffer[Source_buffer_length];
// ---------------------------------------------------------------------------
preprocessor_init() {
line_no = 0;
line_pos = 0;
copy(source_buffer, "");
copy(file_name, "N/A");
}
// ---------------------------------------------------------------------------
preprocessor_get_line_no() {
return line_no;
}
// ---------------------------------------------------------------------------
preprocessor_get_line_pos() {
return line_pos;
}
// ---------------------------------------------------------------------------
preprocessor_get_source_chunk(char src[]) {
copy(src, source_buffer);
copy(source_buffer, "");
}
// ---------------------------------------------------------------------------
preprocessor_read_next_line() {
int chars_read;
chars_read = readln(line, Str_size - 1);
if (chars_read == Str_size - 1) {
error_preprocessor("line too long");
}
if (str_equals(line, ""))
return False;
line_no = line_no + 1;
line_pos = 0;
line_length = length(line);
append(source_buffer, line);
return True;
}
// ---------------------------------------------------------------------------
preprocessor_read_char() {
int c;
if (line_pos >= line_length)
return '\n';
c = line[line_pos];
line_pos = line_pos + 1;
return c;
}
// ---------------------------------------------------------------------------
preprocessor_lookahead1() {
if (line_pos >= line_length)
return '\n';
return line[line_pos];
}
// ---------------------------------------------------------------------------
preprocessor_lookahead2() {
if (line_pos + 1 >= line_length)
return '\n';
return line[line_pos + 1];
}
// ---------------------------------------------------------------------------
preprocessor_compiler_directive() {
int i;
if (starts_with(line, "#line 2 ")) {
line_no = 2;
copy(file_name, "");
i = length("#line 2 ");
while (line[i] != ' ' && line[i] != '\n' && line[i] != 0) {
append_char(file_name, line[i]);
i = i + 1;
}
error_set_file_name(file_name);
error_set_line(2, 0);
}
}
// ---------------------------------------------------------------------------
preprocessor_skip() {
int c;
int c1;
if (line_pos >= line_length) {
if (! preprocessor_read_next_line())
return False;
if (line_length == 0)
return preprocessor_skip();
if (starts_with(line, "#")) {
preprocessor_compiler_directive();
line_pos = 0;
line_length = 0;
return preprocessor_skip();
}
}
c = line[line_pos];
c1 = line[line_pos + 1];
if (c == ' ' || c == '\t' || c == '\n') {
line_pos = line_pos + 1;
return preprocessor_skip();
}
if (c == '/' && c1 == '/') {
line_pos = 0;
line_length = 0;
return preprocessor_skip();
}
return True;
}