6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

新元号が発表されたので元号言語を作ってみた

Posted at

令和!!!!!!!!!!!!!!!!!

新たなる時代の幕開けだ。ちなみにC++で書いた。所謂Brainf*ckってやつ。イキって拡張子付けてみた。

仕様

令和 = '+'
平成 = '-'
昭和 = '>'
大正 = '<'
明治 = '.'
慶応 = ','
元治 = '['
文久 = ']'

コード

Gengo.cpp
# include <cstdio>
# include <string>
# include <fstream>
# include <stack>

int main(int argc, char* argv[]) {
    using Byte = unsigned char;
    Byte buf[30000] = {};
    Byte* ptr = buf;
    std::string s;
    std::stack<int> stack;
    int nest = 0;
    const std::string reiwa = "令和";
    const std::string heisei = "平成";
    const std::string showa = "昭和";
    const std::string taisho = "大正";
    const std::string meiji = "明治";
    const std::string keio = "慶応";
    const std::string genji = "元治";
    const std::string bunkyu = "文久";
    const int rlen = reiwa.length();
    const int hlen = heisei.length();
    const int slen = showa.length();
    const int tlen = taisho.length();
    const int mlen = meiji.length();
    const int klen = keio.length();
    const int glen = genji.length();
    const int blen = bunkyu.length();
    if(argc == 1) {
        printf("Please specify an gengo file.\n");
         return 1;
    }
    std::string file = argv[1];
    int i = file.find(".");
    if(file.find("gengo", i) != (i + 1)) {
        printf("Please specify an gengo file.\n");
        return 1;
    }
    std::ifstream ifs(file);
    while(getline(ifs, s)) {
        for(i = 0; i != s.size();) {
        	if(s.compare(i, rlen, reiwa) == 0) {
                 ++*ptr;
                 i += rlen;
             }
             else if(s.compare(i, hlen, heisei) == 0) {
                 --*ptr;
                 i += hlen;
             }
             else if(s.compare(i, slen, showa) == 0) {
                 ++ptr;
                 i += slen;
             }
             else if(s.compare(i, tlen, taisho) == 0) {
                 --ptr;
                 i += tlen;
             }
             else if(s.compare(i, mlen, meiji) == 0) {
                 putchar(*ptr);
                 fflush(stdout);
                 i += mlen;
             }
             else if(s.compare(i, klen, keio) == 0) {
                 *ptr = getchar();
                 i += klen;
             }
             else if(s.compare(i, glen, genji) == 0) {
                 if(*ptr == 0) {
                     i = s.find(bunkyu, i);
                     nest = 0;
                     do {
                         if(s.compare(i, glen, genji) == 0) {
                             nest++;
                             i += klen;
                         }
                         else if(s.compare(i, blen, bunkyu) == 0) {
                             nest--;
                             i += blen;
                         }
                         else
                             i += rlen/2;
                     } while(nest != 0);
                 }
                 else {
                     stack.push(i);
                     i += glen;
                 }
             }
             else if(s.compare(i, blen, bunkyu) == 0) {
                 if(*ptr != 0) i = stack.top();
                 else i += blen;
                 stack.pop();
             }
             else i += rlen/2;
         }
    }
}

実行ファイル

Hello.gengo
昭和令和令和令和令和令和令和令和令和令和元治大正令和令和令和令和令和令和令和令和昭和平成文久大正明治昭和令和令和令和令和令和令和令和元治大正令和令和令和令和昭和平成文久大正令和明治令和令和令和令和令和令和令和明治明治令和令和令和明治元治平成文久昭和令和令和令和令和令和令和令和令和元治大正令和令和令和令和昭和平成文久大正明治昭和令和令和令和令和令和令和令和令和令和令和令和元治大正令和令和令和令和令和昭和平成文久大正明治昭和令和令和令和令和令和令和令和令和元治大正令和令和令和昭和平成文久大正明治令和令和令和明治平成平成平成平成平成平成明治平成平成平成平成平成平成平成平成明治元治平成文久昭和令和令和令和令和令和令和令和令和元治大正令和令和令和令和昭和平成文久大正令和明治元治平成文久令和令和令和令和令和令和令和令和令和令和明治

実行してみよう

$ g++ -o Gengo Gengo.cpp
$ Gengo Hello.gengo
Hello, World!
6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?