Merge pull request #150 from birth-software/one-complement
One's complement implementation
This commit is contained in:
commit
26a21c828e
@ -11766,6 +11766,34 @@ pub const Builder = struct {
|
||||
.type => |type_index| try builder.resolveContainerLiteral(unit, context, &.{}, type_index),
|
||||
else => |t| @panic(@tagName(t)),
|
||||
},
|
||||
.one_complement => block: {
|
||||
const value = try builder.resolveRuntimeValue(unit, context, type_expect, node.left, .right);
|
||||
const not = try unit.instructions.append(context.my_allocator, .{
|
||||
.integer_binary_operation = .{
|
||||
.id = .bit_xor,
|
||||
.left = value,
|
||||
.right = .{
|
||||
.value = .{
|
||||
.@"comptime" = .{
|
||||
.constant_int = .{
|
||||
.value = @bitCast(@as(i64, -1)),
|
||||
},
|
||||
},
|
||||
},
|
||||
.type = value.type,
|
||||
},
|
||||
.signedness = .unsigned,
|
||||
},
|
||||
});
|
||||
try builder.appendInstruction(unit, context, not);
|
||||
|
||||
break :block V{
|
||||
.type = value.type,
|
||||
.value = .{
|
||||
.runtime = not,
|
||||
},
|
||||
};
|
||||
},
|
||||
else => |t| @panic(@tagName(t)),
|
||||
};
|
||||
|
||||
@ -16831,6 +16859,7 @@ pub const Token = struct {
|
||||
operator_dollar,
|
||||
operator_switch_case,
|
||||
operator_backtick,
|
||||
operator_tilde,
|
||||
// Binary
|
||||
operator_assign,
|
||||
operator_add,
|
||||
|
@ -265,6 +265,10 @@ pub fn analyze(allocator: *MyAllocator, text: []const u8, token_buffer: *Token.B
|
||||
index += 1;
|
||||
break :blk .operator_colon;
|
||||
},
|
||||
'~' => blk: {
|
||||
index += 1;
|
||||
break :blk .operator_tilde;
|
||||
},
|
||||
'!' => blk: {
|
||||
index += 1;
|
||||
switch (text[index]) {
|
||||
|
@ -156,6 +156,7 @@ pub const Node = struct {
|
||||
slice,
|
||||
range,
|
||||
negation,
|
||||
one_complement,
|
||||
anonymous_container_literal,
|
||||
anonymous_array_literal,
|
||||
array_literal,
|
||||
@ -1226,6 +1227,7 @@ const Analyzer = struct {
|
||||
},
|
||||
.operator_bang => .boolean_not,
|
||||
.operator_minus => .negation,
|
||||
.operator_tilde => .one_complement,
|
||||
.fixed_keyword_try => .try_expression,
|
||||
// .tilde => |t| @panic(@tagName(t)),
|
||||
};
|
||||
|
@ -210,6 +210,11 @@ const concatenate_bytes = fn (arena: &Arena, byte_sequences: []const []const u8)
|
||||
return concatenation;
|
||||
}
|
||||
|
||||
const align_forward = fn(value: u64, alignment: u64) u64 {
|
||||
const mask = alignment - 1;
|
||||
return (value + mask) & ~mask;
|
||||
}
|
||||
|
||||
test "concatenate" {
|
||||
var arena = try Arena.init(2*1024*1024);
|
||||
const concatenation = try concatenate_bytes(arena, .{ "ABC", "DEF" }.&);
|
||||
|
@ -31,15 +31,6 @@ const lex = fn (arena: &Arena, bytes: []const u8) *!void {
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
print("Line count: ");
|
||||
print_usize(line_count);
|
||||
print("\n");
|
||||
|
||||
for (line_offsets[0..line_count]) |offset| {
|
||||
print_usize(offset);
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
const get_argument = fn (real_argument: []const u8, wanted_argument: []const u8, command_arguments: []const [&:0]const u8, i_ptr: &usize) ?[]const u8 {
|
||||
|
15
test/standalone/one_complement/main.nat
Normal file
15
test/standalone/one_complement/main.nat
Normal file
@ -0,0 +1,15 @@
|
||||
const std = #import("std");
|
||||
const align_forward = std.align_forward;
|
||||
const expect = std.testing.expect;
|
||||
|
||||
const main = fn () *!void {
|
||||
const a: u64 = 1;
|
||||
const aligned_a = align_forward(a, 8);
|
||||
try expect(aligned_a == 8);
|
||||
const b: u64 = 9;
|
||||
const aligned_b = align_forward(b, 8);
|
||||
try expect(aligned_b == 16);
|
||||
const c = 512;
|
||||
const aligned_c = align_forward(c, 0x1000);
|
||||
try expect(aligned_c == 0x1000);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user