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
15 changes: 2 additions & 13 deletions src/codegen/zig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ impl ZigBackend {
if needs_std {
out.push_str("const std = @import(\"std\");\n");
out.push_str("const __zyre_runtime = @import(\"zyre_runtime.zig\");\n");
out.push_str("const __zyre_std_debug = @import(\"zyre_std_debug.zig\");\n");
out.push_str("const __zyre_std_fs = @import(\"zyre_std_fs.zig\");\n");
}
for (name, path) in &user_imports {
out.push_str(&format!("const {} = @import(\"{}\");\n", name, path));
Expand All @@ -216,19 +218,6 @@ impl ZigBackend {
out.push('\n');
}

// __zyre_print helper (selects {s} or {} based on the value type)
if needs_std {
out.push_str(concat!(
"fn __zyre_print(val: anytype) void {\n",
" if (comptime @typeInfo(@TypeOf(val)) == .pointer) {\n",
" std.debug.print(\"{s}\\n\", .{val});\n",
" } else {\n",
" std.debug.print(\"{}\\n\", .{val});\n",
" }\n",
"}\n\n",
));
}

// Emit hoisted export consts at the top level (preserving declaration order)
for item in program {
if let TopLevel::ConstDecl {
Expand Down
14 changes: 5 additions & 9 deletions src/codegen/zig/stdlib/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ impl ZigBackend {
pub(super) fn gen_fs_call(&mut self, fn_name: &str, args: &[Expr]) -> String {
match fn_name {
"readTextFile" => {
if let Some(arg) = args.first() {
format!(
"std.fs.cwd().readFileAlloc(__zyre_allocator, {}, std.math.maxInt(usize))",
self.gen_expr(arg)
)
} else {
"std.fs.cwd().readFileAlloc(__zyre_allocator, \"\", std.math.maxInt(usize))"
.to_string()
}
let arg = args
.first()
.map(|a| self.gen_expr(a))
.unwrap_or_else(|| "\"\"".to_string());
format!("__zyre_std_fs.readTextFile(__zyre_allocator, {})", arg)
}
_ => {
let args_str = self.gen_args(args);
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/zig/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ impl ZigBackend {
} else {
self.gen_expr(arg)
};
format!("__zyre_print({})", arg_s)
format!("__zyre_std_debug.print({})", arg_s)
} else {
"__zyre_print(\"\")".to_string()
"__zyre_std_debug.print(\"\")".to_string()
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,20 @@ pub fn collect_zy_imports(
}

const ZYRE_RUNTIME: &str = include_str!("../runtime/zyre_runtime.zig");
const ZYRE_STD_DEBUG: &str = include_str!("../runtime/zyre_std_debug.zig");
const ZYRE_STD_FS: &str = include_str!("../runtime/zyre_std_fs.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));
for (name, content) in [
("zyre_runtime.zig", ZYRE_RUNTIME),
("zyre_std_debug.zig", ZYRE_STD_DEBUG),
("zyre_std_fs.zig", ZYRE_STD_FS),
] {
std::fs::write(format!("zyre-cache/{}", name), content)
.unwrap_or_else(|e| panic!("Failed to write {}: {}", name, e));
}

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

pub fn print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{any}\n", .{val});
}
}
5 changes: 5 additions & 0 deletions src/runtime/zyre_std_fs.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const std = @import("std");

pub fn readTextFile(allocator: std.mem.Allocator, path: []const u8) ![]u8 {
return std.fs.cwd().readFileAlloc(allocator, path, std.math.maxInt(usize));
}
18 changes: 11 additions & 7 deletions src/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ fn test_codegen_print_generic_format() {
"#,
);
assert!(
out.contains("__zyre_print("),
"expected __zyre_print call, got:\n{}",
out.contains("__zyre_std_debug.print("),
"expected __zyre_std_debug.print call, got:\n{}",
out
);
assert!(
out.contains("fn __zyre_print("),
"expected __zyre_print definition, got:\n{}",
out.contains("@import(\"zyre_std_debug.zig\")"),
"expected zyre_std_debug import, got:\n{}",
out
);
}
Expand All @@ -78,10 +78,10 @@ fn test_codegen_import_alias() {
let out = compile(
r#"
const s = import("std");
s.print("hello");
s.debug.print("hello");
"#,
);
assert!(out.contains("std.debug.print"), "got:\n{}", out);
assert!(out.contains("__zyre_std_debug.print"), "got:\n{}", out);
assert!(!out.contains("const s = @import"), "got:\n{}", out);
}

Expand Down Expand Up @@ -243,7 +243,11 @@ fn test_codegen_if_expr_as_arg() {
}
"#,
);
assert!(out.contains("__zyre_print(if (flag)"), "got:\n{}", out);
assert!(
out.contains("__zyre_std_debug.print(if (flag)"),
"got:\n{}",
out
);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ 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) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{}\n", .{val});
}
}
const __zyre_std_debug = @import("zyre_std_debug.zig");
const __zyre_std_fs = @import("zyre_std_fs.zig");

fn readIt(__zyre_allocator: std.mem.Allocator) ![]const u8 {
return (try std.fs.cwd().readFileAlloc(__zyre_allocator, "x.txt", std.math.maxInt(usize)));
return (try __zyre_std_fs.readTextFile(__zyre_allocator, "x.txt"));
}

pub fn main() !void {
Expand All @@ -29,5 +23,5 @@ pub fn main() !void {
const data = readIt(__zyre_allocator) catch {
return;
};
__zyre_print(data);
__zyre_std_debug.print(data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ 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) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{}\n", .{val});
}
}
const __zyre_std_debug = @import("zyre_std_debug.zig");
const __zyre_std_fs = @import("zyre_std_fs.zig");

pub fn main() !void {
try __zyre_runtime.Output.init();
Expand All @@ -24,5 +18,5 @@ pub fn main() !void {
_ = __zyre_allocator;

const arr: [3]i32 = .{1, 2, 3};
__zyre_print(arr[0]);
__zyre_std_debug.print(arr[0]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ 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) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{}\n", .{val});
}
}
const __zyre_std_debug = @import("zyre_std_debug.zig");
const __zyre_std_fs = @import("zyre_std_fs.zig");

const Direction = enum {
North,
Expand All @@ -24,10 +18,10 @@ const Direction = enum {

fn describe(d: Direction) void {
switch (d) {
.North => __zyre_print("north"),
.South => __zyre_print("south"),
.East => __zyre_print("east"),
.West => __zyre_print("west"),
.North => __zyre_std_debug.print("north"),
.South => __zyre_std_debug.print("south"),
.East => __zyre_std_debug.print("east"),
.West => __zyre_std_debug.print("west"),
}
}

Expand All @@ -39,5 +33,5 @@ pub fn main() !void {
const __zyre_allocator = __zyre_arena.allocator();
_ = __zyre_allocator;

__zyre_print("ok");
__zyre_std_debug.print("ok");
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ input_file: src/tests/fixtures/explicit_main.zy
---
const std = @import("std");
const __zyre_runtime = @import("zyre_runtime.zig");

fn __zyre_print(val: anytype) void {
if (comptime @typeInfo(@TypeOf(val)) == .pointer) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{}\n", .{val});
}
}
const __zyre_std_debug = @import("zyre_std_debug.zig");
const __zyre_std_fs = @import("zyre_std_fs.zig");

fn __zyre_fn_main() void {
__zyre_print("hello from main");
__zyre_std_debug.print("hello from main");
}

pub fn main() !void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ 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) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{}\n", .{val});
}
}
const __zyre_std_debug = @import("zyre_std_debug.zig");
const __zyre_std_fs = @import("zyre_std_fs.zig");

const a = 2;
const b = 3;
Expand All @@ -27,5 +21,5 @@ pub fn main() !void {
const __zyre_allocator = __zyre_arena.allocator();
_ = __zyre_allocator;

__zyre_print(c);
__zyre_std_debug.print(c);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ 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) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{}\n", .{val});
}
}
const __zyre_std_debug = @import("zyre_std_debug.zig");
const __zyre_std_fs = @import("zyre_std_fs.zig");

fn add(a: i32, b: i32) i32 {
return (a + b);
Expand All @@ -27,5 +21,5 @@ pub fn main() !void {
const __zyre_allocator = __zyre_arena.allocator();
_ = __zyre_allocator;

__zyre_print(add(1, 2));
__zyre_std_debug.print(add(1, 2));
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ 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) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{}\n", .{val});
}
}
const __zyre_std_debug = @import("zyre_std_debug.zig");
const __zyre_std_fs = @import("zyre_std_fs.zig");

pub fn main() !void {
try __zyre_runtime.Output.init();
Expand All @@ -25,6 +19,6 @@ pub fn main() !void {

const x = true;
if (x) {
__zyre_print("yes");
__zyre_std_debug.print("yes");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ 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) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{}\n", .{val});
}
}
const __zyre_std_debug = @import("zyre_std_debug.zig");
const __zyre_std_fs = @import("zyre_std_fs.zig");

pub fn main() !void {
try __zyre_runtime.Output.init();
Expand All @@ -24,5 +18,5 @@ pub fn main() !void {
_ = __zyre_allocator;

const x: i32 = 42;
__zyre_print(x);
__zyre_std_debug.print(x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ 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) {
std.debug.print("{s}\n", .{val});
} else {
std.debug.print("{}\n", .{val});
}
}
const __zyre_std_debug = @import("zyre_std_debug.zig");
const __zyre_std_fs = @import("zyre_std_fs.zig");

pub fn main() !void {
try __zyre_runtime.Output.init();
Expand All @@ -23,5 +17,5 @@ pub fn main() !void {
const __zyre_allocator = __zyre_arena.allocator();
_ = __zyre_allocator;

__zyre_print("hello");
__zyre_std_debug.print("hello");
}
Loading
Loading