Skip to content

Commit

Permalink
remove %% prefix operator
Browse files Browse the repository at this point in the history
See #632
closes #545
closes #510

this makes #651 higher priority
  • Loading branch information
andrewrk committed Jan 9, 2018
1 parent 98a95cc commit 3c09411
Show file tree
Hide file tree
Showing 46 changed files with 551 additions and 642 deletions.
34 changes: 17 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ pub fn build(b: &Builder) {
var docgen_cmd = b.addCommand(null, b.env_map, [][]const u8 {
docgen_exe.getOutputPath(),
"doc/langref.html.in",
%%os.path.join(b.allocator, b.cache_root, "langref.html"),
os.path.join(b.allocator, b.cache_root, "langref.html") catch unreachable,
});
docgen_cmd.step.dependOn(&docgen_exe.step);

var docgen_home_cmd = b.addCommand(null, b.env_map, [][]const u8 {
docgen_exe.getOutputPath(),
"doc/home.html.in",
%%os.path.join(b.allocator, b.cache_root, "home.html"),
os.path.join(b.allocator, b.cache_root, "home.html") catch unreachable,
});
docgen_home_cmd.step.dependOn(&docgen_exe.step);

Expand All @@ -47,7 +47,7 @@ pub fn build(b: &Builder) {
const c_header_files = nextValue(&index, build_info);
const dia_guids_lib = nextValue(&index, build_info);

const llvm = findLLVM(b, llvm_config_exe);
const llvm = findLLVM(b, llvm_config_exe) catch unreachable;

var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
exe.setBuildMode(mode);
Expand Down Expand Up @@ -143,8 +143,8 @@ fn dependOnLib(lib_exe_obj: &std.build.LibExeObjStep, dep: &const LibraryDep) {

fn addCppLib(b: &Builder, lib_exe_obj: &std.build.LibExeObjStep, cmake_binary_dir: []const u8, lib_name: []const u8) {
const lib_prefix = if (lib_exe_obj.target.isWindows()) "" else "lib";
lib_exe_obj.addObjectFile(%%os.path.join(b.allocator, cmake_binary_dir, "zig_cpp",
b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt())));
lib_exe_obj.addObjectFile(os.path.join(b.allocator, cmake_binary_dir, "zig_cpp",
b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt())) catch unreachable);
}

const LibraryDep = struct {
Expand All @@ -154,7 +154,7 @@ const LibraryDep = struct {
includes: ArrayList([]const u8),
};

fn findLLVM(b: &Builder, llvm_config_exe: []const u8) -> LibraryDep {
fn findLLVM(b: &Builder, llvm_config_exe: []const u8) -> %LibraryDep {
const libs_output = b.exec([][]const u8{llvm_config_exe, "--libs", "--system-libs"});
const includes_output = b.exec([][]const u8{llvm_config_exe, "--includedir"});
const libdir_output = b.exec([][]const u8{llvm_config_exe, "--libdir"});
Expand All @@ -169,12 +169,12 @@ fn findLLVM(b: &Builder, llvm_config_exe: []const u8) -> LibraryDep {
var it = mem.split(libs_output, " \r\n");
while (it.next()) |lib_arg| {
if (mem.startsWith(u8, lib_arg, "-l")) {
%%result.system_libs.append(lib_arg[2..]);
try result.system_libs.append(lib_arg[2..]);
} else {
if (os.path.isAbsolute(lib_arg)) {
%%result.libs.append(lib_arg);
try result.libs.append(lib_arg);
} else {
%%result.system_libs.append(lib_arg);
try result.system_libs.append(lib_arg);
}
}
}
Expand All @@ -183,19 +183,19 @@ fn findLLVM(b: &Builder, llvm_config_exe: []const u8) -> LibraryDep {
var it = mem.split(includes_output, " \r\n");
while (it.next()) |include_arg| {
if (mem.startsWith(u8, include_arg, "-I")) {
%%result.includes.append(include_arg[2..]);
try result.includes.append(include_arg[2..]);
} else {
%%result.includes.append(include_arg);
try result.includes.append(include_arg);
}
}
}
{
var it = mem.split(libdir_output, " \r\n");
while (it.next()) |libdir| {
if (mem.startsWith(u8, libdir, "-L")) {
%%result.libdirs.append(libdir[2..]);
try result.libdirs.append(libdir[2..]);
} else {
%%result.libdirs.append(libdir);
try result.libdirs.append(libdir);
}
}
}
Expand All @@ -205,17 +205,17 @@ fn findLLVM(b: &Builder, llvm_config_exe: []const u8) -> LibraryDep {
pub fn installStdLib(b: &Builder, stdlib_files: []const u8) {
var it = mem.split(stdlib_files, ";");
while (it.next()) |stdlib_file| {
const src_path = %%os.path.join(b.allocator, "std", stdlib_file);
const dest_path = %%os.path.join(b.allocator, "lib", "zig", "std", stdlib_file);
const src_path = os.path.join(b.allocator, "std", stdlib_file) catch unreachable;
const dest_path = os.path.join(b.allocator, "lib", "zig", "std", stdlib_file) catch unreachable;
b.installFile(src_path, dest_path);
}
}

pub fn installCHeaders(b: &Builder, c_header_files: []const u8) {
var it = mem.split(c_header_files, ";");
while (it.next()) |c_header_file| {
const src_path = %%os.path.join(b.allocator, "c_headers", c_header_file);
const dest_path = %%os.path.join(b.allocator, "lib", "zig", "include", c_header_file);
const src_path = os.path.join(b.allocator, "c_headers", c_header_file) catch unreachable;
const dest_path = os.path.join(b.allocator, "lib", "zig", "include", c_header_file) catch unreachable;
b.installFile(src_path, dest_path);
}
}
Expand Down
14 changes: 7 additions & 7 deletions doc/docgen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ const os = std.os;

pub fn main() -> %void {
// TODO use a more general purpose allocator here
var inc_allocator = %%std.heap.IncrementingAllocator.init(5 * 1024 * 1024);
var inc_allocator = try std.heap.IncrementingAllocator.init(5 * 1024 * 1024);
defer inc_allocator.deinit();
const allocator = &inc_allocator.allocator;

var args_it = os.args();

if (!args_it.skip()) @panic("expected self arg");

const in_file_name = %%(args_it.next(allocator) ?? @panic("expected input arg"));
const in_file_name = try (args_it.next(allocator) ?? @panic("expected input arg"));
defer allocator.free(in_file_name);

const out_file_name = %%(args_it.next(allocator) ?? @panic("expected output arg"));
const out_file_name = try (args_it.next(allocator) ?? @panic("expected output arg"));
defer allocator.free(out_file_name);

var in_file = %%io.File.openRead(in_file_name, allocator);
var in_file = try io.File.openRead(in_file_name, allocator);
defer in_file.close();

var out_file = %%io.File.openWrite(out_file_name, allocator);
var out_file = try io.File.openWrite(out_file_name, allocator);
defer out_file.close();

var file_in_stream = io.FileInStream.init(&in_file);
Expand All @@ -31,7 +31,7 @@ pub fn main() -> %void {
var buffered_out_stream = io.BufferedOutStream.init(&file_out_stream.stream);

gen(&buffered_in_stream.stream, &buffered_out_stream.stream);
%%buffered_out_stream.flush();
try buffered_out_stream.flush();

}

Expand All @@ -54,7 +54,7 @@ fn gen(in: &io.InStream, out: &io.OutStream) {
switch (state) {
State.Start => switch (byte) {
else => {
%%out.writeByte(byte);
out.writeByte(byte) catch unreachable;
},
},
State.Derp => unreachable,
Expand Down
2 changes: 1 addition & 1 deletion doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -5989,7 +5989,7 @@ ContainerInitBody = list(StructLiteralField, ",") | list(Expression, ",")

StructLiteralField = "." Symbol "=" Expression

PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "%" | "%%" | "??" | "-%" | "try"
PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "%" | "??" | "-%" | "try"

PrimaryExpression = Integer | Float | String | CharLiteral | KeywordLiteral | GroupedExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | FnProto | AsmExpression | ("error" "." Symbol) | ContainerDecl | ("continue" option(":" Symbol))

Expand Down
5 changes: 4 additions & 1 deletion example/guess_number/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub fn main() -> %void {
try stdout.print("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..]) catch |err| {
std.debug.warn("unable to seed random number generator: {}", err);
return err;
};
const seed = std.mem.readInt(seed_bytes, usize, builtin.Endian.Big);
var rand = Rand.init(seed);

Expand Down
1 change: 0 additions & 1 deletion src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ enum PrefixOp {
PrefixOpDereference,
PrefixOpMaybe,
PrefixOpError,
PrefixOpUnwrapError,
PrefixOpUnwrapMaybe,
};

Expand Down
2 changes: 1 addition & 1 deletion src/analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2587,7 +2587,7 @@ TypeTableEntry *get_test_fn_type(CodeGen *g) {
return g->test_fn_type;

FnTypeId fn_type_id = {0};
fn_type_id.return_type = g->builtin_types.entry_void;
fn_type_id.return_type = get_error_type(g, g->builtin_types.entry_void);
g->test_fn_type = get_fn_type(g, &fn_type_id);
return g->test_fn_type;
}
Expand Down
1 change: 0 additions & 1 deletion src/ast_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ static const char *prefix_op_str(PrefixOp prefix_op) {
case PrefixOpDereference: return "*";
case PrefixOpMaybe: return "?";
case PrefixOpError: return "%";
case PrefixOpUnwrapError: return "catch";
case PrefixOpUnwrapMaybe: return "??";
}
zig_unreachable();
Expand Down
2 changes: 0 additions & 2 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3963,8 +3963,6 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNod
return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpMaybe), lval);
case PrefixOpError:
return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpError), lval);
case PrefixOpUnwrapError:
return ir_gen_err_assert_ok(irb, scope, node, node->data.prefix_op_expr.primary_expr, lval);
case PrefixOpUnwrapMaybe:
return ir_gen_maybe_assert_ok(irb, scope, node, lval);
}
Expand Down
1 change: 0 additions & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,6 @@ static PrefixOp tok_to_prefix_op(Token *token) {
case TokenIdStar: return PrefixOpDereference;
case TokenIdMaybe: return PrefixOpMaybe;
case TokenIdPercent: return PrefixOpError;
case TokenIdPercentPercent: return PrefixOpUnwrapError;
case TokenIdDoubleQuestion: return PrefixOpUnwrapMaybe;
case TokenIdStarStar: return PrefixOpDereference;
default: return PrefixOpInvalid;
Expand Down
6 changes: 0 additions & 6 deletions src/tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,11 +816,6 @@ void tokenize(Buf *buf, Tokenization *out) {
end_token(&t);
t.state = TokenizeStateStart;
break;
case '%':
set_token_id(&t, t.cur_tok, TokenIdPercentPercent);
end_token(&t);
t.state = TokenizeStateStart;
break;
default:
t.pos -= 1;
end_token(&t);
Expand Down Expand Up @@ -1564,7 +1559,6 @@ const char * token_name(TokenId id) {
case TokenIdNumberSign: return "#";
case TokenIdPercent: return "%";
case TokenIdPercentDot: return "%.";
case TokenIdPercentPercent: return "%%";
case TokenIdPlus: return "+";
case TokenIdPlusEq: return "+=";
case TokenIdPlusPercent: return "+%";
Expand Down
1 change: 0 additions & 1 deletion src/tokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ enum TokenId {
TokenIdNumberSign,
TokenIdPercent,
TokenIdPercentDot,
TokenIdPercentPercent,
TokenIdPlus,
TokenIdPlusEq,
TokenIdPlusPercent,
Expand Down
6 changes: 3 additions & 3 deletions std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ test "basic ArrayList test" {
defer list.deinit();

{var i: usize = 0; while (i < 10) : (i += 1) {
%%list.append(i32(i + 1));
list.append(i32(i + 1)) catch unreachable;
}}

{var i: usize = 0; while (i < 10) : (i += 1) {
Expand All @@ -126,13 +126,13 @@ test "basic ArrayList test" {
assert(list.pop() == 10);
assert(list.len == 9);

%%list.appendSlice([]const i32 { 1, 2, 3 });
list.appendSlice([]const i32 { 1, 2, 3 }) catch unreachable;
assert(list.len == 12);
assert(list.pop() == 3);
assert(list.pop() == 2);
assert(list.pop() == 1);
assert(list.len == 9);

%%list.appendSlice([]const i32 {});
list.appendSlice([]const i32 {}) catch unreachable;
assert(list.len == 9);
}
6 changes: 3 additions & 3 deletions std/base64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub const Base64Decoder = struct {
/// invalid characters result in error.InvalidCharacter.
/// invalid padding results in error.InvalidPadding.
pub fn decode(decoder: &const Base64Decoder, dest: []u8, source: []const u8) -> %void {
assert(dest.len == %%decoder.calcSize(source));
assert(dest.len == (decoder.calcSize(source) catch unreachable));
assert(source.len % 4 == 0);

var src_cursor: usize = 0;
Expand Down Expand Up @@ -374,8 +374,8 @@ fn calcDecodedSizeExactUnsafe(source: []const u8, pad_char: u8) -> usize {

test "base64" {
@setEvalBranchQuota(5000);
%%testBase64();
comptime %%testBase64();
testBase64() catch unreachable;
comptime (testBase64() catch unreachable);
}

fn testBase64() -> %void {
Expand Down
12 changes: 6 additions & 6 deletions std/buffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,20 @@ pub const Buffer = struct {
test "simple Buffer" {
const cstr = @import("cstr.zig");

var buf = %%Buffer.init(debug.global_allocator, "");
var buf = try Buffer.init(debug.global_allocator, "");
assert(buf.len() == 0);
%%buf.append("hello");
%%buf.appendByte(' ');
%%buf.append("world");
try buf.append("hello");
try buf.appendByte(' ');
try buf.append("world");
assert(buf.eql("hello world"));
assert(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst()));

var buf2 = %%Buffer.initFromBuffer(&buf);
var buf2 = try Buffer.initFromBuffer(&buf);
assert(buf.eql(buf2.toSliceConst()));

assert(buf.startsWith("hell"));
assert(buf.endsWith("orld"));

%%buf2.resize(4);
try buf2.resize(4);
assert(buf.startsWith(buf2.toSliceConst()));
}
Loading

0 comments on commit 3c09411

Please sign in to comment.