Skip to content
← Back

src/syntax/process/numeric_literal_classifier.ghul

1
namespace Syntax.Process is
2
use Logging;
3
use Source.LOCATION;
4
5
use Semantic.Lookups.InnateSymbolLookup;
6
use Semantic.Types.Type;
7
8
use IR.Values;
9
10
// Classify a parsed integer literal: pick the target type from
11
// any trailing type-suffix (`b`/`s`/`i`/`l`/`w`/`c`, optionally
12
// prefixed by `u` for unsigned or `s` for sign-without-width),
13
// strip the suffix from the value string, and range-check the
14
// result against the chosen type. The lexer accepts arbitrarily
15
// long digit strings and any suffix combination; this is where
16
// suffix → type and value → fits-in-type get decided.
17
//
18
// Bounds errors and "character literals cannot be unsigned"
19
// structural errors are logged via the constructor-supplied
20
// logger. Callers receive a non-null `Literal.NUMBER` on success
21
// and null on either kind of failure.
22
//
23
// The `fits` / `parse_hex` / `max_unsigned_for` helpers are
24
// exposed for unit testing in isolation from the suffix path.
25
class NUMERIC_LITERAL_CLASSIFIER is
26
_logger: Logger;
27
_innate_symbol_lookup: InnateSymbolLookup;
28
29
init(logger: Logger, innate_symbol_lookup: InnateSymbolLookup) is
30
super.init();
31
32
_logger = logger;
33
_innate_symbol_lookup = innate_symbol_lookup;
34
si
35
36
classify_integer(location: LOCATION, value_string: string) -> Literal.NUMBER? is
37
let type: Type mut;
38
let suffix mut = "i4";
39
let conv: string? mut = null;
40
let working mut = value_string;
41
42
let last_char = working.get_chars(working.length - 1);
43
44
if "bBcCsSiIlLwWuU".contains(last_char) then
45
let is_unsigned mut = false;
46
let cutoff mut = working.length;
47
48
if last_char == 'u' \/ last_char == 'U' then
49
is_unsigned = true;
50
cutoff = cutoff - 1;
51
elif working.length > 2 then
52
let sign_char = working.get_chars(working.length - 2);
53
54
if sign_char == 'u' \/ sign_char == 'U' then
55
is_unsigned = true;
56
cutoff = cutoff - 1;
57
elif sign_char == 's' \/ sign_char == 'S' then
58
cutoff = cutoff - 1;
59
fi
60
fi
61
62
case last_char
63
when 'b', 'B' then
64
if is_unsigned then
65
type = _innate_symbol_lookup.get_ubyte_type();
66
else
67
type = _innate_symbol_lookup.get_byte_type();
68
fi
69
70
cutoff = cutoff - 1;
71
72
when 'c', 'C' then
73
if is_unsigned then
74
_logger.error(location, "numeric character literal cannot be unsigned");
75
else
76
type = _innate_symbol_lookup.get_char_type();
77
fi
78
79
cutoff = cutoff - 1;
80
81
when 's', 'S' then
82
if is_unsigned then
83
type = _innate_symbol_lookup.get_ushort_type();
84
else
85
type = _innate_symbol_lookup.get_short_type();
86
fi
87
88
cutoff = cutoff - 1;
89
90
when 'i', 'I' then
91
if is_unsigned then
92
type = _innate_symbol_lookup.get_uint_type();
93
else
94
type = _innate_symbol_lookup.get_int_type();
95
fi
96
97
cutoff = cutoff - 1;
98
99
when 'l', 'L' then
100
suffix = "i8";
101
102
if is_unsigned then
103
type = _innate_symbol_lookup.get_ulong_type();
104
else
105
type = _innate_symbol_lookup.get_long_type();
106
fi
107
108
cutoff = cutoff - 1;
109
110
when 'w', 'W' then
111
suffix = "i8";
112
113
if is_unsigned then
114
type = _innate_symbol_lookup.get_uword_type();
115
conv = "conv.u";
116
else
117
type = _innate_symbol_lookup.get_word_type();
118
conv = "conv.i";
119
fi
120
121
cutoff = cutoff - 1;
122
123
else
124
if is_unsigned then
125
type = _innate_symbol_lookup.get_uint_type();
126
else
127
type = _innate_symbol_lookup.get_int_type();
128
fi
129
esac
130
131
working = working.substring(0, cutoff);
132
else
133
type = _innate_symbol_lookup.get_int_type();
134
fi
135
136
// the resolved type may be null
137
@suppress("presence-test-non-optional")
138
if !type? then
139
return null;
140
fi
141
142
if !fits(location, working, type) then
143
return null;
144
fi
145
146
return Literal.NUMBER(working, type, suffix, conv);
147
si
148
149
// Range-check a parsed integer literal against its target
150
// type. Comparing as a ulong using each type's unsigned max
151
// preserves the standard "literal fits in N unsigned bits,
152
// unary minus negates separately" convention. Without this,
153
// oversize literals reach IL emission and ilasm rejects the
154
// `ldc.i4 <bignum>` with an opaque syntax error (issue #580).
155
fits(location: LOCATION, value_string: string, type: Type) -> bool is
156
let (ok, parsed) =
157
if value_string.starts_with("0x") \/ value_string.starts_with("0X") then
158
parse_hex(value_string.substring(2, value_string.length - 2));
159
else
160
let v: ulong mut = _;
161
let parsed_ok = ulong.try_parse(value_string, v ref);
162
(parsed_ok, v);
163
fi;
164
165
if !ok then
166
_logger.error(location, "numeric literal {value_string} does not fit in {type}");
167
return false;
168
fi
169
170
let max_value = max_unsigned_for(type);
171
172
if max_value > 0UL /\ parsed > max_value then
173
_logger.error(location, "numeric literal {value_string} does not fit in {type}");
174
return false;
175
fi
176
177
return true;
178
si
179
180
// Parse a hex digit string (no `0x` prefix) into a ulong.
181
// Returns (false, 0) for empty, > 16 digits, or any non-hex
182
// character.
183
parse_hex(s: string) -> (ok: bool, value: ulong) is
184
if s.length == 0 \/ s.length > 16 then
185
return (false, 0UL);
186
fi
187
188
let value: ulong mut = 0UL;
189
190
for c in s do
191
let digit: int mut;
192
193
if c >= '0' /\ c <= '9' then
194
digit = cast int(c) - cast int('0');
195
elif c >= 'a' /\ c <= 'f' then
196
digit = cast int(c) - cast int('a') + 10;
197
elif c >= 'A' /\ c <= 'F' then
198
digit = cast int(c) - cast int('A') + 10;
199
else
200
return (false, 0UL);
201
fi
202
203
value = (value << 4UL) + cast ulong(digit);
204
od
205
206
return (true, value);
207
si
208
209
// Largest non-negative literal value the type accepts, or 0
210
// to signal "no narrower bound than the ulong-parse range"
211
// (used for ulong / uword: ulong.try_parse already enforces
212
// the 64-bit ceiling). 0 makes `fits` skip the narrower-
213
// bound check.
214
max_unsigned_for(type: Type) -> ulong is
215
if type.matches(_innate_symbol_lookup.get_byte_type()) then
216
return 127UL;
217
elif type.matches(_innate_symbol_lookup.get_ubyte_type()) then
218
return 255UL;
219
elif type.matches(_innate_symbol_lookup.get_short_type()) then
220
return 32767UL;
221
elif type.matches(_innate_symbol_lookup.get_ushort_type()) then
222
return 65535UL;
223
elif type.matches(_innate_symbol_lookup.get_char_type()) then
224
return 65535UL;
225
elif type.matches(_innate_symbol_lookup.get_int_type()) then
226
return 2147483647UL;
227
elif type.matches(_innate_symbol_lookup.get_uint_type()) then
228
return 4294967295UL;
229
elif type.matches(_innate_symbol_lookup.get_long_type()) then
230
return 9223372036854775807UL;
231
elif type.matches(_innate_symbol_lookup.get_word_type()) then
232
return 9223372036854775807UL;
233
fi
234
235
return 0UL;
236
si
237
si
238
si