Skip to content

Commit

Permalink
Add c++23 support
Browse files Browse the repository at this point in the history
  • Loading branch information
andy1li committed Nov 4, 2024
1 parent b020ec0 commit 4ffbc6f
Show file tree
Hide file tree
Showing 34 changed files with 772 additions and 0 deletions.
12 changes: 12 additions & 0 deletions compiled_starters/cpp/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/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

cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
11 changes: 11 additions & 0 deletions compiled_starters/cpp/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -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)/build/interpreter "$@"
1 change: 1 addition & 0 deletions compiled_starters/cpp/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
55 changes: 55 additions & 0 deletions compiled_starters/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

build
vcpkg_installed
9 changes: 9 additions & 0 deletions compiled_starters/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.13)

project(codecrafters-interpreter)

set(CMAKE_CXX_STANDARD 23) # Enable the C++23 standard

file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.hpp)

add_executable(interpreter ${SOURCE_FILES})
50 changes: 50 additions & 0 deletions compiled_starters/cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/interpreter.png)

This is a starting point for C++ solutions to the
["Build your own Interpreter" Challenge](https://app.codecrafters.io/courses/interpreter/overview).

This challenge follows the book
[Crafting Interpreters](https://craftinginterpreters.com/) by Robert Nystrom.

In this challenge you'll build an interpreter for
[Lox](https://craftinginterpreters.com/the-lox-language.html), a simple
scripting language. Along the way, you'll learn about tokenization, ASTs,
tree-walk interpreters and more.

Before starting this challenge, make sure you've read the "Welcome" part of the
book that contains these chapters:

- [Introduction](https://craftinginterpreters.com/introduction.html) (chapter 1)
- [A Map of the Territory](https://craftinginterpreters.com/a-map-of-the-territory.html)
(chapter 2)
- [The Lox Language](https://craftinginterpreters.com/the-lox-language.html)
(chapter 3)

These chapters don't involve writing code, so they won't be covered in this
challenge. This challenge will start from chapter 4,
[Scanning](https://craftinginterpreters.com/scanning.html).

**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 program is in `src/main.cpp`. 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 `cmake` installed locally
2. Run `./your_program.sh` to run your program, which is implemented in
`src/main.cpp`.
3. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
11 changes: 11 additions & 0 deletions compiled_starters/cpp/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -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 C++ version used to run your code
# on Codecrafters.
#
# Available versions: cpp-23
language_pack: cpp-23
55 changes: 55 additions & 0 deletions compiled_starters/cpp/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

std::string read_file_contents(const std::string& filename);

int main(int argc, char *argv[]) {
// Disable output buffering
std::cout << std::unitbuf;
std::cerr << std::unitbuf;

// You can use print statements as follows for debugging, they'll be visible when running tests.
std::cerr << "Logs from your program will appear here!" << std::endl;

if (argc < 3) {
std::cerr << "Usage: ./your_program tokenize <filename>" << std::endl;
return 1;
}

const std::string command = argv[1];

if (command == "tokenize") {
std::string file_contents = read_file_contents(argv[2]);

// Uncomment this block to pass the first stage
//
// if (!file_contents.empty()) {
// std::cerr << "Scanner not implemented" << std::endl;
// std::exit(1);
// }
// std::cout << "EOF null" << std::endl; // Placeholder, remove this line when implementing the scanner

} else {
std::cerr << "Unknown command: " << command << std::endl;
return 1;
}

return 0;
}

std::string read_file_contents(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error reading file: " << filename << std::endl;
std::exit(1);
}

std::stringstream buffer;
buffer << file.rdbuf();
file.close();

return buffer.str();
}
14 changes: 14 additions & 0 deletions compiled_starters/cpp/vcpkg-configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"default-registry": {
"kind": "git",
"baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d",
"repository": "https://github.com/microsoft/vcpkg"
},
"registries": [
{
"kind": "artifact",
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
"name": "microsoft"
}
]
}
3 changes: 3 additions & 0 deletions compiled_starters/cpp/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dependencies": []
}
25 changes: 25 additions & 0 deletions compiled_starters/cpp/your_program.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/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
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./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)/build/interpreter "$@"
2 changes: 2 additions & 0 deletions course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ completion_percentage: 15

languages:
- slug: "c"
- slug: "cpp"
release_status: "alpha"
- slug: "gleam"
- slug: "go"
- slug: "kotlin"
Expand Down
39 changes: 39 additions & 0 deletions dockerfiles/cpp-23.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# syntax=docker/dockerfile:1.7-labs
FROM gcc:14.2.0-bookworm

# Ensures the container is re-built if dependency files change
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="vcpkg.json,vcpkg-configuration.json"

RUN apt-get update && \
apt-get install --no-install-recommends -y zip=3.* && \
apt-get install --no-install-recommends -y g++=4:* && \
apt-get install --no-install-recommends -y build-essential=12.* && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# cmake is required by vcpkg
RUN wget --progress=dot:giga https://github.com/Kitware/CMake/releases/download/v3.30.5/cmake-3.30.5-Linux-x86_64.tar.gz && \
tar -xzvf cmake-3.30.5-Linux-x86_64.tar.gz && \
mv cmake-3.30.5-linux-x86_64/ /cmake

ENV CMAKE_BIN="/cmake/bin"
ENV PATH="${CMAKE_BIN}:$PATH"

RUN git clone https://github.com/microsoft/vcpkg.git && \
./vcpkg/bootstrap-vcpkg.sh -disableMetrics

ENV VCPKG_ROOT="/vcpkg"
ENV PATH="${VCPKG_ROOT}:$PATH"

WORKDIR /app

# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses
COPY --exclude=.git --exclude=README.md . /app

RUN vcpkg install --no-print-usage
RUN sed -i '1s/^/set(VCPKG_INSTALL_OPTIONS --no-print-usage)\n/' ${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake

RUN mkdir -p /app-cached
RUN if [ -d "/app/build" ]; then mv /app/build /app-cached; fi
RUN if [ -d "/app/vcpkg_installed" ]; then mv /app/vcpkg_installed /app-cached/build; fi

12 changes: 12 additions & 0 deletions solutions/cpp/01-ry8/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/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

cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
11 changes: 11 additions & 0 deletions solutions/cpp/01-ry8/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -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)/build/interpreter "$@"
1 change: 1 addition & 0 deletions solutions/cpp/01-ry8/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
55 changes: 55 additions & 0 deletions solutions/cpp/01-ry8/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

build
vcpkg_installed
Loading

0 comments on commit 4ffbc6f

Please sign in to comment.