/
meta2.c
238 lines (201 loc) · 3.7 KB
/
meta2.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright 2011 Peter Stuifzand
*/
#include <stdio.h>
#include <string.h>
#include "lib.h"
#include <stdlib.h>
int label_count = 0;
int ex1(struct parser* p);
void generate_label(int lbl)
{
int c = lbl % 10;
lbl -= c;
lbl /= 10;
int b = lbl % 10;
lbl -= b;
lbl /= 10;
int a = lbl % 26;
printf("%c%c%c\n", a+'A', b+'0', c+'0');
}
void label(char* s)
{
printf("%s\n",s);
}
void out1(char* p)
{
printf(" %-3s\n",p);
}
void out2(char* p, char* s)
{
printf(" %-3s ",p);
if (s) {
printf("%s", s);
}
printf("\n");
}
void out3(char* p, int lbl)
{
printf(" %-3s ",p);
generate_label(lbl);
}
int output1(struct parser* p)
{
whitespace(p);
if (lstring(p, "*1")) {
out1("GN1");
return 1;
}
else if (lstring(p, "*2")) {
out1("GN2");
return 1;
}
else if (lstring(p, "*")) {
out1("CI");
return 1;
}
else if (string(p)) {
out2("CL", p->id);
return 1;
}
return 0;
}
int output(struct parser* p)
{
if (lstring(p, ".OUT")) {
lstring(p, "(");
while (output1(p)) {}
lstring(p, ")");
out1("OUT");
}
else if (lstring(p, ".LABEL")) {
out1("LB");
output1(p);
out1("OUT");
}
else {
//out1("OUT");
return 0;
}
return 1;
}
int ex3(struct parser* p)
{
int label_1 = label_count++;
whitespace(p);
if (lstring(p, ".ID")) {
out1("ID");
return 1;
}
else if (lstring(p, ".NUMBER")) {
out1("NUM");
return 1;
}
else if (lstring(p, ".STRING")) {
out1("SR");
return 1;
}
else if (lstring(p, ".EMPTY")) {
out1("SET");
return 1;
}
else if (lstring(p, "(")) {
ex1(p);
lstring(p, ")");
return 1;
}
else if (*(p->buf->begin) == '$') {
p->buf->begin++;
generate_label(label_1);
ex3(p);
out3("BT", label_1);
out1("SET");
return 1;
}
else if (identifier(p)) {
out2("CLL", p->id);
return 1;
}
else if (string(p)) {
out2("TST", p->id);
return 1;
}
return 0;
}
int ex2(struct parser* p)
{
int label_1 = label_count++;
int stop = 0;
if (ex3(p)) {
out3("BF", label_1);
}
else if (output(p)) {
}
else {
stop = 1;
}
if (stop) return 0;
while (!stop) {
if (ex3(p)) {
out1("BE");
}
else if (output(p)) {
}
else {
stop = 1;
}
}
generate_label(label_1);
return 1;
}
int ex1(struct parser* p)
{
int alt_count=1;
int label_1 = label_count++;
printf(" ; alternation %d\n", alt_count);
ex2(p);
while (lstring(p, "/")) {
alt_count++;
printf(" ; alternation %d\n", alt_count);
out3("BT", label_1);
if (!ex2(p)) break;
}
generate_label(label_1);
return 1;
}
int st(struct parser* p)
{
if (!identifier(p)) {
return 0;
}
label(p->id);
character(p, '=');
ex1(p);
lstring(p, ".,");
out1("R");
return 1;
}
int program(struct parser* p)
{
lstring(p, ".SYNTAX");
identifier(p);
out2("ADR", p->id);
while (st(p)) {}
if (lstring(p, ".END")) {
out1("END");
return 1;
}
return 1;
}
int main(int argc,char** argv)
{
struct buffer b;
b.program = (char*)malloc(sizeof(char)*4096);
struct parser p;
if (argc == 2) {
start(&p, &b, argv[1]);
program(&p);
}
free(b.program);
return 0;
}