diff options
author | chriseth <c@ethdev.com> | 2015-10-16 01:04:52 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-10-16 01:04:52 +0800 |
commit | 984ab6ab2da4566f627a274adb132154f252ca71 (patch) | |
tree | b833150ecf03b1366eac7fb0fc8f901f94f36bc7 | |
parent | 030c868f58bd5156f8c657865ed60b15d81ab7a0 (diff) | |
parent | cb869fd5a447fc70b8c43f14f234316b0898485c (diff) | |
download | dexon-solidity-984ab6ab2da4566f627a274adb132154f252ca71.tar.gz dexon-solidity-984ab6ab2da4566f627a274adb132154f252ca71.tar.zst dexon-solidity-984ab6ab2da4566f627a274adb132154f252ca71.zip |
Merge pull request #135 from chriseth/multiJson
Multiple sources for json compiler.
-rw-r--r-- | solc/jsonCompiler.cpp | 58 |
1 files changed, 47 insertions, 11 deletions
diff --git a/solc/jsonCompiler.cpp b/solc/jsonCompiler.cpp index 0746fc88..1690fadb 100644 --- a/solc/jsonCompiler.cpp +++ b/solc/jsonCompiler.cpp @@ -114,18 +114,16 @@ Json::Value estimateGas(CompilerStack const& _compiler, string const& _contract) 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 errors(Json::arrayValue); CompilerStack compiler; bool success = false; try { - bool succ = compiler.compile(_input, _optimize); + compiler.addSources(_sources); + bool succ = compiler.compile(_optimize); for (auto const& error: compiler.errors()) { 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["gasEstimates"] = estimateGas(compiler, contractName); ostringstream unused; - contractData["assembly"] = compiler.streamAssembly(unused, contractName, sources, true); + contractData["assembly"] = compiler.streamAssembly(unused, contractName, _sources, true); output["contracts"][contractName] = contractData; } output["sources"] = Json::Value(Json::objectValue); - output["sources"][""] = Json::Value(Json::objectValue); - output["sources"][""]["AST"] = ASTJsonConverter(compiler.ast("")).json(); + for (auto const& source: _sources) + { + output["sources"][source.first] = Json::Value(Json::objectValue); + output["sources"][source.first]["AST"] = ASTJsonConverter(compiler.ast(source.first)).json(); + } } 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" { @@ -197,7 +228,12 @@ extern char const* version() } extern char const* compileJSON(char const* _input, bool _optimize) { - outputBuffer = compile(_input, _optimize); - return outputBuffer.c_str(); + s_outputBuffer = compileSingle(_input, _optimize); + return s_outputBuffer.c_str(); +} +extern char const* compileJSONMulti(char const* _input, bool _optimize) +{ + s_outputBuffer = compileMulti(_input, _optimize); + return s_outputBuffer.c_str(); } } |