Merge pull request #135 from chriseth/multiJson

Multiple sources for json compiler.
This commit is contained in:
chriseth 2015-10-15 19:04:52 +02:00
commit 984ab6ab2d

View File

@ -114,18 +114,16 @@ Json::Value estimateGas(CompilerStack const& _compiler, string const& _contract)
return gasEstimates; return gasEstimates;
} }
string compile(string _input, bool _optimize) string compile(StringMap const& _sources, bool _optimize)
{ {
StringMap sources;
sources[""] = _input;
Json::Value output(Json::objectValue); Json::Value output(Json::objectValue);
Json::Value errors(Json::arrayValue); Json::Value errors(Json::arrayValue);
CompilerStack compiler; CompilerStack compiler;
bool success = false; bool success = false;
try try
{ {
bool succ = compiler.compile(_input, _optimize); compiler.addSources(_sources);
bool succ = compiler.compile(_optimize);
for (auto const& error: compiler.errors()) for (auto const& error: compiler.errors())
{ {
auto err = dynamic_pointer_cast<Error const>(error); auto err = dynamic_pointer_cast<Error const>(error);
@ -175,19 +173,52 @@ string compile(string _input, bool _optimize)
contractData["functionHashes"] = functionHashes(compiler.contractDefinition(contractName)); contractData["functionHashes"] = functionHashes(compiler.contractDefinition(contractName));
contractData["gasEstimates"] = estimateGas(compiler, contractName); contractData["gasEstimates"] = estimateGas(compiler, contractName);
ostringstream unused; ostringstream unused;
contractData["assembly"] = compiler.streamAssembly(unused, contractName, sources, true); contractData["assembly"] = compiler.streamAssembly(unused, contractName, _sources, true);
output["contracts"][contractName] = contractData; output["contracts"][contractName] = contractData;
} }
output["sources"] = Json::Value(Json::objectValue); output["sources"] = Json::Value(Json::objectValue);
output["sources"][""] = Json::Value(Json::objectValue); for (auto const& source: _sources)
output["sources"][""]["AST"] = ASTJsonConverter(compiler.ast("")).json(); {
output["sources"][source.first] = Json::Value(Json::objectValue);
output["sources"][source.first]["AST"] = ASTJsonConverter(compiler.ast(source.first)).json();
}
} }
return Json::FastWriter().write(output); return Json::FastWriter().write(output);
} }
static string outputBuffer; string compileMulti(string const& _input, bool _optimize)
{
Json::Reader reader;
Json::Value input;
if (!reader.parse(_input, input, false))
{
Json::Value errors(Json::arrayValue);
errors.append("Error parsing input JSON: " + reader.getFormattedErrorMessages());
Json::Value output(Json::objectValue);
output["errors"] = errors;
return Json::FastWriter().write(output);
}
else
{
StringMap sources;
Json::Value jsonSources = input["sources"];
if (jsonSources.isObject())
for (auto const& sourceName: jsonSources.getMemberNames())
sources[sourceName] = jsonSources[sourceName].asString();
return compile(sources, _optimize);
}
}
string compileSingle(string const& _input, bool _optimize)
{
StringMap sources;
sources[""] = _input;
return compile(sources, _optimize);
}
static string s_outputBuffer;
extern "C" extern "C"
{ {
@ -197,7 +228,12 @@ extern char const* version()
} }
extern char const* compileJSON(char const* _input, bool _optimize) extern char const* compileJSON(char const* _input, bool _optimize)
{ {
outputBuffer = compile(_input, _optimize); s_outputBuffer = compileSingle(_input, _optimize);
return outputBuffer.c_str(); return s_outputBuffer.c_str();
}
extern char const* compileJSONMulti(char const* _input, bool _optimize)
{
s_outputBuffer = compileMulti(_input, _optimize);
return s_outputBuffer.c_str();
} }
} }