1use crate::abend::{AbendCode, Ending, FileStatus, Signal};
4use crate::files::Format;
5use crate::module::ModuleError;
6use crate::module::codec::{Decode, Encode, Reader, Writer};
7use crate::picture::Sym;
8use crate::sql::HostType;
9use crate::storage::Kind;
10use crate::vocab::{AcceptFrom, BinOp, Closing, Figurative, InspectMode, OpenMode, Pos, RelOp, SignClause, SignPosition};
11use crate::{codec_enum, codec_struct};
12use numeric::precision::{Fixed, Places};
13use numeric::options::{Compile, FastsrtAdvPrint, Invdata, Stop, Warnings};
14use numeric::{
15 Arith, BinCheck, CicsReturnWarning, Currency, DispSign, Initcheck, IntDate, Nsymbol, Numcheck, Numproc, Options, Parmcheck, Qualify, Quote, SortKeys, Trunc,
16 TruncCheck, Vlr, VsamOpenFs, ZonCheck,
17};
18use zarch::check::ProgramCheck;
19use zarch::ebcdic::CodePage;
20use zarch::hfp::Precision;
21use zarch::wide::U256;
22
23codec_enum!(Kind {
24 Group = 0,
25 Alnum { justified } = 1,
26 National = 2,
27 Zoned { digits, scale, signed, sign } = 3,
28 Packed { digits, scale, signed } = 4,
29 Binary { digits, scale, signed, native } = 5,
30 Float(precision) = 6,
31 NumericEdited { edit, digits, scale, blank_when_zero } = 7,
32 AlnumEdited { edit } = 8,
33 Pointer = 9,
34 Index = 10,
35 ObjectReference = 11,
36 ProgramPointer = 12,
37});
38codec_struct!(SignClause { position, separate });
39codec_struct!(Pos { file, line, col });
40codec_enum!(SignPosition { Leading = 0, Trailing = 1 });
41codec_enum!(Figurative { Zero = 0, Space = 1, HighValue = 2, LowValue = 3, Quote = 4, Null = 5 });
42codec_enum!(BinOp { Add = 0, Sub = 1, Mul = 2, Div = 3, Pow = 4 });
43codec_enum!(RelOp { Eq = 0, Ne = 1, Lt = 2, Le = 3, Gt = 4, Ge = 5 });
44codec_enum!(AcceptFrom {
45 Sysin = 0,
46 Date { four_digit_year } = 1,
47 Day { four_digit_year } = 2,
48 DayOfWeek = 3,
49 Time = 4,
50});
51codec_enum!(InspectMode { Characters = 0, All = 1, Leading = 2, First = 3 });
52codec_enum!(OpenMode { Input = 0, Output = 1, Extend = 2, InputOutput = 3 });
53codec_enum!(Closing { Volume = 0, NoRewind = 1, Lock = 2 });
54codec_enum!(Format { Fixed = 0, Variable = 1, Text = 2 });
55codec_enum!(HostType {
56 SmallInt { signed } = 0,
57 Integer { signed } = 1,
58 BigInt { signed } = 2,
59 Decimal { digits, scale, signed } = 3,
60 Zoned { digits, scale, signed, sign } = 4,
61 Real = 5,
62 Double = 6,
63 Char(len) = 7,
64 VarChar(max) = 8,
65 Structure(members) = 9,
66});
67codec_enum!(Sym {
68 Nine = 0,
69 Z = 1,
70 Star = 2,
71 FloatLead(c) = 3,
72 Float(c) = 4,
73 Sign(c) = 5,
74 Currency = 6,
75 Cr = 7,
76 Db = 8,
77 Point = 9,
78 Implied = 10,
79 Insert(c) = 11,
80 Char = 12,
81});
82codec_enum!(Precision { Short = 0, Long = 1, Extended = 2 });
83codec_enum!(ProgramCheck {
84 Specification = 0,
85 Data = 1,
86 FixedPointOverflow = 2,
87 FixedPointDivide = 3,
88 DecimalOverflow = 4,
89 DecimalDivide = 5,
90 HfpExponentOverflow = 6,
91 HfpExponentUnderflow = 7,
92 HfpSignificance = 8,
93 HfpDivide = 9,
94});
95
96codec_enum!(Ending { Goback = 0, StopRun = 1, EndOfProgram = 2 });
97codec_enum!(AbendCode {
98 Check(check) = 0,
99 Protection = 1,
100 ModuleNotFound = 2,
101 Io(status) = 3,
102 Cics(code) = 4,
103 User(code) = 5,
104 Ironwork = 6,
105 Exec = 7,
106 Sql = 8,
107 SqlReplay = 9,
108 Java = 10,
109 Signal(signal) = 11,
110 TimeLimit = 12,
111});
112codec_enum!(Signal { StopRun = 0, GoBack = 1, SortStopped = 2, ClosedOutput = 3, DeclarativeExit = 4 });
113codec_enum!(FileStatus {
114 Success = 0,
115 SuccessDuplicate = 1,
116 SuccessWrongLength = 2,
117 SuccessOptional = 3,
118 AtEnd = 4,
119 RelativeKeyOverflow = 5,
120 SequenceError = 6,
121 DuplicateKey = 7,
122 NotFound = 8,
123 BoundaryViolation = 9,
124 PermanentError = 10,
125 FileNotFound = 11,
126 OpenModeUnsupported = 12,
127 AlreadyOpen = 13,
128 NotOpen = 14,
129 NoPriorRead = 15,
130 RecordLengthChanged = 16,
131 NoNextRecord = 17,
132 NotOpenInput = 18,
133 NotOpenOutput = 19,
134 NotOpenInputOutput = 20,
135 SuccessNonReel = 21,
136 ClosedWithLock = 22,
137 SuccessVerified = 23,
138});
139
140codec_struct!(Options {
141 arith, trunc, numproc, codepage, trunc_check, fastsrt, fastsrt_adv_print, sort_keys, adv, thread, dll, rent, dbcs,
142 warnings, compile, dynam, debug, cics_return_warning, invdata, zwb, quote, currency, nsymbol, dispsign, intdate, qualify, initial,
143 vlr, vsamopenfs, numcheck, parmcheck, initcheck, optimize,
144} check options_valid);
145codec_struct!(Invdata { forcenumcmp, cleansign });
146codec_enum!(Arith { Compat = 0, Extend = 1 });
147codec_enum!(Trunc { Std = 0, Opt = 1, Bin = 2 });
148codec_enum!(Numproc { Nopfd = 0, Pfd = 1 });
149codec_enum!(TruncCheck { Report = 0, Silent = 1 });
150codec_enum!(SortKeys { Dfsort = 0, Strict = 1 });
151codec_enum!(FastsrtAdvPrint { Exclude = 0, Include = 1 });
152codec_enum!(Warnings { Proceed = 0, Block = 1 });
153codec_enum!(Compile { Full = 0, Until(stop) = 1, SyntaxOnly = 2 });
154codec_enum!(Stop { W = 0, E = 1, S = 2 });
155codec_enum!(CicsReturnWarning { Once = 0, Always = 1, Never = 2 });
156codec_enum!(Quote { Quote = 0, Apost = 1 });
157codec_enum!(Currency { Char(c) = 0, Hex(b) = 1 });
158codec_enum!(Nsymbol { National = 0, Dbcs = 1 });
159codec_enum!(DispSign { Compat = 0, Sep = 1 });
160codec_enum!(IntDate { Ansi = 0, Lilian = 1 });
161codec_enum!(Qualify { Compat = 0, Extend = 1 });
162codec_enum!(Vlr { Standard = 0, Compat = 1 });
163codec_enum!(VsamOpenFs { Compat = 0, Succ = 1 });
164codec_struct!(Numcheck { zon, pac, bin, abd });
165codec_struct!(ZonCheck { alphnum, lax });
166codec_struct!(BinCheck { truncbin });
167codec_struct!(Parmcheck { abd, bytes });
168codec_enum!(Initcheck { Lax = 0, Strict = 1 });
169
170fn options_valid(options: &Options) -> Result<(), String> {
172 if options.optimize > 2 {
173 return Err(format!("OPTIMIZE({}) is not a level", options.optimize));
174 }
175 match CodePage::by_ccsid(options.codepage) {
176 Some(_) => Ok(()),
177 None => Err(format!("CODEPAGE({}) is not a page the tables carry", options.codepage)),
178 }
179}
180
181codec_struct!(Fixed { negative, magnitude, places });
182codec_struct!(Places { int, dec });
183
184impl Encode for U256 {
186 fn encode(&self, w: &mut Writer) {
187 [self.lo as u64, (self.lo >> 64) as u64, self.hi as u64, (self.hi >> 64) as u64].encode(w);
188 }
189}
190
191impl Decode for U256 {
192 fn decode(r: &mut Reader<'_>) -> Result<Self, ModuleError> {
193 let [a, b, c, d] = <[u64; 4]>::decode(r)?;
194 let limbs = |low: u64, high: u64| u128::from(low) | u128::from(high) << 64;
195 Ok(U256 { hi: limbs(c, d), lo: limbs(a, b) })
196 }
197}