Skip to content

Commit

Permalink
update to zig version 0.12.0-dev.1297+a9e66ed73; Fix compile errors o…
Browse files Browse the repository at this point in the history
…n Windows
  • Loading branch information
der-teufel-programming authored and booniepepper committed Oct 28, 2023
1 parent 28511c2 commit 41adb65
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
12 changes: 7 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ pub fn build(b: *std.Build) !void {

const cross_install = b.addInstallArtifact(cross, .{});

const cross_tar = b.addSystemCommand(&.{ "tar", "--transform", "s|" ++ exe ++ "|dt|", "-czvf", exe ++ ".tgz", switch (cross.target.cpu_arch.?) {
.wasm32 => exe ++ ".wasm",
else => exe,
} });
cross_tar.cwd = "./zig-out/bin/";
const cross_tar = b.addSystemCommand(&.{
"tar", "--transform", "s|" ++ exe ++ "|dt|", "-czvf", exe ++ ".tgz", switch (cross.target.cpu_arch.?) {
.wasm32 => exe ++ ".wasm",
else => exe,
},
});
cross_tar.setCwd(.{ .path = "./zig-out/bin/" });

cross_tar.step.dependOn(&cross_install.step);
cross_step.dependOn(&cross_tar.step);
Expand Down
1 change: 1 addition & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.{
.name = "dt",
.version = "1.3.0", // Update in main.zig as well
.paths = .{ "" },
}
14 changes: 8 additions & 6 deletions src/builtins.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const std = @import("std");
const ArrayList = std.ArrayList;
const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer();

const interpret = @import("interpret.zig");
const Command = interpret.Command;
Expand Down Expand Up @@ -130,6 +127,7 @@ pub fn quit(dt: *DtMachine) !void {
const ctx = try dt.popContext();

if (ctx.items.len > 0) {
const stderr = std.io.getStdErr().writer();
try dt.red();
try stderr.print("warning(quit): Exited with unused values: [ ", .{});

Expand Down Expand Up @@ -365,7 +363,7 @@ pub fn exec(dt: *DtMachine) !void {

while (childArgs.next()) |arg| try argv.append(arg);

var result = std.process.Child.exec(.{
var result = std.process.Child.run(.{
.allocator = dt.alloc,
.argv = argv.items,
}) catch |e| return dt.rewind(log, val, e);
Expand Down Expand Up @@ -546,11 +544,13 @@ pub fn rot(dt: *DtMachine) !void {

pub fn p(dt: *DtMachine) !void {
const val = try dt.pop();
const stdout = std.io.getStdOut().writer();
try _p(val, stdout);
}

pub fn ep(dt: *DtMachine) !void {
const val = try dt.pop();
const stderr = std.io.getStdErr().writer();
try _p(val, stderr);
}

Expand All @@ -575,6 +575,7 @@ pub fn norm(dt: *DtMachine) !void {
}

pub fn @".s"(dt: *DtMachine) !void {
const stderr = std.io.getStdErr().writer();
try stderr.print("[ ", .{});

var top = dt.nest.first orelse {
Expand All @@ -592,6 +593,7 @@ pub fn @".s"(dt: *DtMachine) !void {

pub fn rl(dt: *DtMachine) !void {
var line = ArrayList(u8).init(dt.alloc);
const stdin = std.io.getStdIn().reader();
try stdin.streamUntilDelimiter(line.writer(), '\n', null);

try dt.push(.{ .string = line.items });
Expand Down Expand Up @@ -768,13 +770,13 @@ pub fn abs(dt: *DtMachine) !void {
if (val.isInt()) {
const a = try val.intoInt();

try dt.push(.{ .int = try std.math.absInt(a) });
try dt.push(.{ .int = @intCast(@abs(a)) });
return;
}

const a = val.intoFloat() catch |e| return dt.rewind(log, val, e);

try dt.push(.{ .float = std.math.fabs(a) });
try dt.push(.{ .float = @abs(a) });
}

pub fn rand(dt: *DtMachine) !void {
Expand Down
27 changes: 16 additions & 11 deletions src/interpret.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const ArrayList = std.ArrayList;
const Stack = std.SinglyLinkedList;
const Allocator = std.mem.Allocator;
// const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer();
// const stdout = std.io.getStdOut().writer();
// const stderr = std.io.getStdErr().writer();

const string = @import("string.zig");
const String = string.String;
Expand All @@ -31,6 +31,9 @@ pub const DtMachine = struct {
stdoutConfig: std.io.tty.Config,
stderrConfig: std.io.tty.Config,

stdout: std.fs.File.Writer,
stderr: std.fs.File.Writer,

inspiration: ArrayList(String),

pub fn init(alloc: Allocator) !DtMachine {
Expand All @@ -52,6 +55,8 @@ pub const DtMachine = struct {
.defs = Dictionary.init(alloc),
.stdoutConfig = std.io.tty.detectConfig(std.io.getStdOut()),
.stderrConfig = std.io.tty.detectConfig(std.io.getStdErr()),
.stdout = std.io.getStdOut().writer(),
.stderr = std.io.getStdErr().writer(),
.inspiration = inspirations,
};
}
Expand Down Expand Up @@ -139,21 +144,21 @@ pub const DtMachine = struct {

pub fn red(self: *DtMachine) !void {
try self.norm();
try self.stdoutConfig.setColor(stdout, Color.red);
try self.stderrConfig.setColor(stderr, Color.red);
try self.stdoutConfig.setColor(self.stdout, Color.red);
try self.stderrConfig.setColor(self.stderr, Color.red);
}

pub fn green(self: *DtMachine) !void {
try self.stdoutConfig.setColor(stdout, Color.green);
try self.stdoutConfig.setColor(stdout, Color.bold);
try self.stdoutConfig.setColor(self.stdout, Color.green);
try self.stdoutConfig.setColor(self.stdout, Color.bold);

try self.stderrConfig.setColor(stderr, Color.green);
try self.stdoutConfig.setColor(stdout, Color.bold);
try self.stderrConfig.setColor(self.stderr, Color.green);
try self.stdoutConfig.setColor(self.stdout, Color.bold);
}

pub fn norm(self: *DtMachine) !void {
try self.stdoutConfig.setColor(stdout, Color.reset);
try self.stderrConfig.setColor(stderr, Color.reset);
try self.stdoutConfig.setColor(self.stdout, Color.reset);
try self.stderrConfig.setColor(self.stderr, Color.reset);
}

pub fn child(self: *DtMachine) !DtMachine {
Expand Down Expand Up @@ -202,7 +207,7 @@ pub const DtMachine = struct {

// Removes and returns top N values from the stack from oldest to youngest. Last index is the most recent, 0 is the oldest.
pub fn popN(self: *DtMachine, comptime n: comptime_int) ![n]Val {
var vals: [n]Val = .{};
var vals: [n]Val = undefined;

comptime var i = n - 1;
inline while (i >= 0) : (i -= 1) {
Expand Down
9 changes: 6 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer();

const builtins = @import("builtins.zig");

Expand All @@ -28,6 +25,10 @@ pub fn main() !void {
const stdinPiped = !std.io.getStdIn().isTty();
const stdoutPiped = !std.io.getStdOut().isTty();

if (@import("builtin").os.tag == .windows) {
_ = std.os.windows.kernel32.SetConsoleOutputCP(65001);
}

const firstArgMaybe = try readFirstArg(arena.allocator());

if (firstArgMaybe) |firstArg| {
Expand Down Expand Up @@ -68,6 +69,7 @@ fn readEvalPrintLoop(dt: *DtMachine) !void {
while (true) dt.handleCmd("dt/main-repl") catch |e| switch (e) {
error.EndOfStream => return,
else => {
const stderr = std.io.getStdErr().writer();
try dt.red();
try stderr.print("\nRestarting REPL after error: {s}\n\n", .{@errorName(e)});
try dt.norm();
Expand All @@ -76,6 +78,7 @@ fn readEvalPrintLoop(dt: *DtMachine) !void {
}

fn doneOrDie(dt: *DtMachine, reason: anyerror) !void {
const stderr = std.io.getStdErr().writer();
try stderr.print("\n", .{});
switch (reason) {
error.EndOfStream => {},
Expand Down
10 changes: 5 additions & 5 deletions src/tests/dt_test_utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const allocator = std.heap.page_allocator;

const MAX_FILE_SIZE = 1 << 12;

pub fn dtRunFile(file_path: []const u8) !Child.ExecResult {
pub fn dtRunFile(file_path: []const u8) !Child.RunResult {
const cur_dir = std.fs.cwd();
const contents = try cur_dir.readFileAlloc(allocator, file_path, MAX_FILE_SIZE);
return try dtStdin(contents);
}

pub fn dtStdin(input: []const u8) !Child.ExecResult {
return try Child.exec(.{ .allocator = allocator, .argv = &.{
pub fn dtStdin(input: []const u8) !Child.RunResult {
return try Child.run(.{ .allocator = allocator, .argv = &.{
"./zig-out/bin/dt",
"[\"#\" starts-with? not] filter",
"unwords",
Expand All @@ -20,7 +20,7 @@ pub fn dtStdin(input: []const u8) !Child.ExecResult {
} });
}

pub fn dt(argv: []const []const u8) !Child.ExecResult {
pub fn dt(argv: []const []const u8) !Child.RunResult {
var args = std.ArrayList([]const u8).init(allocator);
defer args.deinit();

Expand All @@ -30,5 +30,5 @@ pub fn dt(argv: []const []const u8) !Child.ExecResult {
try args.append(arg);
}

return try Child.exec(.{ .allocator = allocator, .argv = args.items });
return try Child.run(.{ .allocator = allocator, .argv = args.items });
}

0 comments on commit 41adb65

Please sign in to comment.