#include <stdlib.h>
#include <evalarray.ci>

namespace evalBuilder
{
	void inline writeFunction (const std::string &src, const std::string &varname)
	{
	 	FILE *f = fopen("f.cpp", "w");
		//fprintf(f, "template<class T> inline T f<T> (T);\ntemplate<> inline T f<double> (T %s) { %s }\n#include \"fstatic.c\"", 
		fprintf(f, "#include <math.h>\ninline double f(double %s)\n{\n%s\n}\n#include \"fstatic.c\"\n",
			varname.c_str(), src.c_str());
		fclose(f);
	}

	std::string build2 (const std::string &src)
	{
		writeFunction(src, "x"); 
		FILE *fp = popen("make f.exe 2>&1|grep undeclared", "r");
		char buf[100];
		fgets(buf, 100, fp);
		pclose(fp);
		char *t = strstr(buf, "`");
		t++;
		*strstr(t, "'") = '\0';
		writeFunction(src, t);
		::system("make f.exe");
		unlink("f.cpp");
		std::string ret = getenv("PWD");
		ret += "/f.exe";
		return ret;
	}
}
