1use super::*;
5use crate::collating::Sequence;
6use crate::picture::Sym;
7use crate::files::{Dd, Keying, Open};
8use crate::printer::{self, Space};
9use rt::fileio::{self, Advance, Files};
10use rt::host::{Host, Values};
11use rt::lir::{self, Chars, Replacement};
12use rt::store::{LaxRedefinition, ProgramFacts};
13use rt::text;
14
15#[derive(Clone, Copy)]
16pub struct Facts<'p> {
17 layout: &'p Layout,
18 collating: &'p Sequence,
19 options: Options,
20 page: &'static CodePage,
21 decimal_point: char,
22}
23
24impl ProgramFacts for Facts<'_> {
25 fn options(&self) -> Options {
26 self.options
27 }
28
29 fn page(&self) -> &'static CodePage {
30 self.page
31 }
32
33 fn figurative(&self, f: Figurative) -> u8 {
34 self.collating.figurative(f)
35 }
36
37 fn collation(&self) -> &Collation {
38 self.collating.collation()
39 }
40
41 fn ordinal(&self, byte: u8) -> u16 {
42 self.collating.ordinal(byte)
43 }
44
45 fn character(&self, ordinal: i64) -> Option<u8> {
46 self.collating.character(ordinal)
47 }
48
49 fn characters(&self) -> usize {
50 self.collating.count()
51 }
52
53 fn decimal_point(&self) -> char {
54 self.decimal_point
55 }
56
57 fn edit(&self, edit: u32) -> (&[Sym], &str) {
58 (&self.layout.edits[edit as usize], &self.layout.currencies[edit as usize])
59 }
60
61 fn scaling(&self, item: usize) -> u32 {
62 self.layout.items.get(item).map_or(0, |i| i.scaling)
63 }
64
65 fn item_name(&self, item: usize) -> String {
67 self.layout.items.get(item).map_or("RETURN-CODE".into(), |i| i.name.clone().unwrap_or_else(|| "FILLER".into()))
68 }
69
70 fn lax_redefinition(&self, item: usize) -> Option<LaxRedefinition> {
71 self.layout.numcheck.lax(item)
72 }
73
74 fn numcheck_removed(&self, item: usize, pos: Pos) -> bool {
75 self.layout.numcheck.removed(item, pos)
76 }
77}
78
79impl<'p> Machine<'p, '_, '_> {
80 pub(super) fn facts(&self) -> Facts<'p> {
82 Facts { layout: self.layout, collating: self.collating, options: self.options, page: self.page, decimal_point: self.decimal_point() }
83 }
84
85 pub(super) fn inspect_phrase<'a>(&self, p: &'a InspectPhrase) -> text::InspectPhrase<&'a Ref, &'a Operand> {
87 let by = p.by.as_ref().map(|op| match op {
88 Operand::Literal(Literal::Figurative(f)) => Replacement::Fill(self.collating.figurative(*f)),
89 op => Replacement::Chars(chars(op)),
90 });
91 text::InspectPhrase { mode: p.mode, pattern: p.pattern.as_ref().map(chars), by, counter: p.counter.as_ref(), bounds: bounds(&p.bounds) }
92 }
93}
94
95pub(super) fn chars(op: &Operand) -> Chars<&Ref, &Operand> {
97 match op {
98 Operand::Ref(r) => Chars::Place(r),
99 other => Chars::Value(other),
100 }
101}
102
103pub(super) fn bounds(bounds: &[Bound]) -> Vec<lir::Bound<&Ref, &Operand>> {
104 bounds.iter().map(|b| lir::Bound { after: b.after, value: chars(&b.value) }).collect()
105}
106
107impl<'a, 'p> Host<&'a Ref> for Machine<'p, '_, '_> {
108 type Facts = Facts<'p>;
109
110 fn facts(&self) -> Facts<'p> {
111 Machine::facts(self)
112 }
113
114 fn mem(&mut self) -> &mut [u8] {
115 &mut self.unit.mem
116 }
117
118 fn locate(&mut self, place: &'a Ref, receiving: bool) -> R<Loc> {
119 self.locate_as(place, receiving)
120 }
121
122 fn integer(&mut self, place: &'a Ref, pos: Pos) -> R<i64> {
123 Machine::integer(self, &Expr::Operand(Operand::Ref(place.clone())), pos)
124 }
125
126 fn assign(&mut self, dest: Loc, val: Val, src: Option<Loc>, pos: Pos) -> R<()> {
127 Machine::assign(self, dest, val, src, pos)
128 }
129
130 fn store_fixed(&mut self, dest: Loc, value: &Fixed, pos: Pos) -> R<()> {
131 Machine::store_fixed(self, dest, value, false, pos)
132 }
133}
134
135impl<'a> Values<&'a Ref, &'a Operand> for Machine<'_, '_, '_> {
136 fn value(&mut self, operand: &&'a Operand, pos: Pos) -> R<Val> {
137 self.operand(operand, pos)
138 }
139}
140
141#[derive(Clone, Copy)]
143pub(super) enum Int<'a> {
144 Linage(&'a LinageValue),
145 Count(&'a Expr),
146}
147
148impl<'p> Machine<'p, '_, '_> {
149 pub(super) fn file_desc(&self, k: usize) -> fileio::File<'p, &'p Ref, Int<'p>> {
151 let (program, layout) = (self.program, self.layout);
152 let decl = &program.files[k];
153 let counter = layout.linage_counters.get(k).copied().flatten().map(|i| {
154 let item = &layout.items[i];
155 Loc { offset: self.base + item.offset as usize, len: item.size as usize, kind: item.kind, item: i }
156 });
157 fileio::File {
158 index: k,
159 name: &decl.name,
160 assign: &decl.assign,
161 organization: match decl.organization {
162 Organization::Sequential => lir::Organization::Sequential,
163 Organization::LineSequential => lir::Organization::LineSequential,
164 Organization::Indexed => lir::Organization::Indexed,
165 Organization::Relative => lir::Organization::Relative,
166 },
167 access: match decl.access {
168 Access::Sequential => lir::Access::Sequential,
169 Access::Random => lir::Access::Random,
170 Access::Dynamic => lir::Access::Dynamic,
171 },
172 optional: decl.optional,
173 format: self.described_format(k),
174 status: decl.status.as_ref(),
175 relative: decl.relative_key.as_ref(),
176 linage: decl.linage.as_ref().map(|l| fileio::Linage {
177 lines: Int::Linage(&l.lines),
178 footing: l.footing.as_ref().map(Int::Linage),
179 top: l.top.as_ref().map(Int::Linage),
180 bottom: l.bottom.as_ref().map(Int::Linage),
181 counter,
182 }),
183 carriage: self.carriage[k].map(|c| lir::Carriage { machine: c.machine, reserved: c.reserved }),
184 area: self.area(k),
185 read_lengths: {
186 let (shortest, longest) = compile::read_lengths(decl, layout, k, self.options.vlr);
187 (shortest as usize, longest as usize)
188 },
189 depending: decl.record_depending.as_ref().map(|item| {
190 let (shortest, longest) = compile::varying_lengths(decl, layout, k);
191 fileio::Depending { item, lengths: (shortest as usize, longest as usize) }
192 }),
193 }
194 }
195}
196
197pub(super) fn spacing(space: Space) -> lir::Spacing {
198 match space {
199 Space::Lines(n) => lir::Spacing::Lines(n),
200 Space::Channel(c) => lir::Spacing::Channel(c),
201 Space::PageMode => lir::Spacing::PageMode,
202 }
203}
204
205pub(super) fn advance(a: &Advancing) -> Advance<'_, Int<'_>> {
206 match a {
207 Advancing::Lines { before, count } => Advance::Lines { before: *before, count: Int::Count(count) },
208 Advancing::Page { before } => Advance::Page { before: *before },
209 Advancing::Mnemonic { before, name, environment } => Advance::Mnemonic { before: *before, space: printer::mnemonic_space(environment).map(spacing), name, environment },
210 }
211}
212
213impl<'a> Files<&'a Ref, Int<'a>> for Machine<'_, '_, '_> {
214 fn slot(&mut self, k: usize) -> &mut Option<Open> {
215 self.unit.file(self.me, k)
216 }
217
218 fn locked(&mut self, k: usize) -> &mut bool {
219 self.unit.locked(self.me, k)
220 }
221
222 fn dd(&self, assign: &str) -> Option<Dd> {
223 self.unit.dds.get(assign)
224 }
225
226 fn notify(&mut self, event: Event<'_>) {
227 self.unit.notify(event);
228 }
229
230 fn int(&mut self, value: Int<'a>, pos: Pos) -> R<i64> {
231 match value {
232 Int::Linage(v) => self.linage_value(v, pos),
233 Int::Count(e) => self.integer(e, pos),
234 }
235 }
236
237 fn keying(&mut self, k: usize, pos: Pos) -> R<Keying> {
238 Machine::keying(self, k, pos)
239 }
240
241 fn key_value(&mut self, k: usize, keying: &Keying, key: &'a Ref, partial: bool, pos: Pos) -> R<(usize, Vec<u8>)> {
242 self.key_named(k, keying, key, partial, pos)
243 }
244}