Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SeokminHong committed Jun 22, 2021
0 parents commit 32e5406
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
main
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CXX=g++
CFLAGS=-g -Wall -O3
CXX_STANDARD=-std=c++20

example: examples/main.cpp
$(CXX) $(CFLAGS) $(CXX_STANDARD) examples/main.cpp -o main
9 changes: 9 additions & 0 deletions examples/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "../include/ctfs.hpp"

int main() {
ctfs::print<"Hello %d %s\n">(3, "world");
ctfs::print<"value1: %d, value2: %f\n">(5, 2.3f);

// error: invalid conversion from ‘const char*’ to ‘int’
// ctfs::print<"This will fail to compile: %d %d\n">(3, "");
}
18 changes: 18 additions & 0 deletions include/ctfs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CTFS_HPP
#define CTFS_HPP

#include "ctfs/functions.hpp"
#include "ctfs/parser.hpp"
#include "ctfs/type_list.hpp"

namespace ctfs {
template <fixed_string str> struct execute {
using types =
parser<str, std::integral_constant<size_t, 0>, type_list<>>::result;
static constexpr auto print = &functions<str, types>::print;
};

template <fixed_string str> const static auto print = execute<str>::print;
} // namespace ctfs

#endif // CTFS_HPP
34 changes: 34 additions & 0 deletions include/ctfs/fixed_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef CTFS_FIXED_STRING
#define CTFS_FIXED_STRING

#include <cstddef>

namespace ctfs {
template <size_t N> struct fixed_string {
char content[N + 1] = {};
size_t size = 0;

constexpr fixed_string(const char (&input)[N]) noexcept {
for (size_t i{0}; i < N; ++i) {
if (input[i] == '\0') {
size = i;
return;
}
content[i] = input[i];
}
}

constexpr fixed_string(const fixed_string &other) noexcept {
for (size_t i{0}; i < N; ++i) {
content[i] = other.content[i];
}
size = other.size;
}

constexpr const char &operator[](size_t n) const noexcept {
return content[n];
}
};
} // namespace ctfs

#endif // CTFS_FIXED_STRING
18 changes: 18 additions & 0 deletions include/ctfs/functions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CTFS_FUNCTIONS_HPP
#define CTFS_FUNCTIONS_HPP

#include "fixed_string.hpp"
#include "type_list.hpp"
#include <cstddef>
#include <cstdio>

namespace ctfs {
template <fixed_string str, typename... Ts> struct functions;

template <fixed_string str, typename... Ts>
struct functions<str, type_list<Ts...>> {
static int print(Ts... args) { return printf(str.content, args...); }
};
} // namespace ctfs

#endif // CTFS_FUNCTIONS_HPP
25 changes: 25 additions & 0 deletions include/ctfs/parser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef CTFS_PARSER_HPP
#define CTFS_PARSER_HPP

#include "fixed_string.hpp"
#include "token.hpp"
#include <cstddef>
#include <type_traits>

namespace ctfs {
template <fixed_string str, typename pos, typename list> struct parser {
static constexpr size_t pos_val = pos::value;
using parse_result = token<str[pos_val], str[pos_val + 1], list>;
using result = typename parser<
str,
std::integral_constant<size_t, pos_val + (parse_result::result ? 2 : 1)>,
typename parse_result::types>::result;
};

template <fixed_string str, typename list>
struct parser<str, std::integral_constant<size_t, str.size - 1>, list> {
using result = list;
};
} // namespace ctfs

#endif // CTFS_PARSER_HPP
28 changes: 28 additions & 0 deletions include/ctfs/token.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef CTFS_TOKEN_HPP
#define CTFS_TOKEN_HPP

#include "type_list.hpp"

namespace ctfs {
template <char c0, char c1, typename list> struct token {
constexpr static auto result = false;
using types = list;
};

template <typename list> struct token<'%', 'd', list> {
constexpr static auto result = true;
using types = typename list::add<int>;
};

template <typename list> struct token<'%', 'f', list> {
constexpr static auto result = true;
using types = typename list::add<double>;
};

template <typename list> struct token<'%', 's', list> {
constexpr static auto result = true;
using types = typename list::add<const char *>;
};
} // namespace ctfs

#endif // CTFS_TOKEN_HPP
12 changes: 12 additions & 0 deletions include/ctfs/type_list.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef CTFS_TYPE_LIST_HPP
#define CTFS_TYPE_LIST_HPP

namespace ctfs {
template <typename... Type> struct type_list {
using type = type_list;

template <typename T> using add = type_list<Type..., T>;
};
} // namespace ctfs

#endif // CTFS_TYPE_LIST_HPP

0 comments on commit 32e5406

Please sign in to comment.