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