-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 32e5406
Showing
9 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode | ||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |