Skip to content

Commit

Permalink
feat(fmt): add comma for list of things longer than 2 (#24)
Browse files Browse the repository at this point in the history
* add comma for fmt

* fix build

* add more fmt comma

* add comma fmt with {}
  • Loading branch information
tcoratger authored Oct 26, 2023
1 parent 813f215 commit b38f24f
Show file tree
Hide file tree
Showing 13 changed files with 4,225 additions and 813 deletions.
41 changes: 33 additions & 8 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const package_path = "src/lib.zig";

// List of external dependencies that this package requires.
const external_dependencies = [_]build_helpers.Dependency{
.{ .name = "zig-cli", .module_name = "zig-cli" },
.{
.name = "zig-cli",
.module_name = "zig-cli",
},
};

// Although this function looks imperative, note that its job is to
Expand All @@ -26,10 +29,17 @@ pub fn build(b: *std.Build) void {
// **************************************************************
// * HANDLE DEPENDENCY MODULES *
// **************************************************************
const dependencies_opts = .{ .target = target, .optimize = optimize };
const dependencies_opts = .{
.target = target,
.optimize = optimize,
};

// This array can be passed to add the dependencies to lib, executable, tests, etc using `addModule` function.
const deps = build_helpers.generateModuleDependencies(b, &external_dependencies, dependencies_opts) catch unreachable;
const deps = build_helpers.generateModuleDependencies(
b,
&external_dependencies,
dependencies_opts,
) catch unreachable;

// **************************************************************
// * CAIRO-ZIG AS A MODULE *
Expand All @@ -52,7 +62,10 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
// Add dependency modules to the library.
for (deps) |mod| lib.addModule(mod.name, mod.module);
for (deps) |mod| lib.addModule(
mod.name,
mod.module,
);
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
Expand All @@ -70,7 +83,10 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
// Add dependency modules to the executable.
for (deps) |mod| exe.addModule(mod.name, mod.module);
for (deps) |mod| exe.addModule(
mod.name,
mod.module,
);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
Expand All @@ -96,7 +112,10 @@ pub fn build(b: *std.Build) void {
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
const run_step = b.step(
"run",
"Run the app",
);
run_step.dependOn(&run_cmd.step);

// Creates a step for unit testing. This only builds the test executable
Expand All @@ -107,14 +126,20 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
// Add dependency modules to the tests.
for (deps) |mod| unit_tests.addModule(mod.name, mod.module);
for (deps) |mod| unit_tests.addModule(
mod.name,
mod.module,
);

const run_unit_tests = b.addRunArtifact(unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
const test_step = b.step(
"test",
"Run unit tests",
);
test_step.dependOn(&lib.step);
test_step.dependOn(&run_unit_tests.step);
}
28 changes: 23 additions & 5 deletions build_helpers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,27 @@ pub const Dependency = struct {
/// * `dependencies_opts` - The options to use when generating the dependency modules.
/// # Returns
/// A new array of Build.ModuleDependency.
pub fn generateModuleDependencies(b: *std.Build, external_dependencies: []const Dependency, dependencies_opts: anytype) ![]std.Build.ModuleDependency {
pub fn generateModuleDependencies(
b: *std.Build,
external_dependencies: []const Dependency,
dependencies_opts: anytype,
) ![]std.Build.ModuleDependency {
var dependency_modules = std.ArrayList(*std.build.Module).init(b.allocator);
defer _ = dependency_modules.deinit();

// Populate dependency modules.
for (external_dependencies) |dep| {
const module = b.dependency(dep.name, dependencies_opts).module(dep.module_name);
const module = b.dependency(
dep.name,
dependencies_opts,
).module(dep.module_name);
_ = dependency_modules.append(module) catch unreachable;
}
return try toModuleDependencyArray(b.allocator, dependency_modules.items, external_dependencies);
return try toModuleDependencyArray(
b.allocator,
dependency_modules.items,
external_dependencies,
);
}

/// Convert an array of Build.Module pointers to an array of Build.ModuleDependency.
Expand All @@ -32,11 +43,18 @@ pub fn generateModuleDependencies(b: *std.Build, external_dependencies: []const
/// * `ext_deps` - The array of external dependencies.
/// # Returns
/// A new array of Build.ModuleDependency.
fn toModuleDependencyArray(allocator: std.mem.Allocator, modules: []const *std.Build.Module, ext_deps: []const Dependency) ![]std.Build.ModuleDependency {
fn toModuleDependencyArray(
allocator: std.mem.Allocator,
modules: []const *std.Build.Module,
ext_deps: []const Dependency,
) ![]std.Build.ModuleDependency {
var deps = std.ArrayList(std.Build.ModuleDependency).init(allocator);
defer deps.deinit();

for (modules, 0..) |module_ptr, i| {
for (
modules,
0..,
) |module_ptr, i| {
try deps.append(.{
.name = ext_deps[i].name,
.module = module_ptr,
Expand Down
25 changes: 20 additions & 5 deletions src/cmd/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ var app = &cli.App{

/// Run the CLI app.
pub fn run() !void {
return cli.run(app, gpa_allocator);
return cli.run(
app,
gpa_allocator,
);
}

// ************************************************************
Expand All @@ -73,21 +76,33 @@ pub fn run() !void {

// execute entrypoint
fn execute(_: []const []const u8) !void {
std.log.debug("Running Cairo VM...\n", .{});
std.log.debug(
"Running Cairo VM...\n",
.{},
);

// Create a new VM instance.
var vm = try vm_core.CairoVM.init(&gpa_allocator);
defer vm.deinit(); // <-- This ensures that resources are freed when exiting the scope

const address_1 = relocatable.Relocatable.new(0, 0);
const address_1 = relocatable.Relocatable.new(
0,
0,
);
const encoded_instruction = relocatable.fromU64(0x14A7800080008000);

// Write a value to memory.
try vm.segments.memory.set(address_1, encoded_instruction);
try vm.segments.memory.set(
address_1,
encoded_instruction,
);

// Run a step.
vm.step() catch |err| {
std.debug.print("Error: {}\n", .{err});
std.debug.print(
"Error: {}\n",
.{err},
);
return;
};
}
Loading

0 comments on commit b38f24f

Please sign in to comment.