Swapped mains around

This commit is contained in:
nothke 2024-08-08 20:44:22 +02:00
parent 64be90de80
commit a970c91891
3 changed files with 133 additions and 148 deletions

43
src/main-old.zig Normal file
View File

@ -0,0 +1,43 @@
const std = @import("std");
fn calc(a: i32) i32 {
return a;
}
const SOmethin = enum {
One,
Two,
Three,
};
fn Optional(comptime T: type) type {
return struct {
valid: bool,
value: T,
};
}
fn add(a: anytype, b: anytype) @TypeOf(a + b) {
return a + b;
}
const DivisionError = error{DivisionByZero};
fn divide(a: f32, b: f32) !f32 {
if (b == 0)
return DivisionError.DivisionByZero;
return a / b;
}
pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
const vec = @Vector(4, i32){ 1, 0, 1, 0 };
const vec2 = @Vector(4, i32){ 0, 1, 0, 1 };
std.debug.print("vec: {}", .{vec + vec2});
}

View File

@ -1,43 +1,111 @@
const std = @import("std");
const Other = @import("other.zig");
fn calc(a: i32) i32 {
return a;
}
// Struct
const SOmethin = enum {
One,
Two,
Three,
const Vector2D = packed struct {
x: i5 = 2,
y: i23,
fn name(self: Vector2D) void {
_ = self; // autofix
}
fn init() void {}
fn deinit() void {}
};
// Functions
fn add(a: *i32, b: i32) i32 {
a.* = 3;
return a.* + b;
}
// Error unions
const DivideError = error{
DivivisionError,
};
fn divide(a: f32, b: f32) !f32 {
if (b == 0) {
return DivideError.DivivisionError;
} else {
return a / b;
}
}
// Optional
fn Optional(comptime T: type) type {
return struct {
valid: bool,
value: T,
exists: bool,
};
}
fn add(a: anytype, b: anytype) @TypeOf(a + b) {
return a + b;
}
// Tagged unions
const DivisionError = error{DivisionByZero};
const Person = struct {
name: []const u8,
};
fn divide(a: f32, b: f32) !f32 {
if (b == 0)
return DivisionError.DivisionByZero;
const Horse = struct {
canRide: bool = false,
rider: ?*const Person = null,
};
return a / b;
}
const Duck = struct {
friend: ?*Animal = null,
gone: bool = false,
};
const AnimalType = enum {
Horse,
Duck,
Person,
};
const Animal = union(AnimalType) {
Horse: Horse,
Duck: Duck,
Person: Person,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
defer _ = gpa.deinit();
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
var list = std.ArrayList(i32).init(alloc);
defer list.deinit();
try list.append(324);
try list.append(456);
const vec = @Vector(4, i32){ 1, 0, 1, 0 };
const vec2 = @Vector(4, i32){ 0, 1, 0, 1 };
for (list.items) |item| {
std.log.debug("item: {}", .{item});
}
std.debug.print("vec: {}", .{vec + vec2});
var animal = Animal{ .Horse = .{ .canRide = true } };
const person = Person{ .name = "Dragutin" };
if (animal == .Horse) {
animal.Horse.rider = &person;
}
switch (animal) {
.Horse => |*horse| horse.rider = &person,
else => {},
}
var personAnimal = Animal{ .Person = .{ .name = "Aleksa" } };
var duck = Duck{ .friend = &personAnimal };
duck.gone = true;
std.log.debug("Rider name: {s}", .{if (animal.Horse.rider) |rider| rider.name else "none"});
}

View File

@ -1,126 +0,0 @@
const std = @import("std");
const Other = @import("other.zig");
const WRB = @import("rbuff.zig").WrappingRingBuffer;
const Vector2D = packed struct {
x: i5 = 2,
y: i23,
fn name(self: Vector2D) void {
_ = self; // autofix
}
fn init() void {}
fn deinit() void {}
};
fn add(a: *i32, b: i32) i32 {
a.* = 3;
return a.* + b;
}
const DivideError = error{
DivivisionError,
};
fn divide(a: f32, b: f32) !f32 {
if (b == 0) {
return DivideError.DivivisionError;
} else {
return a / b;
}
}
fn Optional(comptime T: type) type {
return struct {
value: T,
exists: bool,
};
}
const Person = struct {
name: []const u8,
};
const Horse = struct {
canRide: bool = false,
rider: ?*const Person = null,
};
const Duck = struct {
friend: ?*Animal = null,
gone: bool = false,
};
const AnimalType = enum {
Horse,
Duck,
Person,
};
const Animal = union(AnimalType) {
Horse: Horse,
Duck: Duck,
Person: Person,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
defer _ = gpa.deinit();
var list = std.ArrayList(i32).init(alloc);
defer list.deinit();
try list.append(324);
try list.append(456);
for (list.items) |item| {
std.log.debug("item: {}", .{item});
}
var animal = Animal{ .Horse = .{ .canRide = true } };
const person = Person{ .name = "Dragutin" };
if (animal == .Horse) {
animal.Horse.rider = &person;
}
switch (animal) {
.Horse => |*horse| horse.rider = &person,
else => {},
}
var personAnimal = Animal{ .Person = .{ .name = "Aleksa" } };
var duck = Duck{ .friend = &personAnimal };
duck.gone = true;
std.log.debug("Rider name: {s}", .{if (animal.Horse.rider) |rider| rider.name else "none"});
{
std.log.info("Ring buffer!", .{});
var rbuff = try WRB(i32).initCapacity(alloc, 32);
defer rbuff.deinit();
if (rbuff.isEmpty())
try rbuff.pushBack(14);
try rbuff.pushBack(16);
try rbuff.pushBack(2);
const front = rbuff.frontPtr();
front.* = 333;
var iter = rbuff.iter();
while (iter.next()) |*value| {
std.log.info("rbuff value: {}", .{value});
}
std.log.info("the end!", .{});
}
}