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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use std::fmt::{Debug, Display};
use std::io::Write;
use std::ops::Deref;
use std::sync::{Arc, RwLock};

use cpclib_common::itertools::Itertools;
use cpclib_common::smallvec::SmallVec;
use cpclib_tokens::symbols::{MemoryPhysicalAddress, PhysicalAddress};
use cpclib_tokens::ExprResult;

use crate::preamble::{LocatedToken, LocatedTokenInner, MayHaveSpan, SourceString};
/// Generate an output listing.
/// Can be useful to detect issues

#[derive(PartialEq)]
pub enum TokenKind {
    Hidden,
    Label(String),
    Set(String),
    MacroCall,
    MacroDefine(String),
    Displayable
}

impl TokenKind {
    fn is_displayable(&self) -> bool {
        self == &TokenKind::Displayable
    }
}
pub struct ListingOutput {
    /// Writer that will contains the listing/
    /// The listing is produced line by line and not token per token
    writer: Box<dyn Write + Send + Sync>,
    /// Filename of the current line
    current_fname: Option<String>,
    activated: bool,

    /// Bytes collected at the current line
    current_line_bytes: SmallVec<[u8; 4]>,
    /// Complete source
    current_source: Option<&'static str>,
    /// Line number and line content.
    current_line_group: Option<(u32, String)>, // clone view of the line XXX avoid this clone

    current_first_address: u32,
    current_address_kind: AddressKind,
    current_physical_address: PhysicalAddress,
    crunched_section_counter: usize,
    current_token_kind: TokenKind,
    deferred_for_line: Vec<String>,
    counter_update: Vec<String>
}
#[derive(PartialEq)]
pub enum AddressKind {
    Address,
    CrunchedArea,
    Mixed,
    None
}

impl Display for AddressKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                AddressKind::Address => ' ',
                AddressKind::CrunchedArea => 'C',
                AddressKind::Mixed => 'M',
                AddressKind::None => 'N'
            }
        )
    }
}

impl Debug for ListingOutput {
    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Ok(())
    }
}

impl ListingOutput {
    /// Build a new ListingOutput that will write everyting in writter
    pub fn new<W: 'static + Write + Send + Sync>(writer: W) -> Self {
        Self {
            writer: Box::new(writer),
            current_fname: None,
            activated: false,
            current_line_bytes: Default::default(),
            current_line_group: None,
            current_source: None,
            current_first_address: 0,
            current_address_kind: AddressKind::None,
            crunched_section_counter: 0,
            current_physical_address: MemoryPhysicalAddress::new(0, 0).into(),
            current_token_kind: TokenKind::Hidden,
            deferred_for_line: Default::default(),
            counter_update: Vec::new()
        }
    }

    fn bytes_per_line(&self) -> usize {
        8
    }

    /// Check if the token is for the same source
    fn token_is_on_same_source(&self, token: &LocatedToken) -> bool {
        match &self.current_source {
            Some(current_source) => {
                std::ptr::eq(token.context().source.as_ptr(), current_source.as_ptr())
            },
            None => false
        }
    }

    /// Check if the token is for the same line than the previous token
    fn token_is_on_same_line(&self, token: &LocatedToken) -> bool {
        match &self.current_line_group {
            Some((current_location, _current_line)) => {
                self.token_is_on_same_source(token)
                    && *current_location == token.span().location_line()
            },
            None => false
        }
    }

    fn extract_code(token: &LocatedToken) -> String {
        match token {
            LocatedToken {
                inner: either::Left(LocatedTokenInner::Macro { .. } | LocatedTokenInner::Repeat(..)),
                span,
                ..
            } => {
                // 		self.need_to_cut = true;
                span.as_str().to_string()
            },

            _ => {
                // 			self.need_to_cut = false;
                unsafe {
                    std::str::from_utf8_unchecked(token.span().get_line_beginning().as_bytes())
                }
                .to_owned()
            }
        }
    }

    /// Add a token for the current line
    fn add_token(
        &mut self,
        token: &LocatedToken,
        bytes: &[u8],
        address: u32,
        address_kind: AddressKind,
        physical_address: PhysicalAddress
    ) {
        if !self.activated {
            return;
        }

        // dbg!(token);

        let fname_handling = self.manage_fname(token);

        // Check if the current line has to drawn in a different way
        let specific_content = match &self.current_token_kind {
            TokenKind::Hidden => None,
            TokenKind::Label(l) => {
                Some(format!(
                    "{:04X} {:05X} {l}",
                    self.current_first_address,
                    match self.current_physical_address {
                        PhysicalAddress::Memory(adr) => adr.offset_in_cpc(),
                        PhysicalAddress::Bank(adr) => adr.address() as _,
                        PhysicalAddress::Cpr(adr) => adr.address() as _
                    }
                ))
            },
            TokenKind::Set(label) => {
                Some(format!(
                    "{:04X} {} {label}",
                    self.current_first_address, "?????"
                ))
            },
            TokenKind::MacroCall | TokenKind::Displayable => None,
            TokenKind::MacroDefine(name) => Some(format!("MACRO      {name}"))
        };

        // if so, defer its output
        if let Some(specific_content) = &specific_content {
            self.deferred_for_line.push(specific_content.clone());
        }

        {
            // !self.token_is_on_same_line(token)
            if true {
                // if specific_content.is_some() && fname_handling.is_some() {
                // writeln!(self.writer, "{}", fname_handling.take().unwrap()).unwrap();
                // }
                // handle previous line
                if !self.token_is_on_same_line(token) {
                    self.process_current_line(); // request a display
                }

                // handle the new line

                // replace the objects of interest
                self.current_source =
                    Some(unsafe { std::mem::transmute(token.context().complete_source()) });

                // TODO manage differently for macros and so on
                // let current_line = current_line.split("\n").next().unwrap_or(current_line);
                self.current_line_group =
                    Some((token.span().location_line(), Self::extract_code(token)));
                self.current_first_address = address;
                self.current_physical_address = physical_address;
                self.current_address_kind = AddressKind::None;
                self.manage_fname(token);
            }
            else {
                // update the line
                self.current_line_group =
                    Some((token.span().location_line(), Self::extract_code(token)));
            }
        }

        self.current_line_bytes.extend_from_slice(bytes);
        self.current_address_kind = if self.current_address_kind == AddressKind::None {
            address_kind
        }
        else if self.current_address_kind != address_kind {
            AddressKind::Mixed
        }
        else {
            address_kind
        };

        if let Some(line) = fname_handling {
            writeln!(self.writer, "{}", line).unwrap();
        }

        self.current_token_kind = match token.deref() {
            LocatedTokenInner::Label(l) => TokenKind::Label(l.to_string()),
            LocatedTokenInner::Equ { label, .. } | LocatedTokenInner::Assign { label, .. } => {
                TokenKind::Set(label.to_string())
            },
            LocatedTokenInner::Macro { name, .. } => TokenKind::MacroDefine(name.to_string()),
            LocatedTokenInner::MacroCall(..)
            | LocatedTokenInner::Org { .. }
            | LocatedTokenInner::Comment(..)
            | LocatedTokenInner::Include(..)
            | LocatedTokenInner::Repeat(..) => TokenKind::Displayable,
            _ => TokenKind::Hidden
        };
    }

    pub fn process_current_line(&mut self) {
        // retrieve the line
        let (line_number, line) = match &self.current_line_group {
            Some((idx, line)) => (idx, line),
            None => return
        };

        // build the iterators over the line representation of source code and data
        let mut line_representation = line.split("\n");
        let data_representation = &self
            .current_line_bytes
            .iter()
            .chunks(self.bytes_per_line())
            .into_iter()
            .map(|c| c.map(|b| format!("{:02X}", b)).join(" "))
            .collect_vec();
        let mut data_representation = data_representation.iter();

        // TODO manage missing end of files/blocks if needed

        let delta = line_representation.clone().count();
        // TODO add the line representation ?
        for specific_content in self.deferred_for_line.iter() {
            let lines = line.split("\n");
            let lines_count = lines.clone().count(); // line number corresponds to the VERY LAST line and not the FIRST one
            for (line_delta, line) in lines.into_iter().enumerate() {
                writeln!(
                    self.writer,
                    "{:37}{:4} {}",
                    if line_delta == 0 {
                        specific_content
                    }
                    else {
                        ""
                    },
                    line_number + delta as u32 + line_delta as u32 - lines_count as u32,
                    line
                )
                .unwrap();
            }
        }
        self.deferred_for_line.clear();

        // draw all lines that correspond to the instructions to output
        let mut idx = 0;
        loop {
            let current_inner_line = line_representation.next();
            let current_inner_data = data_representation.next();

            if current_inner_data.is_none() && current_inner_line.is_none() {
                break;
            }

            let loc_representation = if current_inner_line.is_none() {
                "    ".to_owned()
            }
            else {
                format!("{:04X}", self.current_first_address)
            };

            // Physical address is only printed if it differs from the code address
            let offset = match self.current_physical_address {
                PhysicalAddress::Memory(adr) => adr.offset_in_cpc(),
                PhysicalAddress::Bank(adr) => adr.address() as _,
                PhysicalAddress::Cpr(adr) => adr.address() as _
            };
            let phys_addr_representation =
                if current_inner_line.is_none() || offset == self.current_first_address {
                    "      ".to_owned()
                }
                else {
                    format!("{:05X}{}", offset, self.current_address_kind)
                };

            let line_nb_representation = if current_inner_line.is_none() {
                "    ".to_owned()
            }
            else {
                format!("{:4}", line_number + idx)
            };

            // missing instruction must be added manually using TokenKind
            if !self.current_line_bytes.is_empty() || self.current_token_kind.is_displayable() {
                writeln!(
                    self.writer,
                    "{loc_representation} {phys_addr_representation} {:bytes_width$} {line_nb_representation} {}",
                    current_inner_data.unwrap_or(&"".to_owned()),
                    current_inner_line.map(|line| line.trim_end()).unwrap_or(""),
                    bytes_width = self.bytes_per_line() * 3
                )
                .unwrap();
            }

            idx += 1;
        }

        if !self.current_line_bytes.is_empty() || self.current_token_kind.is_displayable() {
            for counter in self.counter_update.iter() {
                self.writer
                    .write(format!("{}\n", counter).as_bytes())
                    .unwrap();
            }
            self.counter_update.clear();
        }

        // cleanup all the fields of the current line
        self.current_line_group = None;
        self.current_source = None;
        self.current_line_bytes.clear();
    }

    pub fn finish(&mut self) {
        self.process_current_line();
        if !self.deferred_for_line.is_empty() {
            panic!()
        }
    }

    /// Print filename if needed
    pub fn manage_fname(&mut self, token: &LocatedToken) -> Option<String> {
        // 	dbg!(token);

        let ctx = &token.span().state;
        let fname = ctx
            .filename()
            .map(|p| p.as_os_str().to_str().unwrap_or("<NO FNAME>").to_string())
            .or_else(|| ctx.context_name().map(|s| s.to_owned()));

        match fname {
            Some(fname) => {
                let print = match self.current_fname.as_ref() {
                    Some(current_fname) => *current_fname != fname,
                    None => true
                };

                if print {
                    self.current_fname = Some(fname.clone());
                    Some(format!("Context: {}", fname))
                }
                else {
                    None
                }
            },
            None => None
        }
    }

    pub fn on(&mut self) {
        self.activated = true;
    }

    pub fn off(&mut self) {
        self.finish();
        self.activated = false;
    }

    pub fn enter_crunched_section(&mut self) {
        self.crunched_section_counter += 1;
    }

    pub fn leave_crunched_section(&mut self) {
        self.crunched_section_counter -= 1;
    }
}

/// This structure collects the necessary information to feed the output
#[derive(Clone)]
pub struct ListingOutputTrigger {
    /// the token read before collecting the bytes
    /// Because each token can have a different lifespan, we store them using a pointer
    pub(crate) token: Option<*const LocatedToken>,
    /// the bytes progressively collected
    pub(crate) bytes: Vec<u8>,
    pub(crate) start: u32,
    pub(crate) physical_address: PhysicalAddress,
    pub(crate) builder: Arc<RwLock<ListingOutput>>
}

unsafe impl Sync for ListingOutputTrigger {}

impl ListingOutputTrigger {
    pub fn write_byte(&mut self, b: u8) {
        self.bytes.push(b);
    }

    pub fn new_token(
        &mut self,
        new: *const LocatedToken,
        code: u32,
        kind: AddressKind,
        physical_address: PhysicalAddress
    ) {
        // Retreive the previous token and handle it
        if let Some(token) = &self.token {
            self.builder.write().unwrap().add_token(
                unsafe { &**token },
                &self.bytes,
                self.start,
                kind,
                self.physical_address
            );
        }

        self.token.replace(new.clone()); // TODO remove that clone that is memory/time eager

        // TODO double check if these lines are current. I doubt it is the case when having severl instructions per line
        self.bytes.clear();
        self.start = code;
        self.physical_address = physical_address;
    }

    /// Override the address value by the expression result
    /// BUGGY when it is not a number ...
    pub fn replace_code_address(&mut self, address: &ExprResult) {
        Self::result_to_address(address).map(|a| self.start = a);
    }

    /// Applies the conversion when possible
    fn result_to_address(address: &ExprResult) -> Option<u32> {
        match address {
            ExprResult::Float(_f) => None,
            ExprResult::Value(v) => Some(*v as _),
            ExprResult::Char(v) => Some(*v as _),
            ExprResult::Bool(b) => Some(if *b { 1 } else { 0 }),
            ExprResult::String(s) => Some(s.len() as _),
            ExprResult::List(l) => Some(l.len() as _),
            ExprResult::Matrix {
                width,
                height,
                content: _
            } => Some((*width * *height) as _)
        }
    }

    pub fn replace_physical_address(&mut self, address: PhysicalAddress) {
        self.physical_address = address;
    }

    pub fn finish(&mut self) {
        if let Some(token) = &self.token {
            self.builder.write().unwrap().add_token(
                unsafe { &**token },
                &self.bytes,
                self.start,
                AddressKind::Address,
                self.physical_address
            );
        }
        self.builder.write().unwrap().finish();
    }

    pub fn on(&mut self) {
        self.builder.write().unwrap().on();
    }

    pub fn off(&mut self) {
        self.builder.write().unwrap().off();
    }

    pub fn enter_crunched_section(&mut self) {
        self.builder.write().unwrap().enter_crunched_section();
    }

    pub fn leave_crunched_section(&mut self) {
        self.builder.write().unwrap().leave_crunched_section();
    }

    pub fn repeat_iteration(&mut self, counter: &str, value: Option<&ExprResult>) {
        let line = if let Some(value) = value {
            let value = Self::result_to_address(value);
            if let Some(value) = value {
                format!("{value:04X} ????? {counter}")
            }
            else {
                format!("???? ????? {counter}")
            }
        }
        else {
            format!("???? ???? {counter}")
        };

        self.builder.write().unwrap().counter_update.push(line);
    }
}