interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / tests / native / g1 / caller.zig

36 lines · 1423 bytes Raw Blame History
1// Zig caller for the G1 sample calc: reads "i0 i1" lines, prints "i0 i1 status result" per line.
2// Stdin and stdout go through the C library (std.c.read, std.c.write), which Zig 0.13 and 0.17 share;
3// std.io changed between them.
4const std = @import("std");
5const om = @import("omega_native.zig"); // generated by tools/native, nothing typed by hand
6
7fn put(s: []const u8) void {
8 var off: usize = 0;
9 while (off < s.len) {
10 const n = std.c.write(1, s[off..].ptr, s.len - off);
11 if (n <= 0) std.c.exit(3);
12 off += @intCast(n);
13 }
14}
15
16pub fn main() void {
17 var in: [1 << 16]u8 = undefined;
18 var len: usize = 0;
19 while (len < in.len) {
20 const n = std.c.read(0, in[len..].ptr, in.len - len);
21 if (n < 0) std.c.exit(2);
22 if (n == 0) break;
23 len += @intCast(n);
24 }
25 if (len == in.len) std.c.exit(2);
26 var lines = std.mem.tokenizeScalar(u8, in[0..len], '\n');
27 while (lines.next()) |line| {
28 var it = std.mem.tokenizeScalar(u8, line, ' ');
29 const a = std.fmt.parseInt(u64, it.next() orelse std.c.exit(2), 10) catch std.c.exit(2);
30 const b = std.fmt.parseInt(u64, it.next() orelse std.c.exit(2), 10) catch std.c.exit(2);
31 var r: u64 = 0;
32 const s = om.omega_calc(&r, a, b);
33 var out: [96]u8 = undefined;
34 put(std.fmt.bufPrint(&out, "{d} {d} {d} {d}\n", .{ a, b, s, r }) catch std.c.exit(3));
35 }
36}