-
Notifications
You must be signed in to change notification settings - Fork 71
Universal Code Generator to support new languages easily #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Universal Code Generator to support new languages easily #228
Conversation
Codecov Report
@@ Coverage Diff @@
## stable #228 +/- ##
==========================================
- Coverage 89.64% 89.20% -0.44%
==========================================
Files 55 56 +1
Lines 2877 2363 -514
==========================================
- Hits 2579 2108 -471
+ Misses 298 255 -43
Continue to review full report at Codecov.
|
といった感じのif分岐は全部enumで置き換えたほうがいいですかね?つまり、return self.info["input_func"][type.value]とシンプルに書くことができると思います。 |
self._format = format_ | ||
self._config = config | ||
self.info = toml.load( | ||
Path(__file__).parent / "universal_generator" / "{lang}.toml".format(lang=lang)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このファイル名そのものを外から挿入できるようにしたほうが使い勝手が良いので、お願いできますか?
func get_builtin_code_generator_info_toml_path(lang):
return Path(__file__).parent / "universal_generator" / "{lang}.toml".format(lang=lang))
とかを別途このファイルで定義しておけば、呼び出し側は、
code_parameters = UniversalCodeGenerator(
args.format, args.config, get_builtin_code_generator_info_toml_path("java")).generate_parameters()
とかでよくなります。
これのメリットとして、ディレクトリ構造に非依存になるので、カスタムコードジェネレーターで、わざわざコードをuniversal_generator
以下に配置しなくてもUnivesalCodeGeneratorが使えるようになります。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらは、universal_code_generatorにはpathを渡すという要望で.atcodertools.tomlからcustom_code_generator.pyみたいな感じでtomlを呼べるようにするところまではまだしなくていいんですよね?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
しなくていいです。
ワークアラウンドとして最悪ユーザーのcustom_code_generator.pyにパスをハードコードすればいいので
README.md
Outdated
|
||
|
||
### ユニバーサルコードジェネレーター | ||
ユニバーサルコードジェネレータはループ・配列アクセス方法等のいくつかの言語仕様を記述するだけでカスタムコードジェネレータよりも簡単にコード生成することを意図して作成したジェネレータです。設定ファイル`(言語名).toml`を`atcodertools/codegen/code_generators/universal_generator`に配置してください。設定ファイルの書き方は以下です。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
先程の部分を訂正するとこの部分がいらなくなるはず
設定ファイル
(言語名).toml
をatcodertools/codegen/code_generators/universal_generator
に配置してください。設定ファイルの書き方は以下です。
#include <iostream> | ||
#include <vector> | ||
#include <cassert> | ||
#include<bits/stdc++.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Xcode(macOS)とVisual Studio(Windows)にはbits/stdc++
が存在しない(テストが通らない)ので、普通のヘッダのほうがうれしいです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
代替案としてこのへんが現実的なヘッダかなと思いますがどう思いますか
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cassertがなかったので追加しておきました!
Enumを使う方式に変更してコードをシンプルに
可読性の観点からこれは十分シンプルなのでこれでいいと思います。 メンテ性の観点からはあんまり今後頻繁に型が増えるとは思えないのでこのままで十分です |
bits/stdc++.hを使わないように変更
C++のbits/stdc++.hを使わないようにしました。また、formatのキーワードの指定を一々指定するのではなくて、専用の関数から展開するようにしました。これによって、割とコードがシンプルになったと思います。 上記のコメントを読む前にif分岐の部分もenumから配列アクセスするようにしてしまいましたが、不評でしたら戻します。 |
ありがとうございます。 あとinput_funcに関してなんですが、こっちもより間接的になっている気がします。この記法のユーザー的なメリットを教えて頂けたら納得できるかもしれません。教えて頂けますか? |
README.md
Outdated
|
||
|
||
### ユニバーサルコードジェネレーター | ||
ユニバーサルコードジェネレータはループ・配列アクセス方法等のいくつかの言語仕様を記述するだけでカスタムコードジェネレータよりも簡単にコード生成することを意図して作成したジェネレータです。設定ファイルの書き方は以下です。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trivialな表記揺れですが
- ジェネレータ -> ジェネレーター (またはジェネレータに統一する)
- python -> Python
- perl -> Perl
- php -> PHP
では、if分岐のところはもとに戻します。キーワードをまとめるのも便利だと思ったんですが、間接化してることになっちゃいますかね? input_funcを使うメリットはpythonで確保と入力をfor文を使わないで
|
@chaemon なるほどなるほど、よくわかりました。ではinput_funcに関してはそれでいきましょう。 |
これごめんなさい。戻してくれたところを改めて見て、やっぱり戻さないほうがキレイに見えました。これは完全に僕の勘違い&間違った指示です。スミマセン... |
LGTM, @chaemon マージしてよかったら教えてください |
では、if分岐ではなくenumのvalueを使う方にしました! これでマージしていただいて構いませんよ〜 |
前に出したプルリクには他のが混ざっていたので切り分けました。ついでに以下を行いました。
配列の入力がappendしていく方式になっていましたがこれだとメモリ確保が倍々に行われてしまって非効率と思われるので、最初にサイズを指定する形式に変更しました