annotate lwasm/macro.c @ 444:cf0324651d8f default

Added tag 3.0.1 for changeset 8f9d72cfb897
author lost@l-w.ca
date Sat, 30 Oct 2010 15:21:27 -0600
parents a82c55070624
children
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
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
37 PARSEFUNC(pseudo_parse_macro)
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
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
41 l -> len = 0;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
42
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 if (as -> skipcond)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 as -> skipmacro = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
48
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 if (as -> inmacro)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
51 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
52 return;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
53 }
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 if (!(l -> sym))
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
56 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
57 lwasm_register_error(as, l, "Missing macro name");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61 for (m = as -> macros; m; m = m -> next)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 if (!strcmp(m -> name, l -> sym))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66 if (m)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
68 lwasm_register_error(as, l, "Duplicate macro definition");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
72 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
73 m -> name = lw_strdup(l -> sym);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74 m -> next = as -> macros;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 m -> lines = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 m -> numlines = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 as -> macros = m;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
78
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 while (**p && !isspace(**p))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 (*p)++;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
81
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
82 as -> inmacro = 1;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
85 PARSEFUNC(pseudo_parse_endm)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 {
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
87 l -> len = 0;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 345
diff changeset
88
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 if (as -> skipcond)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 as -> skipmacro = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 if (!as -> inmacro)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
97 lwasm_register_error(as, l, "ENDM without MACRO");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 as -> inmacro = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 // a macro definition counts as a context break for local symbols
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 as -> context = lwasm_next_context(as);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 // the current macro will ALWAYS be the first one in the table
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 int add_macro_line(asmstate_t *as, char *optr)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110 if (!as -> inmacro)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 return 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
113 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
114 as -> macros -> lines[as -> macros -> numlines] = lw_strdup(optr);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115 as -> macros -> numlines += 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 return 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 void macro_add_to_buff(char **buff, int *loc, int *len, char c)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 if (*loc == *len)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
123 *buff = lw_realloc(*buff, *len + 32);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124 *len += 32;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
125 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 (*buff)[(*loc)++] = c;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 // this is just like a regular operation function
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131 macro args are referenced by "\n" where 1 <= n <= 9
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 or by "\{n}"; a \ can be included by writing \\
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
133 a comma separates argument but one can be included with "\,"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
134 whitespace ends argument list but can be included with "\ " or the like
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
135
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
136 */
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
137 int expand_macro(asmstate_t *as, line_t *l, char **p, char *opc)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
139 int lc;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
140 line_t *cl, *nl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141 int oldcontext;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142 macrotab_t *m;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144 char **args = NULL; // macro arguments
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 int nargs = 0; // number of arguments
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
146
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147 char *p2, *p3;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
148
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
149 int bloc, blen;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
150 char *linebuff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
151
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
152 for (m = as -> macros; m; m = m -> next)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 if (!strcmp(opc, m -> name))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
155 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157 // signal no macro expansion
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
158 if (!m)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
159 return -1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
160
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
161 // save current symbol context for after macro expansion
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 oldcontext = as -> context;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
164 cl = l;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
166 as -> context = lwasm_next_context(as);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
167
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
168 while (**p && !isspace(**p) && **p != ',')
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
169 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
170 p2 = *p;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
171 while (*p2 && !isspace(*p2) && *p2 != ',')
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
172 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
173 if (*p2 == '\\')
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
174 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
175 if (p2[1])
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
176 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
177 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
178 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
179 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
180
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
181 // have arg here
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
182 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
183 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
184 args[nargs][p2 - *p] = '\0';
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
185 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
186 *p = p2;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
187
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
188 // now collapse out "\" characters
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
189 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
190 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
191 if (*p2 == '\\' && p2[1])
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
192 {
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
193 p2++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
194 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
195 *p3 = *p2;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
196 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
197 *p3 = '\0';
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
198
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
199 nargs++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
200 if (**p == ',')
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
201 (*p)++;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
202 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
203
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
204
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
205 // 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
206 // 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
207 bloc = blen = 0;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
208 linebuff = NULL;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
209
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
210 for (lc = 0; lc < m -> numlines; lc++)
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 for (p2 = m -> lines[lc]; *p2; p2++)
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 if (*p2 == '\\' && isdigit(p2[1]))
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
215 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
216 int n;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
217
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
218 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
219 n = *p2 - '0';
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
220 if (n == 0)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
221 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
222 for (p3 = m -> name; *p3; p3++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
223 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
224 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
225 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
226 if (n < 1 || n > nargs)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
227 continue;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
228 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
229 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
230 continue;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
231 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
232 else if (*p2 == '{')
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
233 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
234 int n = 0, n2;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
235 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
236 while (*p2 && isdigit(*p2))
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
237 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
238 n2 = *p2 - '0';
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
239 if (n2 < 0 || n2 > 9)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
240 n2 = 0;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
241 n = n * 10 + n2;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
242 p2++;
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
243 }
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
244 if (*p2 == '}')
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
245 p2++;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
246
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
247 if (n == 0)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
248 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
249 for (p3 = m -> name; *p3; p3++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
250 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
251 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
252 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
253 if (n < 1 || n > nargs)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
254 continue;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
255 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
256 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
257 continue;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
258 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
259 else
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
260 {
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, *p2);
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 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
266
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
267 }
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
268
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
269 {
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
270 char ctcbuf[100];
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
271 char *p;
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
272 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
273 for (p = ctcbuf; *p; p++)
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
274 macro_add_to_buff(&linebuff, &bloc, &blen, *p);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
275 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
276
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
277 // 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
278 input_openstring(as, opc, linebuff);
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
279
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
280 lw_free(linebuff);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
281
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
282 // clean up
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
283 if (args)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
284 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
285 while (nargs)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
286 {
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
287 lw_free(args[--nargs]);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
288 }
345
7416c3f9c321 Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents: 339
diff changeset
289 lw_free(args);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
290 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
291
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
292 // indicate a macro was expanded
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
293 return 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
294 }