-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro.cpp
199 lines (186 loc) · 4.97 KB
/
macro.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
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
#include "libstr.h"
#include "asar.h"
#include "autoarray.h"
#include "scapegoat.hpp"
template<typename t> void error(int pass, const char * e_);
bool confirmname(const char * name);
struct macrodata
{
autoarray<string> lines;
int numlines;
int startline;
const char * fname;
const char ** arguments;
int numargs;
};
lightweight_map<string, macrodata*> macros;
string thisname;
macrodata * thisone;
int numlines;
extern const char * thisfilename;
extern int thisline;
extern const char * thisblock;
void assembleline(const char * fname, int linenum, const char * line);
int reallycalledmacros;
int calledmacros;
int macrorecursion;
extern int repeatnext;
extern int numif;
extern int numtrue;
extern const char * callerfilename;
extern int callerline;
void startmacro(const char * line_)
{
thisone=NULL;
if (!confirmqpar(line_)) error<errblock>(0, "Broken macro declaration");
string line=line_;
clean(line);
char * startpar=strqchr(line.str, '(');
if (!startpar) error<errblock>(0, "Broken macro declaration");
*startpar=0;
startpar++;
if (!confirmname(line)) error<errblock>(0, "Bad macro name");
thisname=line;
char * endpar=strqrchr(startpar, ')');
//confirmqpar requires that all parentheses are matched, and a starting one exists, therefore it is harmless to not check for nulls
if (endpar[1]) error<errblock>(0, "Broken macro declaration");
*endpar=0;
for (int i=0;startpar[i];i++)
{
char c=startpar[i];
if (!isalnum(c) && c!='_' && c!=',') error<errline>(0, "Broken macro declaration");
if (c==',' && isdigit(startpar[i+1])) error<errline>(0, "Broken macro declaration");
}
if (*startpar==',' || isdigit(*startpar) || strstr(startpar, ",,") || endpar[-1]==',') error<errline>(0, "Broken macro declaration");
macrodata * ignored;
if (macros.find(thisname, ignored)) error<errblock>(0, "Duplicate macro");
thisone=(macrodata*)malloc(sizeof(macrodata));
new(thisone) macrodata;
if (*startpar)
{
thisone->arguments=(const char**)qpsplit(strdup(startpar), ",", &thisone->numargs);
}
else
{
const char ** noargs=(const char**)malloc(sizeof(const char**));
*noargs=NULL;
thisone->arguments=noargs;
thisone->numargs=0;
}
for (int i=0;thisone->arguments[i];i++)
{
if (!confirmname(thisone->arguments[i])) error<errblock>(0, "Bad macro argument name");
for (int j=i+1;thisone->arguments[j];j++)
{
if (!strcmp(thisone->arguments[i], thisone->arguments[j])) error<errblock>(0, S"Duplicate macro argument '"+thisone->arguments[i]+"'");
}
}
thisone->fname=strdup(thisfilename);
thisone->startline=thisline;
numlines=0;
}
void tomacro(const char * line)
{
if (!thisone) return;
thisone->lines[numlines++]=line;
}
void endmacro(bool insert)
{
if (!thisone) return;
thisone->numlines=numlines;
if (insert) macros.insert(thisname, thisone);
else delete thisone;
}
#define merror(str) do { if (!macrorecursion) { callerfilename=NULL; callerline=-1; } error<errblock>(0, str); } while(0)
void callmacro(const char * data)
{
int numcm=reallycalledmacros++;
macrodata * thisone;
if (!confirmqpar(data)) merror("Broken macro usage");
string line=data;
clean(line);
char * startpar=strqchr(line.str, '(');
if (!startpar) merror("Broken macro usage");
*startpar=0;
startpar++;
if (!confirmname(line)) merror("Bad macro name");
if (!macros.find(line, thisone)) merror("Unknown macro");
char * endpar=strqrchr(startpar, ')');
if (endpar[1]) merror("Broken macro usage");
*endpar=0;
autoptr<const char **> args;
int numargs=0;
char *tmp_asd=strdup(startpar);
if (*startpar) args=(const char**)qpsplit(tmp_asd, ",", &numargs);
//free(tmp_asd);
if (numargs!=thisone->numargs) merror("Wrong number of arguments to macro");
macrorecursion++;
int startif=numif;
for (int i=0;i<thisone->numlines;i++)
{
try
{
thisfilename=thisone->fname;
thisline=thisone->startline+i+1;
thisblock=NULL;
string out;
string intmp=thisone->lines[i];
for (char * in=intmp.str;*in;)
{
if (*in=='<' && in[1]=='<')
{
out+="<<";
in+=2;
}
else if (*in=='<' && isalnum(in[1]))
{
char * end=in+1;
while (*end && *end!='<' && *end!='>') end++;
if (*end!='>')
{
out+=*(in++);
continue;
}
*end=0;
in++;
if (!confirmname(in)) error<errline>(0, "Broken macro contents");
bool found=false;
for (int i=0;thisone->arguments[i];i++)
{
if (!strcmp(in, thisone->arguments[i]))
{
found=true;
if (args[i][0]=='"')
{
string s=args[i];
out+=dequote(s.str);
}
else out+=args[i];
break;
}
}
if (!found) error<errline>(0, "Unknown macro argument");
in=end+1;
}
else out+=*(in++);
}
calledmacros=numcm;
assembleline(thisone->fname, thisone->startline+i, out);
}
catch(errline&){}
}
macrorecursion--;
if (repeatnext!=1)
{
thisblock=NULL;
repeatnext=1;
merror("rep or if at the end of a macro");
}
if (numif!=startif)
{
thisblock=NULL;
numif=startif;
numtrue=startif;
merror("Unclosed if statement");
}
}