Skip to main content

ironwork_rt/lir/
print.rs

1//! The LIR as text (lir.md ยง13): a program's generated code as `ironwork dump` prints it, for any
2//! tool that shows it. The same program prints the same bytes; nothing reads the text back.
3
4use super::{
5    AbendId, AbendText, Access, Advance, Argument, ArithId, ArithPlan, ArithStep, Base, Binding, Block, BlockId, Bound, CallArg, CallId, CallTarget, Ccsid,
6    Chars, CicsId, Class, Comparand, Compare, Cond, CondId, Const, ConstId, Convert, ConvertTable, Count, Debug, DebugId, DisplayId, DisplayItem,
7    Expr, ExprId, FileDesc, FileOpId, FileVerb, Flag, FloatFrom, FromMove, FunctionId, GlobalAt, Image, Indicator, InitValue, InspectId,
8    InspectPhrase, Inspected, IntExpr, InvokeId, Item, JsonLeaf, JsonNode, JsonValue, Marker, MarkupId, MethodName, Mode, MovePlan, Named,
9    NationalFrom, NumberInto, NumericFrom, Odo, Op, Operand, Organization, ParaId, Paragraph, ParseLeaf, ParseNode, ParseValue, Phrase, Place,
10    PlaceId, Plans, Program, Range, RangeId, RangeKind, Receiver, Replacement, ReportOp, ReturnId, SearchAllId, Section, SenderCheck, Services,
11    SetTo, SignTest, SortIo, SortKeys, SortPlan, Spacing, SqlEntry, SqlStatement, SqlTest, SqlcaField, StartKey, StartRel, StepPlan,
12    StorePlan, StringId, SymId, Terminator, TrimSide, UnstringId, UpDown, UserArgument, UserFunctionId, XmlForm, XmlNode, XmlRegister,
13    XmlValue,
14};
15use super::{Markup, ReleaseId, SortId, SqlId};
16use crate::abend::Ending;
17use crate::cics::{Cics, CicsCommand, Datum, Handles};
18use crate::files::Format;
19use crate::report::{Adding, Field, FieldContent, GroupKind, LineNumber, NextGroup, Origin};
20use crate::sql::HostType;
21use crate::storage::Kind;
22use crate::store::LaxRedefinition;
23use crate::vocab::{AcceptFrom, BinOp, Closing, Figurative, InspectMode, OpenMode, Pos, RelOp, SignClause, SignPosition};
24use numeric::Arith;
25use numeric::precision::Fixed;
26use std::cell::Cell;
27use std::collections::BTreeMap;
28use std::convert::Infallible;
29use std::fmt;
30use zarch::ebcdic::CodePage;
31use zarch::hfp::Precision;
32use zarch::wide::U256;
33
34/// What a program's LIR section holds, borrowed: what the printer prints.
35#[derive(Clone, Copy)]
36pub struct Code<'a> {
37    pub id: SymId,
38    pub initial: bool,
39    pub recursive: bool,
40    pub paragraphs: &'a [Paragraph],
41    pub procedure_start: ParaId,
42    pub ranges: &'a [Range],
43    pub blocks: &'a [Block],
44    pub places: &'a [Place],
45    pub exprs: &'a [Expr],
46    pub conds: &'a [Cond],
47    pub consts: &'a [Const],
48    pub plans: &'a Plans,
49    pub services: &'a Services,
50    pub abends: &'a [AbendText],
51    pub symbols: &'a [String],
52}
53
54/// A program's code as text. The other sections name what the code refers to: `items` qualifies a
55/// data name several items share, `debug` gives source positions, `sql` the EXEC SQL statements and
56/// `ccsid` the code page literals are shown in. Each may be left empty, and is then not shown.
57#[derive(Clone, Copy)]
58pub struct Listing<'a> {
59    pub code: Code<'a>,
60    pub items: &'a [Item],
61    pub debug: Option<&'a Debug>,
62    pub sql: &'a [SqlEntry],
63    pub ccsid: Option<u16>,
64}
65
66impl Program {
67    pub fn code(&self) -> Code<'_> {
68        Code {
69            id: self.id,
70            initial: self.initial,
71            recursive: self.recursive,
72            paragraphs: &self.paragraphs,
73            procedure_start: self.procedure_start,
74            ranges: &self.ranges,
75            blocks: &self.blocks,
76            places: &self.places,
77            exprs: &self.exprs,
78            conds: &self.conds,
79            consts: &self.consts,
80            plans: &self.plans,
81            services: &self.services,
82            abends: &self.abends,
83            symbols: &self.symbols,
84        }
85    }
86}
87
88impl<'a> Listing<'a> {
89    pub fn of(program: &'a Program) -> Self {
90        Listing {
91            code: program.code(),
92            items: &program.items,
93            debug: Some(&program.debug),
94            sql: &program.sql,
95            ccsid: Some(program.options.options.codepage),
96        }
97    }
98}
99
100impl fmt::Display for Listing<'_> {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        Printer::new(self).program(f)
103    }
104}
105
106impl fmt::Display for Program {
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        Listing::of(self).fmt(f)
109    }
110}
111
112/// How deep one reference may nest, and how many references it may print, before the rest is
113/// "...": a module is untrusted, and its tables may refer to each other in a cycle.
114const DEPTH: u32 = 256;
115const BUDGET: u32 = 100_000;
116
117struct Printer<'a> {
118    c: Code<'a>,
119    items: &'a [Item],
120    debug: Option<&'a Debug>,
121    sql: &'a [SqlEntry],
122    page: Option<&'static CodePage>,
123    /// Each data name more than one item has, with those items.
124    shared: BTreeMap<&'a str, Vec<usize>>,
125    /// Each paragraph name more than one paragraph has.
126    repeated: BTreeMap<&'a str, usize>,
127    depth: Cell<u32>,
128    spent: Cell<u32>,
129}
130
131fn join(parts: impl IntoIterator<Item = String>, separator: &str) -> String {
132    parts.into_iter().collect::<Vec<_>>().join(separator)
133}
134
135/// ` [a, b]` or ` {a, b}`, nothing when there is nothing to say.
136fn attrs(open: char, parts: Vec<String>) -> String {
137    if parts.is_empty() {
138        return String::new();
139    }
140    let close = if open == '[' { ']' } else { '}' };
141    format!(" {open}{}{close}", parts.join(", "))
142}
143
144fn yes(on: bool, text: &str) -> Option<String> {
145    on.then(|| text.to_owned())
146}
147
148fn label(id: BlockId) -> String {
149    format!("b{id}")
150}
151
152fn abend_ref(id: AbendId) -> String {
153    format!("a{id}")
154}
155
156impl<'a> Printer<'a> {
157    fn new(l: &Listing<'a>) -> Self {
158        let symbol = |id: SymId| l.code.symbols.get(id as usize).map(String::as_str);
159        let mut shared: BTreeMap<&'a str, Vec<usize>> = BTreeMap::new();
160        for (k, item) in l.items.iter().enumerate() {
161            if let Some(name) = item.name.and_then(symbol) {
162                shared.entry(name).or_default().push(k);
163            }
164        }
165        shared.retain(|_, items| items.len() > 1);
166        let mut repeated: BTreeMap<&'a str, usize> = BTreeMap::new();
167        for p in l.code.paragraphs {
168            if let Some(name) = symbol(p.name) {
169                *repeated.entry(name).or_default() += 1;
170            }
171        }
172        repeated.retain(|_, n| *n > 1);
173        Printer {
174            c: l.code,
175            items: l.items,
176            debug: l.debug,
177            sql: l.sql,
178            page: l.ccsid.and_then(CodePage::by_ccsid),
179            shared,
180            repeated,
181            depth: Cell::new(0),
182            spent: Cell::new(0),
183        }
184    }
185
186    fn nested(&self, render: impl FnOnce() -> String) -> String {
187        let depth = self.depth.get();
188        if depth == 0 {
189            self.spent.set(0);
190        }
191        let spent = self.spent.get();
192        if depth >= DEPTH || spent >= BUDGET {
193            return "...".to_owned();
194        }
195        self.depth.set(depth + 1);
196        self.spent.set(spent + 1);
197        let text = render();
198        self.depth.set(depth);
199        text
200    }
201
202    fn symbol(&self, id: SymId) -> Option<&'a str> {
203        self.c.symbols.get(id as usize).map(String::as_str)
204    }
205
206    /// A data, paragraph, file or program name as a word.
207    fn name(&self, id: SymId) -> String {
208        self.symbol(id).map_or_else(|| format!("symbol{id}?"), word)
209    }
210
211    /// Text that is not a name: a message, a statement, a signature.
212    fn string(&self, id: SymId) -> String {
213        self.symbol(id).map_or_else(|| format!("symbol{id}?"), |s| format!("{s:?}"))
214    }
215
216    /// Text that is quoted already, as a JSON string is.
217    fn raw(&self, id: SymId) -> String {
218        self.symbol(id).map_or_else(|| format!("symbol{id}?"), |s| if s.chars().any(char::is_control) { format!("{s:?}") } else { s.to_owned() })
219    }
220
221    /// A literal's text, as COBOL writes it.
222    fn literal(&self, id: SymId) -> String {
223        self.symbol(id).map_or_else(|| format!("symbol{id}?"), quote)
224    }
225
226    fn bytes(&self, bytes: &[u8]) -> String {
227        let decoded = self.page.and_then(|page| {
228            bytes
229                .iter()
230                .map(|&b| {
231                    let ch = page.decode_byte(b);
232                    (!ch.is_control() && page.encode_char(ch) == Some(b)).then_some(ch)
233                })
234                .collect::<Option<String>>()
235        });
236        decoded.map_or_else(|| hex("X", bytes), |text| quote(&text))
237    }
238
239    fn position(&self, pos: &Pos) -> String {
240        if pos.file == 0 {
241            return format!("{}:{}", pos.line, pos.col);
242        }
243        let file = self.debug.and_then(|d| d.sources.get(usize::from(pos.file))).map_or_else(|| format!("file{}", pos.file), |&s| self.name(s));
244        format!("{file}:{}:{}", pos.line, pos.col)
245    }
246
247    fn pos(&self, at: DebugId) -> Option<String> {
248        self.debug.and_then(|d| d.positions.get(at as usize)).map(|p| self.position(p))
249    }
250
251    fn at(&self, at: DebugId) -> String {
252        self.pos(at).map_or_else(String::new, |p| format!("  @{p}"))
253    }
254
255    fn para(&self, id: ParaId) -> String {
256        let Some(p) = self.c.paragraphs.get(id as usize) else { return format!("paragraph{id}?") };
257        let name = self.name(p.name);
258        if !self.symbol(p.name).is_some_and(|n| self.repeated.contains_key(n)) {
259            return name;
260        }
261        let section = (0..id).rev().map(|s| (s, &self.c.paragraphs[s as usize])).find(|(_, s)| s.is_section && s.section_end >= id);
262        match section {
263            Some((_, s)) if !self.symbol(s.name).is_some_and(|n| self.repeated.contains_key(n)) => format!("{name} OF {}", self.name(s.name)),
264            _ => format!("{name} (paragraph {id})"),
265        }
266    }
267
268    /// The paragraph control falls into, or "end" past the last.
269    fn next_para(&self, id: ParaId) -> String {
270        if id as usize == self.c.paragraphs.len() { "end".to_owned() } else { self.para(id) }
271    }
272
273    fn range(&self, id: RangeId) -> String {
274        match self.c.ranges.get(id as usize) {
275            None => format!("r{id}?"),
276            Some(r) if r.first == r.last => format!("r{id} {}", self.para(r.first)),
277            Some(r) => format!("r{id} {} thru {}", self.para(r.first), self.para(r.last)),
278        }
279    }
280
281    fn file(&self, file: u16) -> String {
282        self.c.services.files.get(usize::from(file)).map_or_else(|| format!("file{file}?"), |d| self.name(d.name))
283    }
284
285    fn file_at(&self, file: usize) -> String {
286        u16::try_from(file).map_or_else(|_| format!("file{file}?"), |f| self.file(f))
287    }
288
289    /// LINKAGE record `n` by its 01 or 77's name.
290    fn linkage(&self, n: u16) -> String {
291        let record = self.items.iter().find(|i| i.linkage == Some(n) && i.parent.is_none()).and_then(|i| i.name);
292        record.map_or_else(|| format!("linkage{n}"), |s| self.name(s))
293    }
294
295    /// A place as a reference is written: its name, qualified when other items share it, then
296    /// its subscripts and reference modification.
297    fn place(&self, id: PlaceId) -> String {
298        self.nested(|| match self.c.places.get(id as usize) {
299            None => format!("p{id}?"),
300            Some(p) => self.reference(p, &[]),
301        })
302    }
303
304    fn places(&self, ids: &[PlaceId]) -> String {
305        join(ids.iter().map(|&p| self.place(p)), " ")
306    }
307
308    /// `all` holds the subscripts written ALL, by position.
309    fn reference(&self, p: &Place, all: &[u32]) -> String {
310        let mut text = self.data_name(p);
311        if !p.subscripts.is_empty() {
312            let subscripts = p.subscripts.iter().enumerate().map(|(k, s)| if all.contains(&(k as u32)) { "ALL".to_owned() } else { self.int(&s.value) });
313            text += &format!("({})", join(subscripts, ", "));
314        }
315        if let Some(r) = &p.refmod {
316            text += &format!("({}:{})", self.int(&r.start), r.length.as_ref().map_or_else(String::new, |l| self.int(l)));
317        }
318        text
319    }
320
321    fn data_name(&self, p: &Place) -> String {
322        let name = self.name(p.name);
323        let Some(same) = self.symbol(p.name).and_then(|n| self.shared.get(n)) else { return name };
324        let held = |i: &Item| match p.base {
325            Base::Program => !i.local && i.linkage.is_none(),
326            Base::Local => i.local,
327            Base::Linkage(n) => i.linkage == Some(n),
328            Base::ReturnCode | Base::Eib | Base::SelfRef | Base::JniEnv | Base::Xml(_) => false,
329        };
330        let Some(&own) = same.iter().find(|&&k| self.items[k].offset == p.offset && held(&self.items[k])) else { return name };
331        let mut others: Vec<usize> = same.iter().copied().filter(|&k| k != own).collect();
332        let mut qualifiers: Vec<&str> = Vec::new();
333        for ancestor in self.ancestors(own) {
334            if others.is_empty() {
335                break;
336            }
337            let Some(q) = self.items[ancestor].name.and_then(|s| self.symbol(s)) else { continue };
338            qualifiers.push(q);
339            others.retain(|&k| self.qualifies(k, &qualifiers));
340        }
341        qualifiers.iter().fold(name, |text, q| format!("{text} OF {}", word(q)))
342    }
343
344    /// An item's ancestors, innermost first; a chain that loops stops.
345    fn ancestors(&self, k: usize) -> Vec<usize> {
346        let mut out = Vec::new();
347        let mut next = self.items.get(k).and_then(|i| i.parent);
348        while let Some(p) = next.map(|p| p as usize).filter(|&p| p < self.items.len() && out.len() < self.items.len()) {
349            out.push(p);
350            next = self.items[p].parent;
351        }
352        out
353    }
354
355    /// Item `k` has ancestors named by `qualifiers`, in order.
356    fn qualifies(&self, k: usize, qualifiers: &[&str]) -> bool {
357        let mut wanted = qualifiers.iter().peekable();
358        for a in self.ancestors(k) {
359            if wanted.peek().is_some_and(|&&q| self.items[a].name.and_then(|s| self.symbol(s)) == Some(q)) {
360                wanted.next();
361            }
362        }
363        wanted.peek().is_none()
364    }
365
366    fn konst(&self, id: ConstId) -> String {
367        match self.c.consts.get(id as usize) {
368            None => format!("c{id}?"),
369            Some(c) => self.constant(c),
370        }
371    }
372
373    fn constant(&self, c: &Const) -> String {
374        match c {
375            Const::Bytes(b) => self.bytes(b),
376            Const::National(units) => national(units),
377            Const::Number(n) => decimal(n),
378            Const::Figurative(f) => figurative(*f).to_owned(),
379            Const::All(b) => format!("ALL {}", self.bytes(b)),
380            Const::AllNational(units) => format!("ALL {}", national(units)),
381            Const::Dbcs(b) => hex("GX", b),
382            Const::Refused(a) => format!("refused {}", abend_ref(*a)),
383        }
384    }
385
386    fn operand(&self, o: &Operand) -> String {
387        match *o {
388            Operand::Load(p) => self.place(p),
389            Operand::Const(c) => self.konst(c),
390            Operand::LengthOf(p) => format!("LENGTH OF {}", self.place(p)),
391            Operand::AddressOf(p) => format!("ADDRESS OF {}", self.place(p)),
392            Operand::Function(f) => self.function(f),
393            Operand::UserFunction(u) => self.user_function(u),
394        }
395    }
396
397    fn function(&self, id: FunctionId) -> String {
398        self.nested(|| {
399            let Some(f) = self.c.plans.function.get(id as usize) else { return format!("function{id}?") };
400            let args = f.args.iter().map(|a| match a {
401                Argument::Value(v) => self.comparand(v),
402                Argument::All { element, all } => match self.c.places.get(*element as usize) {
403                    None => format!("p{element}?"),
404                    Some(p) => self.reference(p, &all.iter().map(|(k, _)| *k).collect::<Vec<_>>()),
405                },
406            });
407            let side = match f.side {
408                None => "",
409                Some(TrimSide::Leading) => " LEADING",
410                Some(TrimSide::Trailing) => " TRAILING",
411            };
412            let mut text = format!("FUNCTION {}({}{side})", f.func.name(), join(args, ", "));
413            if let Some(r) = &f.refmod {
414                text += &format!("({}:{})", self.int(&r.start), r.length.as_ref().map_or_else(String::new, |l| self.int(l)));
415            }
416            let how = [f.integer.as_ref().map(|i| format!("integer {}", self.int(i))), f.arity.map(|a| format!("arity {}", abend_ref(a)))];
417            text + &attrs('{', how.into_iter().flatten().collect())
418        })
419    }
420
421    fn user_function(&self, id: UserFunctionId) -> String {
422        self.nested(|| {
423            let Some(u) = self.c.services.user_functions.get(id as usize) else { return format!("user-function{id}?") };
424            let args = u.args.iter().map(|a| match a {
425                UserArgument::Reference(p) => self.place(*p),
426                UserArgument::Value(v) => format!("value {}", self.comparand(v)),
427            });
428            let mut text = format!("FUNCTION {}({})", self.name(u.name), join(args, ", "));
429            if let Some(r) = &u.refmod {
430                text += &format!("({}:{})", self.int(&r.start), r.length.as_ref().map_or_else(String::new, |l| self.int(l)));
431            }
432            let external = (u.external != u.name).then(|| format!("external {}", self.name(u.external)));
433            text + &attrs('{', external.into_iter().collect())
434        })
435    }
436
437    fn expr(&self, id: ExprId) -> String {
438        self.nested(|| match self.c.exprs.get(id as usize) {
439            None => format!("e{id}?"),
440            Some(Expr::Operand(o)) => self.operand(o),
441            Some(Expr::Neg(e)) => format!("-{}", self.term(*e)),
442            Some(Expr::Bin(a, op, b)) => format!("{} {} {}", self.term(*a), binop(*op), self.term(*b)),
443            Some(Expr::Pow(a, n)) => {
444                let exponent = self.int(n);
445                let compound = matches!(n, IntExpr::Fixed { expr, .. } if matches!(self.c.exprs.get(*expr as usize), Some(Expr::Bin(..) | Expr::Pow(..))));
446                if compound { format!("{} ** ({exponent})", self.term(*a)) } else { format!("{} ** {exponent}", self.term(*a)) }
447            }
448        })
449    }
450
451    /// An operand of an operator, in parentheses when it is an operation itself.
452    fn term(&self, id: ExprId) -> String {
453        match self.c.exprs.get(id as usize) {
454            Some(Expr::Bin(..) | Expr::Pow(..)) => format!("({})", self.expr(id)),
455            _ => self.expr(id),
456        }
457    }
458
459    fn int(&self, e: &IntExpr) -> String {
460        match e {
461            IntExpr::Const(n) => n.to_string(),
462            IntExpr::Item(p) => self.place(*p),
463            IntExpr::Fixed { expr, dmax, prepass } => {
464                let how = self.how(*dmax, &Mode::Fixed, prepass);
465                if how.is_empty() { self.expr(*expr) } else { self.term(*expr) + &attrs('{', how) }
466            }
467            IntExpr::Walk(k) => format!("walk{k}"),
468        }
469    }
470
471    /// How an expression is evaluated where that is not plain: dmax, floating point, and the
472    /// places located before it.
473    fn how(&self, dmax: u32, mode: &Mode, prepass: &[PlaceId]) -> Vec<String> {
474        let mut how = Vec::new();
475        if dmax != 0 {
476            how.push(format!("dmax {dmax}"));
477        }
478        match mode {
479            Mode::Fixed => {}
480            Mode::Float(p) => how.push(format!("float {}", precision(*p))),
481        }
482        if !prepass.is_empty() {
483            how.push(format!("prepass {}", self.places(prepass)));
484        }
485        how
486    }
487
488    fn comparand(&self, c: &Comparand) -> String {
489        match c {
490            Comparand::Operand(o) => self.operand(o),
491            Comparand::Expr { expr, dmax, mode, prepass } => self.term(*expr) + &attrs('{', self.how(*dmax, mode, prepass)),
492        }
493    }
494
495    fn cond(&self, id: CondId) -> String {
496        self.nested(|| match self.c.conds.get(id as usize) {
497            None => format!("k{id}?"),
498            Some(Cond::Rel { a, op, b, how }) => format!("{} {} {} [{}]", self.comparand(a), relop(*op), self.comparand(b), compare(*how)),
499            Some(Cond::Class { place, test }) => format!("{} is {}", self.place(*place), byte_class(*test)),
500            Some(Cond::Sign { value, test }) => format!("{} is {}", self.comparand(value), sign_test(*test)),
501            Some(Cond::Name { subject, values, how }) => {
502                let values = values.iter().map(|(low, high)| match high {
503                    None => self.konst(*low),
504                    Some(h) => format!("{} thru {}", self.konst(*low), self.konst(*h)),
505                });
506                format!("{} in ({}) [{}]", self.place(*subject), join(values, ", "), compare(*how))
507            }
508            Some(Cond::Not(c)) => format!("not ({})", self.cond(*c)),
509            Some(Cond::And(a, b)) => format!("{} and {}", self.logical(*a), self.logical(*b)),
510            Some(Cond::Or(a, b)) => format!("{} or {}", self.logical(*a), self.logical(*b)),
511            Some(Cond::Counter(t)) => format!("t{t} > 0"),
512            Some(Cond::InTable { index, count }) => format!("{} within {}", self.place(*index), self.count(count)),
513            Some(Cond::Sql(test)) => format!("sql {}", sql_test(*test)),
514        })
515    }
516
517    /// A side of AND or OR, in parentheses when it is AND or OR itself.
518    fn logical(&self, id: CondId) -> String {
519        match self.c.conds.get(id as usize) {
520            Some(Cond::And(..) | Cond::Or(..)) => format!("({})", self.cond(id)),
521            _ => self.cond(id),
522        }
523    }
524
525    fn count(&self, c: &Count) -> String {
526        match c {
527            Count::Fixed(n) => n.to_string(),
528            Count::Odo(o) => self.odo(o),
529            Count::Temp(t) => format!("t{t}"),
530        }
531    }
532
533    fn odo(&self, o: &Odo) -> String {
534        format!("({} max {} element {}{})", self.int(&o.object), o.max, o.element, if o.check { " check" } else { "" })
535    }
536
537    fn chars(&self, c: &Chars) -> String {
538        match c {
539            Chars::Literal(b) => self.bytes(b),
540            Chars::Place(p) => self.place(*p),
541            Chars::Value(o) => self.operand(o),
542        }
543    }
544
545    fn store(&self, s: &StorePlan) -> String {
546        match *s {
547            StorePlan::Zoned { digits, scale, signed, sign } => format!("zoned {}{}", pic(digits, scale, signed), sign_clause(sign)),
548            StorePlan::Packed { digits, scale, signed } => format!("packed {}", pic(digits, scale, signed)),
549            StorePlan::Binary { digits, scale, signed, native, name: _ } => format!("binary {}{}", pic(digits, scale, signed), if native { " native" } else { "" }),
550            StorePlan::NumericEdited { edit, digits, scale, blank_when_zero } => {
551                format!("numeric-edited edit {edit} {}{}", pic(digits, scale, false), if blank_when_zero { " blank-when-zero" } else { "" })
552            }
553            StorePlan::Float(p) => format!("float {}", precision(p)),
554            StorePlan::Index => "index".to_owned(),
555            StorePlan::Refused(a) => format!("refused {}", abend_ref(a)),
556        }
557    }
558
559    fn step(&self, s: &StepPlan) -> String {
560        format!("{}, dmax {}", self.store(&s.store), s.dmax)
561    }
562
563    fn move_plan(&self, m: &MovePlan) -> String {
564        match *m {
565            MovePlan::Alnum { image: i, justified } => format!("alnum{}{}", image(i), if justified { " justified" } else { "" }),
566            MovePlan::AlnumEdited { image: i, edit, positions } => format!("alnum-edited edit {edit} positions {positions}{}", image(i)),
567            MovePlan::National(from) => format!("national {}", national_from(from)),
568            MovePlan::Dbcs { justified, edit } => {
569                format!("dbcs{}{}", if justified { " justified" } else { "" }, edit.map_or(String::new(), |e| format!(" edit {e}")))
570            }
571            MovePlan::Numeric { from, store } => format!("numeric {} to {}", numeric_from(from), self.store(&store)),
572            MovePlan::Float { from, precision: p } => format!("float {} to {}", float_from(from), precision(p)),
573            MovePlan::Address => "address".to_owned(),
574            MovePlan::Index => "index".to_owned(),
575            MovePlan::Refused(a) => format!("refused {}", abend_ref(a)),
576        }
577    }
578
579    /// ` [plan]` and NUMCHECK's test of the sender.
580    fn moved(&self, plan: &MovePlan, check: SenderCheck) -> String {
581        let check = match check {
582            SenderCheck::None => None,
583            SenderCheck::Item => Some("check item".to_owned()),
584            SenderCheck::Integer => Some("check integer".to_owned()),
585        };
586        attrs('[', std::iter::once(self.move_plan(plan)).chain(check).collect())
587    }
588
589    fn stored(&self, (place, store): &(PlaceId, StorePlan)) -> String {
590        format!("{} [{}]", self.place(*place), self.store(store))
591    }
592
593    fn program(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
594        let start = (!self.c.paragraphs.is_empty()).then(|| format!("start {}", self.para(self.c.procedure_start)));
595        let flags = [start, yes(self.c.initial, "initial"), yes(self.c.recursive, "recursive")];
596        writeln!(f, "program {}{}", self.name(self.c.id), join(flags.into_iter().flatten().map(|s| format!(" {s}")), ""))?;
597        for (k, r) in self.c.ranges.iter().enumerate() {
598            let span = if r.first == r.last { self.para(r.first) } else { format!("{} thru {}", self.para(r.first), self.para(r.last)) };
599            writeln!(f, "range r{k} {} {span}", range_kind(r.kind))?;
600        }
601        self.code(f)?;
602        self.tables(f)
603    }
604
605    fn code(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
606        let mut entries: BTreeMap<BlockId, Vec<ParaId>> = BTreeMap::new();
607        for (k, p) in self.c.paragraphs.iter().enumerate() {
608            entries.entry(p.entry).or_default().push(k as ParaId);
609        }
610        for (b, block) in self.c.blocks.iter().enumerate() {
611            for &p in entries.get(&(b as BlockId)).into_iter().flatten() {
612                writeln!(f, "{}", self.paragraph(p, false))?;
613            }
614            writeln!(f, "{}:", label(b as BlockId))?;
615            let ats = self.debug.and_then(|d| d.ops.get(b));
616            let starts = self.debug.and_then(|d| d.statements.get(b)).map_or(&[][..], Vec::as_slice);
617            let mut statement = None;
618            let starting = |f: &mut fmt::Formatter<'_>, k: usize, statement: &mut Option<String>| -> fmt::Result {
619                for (_, at) in starts.iter().filter(|(i, _)| *i as usize == k) {
620                    let pos = self.pos(*at);
621                    writeln!(f, "    statement {}", pos.as_deref().unwrap_or("?"))?;
622                    *statement = pos;
623                }
624                Ok(())
625            };
626            let suffix = |k: usize, statement: &Option<String>| match ats.and_then(|a| a.get(k)).and_then(|&at| self.pos(at)) {
627                Some(pos) if Some(&pos) != statement.as_ref() => format!("  @{pos}"),
628                _ => String::new(),
629            };
630            for (k, op) in block.ops.iter().enumerate() {
631                starting(f, k, &mut statement)?;
632                writeln!(f, "    {}{}", self.op(op), suffix(k, &statement))?;
633            }
634            let k = block.ops.len();
635            starting(f, k, &mut statement)?;
636            let at = match block.end {
637                Terminator::Jump(_) | Terminator::Select(_) => String::new(),
638                _ => suffix(k, &statement),
639            };
640            writeln!(f, "    {}{at}", self.terminator(&block.end))?;
641        }
642        for (&b, paragraphs) in entries.range(self.c.blocks.len() as BlockId..) {
643            for &p in paragraphs {
644                writeln!(f, "{} entry {}?", self.paragraph(p, true), label(b))?;
645            }
646        }
647        Ok(())
648    }
649
650    fn paragraph(&self, k: ParaId, bare: bool) -> String {
651        let p = &self.c.paragraphs[k as usize];
652        let mut text = format!("paragraph {k} {}", self.name(p.name));
653        if p.is_section {
654            text += &format!(" section thru {}", self.para(p.section_end));
655        }
656        if p.priority != 0 {
657            text += &format!(" priority {}", p.priority);
658        }
659        if let Some(a) = p.abandoned {
660            text += &format!(" abandoned {}", abend_ref(a));
661        }
662        if bare { text } else { text + &self.at(p.at) }
663    }
664
665    fn terminator(&self, t: &Terminator) -> String {
666        match t {
667            Terminator::Jump(b) => format!("Jump {}", label(*b)),
668            Terminator::Branch { cond, then, otherwise } => format!("Branch ({}) {} else {}", self.cond(*cond), label(*then), label(*otherwise)),
669            Terminator::Select(arms) => format!("Select {}", join(arms.iter().map(|&b| label(b)), " ")),
670            Terminator::ParagraphEnd { next } => format!("ParagraphEnd next {}", self.next_para(*next)),
671            Terminator::GoTo(p) => format!("GoTo {}", self.para(*p)),
672            Terminator::Switch { value, targets, otherwise } => {
673                format!("Switch {} {} else {}", self.int(value), join(targets.iter().map(|&p| self.para(p)), " "), label(*otherwise))
674            }
675            Terminator::PerformEnter { range, ret, resume } => {
676                let resume = resume.map_or_else(String::new, |r| format!(" resume {} {}", self.para(r.para), label(r.block)));
677                format!("PerformEnter {} -> {}{resume}", self.range(*range), label(*ret))
678            }
679            Terminator::ExitProgram { next } => format!("ExitProgram next {}", label(*next)),
680            Terminator::End(e) => format!("End {}", ending(*e)),
681            Terminator::Abend(a) => match self.c.abends.get(*a as usize) {
682                Some(text) => format!("Abend {} {} {}", abend_ref(*a), text.code, self.string(text.message)),
683                None => format!("Abend {}?", abend_ref(*a)),
684            },
685            Terminator::AlteredGoTo { para, otherwise } => format!("AlteredGoTo {} else {}", self.para(*para), label(*otherwise)),
686            Terminator::Debug { range, name, next } => format!("Debug {} name {} next {}", self.range(*range), self.literal(*name), label(*next)),
687        }
688    }
689
690    fn op(&self, op: &Op) -> String {
691        match op {
692            Op::Move { from, to, plan, check } => format!("Move {} <- {}{}", self.place(*to), self.operand(from), self.moved(plan, *check)),
693            Op::Set { from, to, plan } => format!("Set {} <- {}{}", self.place(*to), self.operand(from), self.moved(plan, SenderCheck::None)),
694            Op::Initialize { target, plan } => format!("Initialize {} init {plan}", self.place(*target)),
695            Op::Arith(id) => self.arith(*id),
696            Op::SetAddress { records, address } => format!("SetAddress {} <- {}", join(records.iter().map(|&r| self.linkage(r)), " "), self.operand(address)),
697            Op::SetUpDown { by, down, targets } => {
698                let targets = targets.iter().map(|(p, how)| {
699                    let how = match how {
700                        UpDown::Pointer => "pointer".to_owned(),
701                        UpDown::Number(step) => self.step(step),
702                        UpDown::Refused(a) => format!("refused {}", abend_ref(*a)),
703                    };
704                    format!("{} [{how}]", self.place(*p))
705                });
706                format!("SetUpDown {} {} by {}", join(targets, ", "), if *down { "down" } else { "up" }, self.int(by))
707            }
708            Op::SetEntry { entry, targets } => format!("SetEntry {} <- {}", self.places(targets), self.operand(entry)),
709            Op::Step { var, by, plan, prepass } => {
710                let prepass = (!prepass.is_empty()).then(|| format!("prepass {}", self.places(prepass)));
711                format!("Step {} by {} [{}]{}", self.place(*var), self.expr(*by), self.step(plan), attrs('{', prepass.into_iter().collect()))
712            }
713            Op::SetInt { target, value } => format!("SetInt {} <- {}", self.place(*target), self.int(value)),
714            Op::Inspect(id) => self.inspect(*id),
715            Op::String(id) => self.string_op(*id),
716            Op::Unstring(id) => self.unstring(*id),
717            Op::SearchAll(id) => self.search_all(*id),
718            Op::Nest => "Nest".to_owned(),
719            Op::Unnest(n) => format!("Unnest {n}"),
720            Op::SetTemp(t, value) => format!("SetTemp t{t} <- {}", self.int(value)),
721            Op::DecTemp(t) => format!("DecTemp t{t}"),
722            Op::SetCount(t, odo) => format!("SetCount t{t} <- {}", self.odo(odo)),
723            Op::Display(id) => self.display(*id),
724            Op::Accept { target, from, plan } => format!("Accept {} <- {}{}", self.place(*target), accept_from(*from), self.moved(plan, SenderCheck::None)),
725            Op::File(id) => self.file_op(*id),
726            Op::Call(id) => self.call(*id),
727            Op::Cancel(name) => format!("Cancel {}", self.operand(name)),
728            Op::Sort(id) => self.sort(*id),
729            Op::Release(id) => self.release(*id),
730            Op::Return(id) => self.return_op(*id),
731            Op::Report(r) => self.report_op(r),
732            Op::Invoke(id) => self.invoke(*id),
733            Op::Cics(id) => self.cics(*id),
734            Op::Sql(ordinal) => self.sql(*ordinal),
735            Op::Alter { para, to } => format!("Alter {} to {}", self.para(*para), self.para(*to)),
736            Op::EnterSegment(priority) => format!("EnterSegment {priority}"),
737            Op::DebugLine(line) => format!("DebugLine {line}"),
738            Op::DebugAlter { range, name, contents } => format!("DebugAlter {} name {} contents {}", self.range(*range), self.literal(*name), self.literal(*contents)),
739            Op::Markup(id) => self.markup(*id),
740        }
741    }
742
743    fn arith(&self, id: ArithId) -> String {
744        let Some(ArithPlan { dmax, arith, prepass, steps, remainder, handled, per_receiver, inner_dmax }) = self.c.plans.arith.get(id as usize) else {
745            return format!("Arith arith{id}?");
746        };
747        let mut steps: Vec<String> = steps.iter().map(|s| self.arith_step(s)).collect();
748        if let Some(r) = remainder {
749            steps.push(format!(
750                "remainder {} <- {} / {} [{}, quotient scale {}]",
751                self.place(r.target),
752                self.term(r.dividend),
753                self.term(r.divisor),
754                self.store(&r.store),
755                r.quotient_scale
756            ));
757        }
758        let mut how = vec![format!("dmax {dmax}")];
759        if inner_dmax != dmax {
760            how.push(format!("inner dmax {inner_dmax}"));
761        }
762        match arith {
763            Arith::Compat => {}
764            Arith::Extend => how.push("arith extend".to_owned()),
765        }
766        if !prepass.is_empty() {
767            how.push(format!("prepass {}", self.places(prepass)));
768        }
769        how.extend(yes(*per_receiver, "per receiver"));
770        how.extend(yes(*handled, "size error"));
771        format!("Arith {}{}", steps.join("; "), attrs('{', how))
772    }
773
774    fn arith_step(&self, s: &ArithStep) -> String {
775        let mut how = vec![self.store(&s.store)];
776        how.extend(yes(s.rounded, "rounded"));
777        match s.mode {
778            Mode::Fixed => {}
779            Mode::Float(p) => how.push(format!("float {}", precision(p))),
780        }
781        if !s.probe.is_empty() {
782            how.push(format!("probe {}", self.places(&s.probe)));
783        }
784        format!("{} <- {}{}", self.place(s.target), self.expr(s.expr), attrs('[', how))
785    }
786
787    fn display(&self, id: DisplayId) -> String {
788        let Some(d) = self.c.plans.display.get(id as usize) else { return format!("Display display{id}?") };
789        let items = d.items.iter().map(|item| match item {
790            DisplayItem::Bytes(p) => self.place(*p),
791            DisplayItem::National(p) => format!("{} [national]", self.place(*p)),
792            DisplayItem::Digits { place, digits, signed } => format!("{} [digits {digits}{}]", self.place(*place), if *signed { " signed" } else { "" }),
793            DisplayItem::Refused { place, abend } => format!("{} [refused {}]", self.place(*place), abend_ref(*abend)),
794            DisplayItem::Text(text) => self.literal(*text),
795            DisplayItem::Value(o) => self.operand(o),
796        });
797        format!("Display {}{}", join(items, ", "), attrs('{', yes(d.no_advancing, "no advancing").into_iter().collect()))
798    }
799
800    fn inspect(&self, id: InspectId) -> String {
801        let Some(i) = self.c.plans.inspect.get(id as usize) else { return format!("Inspect inspect{id}?") };
802        let mut text = format!(
803            "Inspect {}",
804            match &i.target {
805                Inspected::Item(p) => self.place(*p),
806                Inspected::Value(o) => self.operand(o),
807            }
808        );
809        for phrase in &i.tallying {
810            text += &format!(" tallying {}", self.inspect_phrase(phrase));
811        }
812        for phrase in &i.replacing {
813            text += &format!(" replacing {}", self.inspect_phrase(phrase));
814        }
815        if let Some(c) = &i.converting {
816            text += &match &c.table {
817                ConvertTable::Built(pairs) => {
818                    let (from, to): (Vec<u8>, Vec<u8>) = pairs.iter().copied().unzip();
819                    format!(" converting {} to {} {{built}}", self.bytes(&from), self.bytes(&to))
820                }
821                ConvertTable::Operands { from, to } => format!(" converting {} to {}", self.chars(from), self.chars(to)),
822            };
823            text += &self.bounds(&c.bounds);
824        }
825        text
826    }
827
828    fn inspect_phrase(&self, phrase: &InspectPhrase) -> String {
829        let mut text = String::new();
830        if let Some((p, step)) = &phrase.counter {
831            text += &format!("{} [{}] for ", self.place(*p), self.step(step));
832        }
833        text += match phrase.mode {
834            InspectMode::Characters => "CHARACTERS",
835            InspectMode::All => "ALL",
836            InspectMode::Leading => "LEADING",
837            InspectMode::First => "FIRST",
838        };
839        if let Some(pattern) = &phrase.pattern {
840            text += &format!(" {}", self.chars(pattern));
841        }
842        match &phrase.by {
843            None => {}
844            Some(Replacement::Chars(c)) => text += &format!(" by {}", self.chars(c)),
845            Some(Replacement::Fill(b)) => text += &format!(" by fill {}", hex("X", &[*b])),
846        }
847        text + &self.bounds(&phrase.bounds)
848    }
849
850    fn bounds(&self, bounds: &[Bound]) -> String {
851        join(bounds.iter().map(|b| format!(" {} {}", if b.after { "after" } else { "before" }, self.chars(&b.value))), "")
852    }
853
854    fn string_op(&self, id: StringId) -> String {
855        let Some(s) = self.c.plans.string.get(id as usize) else { return format!("String string{id}?") };
856        let sources = s.sources.iter().map(|source| {
857            let delimiter = source.delimiter.as_ref().map_or_else(|| "size".to_owned(), |d| self.chars(d));
858            format!("{} delimited by {delimiter}", self.chars(&source.chars))
859        });
860        let pointer = s.pointer.as_ref().map_or_else(String::new, |p| format!(" pointer {}", self.stored(p)));
861        format!("String {} <- {}{pointer}", self.place(s.into), join(sources, ", "))
862    }
863
864    fn unstring(&self, id: UnstringId) -> String {
865        let Some(u) = self.c.plans.unstring.get(id as usize) else { return format!("Unstring unstring{id}?") };
866        let mut text = format!("Unstring {}", self.place(u.source));
867        if !u.delimiters.is_empty() {
868            let delimiters = u.delimiters.iter().map(|(all, c)| format!("{}{}", if *all { "ALL " } else { "" }, self.chars(c)));
869            text += &format!(" delimited by {}", join(delimiters, ", "));
870        }
871        let into = u.into.iter().map(|i| {
872            let mut field = format!("{} [{}]", self.place(i.target), self.move_plan(&i.plan));
873            if let Some(d) = &i.delimiter {
874                field += &format!(" delimiter in {} [found {}, none {}]", self.place(d.target), self.move_plan(&d.found), self.move_plan(&d.none));
875            }
876            if let Some(c) = &i.count {
877                field += &format!(" count in {}", self.stored(c));
878            }
879            field
880        });
881        text += &format!(" into {}", join(into, ", "));
882        if let Some(p) = &u.pointer {
883            text += &format!(" pointer {}", self.stored(p));
884        }
885        if let Some((p, step)) = &u.tallying {
886            text += &format!(" tallying {} [{}]", self.place(*p), self.step(step));
887        }
888        text
889    }
890
891    fn search_all(&self, id: SearchAllId) -> String {
892        let Some(s) = self.c.plans.search_all.get(id as usize) else { return format!("SearchAll search-all{id}?") };
893        let keys = s.keys.iter().map(|k| {
894            format!("{} = {} [{}{}]", self.comparand(&k.key), self.comparand(&k.value), compare(k.how), if k.ascending { "" } else { ", descending" })
895        });
896        format!("SearchAll {} [{}] within {} when {}", self.place(s.index), self.store(&s.store), self.count(&s.count), join(keys, " and "))
897    }
898
899    fn file_op(&self, id: FileOpId) -> String {
900        let Some(op) = self.c.services.file_ops.get(id as usize) else { return format!("File file-op{id}?") };
901        let file = self.file(op.file);
902        let (text, at_end) = match &op.verb {
903            FileVerb::Open(mode) => (format!("OPEN {} {file}", open_mode(*mode)), false),
904            FileVerb::Close => (format!("CLOSE {file}"), false),
905            FileVerb::CloseWith(closing) => (format!("CLOSE {file} {}", closing_text(*closing)), false),
906            FileVerb::Read { sequential, previous, into, key } => {
907                let mut text = format!("READ {file}");
908                text += &if *sequential { " sequential".to_owned() } else { format!(" key {key}") };
909                if *previous {
910                    text += " previous";
911                }
912                if let Some((p, plan)) = into {
913                    text += &format!(" into {}{}", self.place(*p), self.moved(plan, SenderCheck::None));
914                }
915                (text, *sequential)
916            }
917            FileVerb::Write { record, from, advancing } => {
918                let mut text = format!("WRITE {}{}", self.place(*record), self.sender(from.as_ref()));
919                if let Some(a) = advancing {
920                    text += &format!(" advancing {}", self.advance(a));
921                }
922                (text, false)
923            }
924            FileVerb::Rewrite { record, from } => (format!("REWRITE {}{}", self.place(*record), self.sender(from.as_ref())), false),
925            FileVerb::Delete => (format!("DELETE {file}"), false),
926            FileVerb::Start { rel, key } => {
927                let rel = match rel {
928                    StartRel::Equal => "=",
929                    StartRel::Greater => ">",
930                    StartRel::NotLess => ">=",
931                };
932                let key = match key {
933                    StartKey::Prime => "prime".to_owned(),
934                    StartKey::Named { key, span } => format!("{key} +{} len {}", span.offset, span.len),
935                    StartKey::Relative(value) => format!("relative {}", self.int(value)),
936                    StartKey::RelativeKey => "relative key".to_owned(),
937                };
938                (format!("START {file} key {rel} {key}"), false)
939            }
940        };
941        let (on, not_on) = if at_end { ("at end", "not at end") } else { ("invalid key", "not invalid key") };
942        let mut phrases = phrase_words(op.phrase, on, not_on);
943        phrases.extend(phrase_words(op.end_of_page, "end-of-page", "not end-of-page"));
944        format!("File {text}{}", attrs('{', phrases))
945    }
946
947    fn sender(&self, from: Option<&FromMove>) -> String {
948        from.map_or_else(String::new, |m| format!(" <- {}{}", self.operand(&m.from), self.moved(&m.plan, m.check)))
949    }
950
951    fn advance(&self, a: &Advance) -> String {
952        let side = |before: bool| if before { "before" } else { "after" };
953        match a {
954            Advance::Lines { before, count } => format!("{} {} lines", side(*before), self.int(count)),
955            Advance::Page { before } => format!("{} page", side(*before)),
956            Advance::Mnemonic { before, space } => format!(
957                "{} {}",
958                side(*before),
959                match space {
960                    Spacing::Lines(n) => format!("{n} lines"),
961                    Spacing::Channel(c) => format!("channel {c}"),
962                    Spacing::PageMode => "page mode".to_owned(),
963                }
964            ),
965        }
966    }
967
968    fn call(&self, id: CallId) -> String {
969        let Some(c) = self.c.services.calls.get(id as usize) else { return format!("Call call{id}?") };
970        let target = match &c.target {
971            CallTarget::Named { name, le: None } => self.literal(*name),
972            CallTarget::Named { name, le: Some(service) } => {
973                let service = format!("{service:?}").to_ascii_uppercase();
974                format!("{} or {service}", self.literal(*name))
975            }
976            CallTarget::Dynamic(o) => self.operand(o),
977            CallTarget::Pointer(p) => format!("pointer {}", self.place(*p)),
978            CallTarget::Entry(p) => format!("entry {}", self.place(*p)),
979        };
980        let mut text = format!("Call {target}");
981        if !c.args.is_empty() {
982            let args = c.args.iter().map(|a| match a {
983                CallArg::Reference(p) => self.place(*p),
984                CallArg::Content(chars) => format!("content {}", self.chars(chars)),
985                CallArg::Value(o) => format!("value {}", self.operand(o)),
986                CallArg::Omitted => "omitted".to_owned(),
987            });
988            text += &format!(" using {}", join(args, ", "));
989        }
990        if let Some(r) = c.returning {
991            text += &format!(" returning {}", self.place(r));
992        }
993        text + &attrs('{', [yes(c.on_exception, "on exception"), yes(c.not_on_exception, "not on exception")].into_iter().flatten().collect())
994    }
995
996    fn sort_keys(&self, keys: &SortKeys) -> String {
997        let named = keys.keys.iter().map(|k| {
998            let item = self.items.get(k.item as usize).and_then(|i| i.name).map_or_else(|| format!("+{} len {}", k.offset, k.len), |s| self.name(s));
999            format!("{} {item}{}", if k.ascending { "ascending" } else { "descending" }, if k.collated { " collated" } else { "" })
1000        });
1001        format!("keys {}{}", join(named, ", "), if keys.collating.is_some() { " {collating sequence}" } else { "" })
1002    }
1003
1004    fn sort(&self, id: SortId) -> String {
1005        match self.c.services.sorts.get(id as usize) {
1006            None => format!("Sort sort{id}?"),
1007            Some(SortPlan::File(s)) => {
1008                let io = |io: &Option<SortIo>, files: &str, procedure: &str| match io {
1009                    None => format!("{procedure} none"),
1010                    Some(SortIo::Files(list)) => format!("{files} {}", join(list.iter().map(|&k| self.file(k)), " ")),
1011                    Some(SortIo::Procedure(r)) => format!("{procedure} procedure {}", self.range(*r)),
1012                };
1013                format!(
1014                    "Sort {} {} {} {} {}{}",
1015                    if s.merge { "merge" } else { "file" },
1016                    self.file(s.sd),
1017                    self.sort_keys(&s.keys),
1018                    io(&s.input, "using", "input"),
1019                    io(&s.output, "giving", "output"),
1020                    attrs('{', vec![format!("sort-return {}", self.place(s.sort_return)), format!("sort-control {}", self.place(s.sort_control))])
1021                )
1022            }
1023            Some(SortPlan::Table(t)) => {
1024                format!("Sort table {} first {} within {} stride {} {}", self.name(t.name), self.place(t.first), self.count(&t.count), t.stride, self.sort_keys(&t.keys))
1025            }
1026        }
1027    }
1028
1029    fn release(&self, id: ReleaseId) -> String {
1030        let Some(r) = self.c.services.releases.get(id as usize) else { return format!("Release release{id}?") };
1031        let file = r.file.map_or_else(|| self.name(r.name), |k| self.file(k));
1032        format!("Release {}{}{}", self.place(r.record), self.sender(r.from.as_ref()), attrs('{', vec![format!("file {file}"), format!("sort-return {}", self.place(r.sort_return))]))
1033    }
1034
1035    fn return_op(&self, id: ReturnId) -> String {
1036        let Some(r) = self.c.services.returns.get(id as usize) else { return format!("Return return{id}?") };
1037        let file = r.file.map_or_else(|| self.name(r.name), |k| self.file(k));
1038        let into = r.into.as_ref().map_or_else(String::new, |(p, plan)| format!(" into {}{}", self.place(*p), self.moved(plan, SenderCheck::None)));
1039        format!("Return {file}{into}{}", attrs('{', vec![format!("sort-return {}", self.place(r.sort_return))]))
1040    }
1041
1042    fn report_name(&self, report: u32) -> String {
1043        self.c.services.report.reports.get(report as usize).map_or_else(|| format!("report{report}?"), |r| word(&r.name))
1044    }
1045
1046    fn report_op(&self, op: &ReportOp) -> String {
1047        match *op {
1048            ReportOp::Initiate(r) => format!("Report Initiate {}", self.report_name(r)),
1049            ReportOp::Generate { report, detail } => {
1050                let group = detail.map_or_else(String::new, |d| {
1051                    let name = self.c.services.report.reports.get(report as usize).and_then(|r| r.groups.get(d as usize)).and_then(|g| g.name.as_deref());
1052                    format!(" detail {}", name.map_or_else(|| format!("group{d}"), word))
1053                });
1054                format!("Report Generate {}{group}", self.report_name(report))
1055            }
1056            ReportOp::Terminate(r) => format!("Report Terminate {}", self.report_name(r)),
1057            ReportOp::Suppress => "Report Suppress".to_owned(),
1058        }
1059    }
1060
1061    fn invoke(&self, id: InvokeId) -> String {
1062        let Some(i) = self.c.services.invokes.get(id as usize) else { return format!("Invoke invoke{id}?") };
1063        let receiver = match &i.receiver {
1064            Receiver::SelfRef => "SELF".to_owned(),
1065            Receiver::Super => "SUPER".to_owned(),
1066            Receiver::Class { name, external } if name == external => format!("class {}", self.name(*name)),
1067            Receiver::Class { name, external } => format!("class {} external {}", self.name(*name), self.string(*external)),
1068            Receiver::Object(p) => self.place(*p),
1069        };
1070        let method = match &i.method {
1071            MethodName::New => "NEW".to_owned(),
1072            MethodName::Named(s) => self.literal(*s),
1073            MethodName::Dynamic(p) => self.place(*p),
1074        };
1075        let mut text = format!("Invoke {receiver} {method}");
1076        if !i.args.is_empty() {
1077            text += &format!(" using {}", join(i.args.iter().map(|(o, sig)| format!("{} as {}", self.operand(o), self.string(*sig))), ", "));
1078        }
1079        if let Some((p, sig)) = &i.returning {
1080            text += &format!(" returning {} as {}", self.place(*p), self.string(*sig));
1081        }
1082        text + &attrs('{', [yes(i.on_exception, "on exception"), yes(i.not_on_exception, "not on exception")].into_iter().flatten().collect())
1083    }
1084
1085    fn cics(&self, id: CicsId) -> String {
1086        let Some(written) = self.c.services.cics.get(id as usize) else { return format!("Cics cics{id}?") };
1087        let Ok(CicsCommand { name: _, command, resp, sinks }) = written.clone().map(&mut Names(self));
1088        let mut words = vec![command.name().to_owned()];
1089        self.cics_options(&command, &mut words);
1090        option(&mut words, "RESP", &resp.resp);
1091        option(&mut words, "RESP2", &resp.resp2);
1092        flag(&mut words, "NOHANDLE", resp.nohandle);
1093        let mut how = Vec::new();
1094        if self.symbol(written.name) != Some(command.name()) {
1095            how.push(format!("written {}", self.string(written.name)));
1096        }
1097        if !sinks.is_empty() {
1098            how.push(format!("sinks {}", join(sinks.iter().map(|(p, s)| format!("{p} {}", s.kind())), " ")));
1099        }
1100        format!("Cics {}{}", words.join(" "), attrs('{', how))
1101    }
1102
1103    fn sql(&self, ordinal: SqlId) -> String {
1104        let Some(e) = (ordinal as usize).checked_sub(1).and_then(|k| self.sql.get(k)) else { return format!("Sql {ordinal}") };
1105        let text = if self.symbol(e.text).is_some_and(|t| !t.is_empty()) { self.string(e.text) } else { self.name(e.verb) };
1106        let hosts = |key: &str, list: &[super::HostPlace]| if list.is_empty() { String::new() } else { format!(" {key} ({})", join(list.iter().map(|h| self.host(h)), ", ")) };
1107        let statement = match &e.statement {
1108            SqlStatement::Query { inputs, into } => format!("query{}{}", hosts("inputs", inputs), hosts("into", into)),
1109            SqlStatement::Change { delete, inputs, current_of } => {
1110                let current = current_of.map_or_else(String::new, |c| format!(" current of {}", self.name(c)));
1111                format!("{}{}{current}", if *delete { "delete" } else { "change" }, hosts("inputs", inputs))
1112            }
1113            SqlStatement::Open { cursor, inputs } => format!("open {}{}", self.name(*cursor), hosts("inputs", inputs)),
1114            SqlStatement::Fetch { cursor, into } => format!("fetch {}{}", self.name(*cursor), hosts("into", into)),
1115            SqlStatement::Close { cursor } => format!("close {}", self.name(*cursor)),
1116            SqlStatement::Commit => "commit".to_owned(),
1117            SqlStatement::Rollback => "rollback".to_owned(),
1118            SqlStatement::Declaration => "declaration".to_owned(),
1119            SqlStatement::Unsupported(what) => format!("unsupported {}", self.string(*what)),
1120            SqlStatement::Connect { what, location } => format!("connect {}{}", self.string(*what), hosts("location", location)),
1121        };
1122        format!("Sql {ordinal} {text} {statement}{}", attrs('{', yes(e.with_hold, "with hold").into_iter().collect()))
1123    }
1124
1125    fn host(&self, h: &super::HostPlace) -> String {
1126        let mut text = self.place(h.var);
1127        if let Some((offset, len)) = h.member {
1128            text += &format!(" member +{offset} len {len}");
1129        }
1130        text += &match &h.ty {
1131            Ok(ty) => format!(" [{}]", host_type(ty)),
1132            Err(a) => format!(" [refused {}]", abend_ref(*a)),
1133        };
1134        if let Some((p, offset)) = h.indicator {
1135            text += &format!(" indicator {} +{offset}", self.place(p));
1136        }
1137        text
1138    }
1139
1140    fn ccsid(&self, c: &Ccsid) -> Option<String> {
1141        match c {
1142            Ccsid::Unnamed => None,
1143            Ccsid::CodePage => Some("encoding codepage".to_owned()),
1144            Ccsid::Operand(o) => Some(format!("encoding {}", self.operand(o))),
1145        }
1146    }
1147
1148    fn markup(&self, id: MarkupId) -> String {
1149        let Some(m) = self.c.services.markup.get(id as usize) else { return format!("Markup markup{id}?") };
1150        let subscripts = |s: &[IntExpr]| (!s.is_empty()).then(|| format!("subscripts ({})", join(s.iter().map(|e| self.int(e)), ", ")));
1151        let (verb, words) = match m {
1152            Markup::JsonGenerate(g) => {
1153                let name = g.name.map_or_else(|| "name omitted".to_owned(), |n| format!("name {}", self.raw(n)));
1154                let mut words = vec![format!("{} <- {}", self.place(g.receiver), self.place(g.from))];
1155                words.extend(subscripts(&g.subscripts));
1156                words.push(name);
1157                words.extend(self.ccsid(&g.encoding));
1158                words.extend(g.count.as_ref().map(|c| format!("count {}", self.stored(c))));
1159                words.push(format!("code {}", self.stored(&g.code)));
1160                ("JSON GENERATE", words)
1161            }
1162            Markup::XmlGenerate(g) => {
1163                let mut words = vec![format!("{} <- {}", self.place(g.receiver), self.place(g.from))];
1164                words.extend(subscripts(&g.subscripts));
1165                words.extend(self.ccsid(&g.encoding));
1166                words.extend(g.namespace.as_ref().map(|o| format!("namespace {}", self.operand(o))));
1167                words.extend(g.prefix.as_ref().map(|o| format!("prefix {}", self.operand(o))));
1168                words.extend(yes(g.declaration, "declaration"));
1169                words.extend(yes(g.suppressing, "suppress"));
1170                words.extend(g.count.as_ref().map(|c| format!("count {}", self.stored(c))));
1171                words.push(format!("code {}", self.stored(&g.code)));
1172                ("XML GENERATE", words)
1173            }
1174            Markup::XmlParse(p) => {
1175                let mut words = vec![self.place(p.document)];
1176                words.extend(p.encoding.as_ref().map(|o| format!("encoding {}", self.operand(o))));
1177                words.extend(yes(p.national, "returning national"));
1178                words.push(format!("procedure {}", self.range(p.procedure)));
1179                words.push(format!("event {}", self.place(p.event)));
1180                words.push(format!("code {}", self.stored(&p.code)));
1181                words.push(format!("information {}", self.stored(&p.information)));
1182                words.push(format!("code-value {}", self.int(&p.code_value)));
1183                ("XML PARSE", words)
1184            }
1185            Markup::JsonParse(p) => {
1186                let mut words = vec![format!("{} <- {}", self.place(p.into), self.place(p.source))];
1187                words.extend(subscripts(&p.subscripts));
1188                words.extend(self.ccsid(&p.encoding));
1189                words.extend(yes(p.ignore_all, "ignoring null for all"));
1190                words.push(format!("code {}", self.stored(&p.code)));
1191                words.push(format!("status {}", self.stored(&p.status)));
1192                ("JSON PARSE", words)
1193            }
1194        };
1195        let (on, not_on) = m.phrases();
1196        let phrases = [yes(on, "on exception"), yes(not_on, "not on exception")].into_iter().flatten().collect();
1197        format!("Markup m{id} {verb} {}{}", words.join(" "), attrs('{', phrases))
1198    }
1199
1200    fn tables(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1201        for (k, p) in self.c.places.iter().enumerate() {
1202            writeln!(f, "{}", self.place_row(k, p))?;
1203        }
1204        for (k, c) in self.c.consts.iter().enumerate() {
1205            writeln!(f, "const c{k} {}", self.constant(c))?;
1206        }
1207        for (k, a) in self.c.abends.iter().enumerate() {
1208            writeln!(f, "abend a{k} {} {}{}", a.code, self.string(a.message), a.at.map_or_else(String::new, |at| self.at(at)))?;
1209        }
1210        let s = self.c.services;
1211        for (k, d) in s.files.iter().enumerate() {
1212            writeln!(f, "file {k} {}", self.file_row(d))?;
1213        }
1214        for (k, e) in s.entries.iter().enumerate() {
1215            let using = join(e.using.iter().map(|&r| self.linkage(r)), " ");
1216            writeln!(f, "entry {k} {} paragraph {} block {} using ({using})", self.name(e.name), self.para(e.paragraph), label(e.block))?;
1217        }
1218        for (k, plan) in self.c.plans.init.iter().enumerate() {
1219            for field in &plan.fields {
1220                let value = match &field.value {
1221                    InitValue::Default(v) => figurative(*v).to_owned(),
1222                    InitValue::Value(c) => self.konst(*c),
1223                    InitValue::Replacing(o) => format!("replacing {}", self.operand(o)),
1224                };
1225                let scaling = if field.scaling == 0 { String::new() } else { format!(" scaling {}", field.scaling) };
1226                writeln!(f, "init {k} +{} len {} <- {value}{}{scaling}", field.offset, field.len, self.moved(&field.store, SenderCheck::None))?;
1227            }
1228        }
1229        for (field, p, ty) in &s.sqlca.fields {
1230            writeln!(f, "sqlca {} {} [{}]", sqlca_field(*field), self.place(*p), host_type(ty))?;
1231        }
1232        let modes = s.declaratives.modes.iter().zip(MODES).filter_map(|(r, mode)| r.map(|r| format!("{mode} {}", self.range(r))));
1233        for mode in modes {
1234            writeln!(f, "declaratives {mode}")?;
1235        }
1236        if let Some((offset, len)) = s.declaratives.debug_item {
1237            writeln!(f, "declaratives debug-item program+{offset} len {len}")?;
1238        }
1239        self.scope(f)?;
1240        if let Some(d) = &s.function {
1241            writeln!(f, "function params ({}) returning {}", self.places(&d.params), self.place(d.returning))?;
1242        }
1243        self.reports(f)?;
1244        for (k, m) in s.markup.iter().enumerate() {
1245            self.markup_nodes(f, k, m)?;
1246        }
1247        if let Some(class) = &s.class {
1248            self.class(f, class)?;
1249        }
1250        Ok(())
1251    }
1252
1253    fn place_row(&self, k: usize, p: &Place) -> String {
1254        let mut text = format!("place p{k} {} {}+{} len {} {}", self.reference(p, &[]), base(p.base), p.offset, p.len, kind(&p.kind));
1255        if p.scaling != 0 {
1256            text += &format!(" scaling {}", p.scaling);
1257        }
1258        for o in &p.moved {
1259            text += &format!(" moved {}", self.odo(o));
1260        }
1261        for s in &p.subscripts {
1262            text += &format!(" stride {}", s.stride);
1263            if let Some(n) = s.check {
1264                text += &format!(" check {n}");
1265            }
1266        }
1267        for o in &p.odo {
1268            text += &format!(" odo {}", self.odo(o));
1269        }
1270        if p.refmod.as_ref().is_some_and(|r| r.check) {
1271            text += " refmod check";
1272        }
1273        match p.numcheck.lax {
1274            None => {}
1275            Some(LaxRedefinition::Signed) => text += " numcheck lax signed",
1276            Some(LaxRedefinition::LeadingSpaces(n)) => text += &format!(" numcheck lax {n} leading spaces"),
1277        }
1278        if p.numcheck.removed {
1279            text += " numcheck removed";
1280        }
1281        text + &self.at(p.at)
1282    }
1283
1284    fn file_row(&self, d: &FileDesc) -> String {
1285        let organization = match d.organization {
1286            Organization::Sequential => "sequential",
1287            Organization::LineSequential => "line-sequential",
1288            Organization::Indexed => "indexed",
1289            Organization::Relative => "relative",
1290        };
1291        let access = match d.access {
1292            Access::Sequential => "sequential",
1293            Access::Random => "random",
1294            Access::Dynamic => "dynamic",
1295        };
1296        let format = match d.format {
1297            Format::Fixed => "fixed",
1298            Format::Variable => "variable",
1299            Format::Text => "text",
1300        };
1301        let mut text = format!("{} assign {} {organization} access {access} format {format} read {} to {}", self.name(d.name), self.name(d.assign), d.read_lengths.0, d.read_lengths.1);
1302        let flags = [yes(d.optional, "optional"), yes(d.fixed, "fixed-length"), yes(d.sort, "sort")];
1303        text += &join(flags.into_iter().flatten().map(|s| format!(" {s}")), "");
1304        if let Some(min) = d.record_min {
1305            text += &format!(" record-min {min}");
1306        }
1307        if let Some(dep) = &d.depending {
1308            text += &format!(" depending {} from {} to {}", self.place(dep.item), dep.lengths.0, dep.lengths.1);
1309        }
1310        if let Some((p, plan)) = &d.status {
1311            text += &format!(" status {}{}", self.place(*p), self.moved(plan, SenderCheck::None));
1312        }
1313        if let Some(keys) = &d.keys {
1314            text += &format!(" key +{} len {}", keys.prime.offset, keys.prime.len);
1315            for (span, duplicates) in &keys.alternates {
1316                text += &format!(" alternate +{} len {}{}", span.offset, span.len, if *duplicates { " duplicates" } else { "" });
1317            }
1318        }
1319        if let Some(r) = &d.relative {
1320            let digits = r.digits.map_or_else(String::new, |n| format!(" digits {n}"));
1321            text += &format!(" relative-key {} [{}] value {}{digits}", self.place(r.place), self.store(&r.store), self.int(&r.value));
1322        }
1323        if let Some(l) = &d.linage {
1324            text += &format!(" linage {}", self.int(&l.lines));
1325            for (key, value) in [("footing", &l.footing), ("top", &l.top), ("bottom", &l.bottom)] {
1326                if let Some(v) = value {
1327                    text += &format!(" {key} {}", self.int(v));
1328                }
1329            }
1330            if let Some(c) = &l.counter {
1331                text += &format!(" counter {}", self.stored(c));
1332            }
1333        }
1334        if let Some(c) = d.carriage {
1335            text += &format!(" carriage {}{}", if c.machine { "machine" } else { "asa" }, if c.reserved { " reserved" } else { "" });
1336        }
1337        if let Some(r) = d.error {
1338            text += &format!(" error {}", self.range(r));
1339        }
1340        text
1341    }
1342
1343    fn scope(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1344        let s = &self.c.services.scope;
1345        for &c in &s.containers {
1346            writeln!(f, "scope container {}", self.name(c))?;
1347        }
1348        for (record, binding) in &s.records {
1349            let binding = match binding {
1350                Binding::External { name, size } => format!("external {} size {size}", self.name(*name)),
1351                Binding::ExternalFile(k) => format!("external file {}", self.file(*k)),
1352                Binding::Global { program, section, name } => format!("global {} of {} in {}", self.name(*name), scope_section(*section), self.name(*program)),
1353            };
1354            writeln!(f, "scope record {} {binding}", self.linkage(*record))?;
1355        }
1356        for shared in &s.files {
1357            let from = match (shared.external, shared.declared_in) {
1358                (true, _) => "external".to_owned(),
1359                (false, Some(p)) => format!("global in {}", self.name(p)),
1360                (false, None) => "global".to_owned(),
1361            };
1362            writeln!(f, "scope file {} {from}", self.file(shared.file))?;
1363        }
1364        for (file, record) in &s.areas {
1365            writeln!(f, "scope area {} {}", self.file(*file), self.linkage(*record))?;
1366        }
1367        for g in &s.globals {
1368            let at = match g.at {
1369                GlobalAt::Program(offset) => format!("program+{offset}"),
1370                GlobalAt::Local(offset) => format!("local+{offset}"),
1371                GlobalAt::Linkage(record) => self.linkage(record),
1372            };
1373            writeln!(f, "scope global {} of {} at {at}", self.name(g.name), scope_section(g.section))?;
1374        }
1375        for (file, range) in &s.global_files {
1376            writeln!(f, "scope global-error {} {}", self.file(*file), self.range(*range))?;
1377        }
1378        for (mode, r) in MODES.iter().zip(&s.global_modes) {
1379            if let Some(r) = r {
1380                writeln!(f, "scope global-error {mode} {}", self.range(*r))?;
1381            }
1382        }
1383        Ok(())
1384    }
1385
1386    fn reports(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1387        let w = &self.c.services.report;
1388        if let Some(p) = w.print_switch {
1389            writeln!(f, "report print-switch {}", self.place_at(p))?;
1390        }
1391        for (k, r) in w.reports.iter().enumerate() {
1392            let mut text = format!("report {k} {} file {} width {}", word(&r.name), self.file_at(r.file), r.width);
1393            if let Some(c) = r.code {
1394                text += &format!(" code {}", self.konst(c));
1395            }
1396            if let Some(p) = r.page {
1397                text += &format!(" page limit {} heading {} first-detail {} last-detail {} footing {}", p.limit, p.heading, p.first_detail, p.last_detail, p.footing);
1398            }
1399            text += &format!(" page-counter {} line-counter {} state {}", self.place_at(r.page_counter), self.place_at(r.line_counter), self.place_at(r.state));
1400            let headings = [("report-heading", r.report_heading), ("page-heading", r.page_heading), ("page-footing", r.page_footing), ("report-footing", r.report_footing)];
1401            for (key, group) in headings {
1402                if let Some(g) = group {
1403                    text += &format!(" {key} group {g}");
1404                }
1405            }
1406            for (key, list) in [("control-headings", &r.control_headings), ("control-footings", &r.control_footings)] {
1407                if list.iter().any(Option::is_some) {
1408                    text += &format!(" {key} ({})", join(list.iter().map(|g| g.map_or_else(|| "-".to_owned(), |g| g.to_string())), " "));
1409                }
1410            }
1411            if let Some(n) = r.first_detail_written {
1412                text += &format!(" first-detail-written {n}");
1413            }
1414            writeln!(f, "{text}")?;
1415            for (c, control) in r.controls.iter().enumerate() {
1416                writeln!(f, "report {k} control {c} {} saved +{} len {}", self.place(control.reference), control.saved, control.len)?;
1417            }
1418            for (g, group) in r.groups.iter().enumerate() {
1419                let name = group.name.as_deref().map_or_else(|| "-".to_owned(), word);
1420                let mut text = format!("report {k} group {g} {name} {} level {}", group_kind(group.kind), group.level);
1421                if let Some(n) = group.next_group {
1422                    text += &format!(" next-group {}", next_group(n));
1423                }
1424                if let Some(i) = group.indicate {
1425                    text += &format!(" indicate +{i}");
1426                }
1427                if let Some(r) = group.declarative {
1428                    text += &format!(" declarative {}", self.range(r));
1429                }
1430                if !group.totals.is_empty() {
1431                    text += &format!(" totals ({})", join(group.totals.iter().map(|t| format!("sum{t}")), " "));
1432                }
1433                writeln!(f, "{text}")?;
1434                for (l, line) in group.lines.iter().enumerate() {
1435                    writeln!(f, "report {k} group {g} line {l} {}", line_number(line.number))?;
1436                    for field in &line.fields {
1437                        writeln!(f, "report {k} group {g} line {l} {}", self.report_field(field))?;
1438                    }
1439                }
1440                for field in &group.unprinted {
1441                    writeln!(f, "report {k} group {g} unprinted {}", self.report_field(field))?;
1442                }
1443                for (key, list) in [("cross", &group.cross), ("roll", &group.rolls)] {
1444                    for (sum, origin) in list {
1445                        writeln!(f, "report {k} group {g} {key} sum{sum} <- {}", self.origin(origin))?;
1446                    }
1447                }
1448            }
1449            for (s, sum) in r.sums.iter().enumerate() {
1450                let reset = sum.reset.map_or_else(String::new, |l| format!(" reset on control {l}"));
1451                writeln!(f, "report {k} sum{s} {}{reset}", self.place_at(sum.total))?;
1452            }
1453            for t in &r.subtotals {
1454                let adding = match &t.adding {
1455                    Adding::EveryGenerate => "every generate".to_owned(),
1456                    Adding::Upon(groups) => format!("upon groups ({})", join(groups.iter().map(usize::to_string), " ")),
1457                    Adding::Correlated(groups) => format!("correlated groups ({})", join(groups.iter().map(usize::to_string), " ")),
1458                };
1459                writeln!(f, "report {k} subtotal sum{} <- {} {adding}", t.sum, self.comparand(&t.operand))?;
1460            }
1461        }
1462        Ok(())
1463    }
1464
1465    fn place_at(&self, place: usize) -> String {
1466        u32::try_from(place).map_or_else(|_| format!("p{place}?"), |p| self.place(p))
1467    }
1468
1469    fn report_field(&self, field: &Field) -> String {
1470        let content = match &field.content {
1471            FieldContent::Source(x) => format!("source {}", self.comparand(x)),
1472            FieldContent::Value(v) => format!("value {}", self.konst(*v)),
1473            FieldContent::Sum(s) => format!("sum{s}"),
1474            FieldContent::Program => "program".to_owned(),
1475        };
1476        let flags = [yes(field.group_indicate, "group indicate"), yes(field.blank_when_zero, "blank-when-zero"), yes(field.rounded, "rounded")];
1477        format!("column {} {} <- {content}{}  @{}", field.column, self.place_at(field.item), attrs('{', flags.into_iter().flatten().collect()), self.position(&field.pos))
1478    }
1479
1480    fn origin(&self, origin: &Origin) -> String {
1481        match origin {
1482            Origin::Source(x) => self.comparand(x),
1483            Origin::Value(v) => self.konst(*v),
1484            Origin::Total(s) => format!("sum{s}"),
1485        }
1486    }
1487
1488    fn markup_nodes(&self, f: &mut fmt::Formatter<'_>, k: usize, m: &Markup) -> fmt::Result {
1489        let rows: Vec<String> = match m {
1490            Markup::JsonGenerate(g) => g.nodes.iter().map(|n| self.json_node(n)).collect(),
1491            Markup::XmlGenerate(g) => g.nodes.iter().map(|n| self.xml_node(n)).collect(),
1492            Markup::XmlParse(_) => Vec::new(),
1493            Markup::JsonParse(p) => p.nodes.iter().map(|n| self.parse_node(n)).collect(),
1494        };
1495        for (n, row) in rows.iter().enumerate() {
1496            writeln!(f, "markup m{k} node {n} {row}")?;
1497        }
1498        Ok(())
1499    }
1500
1501    /// What every node of a markup tree has: where it is, its kind, and how often it occurs.
1502    fn node(&self, name: String, offset: u32, moved: &[Odo], len: u32, kind_of: &Kind, occurs: Option<&Count>) -> String {
1503        let mut text = format!("{name} +{offset} len {len} {}", kind(kind_of));
1504        if let Some(c) = occurs {
1505            text += &format!(" occurs {}", self.count(c));
1506        }
1507        for o in moved {
1508            text += &format!(" moved {}", self.odo(o));
1509        }
1510        text
1511    }
1512
1513    fn marker(&self, m: &Marker) -> String {
1514        match m {
1515            Marker::Byte(Some(b)) => format!("byte {}", hex("X", &[*b])),
1516            Marker::Byte(None) => "byte none".to_owned(),
1517            Marker::Condition(c) => format!("condition ({})", self.cond(*c)),
1518            Marker::Refused(a) => format!("refused {}", abend_ref(*a)),
1519        }
1520    }
1521
1522    fn convert(c: &Convert) -> String {
1523        match c {
1524            Convert::Chars { justified } => if *justified { "chars justified" } else { "chars" }.to_owned(),
1525            Convert::National => "national".to_owned(),
1526            Convert::Dbcs => "dbcs".to_owned(),
1527            Convert::Float(p) => format!("float {}", precision(*p)),
1528            Convert::Fixed { integers } => format!("fixed {integers} integers"),
1529            Convert::Scaled { integers, scaling } => format!("fixed {integers} integers scaled {scaling}"),
1530            Convert::Refused(a) => format!("refused {}", abend_ref(*a)),
1531        }
1532    }
1533
1534    fn suppress(list: &[Figurative]) -> String {
1535        if list.is_empty() { String::new() } else { format!(" suppress {}", join(list.iter().map(|f| figurative(*f).to_owned()), " ")) }
1536    }
1537
1538    fn json_node(&self, n: &JsonNode) -> String {
1539        let mut text = self.node(self.raw(n.name), n.offset, &n.moved, n.len, &n.kind, n.occurs.as_ref());
1540        if let Some((place, marker)) = &n.indicator {
1541            let place = place.map_or_else(|a| format!("refused {}", abend_ref(a)), |p| self.place(p));
1542            text += &format!(" indicator {place} {}", self.marker(marker));
1543        }
1544        if let Some(null) = n.null {
1545            text += &format!(" null {}", figurative(null));
1546        }
1547        text + &match &n.value {
1548            JsonValue::Object { members, eligible } => format!(" object ({}){}", members_list(members), if *eligible { " eligible" } else { "" }),
1549            JsonValue::Leaf(JsonLeaf { suppress, boolean, convert }) => {
1550                let boolean = boolean.as_ref().map_or_else(String::new, |m| format!(" boolean {}", self.marker(m)));
1551                format!(" leaf{}{boolean} {}", Self::suppress(suppress), Self::convert(convert))
1552            }
1553        }
1554    }
1555
1556    fn xml_node(&self, n: &XmlNode) -> String {
1557        let text = self.node(self.string(n.name), n.offset, &n.moved, n.len, &n.kind, n.occurs.as_ref());
1558        text + &match &n.value {
1559            XmlValue::Element { members } => format!(" element ({})", members_list(members)),
1560            XmlValue::Members { members } => format!(" members ({})", members_list(members)),
1561            XmlValue::Leaf { form, suppress, convert } => {
1562                let form = match form {
1563                    XmlForm::Attribute => "attribute",
1564                    XmlForm::Element => "element",
1565                    XmlForm::Content => "content",
1566                };
1567                format!(" leaf {form}{} {}", Self::suppress(suppress), Self::convert(convert))
1568            }
1569        }
1570    }
1571
1572    fn set_to(&self, s: &SetTo) -> String {
1573        match s {
1574            SetTo::Nothing => "nothing".to_owned(),
1575            SetTo::Move { place, value, plan } => format!("{} <- {}{}", self.place(*place), self.konst(*value), self.moved(plan, SenderCheck::None)),
1576            SetTo::Refused(a) => format!("refused {}", abend_ref(*a)),
1577        }
1578    }
1579
1580    fn flag(&self, flag: &Flag) -> String {
1581        match flag {
1582            Flag::Set { on, off } => format!("set on {} off {}", self.set_to(on), self.set_to(off)),
1583            Flag::Literals { on, off } => format!(
1584                "literals on {}{} off {}{}",
1585                self.konst(on.0),
1586                self.moved(&on.1, SenderCheck::None),
1587                self.konst(off.0),
1588                self.moved(&off.1, SenderCheck::None)
1589            ),
1590        }
1591    }
1592
1593    fn parse_node(&self, n: &ParseNode) -> String {
1594        let name = match n.name {
1595            Named::Exactly(s) => self.string(s),
1596            Named::Folded(s) => format!("folded {}", self.name(s)),
1597            Named::Omitted => "omitted".to_owned(),
1598        };
1599        let mut text = self.node(name, n.offset, &n.moved, n.len, &n.kind, n.occurs.as_ref());
1600        if n.ignored {
1601            text += " ignoring null";
1602        }
1603        if let Some(Indicator { place, flag }) = &n.indicator {
1604            let place = match place {
1605                None => "-".to_owned(),
1606                Some(Ok(p)) => self.place(*p),
1607                Some(Err(a)) => format!("refused {}", abend_ref(*a)),
1608            };
1609            text += &format!(" indicator {place} {}", self.flag(flag));
1610        }
1611        if let Some((null, plan)) = &n.null {
1612            text += &format!(" null {}{}", figurative(*null), self.moved(plan, SenderCheck::None));
1613        }
1614        text + &match &n.value {
1615            ParseValue::Object { members } => format!(" object ({})", members_list(members)),
1616            ParseValue::Suppressed => " suppressed".to_owned(),
1617            ParseValue::Leaf(ParseLeaf { boolean, text, number }) => {
1618                let mut leaf = " leaf".to_owned();
1619                if let Some(b) = boolean {
1620                    leaf += &format!(" boolean {}", self.flag(b));
1621                }
1622                if let Some(plan) = text {
1623                    leaf += &format!(" text{}", self.moved(plan, SenderCheck::None));
1624                }
1625                leaf + &match number {
1626                    NumberInto::Float(plan) => format!(" number float{}", self.moved(plan, SenderCheck::None)),
1627                    NumberInto::Store(store) => format!(" number [{}]", self.store(store)),
1628                    NumberInto::Edited(plan) => format!(" number edited{}", self.moved(plan, SenderCheck::None)),
1629                    NumberInto::Digits => " number digits".to_owned(),
1630                    NumberInto::Incompatible => " number incompatible".to_owned(),
1631                    NumberInto::StoreScaled { store, scaling } => format!(" number [{}] scaled {scaling}", self.store(store)),
1632                    NumberInto::EditedScaled { plan, scaling } => format!(" number edited{} scaled {scaling}", self.moved(plan, SenderCheck::None)),
1633                }
1634            }
1635        }
1636    }
1637
1638    /// A class's data and methods are programs of their own, each printed after its heading.
1639    fn class(&self, f: &mut fmt::Formatter<'_>, class: &Class) -> fmt::Result {
1640        writeln!(f, "class {} inherits {}", self.string(class.external), self.string(class.parent))?;
1641        for (key, part) in [("factory", &class.factory), ("object", &class.object)] {
1642            if let Some(part) = part {
1643                writeln!(f, "class {key} data records ({})", join(part.records.iter().map(|r| format!("+{r}")), " "))?;
1644                write!(f, "{}", Listing::of(&part.data))?;
1645            }
1646        }
1647        for (k, m) in class.methods.iter().enumerate() {
1648            let params = join(m.params.iter().map(|&s| self.string(s)), " ");
1649            let returns = m.returns.map_or_else(String::new, |s| format!(" returns {}", self.string(s)));
1650            let factory = if m.factory { " factory" } else { "" };
1651            writeln!(f, "class method {k} {}{factory} params ({params}){returns} own-records {}", self.name(m.name), m.own_records)?;
1652            write!(f, "{}", Listing::of(&m.code))?;
1653        }
1654        Ok(())
1655    }
1656}
1657
1658/// Maps a CICS command's handles to their text.
1659struct Names<'p, 'a>(&'p Printer<'a>);
1660
1661impl Handles<PlaceId, Operand, SymId> for Names<'_, '_> {
1662    type Place = String;
1663    type Value = String;
1664    type Text = String;
1665    type Error = Infallible;
1666
1667    fn place(&mut self, place: PlaceId) -> Result<String, Infallible> {
1668        Ok(self.0.place(place))
1669    }
1670
1671    fn value(&mut self, value: Operand) -> Result<String, Infallible> {
1672        Ok(self.0.operand(&value))
1673    }
1674
1675    fn text(&mut self, text: SymId) -> Result<String, Infallible> {
1676        Ok(self.0.name(text))
1677    }
1678}
1679
1680type CicsOpt = Option<Datum<String, String, String>>;
1681
1682fn option(words: &mut Vec<String>, key: &str, value: &CicsOpt) {
1683    match value {
1684        None => {}
1685        Some(Datum::Place(v) | Datum::Value(v) | Datum::Text(v)) => words.push(format!("{key}({v})")),
1686        Some(Datum::Bare) => words.push(key.to_owned()),
1687    }
1688}
1689
1690fn flag(words: &mut Vec<String>, key: &str, on: bool) {
1691    if on {
1692        words.push(key.to_owned());
1693    }
1694}
1695
1696impl Printer<'_> {
1697    /// A command's options as EXEC CICS writes them, in the order of its fields.
1698    fn cics_options(&self, command: &Cics<String, String, String>, w: &mut Vec<String>) {
1699        match command {
1700            Cics::File { verb: _, file, options: o } => {
1701                option(w, "FILE", file);
1702                for (key, value) in [("RIDFLD", &o.ridfld), ("KEYLENGTH", &o.keylength), ("REQID", &o.reqid), ("FROM", &o.from), ("NUMREC", &o.numrec)] {
1703                    option(w, key, value);
1704                }
1705                for (key, value) in [("INTO", &o.record.into), ("SET", &o.record.set), ("LENGTH", &o.record.length)] {
1706                    option(w, key, value);
1707                }
1708                for (key, on) in [("GENERIC", o.generic), ("RRN", o.rrn), ("GTEQ", o.gteq), ("EQUAL", o.equal), ("UPDATE", o.update)] {
1709                    flag(w, key, on);
1710                }
1711            }
1712            Cics::Return { transid, commarea, length, channel, immediate } => {
1713                for (key, value) in [("TRANSID", transid), ("COMMAREA", commarea), ("LENGTH", length), ("CHANNEL", channel)] {
1714                    option(w, key, value);
1715                }
1716                flag(w, "IMMEDIATE", *immediate);
1717            }
1718            Cics::Link(t) | Cics::Xctl(t) => {
1719                for (key, value) in [("PROGRAM", &t.program), ("COMMAREA", &t.commarea), ("LENGTH", &t.length)] {
1720                    option(w, key, value);
1721                }
1722            }
1723            Cics::Abend { abcode, cancel } => {
1724                option(w, "ABCODE", abcode);
1725                flag(w, "CANCEL", *cancel);
1726            }
1727            Cics::HandleCondition(labels) => {
1728                w.extend(labels.iter().map(|(c, label)| label.map_or_else(|| c.name().to_owned(), |p| format!("{}({})", c.name(), self.para(p)))));
1729            }
1730            Cics::IgnoreCondition(conditions) => w.extend(conditions.iter().map(|c| c.name().to_owned())),
1731            Cics::PushHandle | Cics::PopHandle | Cics::HandleAid | Cics::Freemain | Cics::Enq | Cics::Deq | Cics::Delay | Cics::Unsupported => {}
1732            Cics::HandleAbend { program, label, reset } => {
1733                option(w, "PROGRAM", program);
1734                if let Some(p) = label {
1735                    w.push(format!("LABEL({})", self.para(*p)));
1736                }
1737                flag(w, "RESET", *reset);
1738            }
1739            Cics::SendMap { map, mapset, from, maponly, dataonly, cursor, control } => {
1740                for (key, value) in [("MAP", map), ("MAPSET", mapset), ("FROM", from), ("CURSOR", cursor)] {
1741                    option(w, key, value);
1742                }
1743                flag(w, "MAPONLY", *maponly);
1744                flag(w, "DATAONLY", *dataonly);
1745                control_flags(w, control);
1746            }
1747            Cics::ReceiveMap { map, mapset, into, set } => {
1748                for (key, value) in [("MAP", map), ("MAPSET", mapset), ("INTO", into), ("SET", set)] {
1749                    option(w, key, value);
1750                }
1751            }
1752            Cics::SendControl { cursor, control } => {
1753                option(w, "CURSOR", cursor);
1754                control_flags(w, control);
1755            }
1756            Cics::Receive(r) => {
1757                for (key, value) in [("INTO", &r.into), ("SET", &r.set), ("LENGTH", &r.length)] {
1758                    option(w, key, value);
1759                }
1760            }
1761            Cics::Asktime { abstime } => option(w, "ABSTIME", abstime),
1762            Cics::Formattime { abstime, datesep, timesep, outputs } => {
1763                for (key, value) in [("ABSTIME", abstime), ("DATESEP", datesep), ("TIMESEP", timesep)] {
1764                    option(w, key, value);
1765                }
1766                for (key, value) in outputs {
1767                    option(w, key, &Some(value.clone()));
1768                }
1769            }
1770            Cics::Assign(a) => {
1771                let options = [
1772                    ("APPLID", &a.applid),
1773                    ("SYSID", &a.sysid),
1774                    ("USERID", &a.userid),
1775                    ("NETNAME", &a.netname),
1776                    ("FACILITY", &a.facility),
1777                    ("STARTCODE", &a.startcode),
1778                    ("ABCODE", &a.abcode),
1779                    ("PROGRAM", &a.program),
1780                    ("CWALENG", &a.cwaleng),
1781                    ("TWALENG", &a.twaleng),
1782                ];
1783                for (key, value) in options {
1784                    option(w, key, value);
1785                }
1786            }
1787            Cics::Getmain { flength, length, initimg, set } => {
1788                for (key, value) in [("FLENGTH", flength), ("LENGTH", length), ("INITIMG", initimg), ("SET", set)] {
1789                    option(w, key, value);
1790                }
1791            }
1792            Cics::Syncpoint { rollback } => flag(w, "ROLLBACK", *rollback),
1793            Cics::Address { eib, commarea, cwa, twa } => {
1794                for (key, value) in [("EIB", eib), ("COMMAREA", commarea), ("CWA", cwa), ("TWA", twa)] {
1795                    option(w, key, value);
1796                }
1797            }
1798            Cics::SendText { from, length } => {
1799                option(w, "FROM", from);
1800                option(w, "LENGTH", length);
1801            }
1802            Cics::WriteOperator { text, textlength } => {
1803                option(w, "TEXT", text);
1804                option(w, "TEXTLENGTH", textlength);
1805            }
1806            Cics::WriteqTs { queue, from, length, rewrite, item, numitems } => {
1807                for (key, value) in [("QUEUE", queue), ("FROM", from), ("LENGTH", length), ("ITEM", item), ("NUMITEMS", numitems)] {
1808                    option(w, key, value);
1809                }
1810                flag(w, "REWRITE", *rewrite);
1811            }
1812            Cics::ReadqTs { queue, next, item, numitems, record } => {
1813                option(w, "QUEUE", queue);
1814                flag(w, "NEXT", *next);
1815                for (key, value) in [("ITEM", item), ("NUMITEMS", numitems), ("INTO", &record.into), ("SET", &record.set), ("LENGTH", &record.length)] {
1816                    option(w, key, value);
1817                }
1818            }
1819            Cics::DeleteqTs { queue } | Cics::DeleteqTd { queue } => option(w, "QUEUE", queue),
1820            Cics::WriteqTd { queue, from, length } => {
1821                for (key, value) in [("QUEUE", queue), ("FROM", from), ("LENGTH", length)] {
1822                    option(w, key, value);
1823                }
1824            }
1825            Cics::ReadqTd { queue, record } => {
1826                for (key, value) in [("QUEUE", queue), ("INTO", &record.into), ("SET", &record.set), ("LENGTH", &record.length)] {
1827                    option(w, key, value);
1828                }
1829            }
1830            Cics::Refused(why) => w.push(format!("{why:?}")),
1831        }
1832    }
1833}
1834
1835fn control_flags(w: &mut Vec<String>, c: &crate::cics::Control) {
1836    for (key, on) in [("ERASE", c.erase), ("FREEKB", c.freekb), ("ALARM", c.alarm), ("FRSET", c.frset)] {
1837        flag(w, key, on);
1838    }
1839}
1840
1841/// A name as it is, or quoted and escaped when it is not one word of printable characters.
1842fn word(s: &str) -> String {
1843    if !s.is_empty() && s.chars().all(|c| !c.is_control() && !c.is_whitespace()) { s.to_owned() } else { format!("{s:?}") }
1844}
1845
1846/// A literal in quotes, a quote doubled within it, as COBOL writes one; escaped when it holds a
1847/// control character.
1848fn quote(s: &str) -> String {
1849    if s.chars().any(char::is_control) { format!("{s:?}") } else { format!("'{}'", s.replace('\'', "''")) }
1850}
1851
1852fn hex(prefix: &str, bytes: &[u8]) -> String {
1853    format!("{prefix}'{}'", join(bytes.iter().map(|b| format!("{b:02X}")), ""))
1854}
1855
1856/// UTF-16 units, big-endian, as N'...'; NX'...' when they are not printable text.
1857fn national(units: &[u8]) -> String {
1858    let (pairs, odd) = units.as_chunks::<2>();
1859    let text = odd
1860        .is_empty()
1861        .then(|| char::decode_utf16(pairs.iter().map(|&u| u16::from_be_bytes(u))).collect::<Result<String, _>>().ok())
1862        .flatten()
1863        .filter(|t| !t.chars().any(char::is_control));
1864    text.map_or_else(|| hex("NX", units), |t| format!("N{}", quote(&t)))
1865}
1866
1867/// A number with all its decimal places.
1868fn decimal(n: &Fixed) -> String {
1869    const MOST: u32 = 100;
1870    if n.places.dec > MOST {
1871        return format!("{:?}", n.magnitude);
1872    }
1873    let mut digits = Vec::new();
1874    let mut rest = n.magnitude;
1875    while !rest.is_zero() {
1876        let (q, r) = rest.div_rem(U256::from_u128(10));
1877        digits.push(char::from(b'0' + r.to_u128().and_then(|d| u8::try_from(d).ok()).unwrap_or(0)));
1878        rest = q;
1879    }
1880    let dec = n.places.dec as usize;
1881    while digits.len() <= dec {
1882        digits.push('0');
1883    }
1884    digits.reverse();
1885    let (int, frac): (String, String) = (digits[..digits.len() - dec].iter().collect(), digits[digits.len() - dec..].iter().collect());
1886    let sign = if n.negative { "-" } else { "" };
1887    if frac.is_empty() { format!("{sign}{int}") } else { format!("{sign}{int}.{frac}") }
1888}
1889
1890fn pic(digits: u32, scale: u32, signed: bool) -> String {
1891    let int = digits.saturating_sub(scale);
1892    let mut text = if signed { "S".to_owned() } else { String::new() };
1893    if int > 0 || scale == 0 {
1894        text += &format!("9({int})");
1895    }
1896    if scale > 0 {
1897        text += &format!("V9({scale})");
1898    }
1899    text
1900}
1901
1902fn sign_clause(sign: Option<SignClause>) -> String {
1903    sign.map_or_else(String::new, |s| {
1904        let position = match s.position {
1905            SignPosition::Leading => "leading",
1906            SignPosition::Trailing => "trailing",
1907        };
1908        format!(" sign {position}{}", if s.separate { " separate" } else { "" })
1909    })
1910}
1911
1912fn kind(k: &Kind) -> String {
1913    match *k {
1914        Kind::Group => "group".to_owned(),
1915        Kind::Alnum { justified } => if justified { "alnum justified" } else { "alnum" }.to_owned(),
1916        Kind::National => "national".to_owned(),
1917        Kind::Dbcs { justified, edit } => format!("dbcs{}{}", if justified { " justified" } else { "" }, edit.map_or(String::new(), |e| format!(" edit {e}"))),
1918        Kind::Zoned { digits, scale, signed, sign } => format!("zoned {}{}", pic(digits, scale, signed), sign_clause(sign)),
1919        Kind::Packed { digits, scale, signed } => format!("packed {}", pic(digits, scale, signed)),
1920        Kind::Binary { digits, scale, signed, native } => format!("binary {}{}", pic(digits, scale, signed), if native { " native" } else { "" }),
1921        Kind::Float(p) => format!("float {}", precision(p)),
1922        Kind::NumericEdited { edit, digits, scale, blank_when_zero } => {
1923            format!("numeric-edited edit {edit} {}{}", pic(digits, scale, false), if blank_when_zero { " blank-when-zero" } else { "" })
1924        }
1925        Kind::AlnumEdited { edit } => format!("alnum-edited edit {edit}"),
1926        Kind::Pointer => "pointer".to_owned(),
1927        Kind::Index => "index".to_owned(),
1928        Kind::ObjectReference => "object-reference".to_owned(),
1929        Kind::ProgramPointer => "program-pointer".to_owned(),
1930    }
1931}
1932
1933fn base(b: Base) -> String {
1934    match b {
1935        Base::Program => "program".to_owned(),
1936        Base::Local => "local".to_owned(),
1937        Base::Linkage(n) => format!("linkage{n}"),
1938        Base::ReturnCode => "return-code".to_owned(),
1939        Base::Eib => "eib".to_owned(),
1940        Base::SelfRef => "self".to_owned(),
1941        Base::JniEnv => "jnienv".to_owned(),
1942        Base::Xml(r) => match r {
1943            XmlRegister::Text => "xml-text",
1944            XmlRegister::NText => "xml-ntext",
1945            XmlRegister::Namespace => "xml-namespace",
1946            XmlRegister::NNamespace => "xml-nnamespace",
1947            XmlRegister::Prefix => "xml-namespace-prefix",
1948            XmlRegister::NPrefix => "xml-nnamespace-prefix",
1949        }
1950        .to_owned(),
1951    }
1952}
1953
1954fn precision(p: Precision) -> &'static str {
1955    match p {
1956        Precision::Short => "short",
1957        Precision::Long => "long",
1958        Precision::Extended => "extended",
1959    }
1960}
1961
1962fn figurative(f: Figurative) -> &'static str {
1963    match f {
1964        Figurative::Zero => "ZERO",
1965        Figurative::Space => "SPACE",
1966        Figurative::HighValue => "HIGH-VALUE",
1967        Figurative::LowValue => "LOW-VALUE",
1968        Figurative::Quote => "QUOTE",
1969        Figurative::Null => "NULL",
1970    }
1971}
1972
1973fn binop(op: BinOp) -> &'static str {
1974    match op {
1975        BinOp::Add => "+",
1976        BinOp::Sub => "-",
1977        BinOp::Mul => "*",
1978        BinOp::Div => "/",
1979        BinOp::Pow => "**",
1980    }
1981}
1982
1983fn relop(op: RelOp) -> &'static str {
1984    match op {
1985        RelOp::Eq => "=",
1986        RelOp::Ne => "<>",
1987        RelOp::Lt => "<",
1988        RelOp::Le => "<=",
1989        RelOp::Gt => ">",
1990        RelOp::Ge => ">=",
1991    }
1992}
1993
1994fn compare(c: Compare) -> String {
1995    match c {
1996        Compare::PackedPfd => "packed-pfd".to_owned(),
1997        Compare::Address => "address".to_owned(),
1998        Compare::Float => "float".to_owned(),
1999        Compare::Fixed => "fixed".to_owned(),
2000        Compare::National => "national".to_owned(),
2001        Compare::Dbcs => "dbcs".to_owned(),
2002        Compare::Alphanumeric => "alnum".to_owned(),
2003        Compare::Refused(a) => format!("refused {}", abend_ref(a)),
2004        Compare::References => "references".to_owned(),
2005        Compare::ZonedBytes { zoned_first } => format!("zoned-bytes {}", if zoned_first { "left" } else { "right" }),
2006    }
2007}
2008
2009fn byte_class(c: super::ByteClass) -> &'static str {
2010    use super::ByteClass;
2011    match c {
2012        ByteClass::Packed { signed: true } => "numeric [packed signed]",
2013        ByteClass::Packed { signed: false } => "numeric [packed]",
2014        ByteClass::Zoned { signed: true } => "numeric [zoned signed]",
2015        ByteClass::Zoned { signed: false } => "numeric [zoned]",
2016        ByteClass::Digits => "numeric [digits]",
2017        ByteClass::Alphabetic => "alphabetic",
2018        ByteClass::AlphabeticLower => "alphabetic-lower",
2019        ByteClass::AlphabeticUpper => "alphabetic-upper",
2020        ByteClass::Dbcs => "dbcs",
2021        ByteClass::Kanji => "kanji",
2022    }
2023}
2024
2025fn sign_test(t: SignTest) -> &'static str {
2026    match t {
2027        SignTest::Positive => "positive",
2028        SignTest::Negative => "negative",
2029        SignTest::Zero => "zero",
2030    }
2031}
2032
2033fn sql_test(t: SqlTest) -> &'static str {
2034    match t {
2035        SqlTest::Error => "error",
2036        SqlTest::NotFound => "not found",
2037        SqlTest::Warning => "warning",
2038    }
2039}
2040
2041fn ending(e: Ending) -> &'static str {
2042    match e {
2043        Ending::Goback => "goback",
2044        Ending::StopRun => "stop-run",
2045        Ending::EndOfProgram => "end-of-program",
2046    }
2047}
2048
2049fn range_kind(k: RangeKind) -> &'static str {
2050    match k {
2051        RangeKind::Perform => "perform",
2052        RangeKind::SortProcedure => "sort-procedure",
2053        RangeKind::UseBeforeReporting => "use-before-reporting",
2054        RangeKind::UseProcedure => "use-procedure",
2055        RangeKind::Debugging => "debugging",
2056        RangeKind::Processing => "processing",
2057    }
2058}
2059
2060fn image(i: Image) -> String {
2061    match i {
2062        Image::Bytes => String::new(),
2063        Image::All => " all".to_owned(),
2064        Image::Figurative => " figurative".to_owned(),
2065        Image::Digits { digits } => format!(" digits {digits}"),
2066        Image::Stored => " stored".to_owned(),
2067    }
2068}
2069
2070fn national_from(n: NationalFrom) -> &'static str {
2071    match n {
2072        NationalFrom::Units => "units",
2073        NationalFrom::Decoded => "decoded",
2074        NationalFrom::Figurative => "figurative",
2075        NationalFrom::Dbcs => "dbcs",
2076    }
2077}
2078
2079fn numeric_from(n: NumericFrom) -> String {
2080    match n {
2081        NumericFrom::Value => "value".to_owned(),
2082        NumericFrom::PackedCopy => "packed-copy".to_owned(),
2083        NumericFrom::Float => "float".to_owned(),
2084        NumericFrom::Zero => "zero".to_owned(),
2085        NumericFrom::Fill => "fill".to_owned(),
2086        NumericFrom::Zoned => "zoned".to_owned(),
2087        NumericFrom::DeEdit { edit, digits, scale } => format!("de-edit edit {edit} {}", pic(digits, scale, false)),
2088    }
2089}
2090
2091fn float_from(f: FloatFrom) -> &'static str {
2092    match f {
2093        FloatFrom::Float => "float",
2094        FloatFrom::Fixed => "fixed",
2095        FloatFrom::Zero => "zero",
2096    }
2097}
2098
2099fn accept_from(a: AcceptFrom) -> &'static str {
2100    match a {
2101        AcceptFrom::Sysin => "SYSIN",
2102        AcceptFrom::Date { four_digit_year: false } => "DATE",
2103        AcceptFrom::Date { four_digit_year: true } => "DATE YYYYMMDD",
2104        AcceptFrom::Day { four_digit_year: false } => "DAY",
2105        AcceptFrom::Day { four_digit_year: true } => "DAY YYYYDDD",
2106        AcceptFrom::DayOfWeek => "DAY-OF-WEEK",
2107        AcceptFrom::Time => "TIME",
2108    }
2109}
2110
2111fn open_mode(m: OpenMode) -> &'static str {
2112    match m {
2113        OpenMode::Input => "INPUT",
2114        OpenMode::Output => "OUTPUT",
2115        OpenMode::Extend => "EXTEND",
2116        OpenMode::InputOutput => "I-O",
2117    }
2118}
2119
2120fn closing_text(c: Closing) -> &'static str {
2121    match c {
2122        Closing::Volume => "volume",
2123        Closing::NoRewind => "no rewind",
2124        Closing::Lock => "lock",
2125    }
2126}
2127
2128fn phrase_words(p: Option<Phrase>, on: &str, not_on: &str) -> Vec<String> {
2129    p.map_or_else(Vec::new, |p| [yes(p.on, on), yes(p.not_on, not_on)].into_iter().flatten().collect())
2130}
2131
2132fn host_type(t: &HostType) -> String {
2133    let unsigned = |signed: bool| if signed { "" } else { " unsigned" };
2134    match t {
2135        HostType::SmallInt { signed } => format!("smallint{}", unsigned(*signed)),
2136        HostType::Integer { signed } => format!("integer{}", unsigned(*signed)),
2137        HostType::BigInt { signed } => format!("bigint{}", unsigned(*signed)),
2138        HostType::Decimal { digits, scale, signed } => format!("decimal({digits},{scale}){}", unsigned(*signed)),
2139        HostType::Zoned { digits, scale, signed, sign } => format!("zoned({digits},{scale}){}{}", unsigned(*signed), sign_clause(*sign)),
2140        HostType::Real => "real".to_owned(),
2141        HostType::Double => "double".to_owned(),
2142        HostType::Char(n) => format!("char({n})"),
2143        HostType::VarChar(n) => format!("varchar({n})"),
2144        HostType::Graphic(n) => format!("graphic({n})"),
2145        HostType::VarGraphic(n) => format!("vargraphic({n})"),
2146        HostType::Structure(members) => format!("structure of {}", members.len()),
2147    }
2148}
2149
2150fn sqlca_field(f: SqlcaField) -> String {
2151    match f {
2152        SqlcaField::CaId => "SQLCAID".to_owned(),
2153        SqlcaField::CaBc => "SQLCABC".to_owned(),
2154        SqlcaField::Code => "SQLCODE".to_owned(),
2155        SqlcaField::ErrMl => "SQLERRML".to_owned(),
2156        SqlcaField::ErrMc => "SQLERRMC".to_owned(),
2157        SqlcaField::ErrP => "SQLERRP".to_owned(),
2158        SqlcaField::State => "SQLSTATE".to_owned(),
2159        SqlcaField::ErrD(n) => format!("SQLERRD({n})"),
2160        SqlcaField::Warn(n) => format!("SQLWARN{}", if n == 10 { "A".to_owned() } else { n.to_string() }),
2161    }
2162}
2163
2164fn scope_section(s: Section) -> &'static str {
2165    match s {
2166        Section::WorkingStorage => "working-storage",
2167        Section::LocalStorage => "local-storage",
2168        Section::Linkage => "linkage",
2169        Section::File => "file",
2170    }
2171}
2172
2173fn group_kind(k: GroupKind) -> &'static str {
2174    match k {
2175        GroupKind::ReportHeading => "report-heading",
2176        GroupKind::PageHeading => "page-heading",
2177        GroupKind::ControlHeading => "control-heading",
2178        GroupKind::Detail => "detail",
2179        GroupKind::ControlFooting => "control-footing",
2180        GroupKind::PageFooting => "page-footing",
2181        GroupKind::ReportFooting => "report-footing",
2182    }
2183}
2184
2185fn next_group(n: NextGroup) -> String {
2186    match n {
2187        NextGroup::Line(l) => format!("line {l}"),
2188        NextGroup::Plus(l) => format!("plus {l}"),
2189        NextGroup::NextPage => "next page".to_owned(),
2190    }
2191}
2192
2193fn line_number(n: LineNumber) -> String {
2194    match n {
2195        LineNumber::Line(l) => format!("at {l}"),
2196        LineNumber::Plus(l) => format!("plus {l}"),
2197        LineNumber::NextPage(None) => "next page".to_owned(),
2198        LineNumber::NextPage(Some(l)) => format!("next page at {l}"),
2199    }
2200}
2201
2202fn members_list(members: &[u32]) -> String {
2203    join(members.iter().map(u32::to_string), " ")
2204}
2205
2206/// The open modes `Declaratives.modes` and `Scope.global_modes` are in.
2207const MODES: [&str; 4] = ["input", "output", "i-o", "extend"];
2208
2209#[cfg(test)]
2210mod tests {
2211    use super::*;
2212    use numeric::precision::Places;
2213
2214    #[test]
2215    fn a_number_prints_with_every_decimal_place_it_holds() {
2216        let n = |value: i128, int: u32, dec: u32| decimal(&Fixed::new(value, Places::new(int, dec)));
2217        assert_eq!([n(125, 3, 1), n(-5, 1, 2), n(0, 1, 0), n(1250, 3, 2), n(7, 1, 0)], ["12.5", "-0.05", "0", "12.50", "7"]);
2218    }
2219
2220    #[test]
2221    fn text_prints_on_one_line_whatever_it_holds() {
2222        assert_eq!(quote("IT'S"), "'IT''S'");
2223        assert_eq!(quote("A\nB"), "\"A\\nB\"");
2224        assert_eq!(word("WS-A"), "WS-A");
2225        assert_eq!(word("A B"), "\"A B\"");
2226        assert_eq!(national(&[0, 0x41, 0, 0x42]), "N'AB'");
2227        assert_eq!(national(&[0xD8, 0x00]), "NX'D800'");
2228        assert_eq!(pic(9, 2, true), "S9(7)V9(2)");
2229        assert_eq!(pic(2, 2, false), "V9(2)");
2230        assert_eq!(pic(4, 0, false), "9(4)");
2231    }
2232
2233    #[test]
2234    fn tables_that_refer_to_each_other_in_a_cycle_print_and_stop() {
2235        let (plans, services) = (Plans::default(), Services::default());
2236        let exprs = [Expr::Bin(0, BinOp::Add, 0)];
2237        let conds = [Cond::Rel { a: Comparand::Expr { expr: 0, dmax: 0, mode: Mode::Fixed, prepass: Vec::new() }, op: RelOp::Eq, b: Comparand::Operand(Operand::Const(9)), how: Compare::Fixed }];
2238        let blocks = [Block { ops: vec![Op::Display(4)], end: Terminator::Branch { cond: 0, then: 0, otherwise: 1 } }];
2239        let code = Code {
2240            id: 0,
2241            initial: false,
2242            recursive: false,
2243            paragraphs: &[],
2244            procedure_start: 0,
2245            ranges: &[],
2246            blocks: &blocks,
2247            places: &[],
2248            exprs: &exprs,
2249            conds: &conds,
2250            consts: &[],
2251            plans: &plans,
2252            services: &services,
2253            abends: &[],
2254            symbols: &[],
2255        };
2256        let text = Listing { code, items: &[], debug: None, sql: &[], ccsid: None }.to_string();
2257        let lines: Vec<&str> = text.lines().collect();
2258        assert_eq!(lines[..3], ["program symbol0?", "b0:", "    Display display4?"]);
2259        assert!(lines[3].starts_with("    Branch (((((") && lines[3].contains("...") && lines[3].ends_with(" = c9? [fixed]) b0 else b1"), "{text}");
2260    }
2261}