Skip to content

Commit

Permalink
change slicing syntax from ... to ..
Browse files Browse the repository at this point in the history
See #359
  • Loading branch information
andrewrk committed May 19, 2017
1 parent b483db4 commit 051ee8e
Show file tree
Hide file tree
Showing 40 changed files with 164 additions and 158 deletions.
2 changes: 1 addition & 1 deletion doc/langref.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ FnCallExpression = "(" list(Expression, ",") ")"
ArrayAccessExpression = "[" Expression "]"
SliceExpression = "[" Expression "..." option(Expression) "]"
SliceExpression = "[" Expression ".." option(Expression) "]"
ContainerInitExpression = "{" ContainerInitBody "}"
Expand Down
4 changes: 2 additions & 2 deletions example/cat/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn cat_stream(is: &io.InStream) -> %void {
var buf: [1024 * 4]u8 = undefined;

while (true) {
const bytes_read = is.read(buf[0...]) %% |err| {
const bytes_read = is.read(buf[0..]) %% |err| {
%%io.stderr.printf("Unable to read from stream: {}\n", @errorName(err));
return err;
};
Expand All @@ -49,7 +49,7 @@ fn cat_stream(is: &io.InStream) -> %void {
break;
}

io.stdout.write(buf[0...bytes_read]) %% |err| {
io.stdout.write(buf[0..bytes_read]) %% |err| {
%%io.stderr.printf("Unable to write to stdout: {}\n", @errorName(err));
return err;
};
Expand Down
6 changes: 3 additions & 3 deletions example/guess_number/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn main() -> %void {
%%io.stdout.printf("Welcome to the Guess Number Game in Zig.\n");

var seed_bytes: [@sizeOf(usize)]u8 = undefined;
%%os.getRandomBytes(seed_bytes[0...]);
%%os.getRandomBytes(seed_bytes[0..]);
const seed = std.mem.readInt(seed_bytes, usize, true);
var rand = Rand.init(seed);

Expand All @@ -18,12 +18,12 @@ pub fn main() -> %void {
%%io.stdout.printf("\nGuess a number between 1 and 100: ");
var line_buf : [20]u8 = undefined;

const line_len = io.stdin.read(line_buf[0...]) %% |err| {
const line_len = io.stdin.read(line_buf[0..]) %% |err| {
%%io.stdout.printf("Unable to read from stdin: {}\n", @errorName(err));
return err;
};

const guess = fmt.parseUnsigned(u8, line_buf[0...line_len - 1], 10) %% {
const guess = fmt.parseUnsigned(u8, line_buf[0..line_len - 1], 10) %% {
%%io.stdout.printf("Invalid number.\n");
continue;
};
Expand Down
4 changes: 2 additions & 2 deletions example/mix_o_files/base64.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const base64 = @import("std").base64;

export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8, source_len: usize) -> usize {
const src = source_ptr[0...source_len];
const dest = dest_ptr[0...dest_len];
const src = source_ptr[0..source_len];
const dest = dest_ptr[0..dest_len];
return base64.decode(dest, src).len;
}
8 changes: 4 additions & 4 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {
}

Token *ellipsis_tok = &pc->tokens->at(*token_index);
if (ellipsis_tok->id == TokenIdEllipsis) {
if (ellipsis_tok->id == TokenIdEllipsis3) {
*token_index += 1;
node->data.param_decl.is_var_args = true;
} else {
Expand Down Expand Up @@ -879,7 +879,7 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,

Token *ellipsis_or_r_bracket = &pc->tokens->at(*token_index);

if (ellipsis_or_r_bracket->id == TokenIdEllipsis) {
if (ellipsis_or_r_bracket->id == TokenIdEllipsis2) {
*token_index += 1;

AstNode *node = ast_create_node(pc, NodeTypeSliceExpr, first_token);
Expand Down Expand Up @@ -1730,7 +1730,7 @@ static AstNode *ast_parse_for_expr(ParseContext *pc, size_t *token_index, bool m
/*
SwitchExpression = "switch" "(" Expression ")" "{" many(SwitchProng) "}"
SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbol "|") Expression ","
SwitchItem : Expression | (Expression "..." Expression)
SwitchItem = Expression | (Expression "..." Expression)
*/
static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
Token *switch_token = &pc->tokens->at(*token_index);
Expand Down Expand Up @@ -1767,7 +1767,7 @@ static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, boo
} else for (;;) {
AstNode *expr1 = ast_parse_expression(pc, token_index, true);
Token *ellipsis_tok = &pc->tokens->at(*token_index);
if (ellipsis_tok->id == TokenIdEllipsis) {
if (ellipsis_tok->id == TokenIdEllipsis3) {
*token_index += 1;

AstNode *range_node = ast_create_node(pc, NodeTypeSwitchRange, ellipsis_tok);
Expand Down
11 changes: 8 additions & 3 deletions src/tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ void tokenize(Buf *buf, Tokenization *out) {
switch (c) {
case '.':
t.state = TokenizeStateSawDotDot;
set_token_id(&t, t.cur_tok, TokenIdEllipsis);
set_token_id(&t, t.cur_tok, TokenIdEllipsis2);
break;
default:
t.pos -= 1;
Expand All @@ -601,10 +601,14 @@ void tokenize(Buf *buf, Tokenization *out) {
switch (c) {
case '.':
t.state = TokenizeStateStart;
set_token_id(&t, t.cur_tok, TokenIdEllipsis3);
end_token(&t);
break;
default:
tokenize_error(&t, "invalid character: '%c'", c);
t.pos -= 1;
end_token(&t);
t.state = TokenizeStateStart;
continue;
}
break;
case TokenizeStateSawGreaterThan:
Expand Down Expand Up @@ -1436,7 +1440,8 @@ const char * token_name(TokenId id) {
case TokenIdDivEq: return "/=";
case TokenIdDot: return ".";
case TokenIdDoubleQuestion: return "??";
case TokenIdEllipsis: return "...";
case TokenIdEllipsis3: return "...";
case TokenIdEllipsis2: return "..";
case TokenIdEof: return "EOF";
case TokenIdEq: return "=";
case TokenIdFatArrow: return "=>";
Expand Down
3 changes: 2 additions & 1 deletion src/tokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ enum TokenId {
TokenIdDivEq,
TokenIdDot,
TokenIdDoubleQuestion,
TokenIdEllipsis,
TokenIdEllipsis3,
TokenIdEllipsis2,
TokenIdEof,
TokenIdEq,
TokenIdFatArrow,
Expand Down
4 changes: 2 additions & 2 deletions std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ pub fn ArrayList(comptime T: type) -> type{
}

pub fn toSlice(l: &Self) -> []T {
return l.items[0...l.len];
return l.items[0..l.len];
}

pub fn toSliceConst(l: &const Self) -> []const T {
return l.items[0...l.len];
return l.items[0..l.len];
}

pub fn append(l: &Self, item: &const T) -> %void {
Expand Down
10 changes: 5 additions & 5 deletions std/base64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn encodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8)
out_index += 1;
}

return dest[0...out_index];
return dest[0..out_index];
}

pub fn decodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8) -> []u8 {
Expand All @@ -67,7 +67,7 @@ pub fn decodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8)
ascii6[c] = u8(i);
}

return decodeWithAscii6BitMap(dest, source, ascii6[0...], alphabet[64]);
return decodeWithAscii6BitMap(dest, source, ascii6[0..], alphabet[64]);
}

pub fn decodeWithAscii6BitMap(dest: []u8, source: []const u8, ascii6: []const u8, pad_char: u8) -> []u8 {
Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn decodeWithAscii6BitMap(dest: []u8, source: []const u8, ascii6: []const u8
dest_index += 1;
}

return dest[0...dest_index];
return dest[0..dest_index];
}

pub fn calcEncodedSize(source_len: usize) -> usize {
Expand Down Expand Up @@ -174,11 +174,11 @@ fn testBase64Case(expected_decoded: []const u8, expected_encoded: []const u8) {

var buf: [100]u8 = undefined;

const actual_decoded = decode(buf[0...], expected_encoded);
const actual_decoded = decode(buf[0..], expected_encoded);
assert(actual_decoded.len == expected_decoded.len);
assert(mem.eql(u8, expected_decoded, actual_decoded));

const actual_encoded = encode(buf[0...], expected_decoded);
const actual_encoded = encode(buf[0..], expected_decoded);
assert(actual_encoded.len == expected_encoded.len);
assert(mem.eql(u8, expected_encoded, actual_encoded));
}
2 changes: 1 addition & 1 deletion std/buf_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub const BufMap = struct {

fn free(self: &BufMap, value: []const u8) {
// remove the const
const mut_value = @ptrCast(&u8, value.ptr)[0...value.len];
const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
self.hash_map.allocator.free(mut_value);
}

Expand Down
2 changes: 1 addition & 1 deletion std/buf_set.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub const BufSet = struct {

fn free(self: &BufSet, value: []const u8) {
// remove the const
const mut_value = @ptrCast(&u8, value.ptr)[0...value.len];
const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
self.hash_map.allocator.free(mut_value);
}

Expand Down
10 changes: 5 additions & 5 deletions std/buffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ pub const Buffer = struct {
}

pub fn toSlice(self: &Buffer) -> []u8 {
return self.list.toSlice()[0...self.len()];
return self.list.toSlice()[0..self.len()];
}

pub fn toSliceConst(self: &const Buffer) -> []const u8 {
return self.list.toSliceConst()[0...self.len()];
return self.list.toSliceConst()[0..self.len()];
}

pub fn resize(self: &Buffer, new_len: usize) -> %void {
Expand All @@ -66,7 +66,7 @@ pub const Buffer = struct {
pub fn append(self: &Buffer, m: []const u8) -> %void {
const old_len = self.len();
%return self.resize(old_len + m.len);
mem.copy(u8, self.list.toSlice()[old_len...], m);
mem.copy(u8, self.list.toSlice()[old_len..], m);
}

pub fn appendByte(self: &Buffer, byte: u8) -> %void {
Expand All @@ -80,14 +80,14 @@ pub const Buffer = struct {

pub fn startsWith(self: &const Buffer, m: []const u8) -> bool {
if (self.len() < m.len) return false;
return mem.eql(u8, self.list.items[0...m.len], m);
return mem.eql(u8, self.list.items[0..m.len], m);
}

pub fn endsWith(self: &const Buffer, m: []const u8) -> bool {
const l = self.len();
if (l < m.len) return false;
const start = l - m.len;
return mem.eql(u8, self.list.items[start...], m);
return mem.eql(u8, self.list.items[start..], m);
}

pub fn replaceContents(self: &const Buffer, m: []const u8) -> %void {
Expand Down
2 changes: 1 addition & 1 deletion std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ pub const Builder = struct {
};
self.addRPath(rpath);
} else if (word.len > 2 and word[0] == '-' and word[1] == 'L') {
const lib_path = word[2...];
const lib_path = word[2..];
self.addLibPath(lib_path);
} else {
%%io.stderr.printf("Unrecognized C flag from NIX_LDFLAGS: {}\n", word);
Expand Down
4 changes: 2 additions & 2 deletions std/cstr.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ pub fn cmp(a: &const u8, b: &const u8) -> i8 {
}

pub fn toSliceConst(str: &const u8) -> []const u8 {
return str[0...len(str)];
return str[0..len(str)];
}

pub fn toSlice(str: &u8) -> []u8 {
return str[0...len(str)];
return str[0..len(str)];
}

test "cstr fns" {
Expand Down
6 changes: 3 additions & 3 deletions std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ fn printLineFromFile(allocator: &mem.Allocator, out_stream: &io.OutStream, line_
var column: usize = 1;
var abs_index: usize = 0;
while (true) {
const amt_read = %return f.read(buf[0...]);
const slice = buf[0...amt_read];
const amt_read = %return f.read(buf[0..]);
const slice = buf[0..amt_read];

for (slice) |byte| {
if (line == line_info.line) {
Expand Down Expand Up @@ -939,7 +939,7 @@ var some_mem: [100 * 1024]u8 = undefined;
var some_mem_index: usize = 0;

fn globalAlloc(self: &mem.Allocator, n: usize) -> %[]u8 {
const result = some_mem[some_mem_index ... some_mem_index + n];
const result = some_mem[some_mem_index .. some_mem_index + n];
some_mem_index += n;
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion std/elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub const Elf = struct {
elf.auto_close_stream = false;

var magic: [4]u8 = undefined;
%return elf.in_stream.readNoEof(magic[0...]);
%return elf.in_stream.readNoEof(magic[0..]);
if (!mem.eql(u8, magic, "\x7fELF")) return error.InvalidFormat;

elf.is_64 = switch (%return elf.in_stream.readByte()) {
Expand Down
2 changes: 1 addition & 1 deletion std/endian.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {

pub fn swap(comptime T: type, x: T) -> T {
var buf: [@sizeOf(T)]u8 = undefined;
mem.writeInt(buf[0...], x, false);
mem.writeInt(buf[0..], x, false);
return mem.readInt(buf, T, true);
}
Loading

0 comments on commit 051ee8e

Please sign in to comment.