implementing constants

This commit is contained in:
2024-08-24 15:11:45 +02:00
parent a8257c2b03
commit 9e3ced0f2e
3 changed files with 82 additions and 11 deletions

43
src/values.zig Normal file
View File

@ -0,0 +1,43 @@
const std = @import("std");
const debug = std.debug;
const Allocator = std.mem.Allocator;
const utils = @import("./utils.zig");
pub const Value = f64;
pub const ValueArray = struct {
capacity: usize,
count: usize,
values: []Value,
pub fn new() ValueArray {
return ValueArray{
.capacity = 0,
.count = 0,
.values = &.{},
};
}
pub fn write(self: *ValueArray, allocator: Allocator, value: Value) !void {
if (self.capacity < self.count + 1) {
const old_capacity = self.capacity;
self.capacity = utils.grow_capacity(old_capacity);
self.values = try allocator.realloc(self.values, self.capacity);
}
self.values[self.count] = value;
self.count += 1;
}
pub fn free(self: *ValueArray, allocator: Allocator) void {
if (self.capacity > 0) {
allocator.free(self.values);
}
}
};
pub fn print_value(value: Value) void {
debug.print("{any}", .{value});
}