summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcodecrafters-bot <hello@codecrafters.io>2025-12-05 01:22:52 +0000
committercodecrafters-bot <hello@codecrafters.io>2025-12-05 01:22:52 +0000
commitdd479ec0e15a670cfa0ef064b1662c45d5be48dc (patch)
tree43c91ceb426a33c8c048eba5c691e8334e81a50e
downloadshell-zig-dd479ec0e15a670cfa0ef064b1662c45d5be48dc.tar.gz
shell-zig-dd479ec0e15a670cfa0ef064b1662c45d5be48dc.zip
init [skip ci]
-rwxr-xr-x.codecrafters/compile.sh11
-rwxr-xr-x.codecrafters/run.sh11
-rw-r--r--.gitattributes1
-rw-r--r--.gitignore18
-rw-r--r--README.md34
-rw-r--r--build.zig34
-rw-r--r--build.zig.zon68
-rw-r--r--codecrafters.yml11
-rw-r--r--src/main.zig9
-rwxr-xr-xyour_program.sh24
10 files changed, 221 insertions, 0 deletions
diff --git a/.codecrafters/compile.sh b/.codecrafters/compile.sh
new file mode 100755
index 0000000..5782342
--- /dev/null
+++ b/.codecrafters/compile.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+#
+# This script is used to compile your program on CodeCrafters
+#
+# This runs before .codecrafters/run.sh
+#
+# Learn more: https://codecrafters.io/program-interface
+
+set -e # Exit on failure
+
+zig build
diff --git a/.codecrafters/run.sh b/.codecrafters/run.sh
new file mode 100755
index 0000000..18ffaf9
--- /dev/null
+++ b/.codecrafters/run.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+#
+# This script is used to run your program on CodeCrafters
+#
+# This runs after .codecrafters/compile.sh
+#
+# Learn more: https://codecrafters.io/program-interface
+
+set -e # Exit on failure
+
+exec "$(dirname "$0")"/zig-out/bin/main "$@"
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b7bb740
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,18 @@
+# Zig build artifacts
+main
+zig-cache/
+.zig-cache/
+zig-out/
+
+# Compiled binary output
+bin/
+!bin/.gitkeep
+
+# Ignore any .o or .h files generated during build
+*.o
+*.h
+
+# Ignore OS and editor specific files
+**/.DS_Store
+*.swp
+*~
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2612ba9
--- /dev/null
+++ b/README.md
@@ -0,0 +1,34 @@
+[![progress-banner](https://backend.codecrafters.io/progress/shell/38e570f4-0f52-40ff-8f0d-1d95e3edeb1c)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF)
+
+This is a starting point for Zig solutions to the
+["Build Your Own Shell" Challenge](https://app.codecrafters.io/courses/shell/overview).
+
+In this challenge, you'll build your own POSIX compliant shell that's capable of
+interpreting shell commands, running external programs and builtin commands like
+cd, pwd, echo and more. Along the way, you'll learn about shell command parsing,
+REPLs, builtin commands, and more.
+
+**Note**: If you're viewing this repo on GitHub, head over to
+[codecrafters.io](https://codecrafters.io) to try the challenge.
+
+# Passing the first stage
+
+The entry point for your `shell` implementation is in `src/main.zig`. Study and
+uncomment the relevant code, and push your changes to pass the first stage:
+
+```sh
+git commit -am "pass 1st stage" # any msg
+git push origin master
+```
+
+Time to move on to the next stage!
+
+# Stage 2 & beyond
+
+Note: This section is for stages 2 and beyond.
+
+1. Ensure you have `zig (0.15)` installed locally
+1. Run `./your_program.sh` to run your program, which is implemented in
+ `src/main.zig`.
+1. Commit your changes and run `git push origin master` to submit your solution
+ to CodeCrafters. Test output will be streamed to your terminal.
diff --git a/build.zig b/build.zig
new file mode 100644
index 0000000..0eccf2a
--- /dev/null
+++ b/build.zig
@@ -0,0 +1,34 @@
+const std = @import("std");
+
+// Learn more about this file here: https://ziglang.org/learn/build-system
+pub fn build(b: *std.Build) void {
+ const exe = b.addExecutable(.{
+ .name = "main",
+ .root_module = b.createModule(.{
+ .root_source_file = b.path("src/main.zig"),
+ .target = b.graph.host,
+ }),
+ });
+
+ // 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`).
+ b.installArtifact(exe);
+
+ // This *creates* a Run step in the build graph, to be executed when another
+ // step is evaluated that depends on it. The next line below will establish
+ // such a dependency.
+ const run_cmd = b.addRunArtifact(exe);
+
+ // 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");
+ run_step.dependOn(&run_cmd.step);
+
+ // This allows the user to pass arguments to the application in the build
+ // command itself, like this: `zig build run -- arg1 arg2 etc`
+ if (b.args) |args| {
+ run_cmd.addArgs(args);
+ }
+}
diff --git a/build.zig.zon b/build.zig.zon
new file mode 100644
index 0000000..5977354
--- /dev/null
+++ b/build.zig.zon
@@ -0,0 +1,68 @@
+.{
+ .name = .codecrafters_shell,
+ .fingerprint = 0xc6b1d97d3a5e8994,
+
+ // This is a [Semantic Version](https://semver.org/).
+ // In a future version of Zig it will be used for package deduplication.
+ .version = "0.0.0",
+
+ // Tracks the earliest Zig version that the package considers to be a
+ // supported use case.
+ .minimum_zig_version = "0.15.1",
+
+ // This field is optional.
+ // Each dependency must either provide a `url` and `hash`, or a `path`.
+ // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
+ // Once all dependencies are fetched, `zig build` no longer requires
+ // internet connectivity.
+ .dependencies = .{
+ // See `zig fetch --save <url>` for a command-line interface for adding dependencies.
+ //.example = .{
+ // // When updating this field to a new URL, be sure to delete the corresponding
+ // // `hash`, otherwise you are communicating that you expect to find the old hash at
+ // // the new URL.
+ // .url = "https://example.com/foo.tar.gz",
+ //
+ // // This is computed from the file contents of the directory of files that is
+ // // obtained after fetching `url` and applying the inclusion rules given by
+ // // `paths`.
+ // //
+ // // This field is the source of truth; packages do not come from a `url`; they
+ // // come from a `hash`. `url` is just one of many possible mirrors for how to
+ // // obtain a package matching this `hash`.
+ // //
+ // // Uses the [multihash](https://multiformats.io/multihash/) format.
+ // .hash = "...",
+ //
+ // // When this is provided, the package is found in a directory relative to the
+ // // build root. In this case the package's hash is irrelevant and therefore not
+ // // computed. This field and `url` are mutually exclusive.
+ // .path = "foo",
+
+ // // When this is set to `true`, a package is declared to be lazily
+ // // fetched. This makes the dependency only get fetched if it is
+ // // actually used.
+ // .lazy = false,
+ //},
+ },
+
+ // Specifies the set of files and directories that are included in this package.
+ // Only files and directories listed here are included in the `hash` that
+ // is computed for this package.
+ // Paths are relative to the build root. Use the empty string (`""`) to refer to
+ // the build root itself.
+ // A directory listed here means that all files within, recursively, are included.
+ .paths = .{
+ // This makes *all* files, recursively, included in this package. It is generally
+ // better to explicitly list the files and directories instead, to insure that
+ // fetching from tarballs, file system paths, and version control all result
+ // in the same contents hash.
+ "",
+ // For example...
+ //"build.zig",
+ //"build.zig.zon",
+ //"src",
+ //"LICENSE",
+ //"README.md",
+ },
+}
diff --git a/codecrafters.yml b/codecrafters.yml
new file mode 100644
index 0000000..31d465e
--- /dev/null
+++ b/codecrafters.yml
@@ -0,0 +1,11 @@
+# Set this to true if you want debug logs.
+#
+# These can be VERY verbose, so we suggest turning them off
+# unless you really need them.
+debug: false
+
+# Use this to change the Zig version used to run your code
+# on Codecrafters.
+#
+# Available versions: zig-0.15.2
+buildpack: zig-0.15.2
diff --git a/src/main.zig b/src/main.zig
new file mode 100644
index 0000000..37d4949
--- /dev/null
+++ b/src/main.zig
@@ -0,0 +1,9 @@
+const std = @import("std");
+
+var stdout_writer = std.fs.File.stdout().writerStreaming(&.{});
+const stdout = &stdout_writer.interface;
+
+pub fn main() !void {
+ // TODO: Uncomment the code below to pass the first stage
+ // try stdout.print("$ ", .{});
+}
diff --git a/your_program.sh b/your_program.sh
new file mode 100755
index 0000000..9691601
--- /dev/null
+++ b/your_program.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+#
+# Use this script to run your program LOCALLY.
+#
+# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
+#
+# Learn more: https://codecrafters.io/program-interface
+
+set -e # Exit early if any commands fail
+
+# Copied from .codecrafters/compile.sh
+#
+# - Edit this to change how your program compiles locally
+# - Edit .codecrafters/compile.sh to change how your program compiles remotely
+(
+ cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
+ zig build
+)
+
+# Copied from .codecrafters/run.sh
+#
+# - Edit this to change how your program runs locally
+# - Edit .codecrafters/run.sh to change how your program runs remotely
+exec "$(dirname "$0")"/zig-out/bin/main "$@"