Skip to content

Commit

Permalink
sysgpu/shader: Don't confuse hex digits with exponents and suffixes i…
Browse files Browse the repository at this point in the history
…n numbers.

Fixed the other issues described in hexops#1340
  • Loading branch information
msg-programs committed Feb 8, 2025
1 parent c454cff commit e6321e7
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/sysgpu/shader/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1393,9 +1393,9 @@ fn genNumber(astgen: *AstGen, node: NodeIndex) !InstIndex {

var i: usize = 0;
var suffix: u8 = 0;
var base: u8 = 10;
var exponent = false;
var dot = false;
var is_hex: bool = false;
var has_exponent = false;
var has_dot = false;

if (bytes.len >= 2 and bytes[0] == '0') switch (bytes[1]) {
'0'...'9' => {
Expand All @@ -1404,39 +1404,60 @@ fn genNumber(astgen: *AstGen, node: NodeIndex) !InstIndex {
},
'x', 'X' => {
i = 2;
base = 16;
is_hex = true;
},
else => {},
};

while (i < bytes.len) : (i += 1) {
while (i < (bytes.len - 1)) : (i += 1) {
const c = bytes[i];
switch (c) {
'f', 'h' => suffix = c,
'i', 'u' => {
if (dot or suffix == 'f' or suffix == 'h' or exponent) {
try astgen.errors.add(node_loc, "suffix '{c}' on float literal", .{c}, null);
'e', 'E' => {
if (is_hex) {
continue;
}
if (has_exponent) {
try astgen.errors.add(node_loc, "duplicate exponent '{c}'", .{c}, null);
return error.AnalysisFail;
}

suffix = c;
has_exponent = true;
},
'e', 'E', 'p', 'P' => {
if (exponent) {
try astgen.errors.add(node_loc, "duplicate exponent '{c}'", .{c}, null);
'p', 'P' => {
if (!is_hex) {
try astgen.errors.add(node_loc, "hexadecimal exponent '{c}' in decimal float", .{c}, null);
return error.AnalysisFail;
}

exponent = true;
// TODO
try astgen.errors.add(node_loc, "hexadecimal float literals not implemented", .{}, null);
return error.AnalysisFail;
},
'.' => dot = true,
'.' => has_dot = true,
else => {},
}
}

switch (bytes[bytes.len - 1]) {
'i', 'u' => |c| {
if (has_dot or has_exponent) {
try astgen.errors.add(node_loc, "int suffix '{c}' on float literal", .{c}, null);
return error.AnalysisFail;
} else {
suffix = c;
}
},
'h' => |c| suffix = c,
'f' => |c| {
if (!is_hex or has_dot or has_exponent) {
suffix = c;
}
// else is part of a hex literal
},
else => {},
}

var inst: Inst = undefined;
if (dot or exponent or suffix == 'f' or suffix == 'h') {
if (base == 16) {
if (has_dot or has_exponent or suffix == 'f' or suffix == 'h') {
if (is_hex) {
// TODO
try astgen.errors.add(node_loc, "hexadecimal float literals not implemented", .{}, null);
return error.AnalysisFail;
Expand Down

0 comments on commit e6321e7

Please sign in to comment.