annotate lwasm/macro.c @ 345:7416c3f9c321

Basic macro processor ported forward; added context break handling for local symbols
author lost@starbug
date Thu, 25 Mar 2010 23:17:54 -0600
parents old-trunk/lwasm/old/macro.c@eb230fa7d28e
children a82c55070624
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 macro.c
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2008 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWASM.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20 Contains stuff associated with macro processing
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #include <ctype.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 #include <string.h>
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
28 #include <stdio.h>
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
29
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
30 #include <lw_alloc.h>
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
31 #include <lw_string.h>
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
32
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33 #include "lwasm.h"
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
34 #include "input.h"
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 #include "instab.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
37 PARSEFUNC(pseudo_macro_parse)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 macrotab_t *m;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
40
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 if (as -> skipcond)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 as -> skipmacro = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
46
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 if (as -> inmacro)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
49 lwasm_register_error(as, l, "Attempt to define a macro inside a macro");
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
50 return;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
51 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
52
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
53 if (!(l -> sym))
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
54 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
55 lwasm_register_error(as, l, "Missing macro name");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 for (m = as -> macros; m; m = m -> next)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61 if (!strcmp(m -> name, l -> sym))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 if (m)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
66 lwasm_register_error(as, l, "Duplicate macro definition");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
70 m = lw_alloc(sizeof(macrotab_t));
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
71 m -> name = lw_strdup(l -> sym);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 m -> next = as -> macros;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 m -> lines = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74 m -> numlines = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 as -> macros = m;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
76
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 while (**p && !isspace(**p))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78 (*p)++;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
79
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
80 as -> inmacro = 1;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
83 PARSEFUNC(pseudo_endm_parse)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 if (as -> skipcond)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 as -> skipmacro = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 if (!as -> inmacro)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
93 lwasm_register_error(as, l, "ENDM without MACRO");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 as -> inmacro = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 // a macro definition counts as a context break for local symbols
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 as -> context = lwasm_next_context(as);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 // the current macro will ALWAYS be the first one in the table
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 int add_macro_line(asmstate_t *as, char *optr)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 if (!as -> inmacro)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 return 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
109 as -> macros -> lines = lw_realloc(as -> macros -> lines, sizeof(char *) * (as -> macros -> numlines + 1));
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
110 as -> macros -> lines[as -> macros -> numlines] = lw_strdup(optr);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 as -> macros -> numlines += 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 return 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115 void macro_add_to_buff(char **buff, int *loc, int *len, char c)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 if (*loc == *len)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
119 *buff = lw_realloc(*buff, *len + 32);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 *len += 32;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 (*buff)[(*loc)++] = c;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
123 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
125 // this is just like a regular operation function
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 macro args are referenced by "\n" where 1 <= n <= 9
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128 or by "\{n}"; a \ can be included by writing \\
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 a comma separates argument but one can be included with "\,"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130 whitespace ends argument list but can be included with "\ " or the like
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 */
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
133 int expand_macro(asmstate_t *as, line_t *l, char **p, char *opc)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
134 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
135 int lc;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
136 line_t *cl, *nl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137 int oldcontext;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138 macrotab_t *m;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
139
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140 char **args = NULL; // macro arguments
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141 int nargs = 0; // number of arguments
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143 char *p2, *p3;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 int bloc, blen;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
146 char *linebuff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
148 for (m = as -> macros; m; m = m -> next)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
149 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
150 if (!strcmp(opc, m -> name))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
151 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
152 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 // signal no macro expansion
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 if (!m)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
155 return -1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157 // save current symbol context for after macro expansion
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
158 oldcontext = as -> context;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
159
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
160 cl = l;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
161
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 as -> context = lwasm_next_context(as);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
164 while (**p && !isspace(**p) && **p != ',')
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
166 p2 = *p;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
167 while (*p2 && !isspace(*p2) && *p2 != ',')
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
168 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
169 if (*p2 == '\\')
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
170 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
171 if (p2[1])
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
172 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
173 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
174 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
175 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
176
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
177 // have arg here
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
178 args = lw_realloc(args, sizeof(char *) * (nargs + 1));
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
179 args[nargs] = lw_alloc(p2 - *p + 1);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
180 args[nargs][p2 - *p] = '\0';
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
181 memcpy(args[nargs], *p, p2 - *p);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
182 *p = p2;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
183
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
184 // now collapse out "\" characters
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
185 for (p3 = p2 = args[nargs]; *p2; p2++, p3++)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
186 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
187 if (*p2 == '\\' && p2[1])
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
188 {
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
189 p2++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
190 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
191 *p3 = *p2;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
192 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
193 *p3 = '\0';
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
194
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
195 nargs++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
196 if (**p == ',')
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
197 (*p)++;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
198 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
199
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
200
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
201 // now create a string for the macro
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
202 // and push it into the front of the input stack
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
203 bloc = blen = 0;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
204 linebuff = NULL;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
205
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
206 for (lc = 0; lc < m -> numlines; lc++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
207 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
208 for (p2 = m -> lines[lc]; *p2; p2++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
209 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
210 if (*p2 == '\\' && isdigit(p2[1]))
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
211 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
212 int n;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
213
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
214 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
215 n = *p2 - '0';
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
216 if (n == 0)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
217 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
218 for (p3 = m -> name; *p3; p3++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
219 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
220 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
221 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
222 if (n < 1 || n > nargs)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
223 continue;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
224 for (p3 = args[n - 1]; *p3; p3++)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
225 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
226 continue;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
227 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
228 else if (*p2 == '{')
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
229 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
230 int n = 0, n2;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
231 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
232 while (*p2 && isdigit(*p2))
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
233 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
234 n2 = *p2 - '0';
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
235 if (n2 < 0 || n2 > 9)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
236 n2 = 0;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
237 n = n * 10 + n2;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
238 p2++;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
239 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
240 if (*p2 == '}')
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
241 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
242
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
243 if (n == 0)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
244 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
245 for (p3 = m -> name; *p3; p3++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
246 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
247 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
248 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
249 if (n < 1 || n > nargs)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
250 continue;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
251 for (p3 = args[n - 1]; *p3; p3++)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
252 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
253 continue;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
254 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
255 else
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
256 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
257 macro_add_to_buff(&linebuff, &bloc, &blen, *p2);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
258 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
259 }
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
260
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
261 macro_add_to_buff(&linebuff, &bloc, &blen, '\n');
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
262
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
263 }
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
264
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
265 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
266 char ctcbuf[100];
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
267 char *p;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
268 snprintf(ctcbuf, 100, "\001\001SETCONTEXT %d\n", oldcontext);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
269 for (p = ctcbuf; *p; p++)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
270 macro_add_to_buff(&linebuff, &bloc, &blen, *p);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
271 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
272
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
273 // push the macro into the front of the stream
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
274 input_openstring(as, opc, linebuff);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
275
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
276 lw_free(linebuff);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
277
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
278 // clean up
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
279 if (args)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
280 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
281 while (nargs)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
282 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
283 lw_free(args[--nargs]);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
284 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
285 lw_free(args);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
286 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
287
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
288 // indicate a macro was expanded
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
289 return 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
290 }