完全にお遊びですが、
↓↓こんなソースコードを生成してくれるPythonプログラムを作ってみました。
(任意の英文に対応しています。)
main.cpp
struct pen_{ int dummy = 0; };
struct a_{ pen_ pen; };
struct is_{ a_ a; };
struct This_{ is_ is; };
This_ This;
int main()
{
This.is.a.pen;
}
プログラム
プログラム全文(長い)
generate.py
import os
CPP_KEYWORDS: list[str] = {
"alignas",
"alignof",
"and",
"and_eq",
"asm",
"auto",
"bitand",
"bitor",
"bool",
"break",
"case",
"catch",
"char",
"char8_t",
"char16_t",
"char32_t",
"class",
"compl",
"concept",
"const",
"consteval",
"constexpr",
"constinit",
"const_cast",
"continue",
"co_await",
"co_return",
"co_yield",
"decltype",
"default",
"delete",
"do",
"double",
"dynamic_cast",
"else",
"enum",
"explicit",
"export",
"extern",
"false",
"float",
"for",
"friend",
"goto",
"if",
"inline",
"int",
"long",
"mutable",
"namespace",
"new",
"noexcept",
"not",
"not_eq",
"nullptr",
"operator",
"or",
"or_eq",
"private",
"protected",
"public",
"register",
"reinterpret_cast",
"requires",
"return",
"short",
"signed",
"sizeof",
"static",
"static_assert",
"static_cast",
"struct",
"switch",
"template",
"this",
"thread_local",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"union",
"unsigned",
"using",
"virtual",
"void",
"volatile",
"wchar_t",
"while",
"xor",
"xor_eq",
"main",
}
SENTENCE: str = "This is a pen."
words: list[str] = SENTENCE.rstrip(".").split(" ")
words = [word if word not in CPP_KEYWORDS else f"{word}_" for word in words]
reversed_words: list[str] = list(reversed(words))
words_count: int = len(words)
with open("Makefile", "w", encoding="utf-8") as f:
f.write("main:\n\tclang++ main.cpp -o main\n")
with open("main.cpp", "w", encoding="utf-8") as f:
for i, word in enumerate(reversed_words):
f.write(
f"struct {word}_{{ {"int dummy = 0;"
if i == 0
else f"{reversed_words[i - 1]}_ {reversed_words[i - 1]};"} }};\n"
)
if i == words_count - 1:
f.write(f"{word}_ {word};\n\n")
f.write("int main()\n{\n ")
for i, word in enumerate(words):
f.write(f"{word}{";\n" if i == words_count - 1 else "."}")
f.write("}\n")
os.system("make && ./main")
CPP_KEYWORDS の定義が長いので、そこを省くとこんな↓↓↓感じです。
Makefileもついでに作って、自動でコンパイルを(clangで)走らせるようにしました。
もしソース生成に失敗したら、コンソールにエラーを出して気づかせるためです。
generate.py
import os
SENTENCE: str = "This is a pen."
words: list[str] = SENTENCE.rstrip(".").split(" ")
words = [word if word not in CPP_KEYWORDS else f"{word}_" for word in words]
reversed_words: list[str] = list(reversed(words))
words_count: int = len(words)
with open("Makefile", "w", encoding="utf-8") as f:
f.write("main:\n\tclang++ main.cpp -o main\n")
with open("main.cpp", "w", encoding="utf-8") as f:
for i, word in enumerate(reversed_words):
f.write(
f"struct {word}_{{ {"int dummy = 0;"
if i == 0
else f"{reversed_words[i - 1]}_ {reversed_words[i - 1]};"} }};\n"
)
if i == words_count - 1:
f.write(f"{word}_ {word};\n\n")
f.write("int main()\n{\n ")
for i, word in enumerate(words):
f.write(f"{word}{";\n" if i == words_count - 1 else "."}")
f.write("}\n")
os.system("make && ./main")
処理の流れ
空白で区切り、単語に分割します。
C++の予約語とぶつかってしまったら、末尾にアンダースコアを挿入します。
あとは、順番に構造体を作って、入れ子にしていく感じです。
生成結果
「This is a pen.」
生成されたソースコード
struct pen_{ int dummy = 0; };
struct a_{ pen_ pen; };
struct is_{ a_ a; };
struct This_{ is_ is; };
This_ This;
int main()
{
This.is.a.pen;
}
「and const this unsigned.」
生成されたソースコード
struct unsigned__{ int dummy = 0; };
struct this__{ unsigned__ unsigned_; };
struct const__{ this__ this_; };
struct and__{ const__ const_; };
and__ and_;
int main()
{
and_.const_.this_.unsigned_;
}
「The quick brown fox jumps over the lazy dog.」
生成されたソースコード
struct dog_{ int dummy = 0; };
struct lazy_{ dog_ dog; };
struct the_{ lazy_ lazy; };
struct over_{ the_ the; };
struct jumps_{ over_ over; };
struct fox_{ jumps_ jumps; };
struct brown_{ fox_ fox; };
struct quick_{ brown_ brown; };
struct The_{ quick_ quick; };
The_ The;
int main()
{
The.quick.brown.fox.jumps.over.the.lazy.dog;
}
「Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.」
生成されたソースコード
struct laborum_{ int dummy = 0; };
struct est_{ laborum_ laborum; };
struct id_{ est_ est; };
struct anim_{ id_ id; };
struct mollit_{ anim_ anim; };
struct deserunt_{ mollit_ mollit; };
struct officia_{ deserunt_ deserunt; };
struct qui_{ officia_ officia; };
struct culpa_{ qui_ qui; };
struct in_{ culpa_ culpa; };
struct sunt_{ in_ in; };
struct proident_{ sunt_ sunt; };
struct non_{ proident_ proident; };
struct cupidatat_{ non_ non; };
struct occaecat_{ cupidatat_ cupidatat; };
struct sint_{ occaecat_ occaecat; };
struct Excepteur_{ sint_ sint; };
struct pariatur_{ Excepteur_ Excepteur; };
struct nulla_{ pariatur_ pariatur; };
struct fugiat_{ nulla_ nulla; };
struct eu_{ fugiat_ fugiat; };
struct dolore_{ eu_ eu; };
struct cillum_{ dolore_ dolore; };
struct esse_{ cillum_ cillum; };
struct velit_{ esse_ esse; };
struct voluptate_{ velit_ velit; };
struct in_{ voluptate_ voluptate; };
struct reprehenderit_{ in_ in; };
struct in_{ reprehenderit_ reprehenderit; };
struct dolor_{ in_ in; };
struct irure_{ dolor_ dolor; };
struct aute_{ irure_ irure; };
struct Duis_{ aute_ aute; };
struct consequat_{ Duis_ Duis; };
struct commodo_{ consequat_ consequat; };
struct ea_{ commodo_ commodo; };
struct ex_{ ea_ ea; };
struct aliquip_{ ex_ ex; };
struct ut_{ aliquip_ aliquip; };
struct nisi_{ ut_ ut; };
struct laboris_{ nisi_ nisi; };
struct ullamco_{ laboris_ laboris; };
struct exercitation_{ ullamco_ ullamco; };
struct nostrud_{ exercitation_ exercitation; };
struct quis_{ nostrud_ nostrud; };
struct veniam_{ quis_ quis; };
struct minim_{ veniam_ veniam; };
struct ad_{ minim_ minim; };
struct enim_{ ad_ ad; };
struct Ut_{ enim_ enim; };
struct aliqua_{ Ut_ Ut; };
struct magna_{ aliqua_ aliqua; };
struct dolore_{ magna_ magna; };
struct et_{ dolore_ dolore; };
struct labore_{ et_ et; };
struct ut_{ labore_ labore; };
struct incididunt_{ ut_ ut; };
struct tempor_{ incididunt_ incididunt; };
struct eiusmod_{ tempor_ tempor; };
struct do__{ eiusmod_ eiusmod; };
struct sed_{ do__ do_; };
struct elit_{ sed_ sed; };
struct adipisicing_{ elit_ elit; };
struct consectetur_{ adipisicing_ adipisicing; };
struct amet_{ consectetur_ consectetur; };
struct sit_{ amet_ amet; };
struct dolor_{ sit_ sit; };
struct ipsum_{ dolor_ dolor; };
struct Lorem_{ ipsum_ ipsum; };
Lorem_ Lorem;
int main()
{
Lorem.ipsum.dolor.sit.amet.consectetur.adipisicing.elit.sed.do_.eiusmod.tempor.incididunt.ut.labore.et.dolore.magna.aliqua.Ut.enim.ad.minim.veniam.quis.nostrud.exercitation.ullamco.laboris.nisi.ut.aliquip.ex.ea.commodo.consequat.Duis.aute.irure.dolor.in.reprehenderit.in.voluptate.velit.esse.cillum.dolore.eu.fugiat.nulla.pariatur.Excepteur.sint.occaecat.cupidatat.non.proident.sunt.in.culpa.qui.officia.deserunt.mollit.anim.id.est.laborum;
}
文中に記号が来るのは対応していなかったので、プログラムを一部変更しました。
記号が含まれている時に、もう少し丁寧にバリデーションしたいですね。
- words: list[str] = SENTENCE.rstrip(".").split(" ")
+ words: list[str] = SENTENCE.replace(".", "").replace(",", "").split(" ")