6 Commits

4 changed files with 13 additions and 8 deletions
+1
View File
@@ -81,6 +81,7 @@ pub fn build(b: *Build) !void {
const run = b.addRunArtifact(elrond); const run = b.addRunArtifact(elrond);
run.addArg(b.fmt("--zig={s}", .{b.graph.zig_exe})); run.addArg(b.fmt("--zig={s}", .{b.graph.zig_exe}));
run.addArg(b.fmt("--work-path={s}", .{work_path})); run.addArg(b.fmt("--work-path={s}", .{work_path}));
run.addArg(b.fmt("--root-path={s}", .{b.root.root_dir.path.?}));
if (exno) |n| { if (exno) |n| {
run.addArg(b.fmt("--only={d}", .{n})); run.addArg(b.fmt("--only={d}", .{n}));
+1 -1
View File
@@ -94,7 +94,7 @@ pub fn main() void {
print("He has room in his heart for:", .{}); print("He has room in his heart for:", .{});
// `field_names` is a slice of strings and it holds the names of the struct's fields // `field_names` is a slice of strings and it holds the names of the struct's fields
// `field_types` is a slice of strings and it holds the types of the struct's fields, // `field_types` is a slice of types and it holds the types of the struct's fields,
// it is guaranteed to be the same length as `field_names` // it is guaranteed to be the same length as `field_names`
const field_names = @typeInfo(Narcissus).@"struct".field_names; const field_names = @typeInfo(Narcissus).@"struct".field_names;
const field_types = @typeInfo(Narcissus).@"struct".field_types; const field_types = @typeInfo(Narcissus).@"struct".field_types;
+5 -6
View File
@@ -75,10 +75,10 @@ pub fn main() !void {
std.debug.print("Starting work...\n", .{}); std.debug.print("Starting work...\n", .{});
// These curly braces are very important, they are necessary // These curly braces are very important, they are necessary
// to enclose the area where the threads are called. // to enclose the area where the threads are called and joined.
// Without these braces, the program would not wait for the // With these braces, the program will block and wait for all threads
// end of the threads and they would continue to run beyond the // to finish right at the closing brace of this block, ensuring
// end of the program. // "Zig is cool!" is always printed last.
{ {
// Now we start the first thread, with the number as parameter // Now we start the first thread, with the number as parameter
const handle = try std.Thread.spawn(.{}, thread_function, .{1}); const handle = try std.Thread.spawn(.{}, thread_function, .{1});
@@ -102,8 +102,7 @@ pub fn main() !void {
try io.sleep(std.Io.Duration.fromMilliseconds(400), .awake); try io.sleep(std.Io.Duration.fromMilliseconds(400), .awake);
std.debug.print("Some weird stuff, after starting the threads.\n", .{}); std.debug.print("Some weird stuff, after starting the threads.\n", .{});
} }
// After we have left the closed area, we wait until // The threads are guaranteed to be finished by the time we reach here.
// the threads have run through, if this has not yet been the case.
std.debug.print("Zig is cool!\n", .{}); std.debug.print("Zig is cool!\n", .{});
} }
+6 -1
View File
@@ -109,6 +109,7 @@ const Context = struct {
arena: std.mem.Allocator, arena: std.mem.Allocator,
zig_exe: []const u8, zig_exe: []const u8,
work_path: []const u8, work_path: []const u8,
root_path: []const u8,
}; };
const Error = error{Failed}; const Error = error{Failed};
@@ -158,6 +159,7 @@ pub fn main(init: std.process.Init) !void {
var zig_exe: []const u8 = "zig"; var zig_exe: []const u8 = "zig";
var work_path: []const u8 = "exercises"; var work_path: []const u8 = "exercises";
var root_path: []const u8 = "";
var mode: Mode = .normal; var mode: Mode = .normal;
var only_n: ?usize = null; var only_n: ?usize = null;
var start_n: ?usize = null; var start_n: ?usize = null;
@@ -170,6 +172,8 @@ pub fn main(init: std.process.Init) !void {
zig_exe = v; zig_exe = v;
} else if (cutPrefix(u8, arg, "--work-path=")) |v| { } else if (cutPrefix(u8, arg, "--work-path=")) |v| {
work_path = v; work_path = v;
} else if (cutPrefix(u8, arg, "--root-path=")) |v| {
root_path = v;
} else if (cutPrefix(u8, arg, "--only=")) |v| { } else if (cutPrefix(u8, arg, "--only=")) |v| {
only_n = std.fmt.parseInt(usize, v, 10) catch { only_n = std.fmt.parseInt(usize, v, 10) catch {
print("invalid --only value: {s}\n", .{v}); print("invalid --only value: {s}\n", .{v});
@@ -197,6 +201,7 @@ pub fn main(init: std.process.Init) !void {
.arena = arena, .arena = arena,
.zig_exe = zig_exe, .zig_exe = zig_exe,
.work_path = work_path, .work_path = work_path,
.root_path = root_path,
}; };
switch (mode) { switch (mode) {
@@ -310,7 +315,7 @@ fn runExe(ctx: Context, ex: Exercise) !void {
const arena = ctx.arena; const arena = ctx.arena;
print("Compiling {s}...\n", .{ex.main_file}); print("Compiling {s}...\n", .{ex.main_file});
const path = std.fs.path.join(arena, &.{ ctx.work_path, ex.main_file }) catch @panic("OOM"); const path = std.fs.path.join(arena, &.{ ctx.root_path, ctx.work_path, ex.main_file }) catch @panic("OOM");
var argv = std.ArrayList([]const u8).initCapacity(arena, 8) catch @panic("OOM"); var argv = std.ArrayList([]const u8).initCapacity(arena, 8) catch @panic("OOM");
argv.append(arena, ctx.zig_exe) catch @panic("OOM"); argv.append(arena, ctx.zig_exe) catch @panic("OOM");