[C++]C++でeval(), それWandboxでできます
C++でevalできないなんて誰が言ったのか。C++,JavaScript,C#,perl,python,ruby,mruby,Erlang,haskellぜんぶC++から簡単にevalできます。
書いたもの
https://github.com/nyanp/frisbee
200行弱で書けてしまった。コンパイルにはboost,実行にはcurlが必要。evalしたい言語の環境は不要。
サンプル
#include <iostream> #include "frisbee.h" using namespace std; using namespace frisbee; int main(void) { try { // eval // 受けたい型で特殊化する。コンパイルエラーなら例外 cout << eval<string>("C++", "#include <iostream>\n int main(){ std::cout <<\"C++\"<< std::endl; }"); cout << eval<string>("C#", "class Test { static void Main(string[] args){System.Console.WriteLine(\"C#\");}}"); cout << eval<string>("haskell", "fib = 1:1:zipWith (+) fib (tail fib)\nmain = do print $ take 5 $ fib"); cout << eval<string>("perl", "print (1..10)"); cout << eval<int>("ruby", "$,=\" \"; p (1..10).inject(0) {|sum, i| sum + i }"); // コンパイル通るかどうかチェック auto result = compile("C++", "#include <iostream>"); if (result.is_error()) cout << result.compiler_error(); // コンパイルエラーを表示 // compile from file result = compile_fromfile("C++", "foo.cpp"); } catch (const exception& e) { cout << e.what(); } }
しくみ
殆ど自分では何もやっていない感じ。犬にひたすら投げて返ってくるのを待つだけです。Wandbox APIは以下。
wandbox/kennel/API.rst at master · melpon/wandbox · GitHub
負荷を気にしつつ、用法用量を守って正しく投げましょう。