Skip to content

Commit

Permalink
feat: Properly support hex/float/dec for constants
Browse files Browse the repository at this point in the history
  • Loading branch information
F0bes committed Mar 22, 2024
1 parent 5399f02 commit 10c83e0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 227 deletions.
43 changes: 21 additions & 22 deletions gifscript.rl
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,28 @@ void FailError(const char* ts, const char* te);
}
}

# Integer Constants
# Constants
action int_const_tok {
std::string s(ts, te - ts);
Parse(lparser, NUMBER_LITERAL, new std::any(std::stoi(s)), &valid);
Parse(lparser, NUMBER_LITERAL, new std::any(static_cast<uint32_t>(std::stoi(s))), &valid);
if(!valid)
{
FailError(ts, te);
}
}

# Hexadecimal Constants
action hex_const_tok {
std::string s(ts, te - ts);
Parse(lparser, NUMBER_LITERAL, new std::any(std::stoi(s, nullptr, 16)), &valid);
Parse(lparser, NUMBER_LITERAL, new std::any(static_cast<uint32_t>(std::stoi(s, nullptr, 16))), &valid);
if(!valid)
{
FailError(ts, te);
}
}

action float_const_tok {
std::string s(ts, te - ts);
Parse(lparser, NUMBER_LITERAL, new std::any(std::bit_cast<uint32_t>(std::stof(s))), &valid);
if(!valid)
{
FailError(ts, te);
Expand Down Expand Up @@ -253,11 +261,6 @@ void FailError(const char* ts, const char* te);
}
}

# Floating literals.
fract_const = digit* '.' digit+ | digit+ '.';
exponent = [eE] [+\-]? digit+;
float_suffix = [flFL];

c_comment :=
any* :>> '*/'
@{ fgoto main; };
Expand Down Expand Up @@ -290,18 +293,15 @@ void FailError(const char* ts, const char* te);
mod_fogging = (/fogging/i|/fog/i);
mod_aa1 = /aa1/i;

# Vectors
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]+)?;
vec3 = [0-9a-fA-F]+ ('.' [0-9]+)? ',' [0-9a-fA-F]+ ('.' [0-9]+)? ',' [0-9a-fA-F]+ ('.' [0-9]+)?;
vec2 = [0-9a-fA-F]+ ('.' [0-9a-fA-F]+)? ',' [0-9a-fA-F]+ ('.' [0-9a-fA-F]+)?;

# Integer Constants
# Constants
int_const = digit+;
float_const = digit+ '.' digit+ (/f/i)?;
hex_const = ([0])? [xX] xdigit+;

# Hexadecimal Constants
hex_prefix = '0' [xX];
hex_digit = [0-9a-fA-F];
hex_const = hex_prefix hex_digit+;
# Vectors
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);
vec3 = (int_const|float_const|hex_const) ',' (int_const|float_const|hex_const) ',' (int_const|float_const|hex_const);
vec2 = (int_const|float_const|hex_const) ',' (int_const|float_const|hex_const);

# Block controls
block_begin = /{/i;
Expand Down Expand Up @@ -347,10 +347,9 @@ void FailError(const char* ts, const char* te);
vec3 => vec3_tok;
vec2 => vec2_tok;

# Integer Constants
# Constants
int_const => int_const_tok;

# Hexadecimal Constants
float_const => float_const_tok;
hex_const => hex_const_tok;

# Block controls
Expand Down
2 changes: 1 addition & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ set_register ::= REG(A) MOD(B) MOD(C) MOD(D) MOD(E). {
}

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

Expand Down
Loading

0 comments on commit 10c83e0

Please sign in to comment.