zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 7b68385d7d4448e81cc882d9a5464bf58d10dc0d (tree)
parent d98aed6eff61628f97597e1fa68335db5b2fd466
Author: Vexu <git@vexu.eu>
Date:   Fri, 19 Jun 2020 20:52:06 +0300

self-hosted: astGenIntegerLiteral support other bases

Diffstat:
Msrc-self-hosted/Module.zig | 21++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/src-self-hosted/Module.zig b/src-self-hosted/Module.zig @@ -1314,16 +1314,19 @@ fn astGenStringLiteral(self: *Module, scope: *Scope, str_lit: *ast.Node.StringLi fn astGenIntegerLiteral(self: *Module, scope: *Scope, int_lit: *ast.Node.IntegerLiteral) InnerError!*zir.Inst { const arena = scope.arena(); const tree = scope.tree(); - const bytes = tree.tokenSlice(int_lit.token); + var bytes = tree.tokenSlice(int_lit.token); + const base = if (mem.startsWith(u8, bytes, "0x")) + 16 + else if (mem.startsWith(u8, bytes, "0o")) + 8 + else if (mem.startsWith(u8, bytes, "0b")) + 2 + else + @as(u8, 10); - if (mem.startsWith(u8, bytes, "0x")) { - return self.failTok(scope, int_lit.token, "TODO implement 0x int prefix", .{}); - } else if (mem.startsWith(u8, bytes, "0o")) { - return self.failTok(scope, int_lit.token, "TODO implement 0o int prefix", .{}); - } else if (mem.startsWith(u8, bytes, "0b")) { - return self.failTok(scope, int_lit.token, "TODO implement 0b int prefix", .{}); - } - if (std.fmt.parseInt(u64, bytes, 10)) |small_int| { + if (base != 10) bytes = bytes[2..]; + + if (std.fmt.parseInt(u64, bytes, base)) |small_int| { const int_payload = try arena.create(Value.Payload.Int_u64); int_payload.* = .{ .int = small_int }; const src = tree.token_locs[int_lit.token].start;