まえがき
早く暖かくなってほしいがそういえばなんでうちにはこたつがないのか
疑問に思っているdangomushiです。
作ってみた
はい。いつものごとく何もやることがありませんので作ってみました。最近ファイルとかの
処理しかしてないんだが、、、、w
気を取り直して、概要です。
どんなん?
例えば、このコードは(C++に翻訳してそのコードを)実行できます。
どうしてもOmega言語にしたい筆者
test.om
import "iostream"
use std;
def main() {
print "OK";
return;
}
これをpythonで読みこんでC++コードにすると
test.cpp
#include "iostream"
using namespace std;
int main() {
cout << "OK";
return 0;
}
こうなる。
コードです。
pythonコード
main.py
import sys
class Read:
def __init__(self):
text = open(sys.argv[1])
self.data = text.read()
text.close()
def ope(self):
return self.data
def run(self):
return self.ope().replace("import", "#include").replace("return;", "return 0;").replace("def", "int").replace("print", "cout <<").replace("use", "using namespace")
class OmegaMain:
def __init__(self):
self.cpp = open(sys.argv[1].split(".")[0]+".cpp", "w", encoding="utf_8")
def run(self):
self.cpp.write(Read().run())
self.cpp.close()
def main():
OmegaMain().run()
if __name__ == '__main__':
main()
今回は短いのでコードつき。
まとめ
今回は、pythonとC++の中間言語を作りました。Githubに公開したら追記します。あんま意味ないと思うけど。
それでは、また次の記事でお会いしましょう!
さようなら!