Integer formats
All checks were successful
All checks were successful
This commit is contained in:
parent
3cbd427f14
commit
b8873564af
@ -107,6 +107,14 @@ fn is_decimal_ch(ch: u8) bool {
|
|||||||
return ch >= '0' and ch <= '9';
|
return ch >= '0' and ch <= '9';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_octal_ch(ch: u8) bool {
|
||||||
|
return ch >= '0' and ch <= '7';
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_binary_ch(ch: u8) bool {
|
||||||
|
return ch == '0' or ch == '1';
|
||||||
|
}
|
||||||
|
|
||||||
fn is_identifier_ch(ch: u8) bool {
|
fn is_identifier_ch(ch: u8) bool {
|
||||||
return is_identifier_start_ch(ch) or is_decimal_ch(ch);
|
return is_identifier_start_ch(ch) or is_decimal_ch(ch);
|
||||||
}
|
}
|
||||||
@ -1173,6 +1181,36 @@ pub const Module = struct {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_octal(noalias module: *Module) u64 {
|
||||||
|
var value: u64 = 0;
|
||||||
|
while (true) {
|
||||||
|
const ch = module.content[module.offset];
|
||||||
|
if (!is_octal_ch(ch)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.offset += 1;
|
||||||
|
value = lib.parse.accumulate_octal(value, ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_binary(noalias module: *Module) u64 {
|
||||||
|
var value: u64 = 0;
|
||||||
|
while (true) {
|
||||||
|
const ch = module.content[module.offset];
|
||||||
|
if (!is_binary_ch(ch)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.offset += 1;
|
||||||
|
value = lib.parse.accumulate_binary(value, ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
const AttributeBuildOptions = struct {
|
const AttributeBuildOptions = struct {
|
||||||
return_type_abi: Abi.Information,
|
return_type_abi: Abi.Information,
|
||||||
abi_argument_types: []const *Type,
|
abi_argument_types: []const *Type,
|
||||||
@ -2023,16 +2061,32 @@ pub const Module = struct {
|
|||||||
'x' => .hexadecimal,
|
'x' => .hexadecimal,
|
||||||
'o' => .octal,
|
'o' => .octal,
|
||||||
'b' => .binary,
|
'b' => .binary,
|
||||||
|
'd' => .decimal,
|
||||||
else => .decimal,
|
else => .decimal,
|
||||||
};
|
};
|
||||||
const value: u64 = switch (token_integer_kind) {
|
const value: u64 = switch (token_integer_kind) {
|
||||||
.binary => @trap(),
|
.binary => b: {
|
||||||
.octal => @trap(),
|
module.offset += 2;
|
||||||
|
const v = module.parse_binary();
|
||||||
|
break :b v;
|
||||||
|
},
|
||||||
|
.octal => b: {
|
||||||
|
module.offset += 2;
|
||||||
|
const v = module.parse_octal();
|
||||||
|
break :b v;
|
||||||
|
},
|
||||||
.decimal => switch (next_ch) {
|
.decimal => switch (next_ch) {
|
||||||
0...9 => module.report_error(),
|
0...9 => module.report_error(),
|
||||||
else => b: {
|
else => switch (next_ch) {
|
||||||
module.offset += 1;
|
'd' => b: {
|
||||||
break :b 0;
|
module.offset += 2;
|
||||||
|
const v = module.parse_decimal();
|
||||||
|
break :b v;
|
||||||
|
},
|
||||||
|
else => b: {
|
||||||
|
module.offset += 1;
|
||||||
|
break :b 0;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
.hexadecimal => b: {
|
.hexadecimal => b: {
|
||||||
@ -4087,13 +4141,17 @@ pub const Module = struct {
|
|||||||
module.offset += 1;
|
module.offset += 1;
|
||||||
break :b module.parse_hexadecimal();
|
break :b module.parse_hexadecimal();
|
||||||
},
|
},
|
||||||
'o' => {
|
'd' => b: {
|
||||||
// TODO: parse octal
|
module.offset += 1;
|
||||||
module.report_error();
|
break :b module.parse_decimal();
|
||||||
},
|
},
|
||||||
'b' => {
|
'o' => b: {
|
||||||
// TODO: parse binary
|
module.offset += 1;
|
||||||
module.report_error();
|
break :b module.parse_octal();
|
||||||
|
},
|
||||||
|
'b' => b: {
|
||||||
|
module.offset += 1;
|
||||||
|
break :b module.parse_binary();
|
||||||
},
|
},
|
||||||
'0'...'9' => {
|
'0'...'9' => {
|
||||||
module.report_error();
|
module.report_error();
|
||||||
|
@ -174,6 +174,7 @@ os_commit = fn (address: u64, size: u64, protection: OS_ProtectionFlags) void
|
|||||||
|
|
||||||
os_make_directory = fn (path: &u8) void
|
os_make_directory = fn (path: &u8) void
|
||||||
{
|
{
|
||||||
|
>result = mkdir(path, 0o755);
|
||||||
}
|
}
|
||||||
|
|
||||||
Arena = struct
|
Arena = struct
|
||||||
|
@ -2865,6 +2865,14 @@ pub const parse = struct {
|
|||||||
pub fn accumulate_decimal(accumulator: u64, ch: u8) u64 {
|
pub fn accumulate_decimal(accumulator: u64, ch: u8) u64 {
|
||||||
return (accumulator * 10) + (ch - '0');
|
return (accumulator * 10) + (ch - '0');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn accumulate_octal(accumulator: u64, ch: u8) u64 {
|
||||||
|
return (accumulator * 8) + (ch - '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn accumulate_binary(accumulator: u64, ch: u8) u64 {
|
||||||
|
return (accumulator * 2) + (ch - '0');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
test "parse integer decimal" {
|
test "parse integer decimal" {
|
||||||
|
@ -320,4 +320,6 @@ const names = &[_][]const u8{
|
|||||||
"pointer_decay",
|
"pointer_decay",
|
||||||
"enum_name",
|
"enum_name",
|
||||||
"slice_of_slices",
|
"slice_of_slices",
|
||||||
|
"type_alias",
|
||||||
|
"integer_formats",
|
||||||
};
|
};
|
||||||
|
7
tests/integer_formats.bbb
Normal file
7
tests/integer_formats.bbb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[export] main = fn [cc(c)] () s32
|
||||||
|
{
|
||||||
|
>a: s32 = 0o10;
|
||||||
|
>b: s32 = 0b1000;
|
||||||
|
>c: s32 = 0d0;
|
||||||
|
return a - b + c;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user