Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const std = @import("std"); | ||
const io = std.io; | ||
const mem = std.mem; | ||
const os = std.os; | ||
const warn = std.debug.warn; | ||
const allocator = std.debug.global_allocator; | ||
|
||
pub fn main() !void { | ||
var args_it = os.args(); | ||
const exe = try unwrapArg(args_it.next(allocator).?); | ||
var catted_anything = false; | ||
var stdout_file = try io.getStdOut(); | ||
|
||
while (args_it.next(allocator)) |arg_or_err| { | ||
const arg = try unwrapArg(arg_or_err); | ||
if (mem.eql(u8, arg, "-")) { | ||
catted_anything = true; | ||
var stdin_file = try io.getStdIn(); | ||
try cat_file(&stdout_file, &stdin_file); | ||
} else if (arg[0] == '-') { | ||
return usage(exe); | ||
} else { | ||
var file = os.File.openRead(arg) catch |err| { | ||
warn("Unable to open file: {}\n", @errorName(err)); | ||
return err; | ||
}; | ||
defer file.close(); | ||
|
||
catted_anything = true; | ||
try cat_file(&stdout_file, &file); | ||
} | ||
} | ||
if (!catted_anything) { | ||
var stdin_file = try io.getStdIn(); | ||
try cat_file(&stdout_file, &stdin_file); | ||
} | ||
} | ||
|
||
fn usage(exe: []const u8) !void { | ||
warn("Usage: {} [FILE]...\n", exe); | ||
return error.Invalid; | ||
} | ||
|
||
fn cat_file(stdout: *os.File, file: *os.File) !void { | ||
var buf: [1024 * 4]u8 = undefined; | ||
|
||
while (true) { | ||
const bytes_read = file.read(buf[0..]) catch |err| { | ||
warn("Unable to read from stream: {}\n", @errorName(err)); | ||
return err; | ||
}; | ||
|
||
if (bytes_read == 0) { | ||
break; | ||
} | ||
|
||
stdout.write(buf[0..bytes_read]) catch |err| { | ||
warn("Unable to write to stdout: {}\n", @errorName(err)); | ||
return err; | ||
}; | ||
} | ||
} | ||
|
||
fn unwrapArg(arg: anyerror![]u8) ![]u8 { | ||
return arg catch |err| { | ||
warn("Unable to parse command line: {}\n", err); | ||
return err; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const builtin = @import("builtin"); | ||
const std = @import("std"); | ||
const io = std.io; | ||
const fmt = std.fmt; | ||
const os = std.os; | ||
|
||
pub fn main() !void { | ||
var stdout_file = try io.getStdOut(); | ||
const stdout = &stdout_file.outStream().stream; | ||
|
||
try stdout.print("Welcome to the Guess Number Game in Zig.\n"); | ||
|
||
var seed_bytes: [@sizeOf(u64)]u8 = undefined; | ||
os.getRandomBytes(seed_bytes[0..]) catch |err| { | ||
std.debug.warn("unable to seed random number generator: {}", err); | ||
return err; | ||
}; | ||
const seed = std.mem.readIntNative(u64, &seed_bytes); | ||
var prng = std.rand.DefaultPrng.init(seed); | ||
|
||
const answer = prng.random.range(u8, 0, 100) + 1; | ||
|
||
while (true) { | ||
try stdout.print("\nGuess a number between 1 and 100: "); | ||
var line_buf: [20]u8 = undefined; | ||
|
||
const line = io.readLineSlice(line_buf[0..]) catch |err| switch (err) { | ||
error.OutOfMemory => { | ||
try stdout.print("Input too long.\n"); | ||
continue; | ||
}, | ||
else => return err, | ||
}; | ||
|
||
const guess = fmt.parseUnsigned(u8, line, 10) catch { | ||
try stdout.print("Invalid number.\n"); | ||
continue; | ||
}; | ||
if (guess > answer) { | ||
try stdout.print("Guess lower.\n"); | ||
} else if (guess < answer) { | ||
try stdout.print("Guess higher.\n"); | ||
} else { | ||
try stdout.print("You win!\n"); | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const std = @import("std"); | ||
|
||
pub fn main() !void { | ||
// If this program is run without stdout attached, exit with an error. | ||
var stdout_file = try std.io.getStdOut(); | ||
// If this program encounters pipe failure when printing to stdout, exit | ||
// with an error. | ||
try stdout_file.write("Hello, world!\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule sublime-zig-language
added at
29b9a0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
type: grammar | ||
name: sublime-zig-language | ||
version: 29b9a012613da924bafd68de1f9a3f27ceccf126 | ||
license: mit | ||
--- | ||
The MIT License (Expat) | ||
|
||
Copyright (c) 2018 Jeff Fowler | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |