Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/codegen/zig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ impl ZigBackend {
let mut out = String::new();
if needs_std {
out.push_str("const std = @import(\"std\");\n");
out.push_str("const __zyre_runtime = @import(\"zyre_runtime.zig\");\n");
}
for (name, path) in &user_imports {
out.push_str(&format!("const {} = @import(\"{}\");\n", name, path));
Expand Down Expand Up @@ -291,6 +292,8 @@ impl ZigBackend {
if !body_stmts.is_empty() && !has_explicit_main {
let needs_alloc = self.uses_allocator(&body_stmts);
out.push_str("pub fn main() !void {\n");
out.push_str(" try __zyre_runtime.Output.init();\n");
out.push_str(" defer __zyre_runtime.Output.restore();\n");
out.push_str(&Self::gen_arena_setup(needs_alloc));
for stmt in &body_stmts {
out.push_str(&self.gen_stmt(stmt, 1));
Expand Down Expand Up @@ -350,6 +353,8 @@ impl ZigBackend {
let mut out = format!("{}fn {}({}) {} {{\n", pub_prefix, f.name, params_str, ret);

if f.name == "main" {
out.push_str(" try __zyre_runtime.Output.init();\n");
out.push_str(" defer __zyre_runtime.Output.restore();\n");
out.push_str(&Self::gen_arena_setup(needs_alloc));
}

Expand Down
4 changes: 4 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ pub fn collect_zy_imports(
result
}

const ZYRE_RUNTIME: &str = include_str!("../runtime/zyre_runtime.zig");

/// Generates a .zig file in the cache from a pre-parsed AST. Returns (stem, zig_path).
pub fn emit_zig(input_path: &str, ast: &crate::parser::Program) -> (String, String) {
std::fs::create_dir_all("zyre-cache").unwrap();
std::fs::write("zyre-cache/zyre_runtime.zig", ZYRE_RUNTIME)
.unwrap_or_else(|e| panic!("Failed to write zyre_runtime.zig: {}", e));

let mut visited = std::collections::HashSet::new();
let source_dir = Path::new(input_path).parent().unwrap_or(Path::new("."));
Expand Down
55 changes: 55 additions & 0 deletions src/runtime/zyre_runtime.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const std = @import("std");
const builtin = @import("builtin");

const is_windows = builtin.os.tag == .windows;

pub const Output = struct {
pub fn init() !void {
if (comptime is_windows) {
try WindowsOutput.init();
}
}
pub fn restore() void {
if (comptime is_windows) {
WindowsOutput.restore();
}
}
};

const WindowsOutput = struct {
const win = std.os.windows;
const k32 = win.kernel32;
var console_output_cp: c_uint = @as(u32, 0);

fn setAbortSignalHandler(comptime handler: *const fn () void) !void {
const handler_routine = struct {
fn handler_routine(dwCtrlType: win.DWORD) callconv(.winapi) win.BOOL {
if (dwCtrlType == win.CTRL_C_EVENT) {
handler();
return win.TRUE;
} else {
return win.FALSE;
}
}
}.handler_routine;

try win.SetConsoleCtrlHandler(handler_routine, true);
}

fn abortSignalHandler() void {
restore();
std.process.exit(130);
}

pub fn init() !void {
const CP_UTF8 = 65001;
console_output_cp = k32.GetConsoleOutputCP();
try setAbortSignalHandler(abortSignalHandler);
_ = k32.SetConsoleOutputCP(CP_UTF8);
}

pub fn restore() void {
if (console_output_cp != 0)
_ = k32.SetConsoleOutputCP(console_output_cp);
}
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/alloc_propagate.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -18,6 +20,8 @@ fn readIt(__zyre_allocator: std.mem.Allocator) ![]const u8 {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/array.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -14,6 +16,8 @@ fn __zyre_print(val: anytype) void {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/enum_decl.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -30,6 +32,8 @@ fn describe(d: Direction) void {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/export_const.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -18,6 +20,8 @@ const b = 3;
pub const c = (a * b);

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/fn_call.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -18,6 +20,8 @@ fn add(a: i32, b: i32) i32 {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/if_stmt.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -14,6 +16,8 @@ fn __zyre_print(val: anytype) void {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/print_int.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -14,6 +16,8 @@ fn __zyre_print(val: anytype) void {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/print_str.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -14,6 +16,8 @@ fn __zyre_print(val: anytype) void {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/read_text_file.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -14,6 +16,8 @@ fn __zyre_print(val: anytype) void {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/struct_decl.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -23,6 +25,8 @@ fn getX(p: Point) i32 {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/switch_stmt.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -22,6 +24,8 @@ fn check(n: i32) void {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/while_loop.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
Expand All @@ -14,6 +16,8 @@ fn __zyre_print(val: anytype) void {
}

pub fn main() !void {
try __zyre_runtime.Output.init();
defer __zyre_runtime.Output.restore();
var __zyre_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer __zyre_arena.deinit();
const __zyre_allocator = __zyre_arena.allocator();
Expand Down