Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ziglang/zig
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f1072d0d9fba
Choose a base ref
...
head repository: ziglang/zig
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9e234d420872
Choose a head ref
  • 7 commits
  • 29 files changed
  • 1 contributor

Commits on Oct 21, 2017

  1. self hosted zig: print usage

    andrewrk committed Oct 21, 2017
    Copy the full SHA
    92751d5 View commit details

Commits on Oct 24, 2017

  1. wip self hosted code

    andrewrk committed Oct 24, 2017
    Copy the full SHA
    4f4da3c View commit details

Commits on Oct 27, 2017

  1. Copy the full SHA
    4c306af View commit details
  2. Copy the full SHA
    540bac0 View commit details
  3. delete -municode command line argument

    The solution to this is to always have it on and only
    use the 'W' versions of respective windows APIs.
    
    See the issue for this.
    andrewrk committed Oct 27, 2017
    Copy the full SHA
    1a414c7 View commit details
  4. Copy the full SHA
    7a96aca View commit details

Commits on Oct 31, 2017

  1. breaking change to std.io API

     * Merge io.InStream and io.OutStream into io.File
     * Introduce io.OutStream and io.InStream interfaces
       - io.File implements both of these
     * Move mem.IncrementingAllocator to heap.IncrementingAllocator
    
    Instead of:
    
    ```
    %return std.io.stderr.printf("hello\n");
    ```
    
    now do:
    
    ```
    std.debug.warn("hello\n");
    ```
    
    To print to stdout, see `io.getStdOut()`.
    
     * Rename std.ArrayList.resizeDown to std.ArrayList.shrink.
    andrewrk committed Oct 31, 2017
    Copy the full SHA
    9e234d4 View commit details
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -521,6 +521,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/fmt/errol/index.zig" DESTINATION "${ZIG_S
install(FILES "${CMAKE_SOURCE_DIR}/std/fmt/errol/lookup.zig" DESTINATION "${ZIG_STD_DEST}/fmt/errol")
install(FILES "${CMAKE_SOURCE_DIR}/std/fmt/index.zig" DESTINATION "${ZIG_STD_DEST}/fmt")
install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/heap.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/index.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/linked_list.zig" DESTINATION "${ZIG_STD_DEST}")
@@ -605,10 +606,10 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/fixunstfdi.zig" DESTI
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/fixunstfsi.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/fixunstfti.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/index.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivti3.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmod.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmoddi4.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmodti4.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivti3.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/umodti3.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/panic.zig" DESTINATION "${ZIG_STD_DEST}/special")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/test_runner.zig" DESTINATION "${ZIG_STD_DEST}/special")
10 changes: 10 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -2,6 +2,16 @@ const Builder = @import("std").build.Builder;
const tests = @import("test/tests.zig");

pub fn build(b: &Builder) {
const mode = b.standardReleaseOptions();

var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
exe.setBuildMode(mode);
exe.linkSystemLibrary("c");
b.default_step.dependOn(&exe.step);

b.installArtifact(exe);


const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
const with_lldb = b.option(bool, "with-lldb", "Run tests in LLDB to get a backtrace if one fails") ?? false;
const test_step = b.step("test", "Run all the tests");
31 changes: 18 additions & 13 deletions example/cat/main.zig
Original file line number Diff line number Diff line change
@@ -2,64 +2,69 @@ const std = @import("std");
const io = std.io;
const mem = std.mem;
const os = std.os;
const warn = std.debug.warn;

pub fn main() -> %void {
const allocator = &std.debug.global_allocator;
var args_it = os.args();
const exe = %return unwrapArg(??args_it.next(allocator));
var catted_anything = false;
var stdout_file = %return io.getStdOut();
const stdout = &stdout_file.out_stream;

while (args_it.next(allocator)) |arg_or_err| {
const arg = %return unwrapArg(arg_or_err);
if (mem.eql(u8, arg, "-")) {
catted_anything = true;
%return cat_stream(&io.stdin);
var stdin_file = %return io.getStdIn();
%return cat_stream(stdout, &stdin_file.in_stream);
} else if (arg[0] == '-') {
return usage(exe);
} else {
var is = io.InStream.open(arg, null) %% |err| {
%%io.stderr.printf("Unable to open file: {}\n", @errorName(err));
var file = io.File.openRead(arg, null) %% |err| {
warn("Unable to open file: {}\n", @errorName(err));
return err;
};
defer is.close();
defer file.close();

catted_anything = true;
%return cat_stream(&is);
%return cat_stream(stdout, &file.in_stream);
}
}
if (!catted_anything) {
%return cat_stream(&io.stdin);
var stdin_file = %return io.getStdIn();
%return cat_stream(stdout, &stdin_file.in_stream);
}
%return io.stdout.flush();
}

fn usage(exe: []const u8) -> %void {
%%io.stderr.printf("Usage: {} [FILE]...\n", exe);
warn("Usage: {} [FILE]...\n", exe);
return error.Invalid;
}

fn cat_stream(is: &io.InStream) -> %void {
fn cat_stream(stdout: &io.OutStream, is: &io.InStream) -> %void {
var buf: [1024 * 4]u8 = undefined;

while (true) {
const bytes_read = is.read(buf[0..]) %% |err| {
%%io.stderr.printf("Unable to read from stream: {}\n", @errorName(err));
warn("Unable to read from stream: {}\n", @errorName(err));
return err;
};

if (bytes_read == 0) {
break;
}

io.stdout.write(buf[0..bytes_read]) %% |err| {
%%io.stderr.printf("Unable to write to stdout: {}\n", @errorName(err));
stdout.write(buf[0..bytes_read]) %% |err| {
warn("Unable to write to stdout: {}\n", @errorName(err));
return err;
};
}
}

fn unwrapArg(arg: %[]u8) -> %[]u8 {
return arg %% |err| {
%%io.stderr.printf("Unable to parse command line: {}\n", err);
warn("Unable to parse command line: {}\n", err);
return err;
};
}
22 changes: 14 additions & 8 deletions example/guess_number/main.zig
Original file line number Diff line number Diff line change
@@ -5,7 +5,13 @@ const Rand = std.rand.Rand;
const os = std.os;

pub fn main() -> %void {
%%io.stdout.printf("Welcome to the Guess Number Game in Zig.\n");
var stdout_file = %return io.getStdOut();
const stdout = &stdout_file.out_stream;

var stdin_file = %return io.getStdIn();
const stdin = &stdin_file.in_stream;

%return stdout.print("Welcome to the Guess Number Game in Zig.\n");

var seed_bytes: [@sizeOf(usize)]u8 = undefined;
%%os.getRandomBytes(seed_bytes[0..]);
@@ -15,24 +21,24 @@ pub fn main() -> %void {
const answer = rand.range(u8, 0, 100) + 1;

while (true) {
%%io.stdout.printf("\nGuess a number between 1 and 100: ");
%return stdout.print("\nGuess a number between 1 and 100: ");
var line_buf : [20]u8 = undefined;

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

const guess = fmt.parseUnsigned(u8, line_buf[0..line_len - 1], 10) %% {
%%io.stdout.printf("Invalid number.\n");
%return stdout.print("Invalid number.\n");
continue;
};
if (guess > answer) {
%%io.stdout.printf("Guess lower.\n");
%return stdout.print("Guess lower.\n");
} else if (guess < answer) {
%%io.stdout.printf("Guess higher.\n");
%return stdout.print("Guess higher.\n");
} else {
%%io.stdout.printf("You win!\n");
%return stdout.print("You win!\n");
return;
}
}
9 changes: 7 additions & 2 deletions example/hello_world/hello.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const io = @import("std").io;
const std = @import("std");

pub fn main() -> %void {
%return io.stdout.printf("Hello, world!\n");
// If this program is run without stdout attached, exit with an error.
var stdout_file = %return std.io.getStdOut();
const stdout = &stdout_file.out_stream;
// If this program encounters pipe failure when printing to stdout, exit
// with an error.
%return stdout.print("Hello, world!\n");
}
Loading