Skip to content

Commit 10c83e0

Browse files
committed
feat: Properly support hex/float/dec for constants
1 parent 5399f02 commit 10c83e0

File tree

4 files changed

+101
-227
lines changed

4 files changed

+101
-227
lines changed

gifscript.rl

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -196,20 +196,28 @@ void FailError(const char* ts, const char* te);
196196
}
197197
}
198198

199-
# Integer Constants
199+
# Constants
200200
action int_const_tok {
201201
std::string s(ts, te - ts);
202-
Parse(lparser, NUMBER_LITERAL, new std::any(std::stoi(s)), &valid);
202+
Parse(lparser, NUMBER_LITERAL, new std::any(static_cast<uint32_t>(std::stoi(s))), &valid);
203203
if(!valid)
204204
{
205205
FailError(ts, te);
206206
}
207207
}
208208

209-
# Hexadecimal Constants
210209
action hex_const_tok {
211210
std::string s(ts, te - ts);
212-
Parse(lparser, NUMBER_LITERAL, new std::any(std::stoi(s, nullptr, 16)), &valid);
211+
Parse(lparser, NUMBER_LITERAL, new std::any(static_cast<uint32_t>(std::stoi(s, nullptr, 16))), &valid);
212+
if(!valid)
213+
{
214+
FailError(ts, te);
215+
}
216+
}
217+
218+
action float_const_tok {
219+
std::string s(ts, te - ts);
220+
Parse(lparser, NUMBER_LITERAL, new std::any(std::bit_cast<uint32_t>(std::stof(s))), &valid);
213221
if(!valid)
214222
{
215223
FailError(ts, te);
@@ -253,11 +261,6 @@ void FailError(const char* ts, const char* te);
253261
}
254262
}
255263

256-
# Floating literals.
257-
fract_const = digit* '.' digit+ | digit+ '.';
258-
exponent = [eE] [+\-]? digit+;
259-
float_suffix = [flFL];
260-
261264
c_comment :=
262265
any* :>> '*/'
263266
@{ fgoto main; };
@@ -290,18 +293,15 @@ void FailError(const char* ts, const char* te);
290293
mod_fogging = (/fogging/i|/fog/i);
291294
mod_aa1 = /aa1/i;
292295

293-
# Vectors
294-
vec4 = [0-9a-fA-F]+ ('.' [0-9]+)? ',' [0-9a-fA-F]+ ('.' [0-9]+)? ',' [0-9a-fA-F]+ ('.' [0-9]+)? ',' [0-9a-fA-F]+ ('.' [0-9]+)?;
295-
vec3 = [0-9a-fA-F]+ ('.' [0-9]+)? ',' [0-9a-fA-F]+ ('.' [0-9]+)? ',' [0-9a-fA-F]+ ('.' [0-9]+)?;
296-
vec2 = [0-9a-fA-F]+ ('.' [0-9a-fA-F]+)? ',' [0-9a-fA-F]+ ('.' [0-9a-fA-F]+)?;
297-
298-
# Integer Constants
296+
# Constants
299297
int_const = digit+;
298+
float_const = digit+ '.' digit+ (/f/i)?;
299+
hex_const = ([0])? [xX] xdigit+;
300300

301-
# Hexadecimal Constants
302-
hex_prefix = '0' [xX];
303-
hex_digit = [0-9a-fA-F];
304-
hex_const = hex_prefix hex_digit+;
301+
# Vectors
302+
vec4 = (int_const|float_const|hex_const) ',' (int_const|float_const|hex_const) ',' (int_const|float_const|hex_const) ',' (int_const|float_const|hex_const);
303+
vec3 = (int_const|float_const|hex_const) ',' (int_const|float_const|hex_const) ',' (int_const|float_const|hex_const);
304+
vec2 = (int_const|float_const|hex_const) ',' (int_const|float_const|hex_const);
305305

306306
# Block controls
307307
block_begin = /{/i;
@@ -347,10 +347,9 @@ void FailError(const char* ts, const char* te);
347347
vec3 => vec3_tok;
348348
vec2 => vec2_tok;
349349

350-
# Integer Constants
350+
# Constants
351351
int_const => int_const_tok;
352-
353-
# Hexadecimal Constants
352+
float_const => float_const_tok;
354353
hex_const => hex_const_tok;
355354

356355
# Block controls

parser.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ set_register ::= REG(A) MOD(B) MOD(C) MOD(D) MOD(E). {
119119
}
120120

121121
set_register ::= REG(A) NUMBER_LITERAL(B). {
122-
if(!machine.TrySetRegister(GenReg(std::any_cast<GifRegisters>(*A))) || !machine.TryPushReg(std::any_cast<int32_t>(*B))) {
122+
if(!machine.TrySetRegister(GenReg(std::any_cast<GifRegisters>(*A))) || !machine.TryPushReg(std::any_cast<uint32_t>(*B))) {
123123
*valid = false;
124124
}
125125

0 commit comments

Comments
 (0)