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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::ops::Deref;
use cpclib_common::itertools::{EitherOrBoth, Itertools};
use cpclib_tokens::symbols::{Macro, Source, Struct};
use cpclib_tokens::{MacroParamElement, Token};
use crate::error::AssemblerError;
use crate::preamble::Z80Span;
use crate::{Env, ParserContext};
/// To be implemented for each element that can be expended based on some patterns (i.e. macros, structs)
pub trait Expandable {
/// Returns a string version of the element after expansion
fn expand(&self, env: &Env) -> Result<String, AssemblerError>;
}
fn expand_param<P: MacroParamElement>(m: &P, env: &Env) -> Result<String, AssemblerError> {
if m.is_single() {
let s = m.single_argument();
let trimmed = s.trim();
const EVAL: &str = "{eval}";
if trimmed.starts_with(EVAL) {
let src = &s[EVAL.len()..];
let ctx = ParserContext::default(); // TODO really use the good context
let src = Z80Span::new_extra(src, &ctx);
let expr_token = crate::parser::located_expr(src)
.map_err(|e| AssemblerError::AssemblingError { msg: e.to_string() })?
.1;
let value = env.resolve_expr_must_never_fail(&expr_token)?;
return Ok(value.to_string());
}
else {
Ok(s.to_owned())
}
}
else {
let l = m.list_argument();
Ok(format!(
"{}",
l.iter()
.map(|p| expand_param(p.deref(), env))
.collect::<Result<Vec<_>, AssemblerError>>()?
.join(",")
))
}
}
/// Encodes both the arguments and the macro
pub struct MacroWithArgs<'m, 'a, P: MacroParamElement> {
r#macro: &'m Macro,
args: &'a [P]
}
impl<'m, 'a, P: MacroParamElement> MacroWithArgs<'m, 'a, P> {
/// The construction fails if the number pf arguments is incorrect
pub fn build(r#macro: &'m Macro, args: &'a [P]) -> Result<Self, AssemblerError> {
if r#macro.nb_args() != args.len() {
Err(AssemblerError::MacroError {
name: r#macro.name().into(),
root: box AssemblerError::AssemblingError {
msg: format!(
"{} arguments provided, but {} expected.",
args.len(),
r#macro.nb_args()
)
}
})
}
else {
Ok(Self { r#macro, args })
}
}
pub fn source(&self) -> Option<&Source> {
self.r#macro.source()
}
}
impl<'m, 'a, P: MacroParamElement> Expandable for MacroWithArgs<'m, 'a, P> {
/// Develop the macro with the given arguments
fn expand(&self, env: &Env) -> Result<String, AssemblerError> {
// assert_eq!(args.len(), self.nb_args());
let mut listing = self.r#macro.code().to_string();
// replace the arguments for the listing
for (argname, argvalue) in self.r#macro.params().iter().zip(self.args.iter()) {
listing = listing.replace(&format!("{{{}}}", argname), &expand_param(argvalue, env)?);
}
Ok(listing)
}
}
pub struct StructWithArgs<'s, 'a, P: MacroParamElement> {
r#struct: &'s Struct,
args: &'a [P]
}
impl<'s, 'a, P: MacroParamElement> StructWithArgs<'s, 'a, P> {
pub fn r#struct(&self) -> &Struct {
self.r#struct
}
/// The construction fails if the number pf arguments is incorrect
pub fn build(r#struct: &'s Struct, args: &'a [P]) -> Result<Self, AssemblerError> {
if r#struct.nb_args() < args.len() {
Err(AssemblerError::MacroError {
name: r#struct.name().into(),
root: box AssemblerError::AssemblingError {
msg: format!(
"{} arguments provided, but at most {} expected.",
args.len(),
r#struct.nb_args()
)
}
})
}
else {
Ok(Self { r#struct, args })
}
}
pub fn source(&self) -> Option<&Source> {
self.r#struct.source()
}
}
impl<'s, 'a, P: MacroParamElement> Expandable for StructWithArgs<'s, 'a, P> {
/// Generate the token that correspond to the current structure
/// Current bersion does not handle at all directive with several arguments
/// BUG does not work when directives have a prefix
fn expand(&self, env: &Env) -> Result<String, AssemblerError> {
// dbg!("{:?} != {:?}", self.args, self.r#struct().content());
let prefix = ""; // TODO acquire this prefix
// self.args has priority over self.content information
let mut developped: String = self
.r#struct()
.content()
.iter()
.zip_longest(self.args.iter()) // by construction longest is the struct template
.enumerate()
.map(
|(_idx, current_information)| -> Result<String, AssemblerError> {
let ((_name, token), provided_param) = {
match current_information {
EitherOrBoth::Both((name, token), provided_param) => {
((name, token), Some(provided_param))
}
EitherOrBoth::Left((name, token)) => ((name, token), None),
_ => unreachable!()
}
};
match token {
Token::Defb(c) | Token::Defw(c) => {
assert_eq!(c.len(), 1);
let tok = if matches!(token, Token::Defb(_)) {
"DB"
}
else {
"DW"
};
let elem = match provided_param {
Some(provided_param) => {
let elem = expand_param(provided_param, env)?;
if elem.is_empty() {
c[0].to_string()
}
else {
elem
}
}
None => c[0].to_string()
};
Ok(format!(" {}{} {}", prefix, tok, elem))
}
Token::MacroCall(r#macro, current_default_args) => {
let mut call = format!(" {}{} ", prefix, r#macro);
let args = match provided_param {
Some(provided_param2) => {
if provided_param2.is_single() {
provided_param
.into_iter()
.zip_longest(current_default_args)
.map(|a| {
match a {
EitherOrBoth::Both(provided, _)
| EitherOrBoth::Left(provided) => {
(
provided.is_single(),
expand_param(provided, env)
)
}
EitherOrBoth::Right(default) => {
(
default.is_single(),
expand_param(default, env)
)
}
}
})
.map(|(is_single, a)| {
a.map(|repr| {
if is_single {
repr
}
else {
format!("[{}]", repr)
}
})
})
.collect::<Result<Vec<String>, AssemblerError>>()?
}
else {
provided_param2
.list_argument()
.into_iter()
.zip_longest(current_default_args)
.map(|a| {
match a {
EitherOrBoth::Both(provided, _)
| EitherOrBoth::Left(provided) => {
(
provided.is_single(),
expand_param(provided.deref(), env)
)
}
EitherOrBoth::Right(default) => {
(
default.is_single(),
expand_param(default, env)
)
}
}
})
.map(|(is_single, a)| {
a.map(|repr| {
if is_single {
repr
}
else {
format!("[{}]", repr)
}
})
})
.collect::<Result<Vec<String>, AssemblerError>>()?
}
}
None => {
current_default_args
.iter()
.map(|a| expand_param(a, env))
.collect::<Result<Vec<String>, AssemblerError>>()?
}
};
call.push_str(&args.join(","));
Ok(call)
}
_ => unreachable!("{:?}", token)
}
}
)
.collect::<Result<Vec<String>, AssemblerError>>()?
.join("\n");
let last = developped.pop().unwrap();
developped.push(last);
if last != 'n' {
developped.push('\n');
}
Ok(developped)
}
}