aboutsummaryrefslogtreecommitdiffstats
path: root/CompilerStack.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'CompilerStack.cpp')
-rw-r--r--CompilerStack.cpp87
1 files changed, 51 insertions, 36 deletions
diff --git a/CompilerStack.cpp b/CompilerStack.cpp
index 6535e00d..8f8c84fe 100644
--- a/CompilerStack.cpp
+++ b/CompilerStack.cpp
@@ -45,10 +45,14 @@ void CompilerStack::parse()
{
if (!m_scanner)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Source not available."));
- m_contractASTNode = Parser().parse(m_scanner);
+ m_sourceUnitASTNode = Parser().parse(m_scanner);
m_globalContext = make_shared<GlobalContext>();
- m_globalContext->setCurrentContract(*m_contractASTNode);
- NameAndTypeResolver(m_globalContext->getDeclarations()).resolveNamesAndTypes(*m_contractASTNode);
+ for (ASTPointer<ASTNode> const& node: m_sourceUnitASTNode->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ m_globalContext->setCurrentContract(*contract);
+ NameAndTypeResolver(m_globalContext->getDeclarations()).resolveNamesAndTypes(*contract);
+ }
m_parseSuccessful = true;
}
@@ -62,10 +66,16 @@ bytes const& CompilerStack::compile(bool _optimize)
{
if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
- m_bytecode.clear();
- m_compiler = make_shared<Compiler>();
- m_compiler->compileContract(*m_contractASTNode, m_globalContext->getMagicVariables());
- return m_bytecode = m_compiler->getAssembledBytecode(_optimize);
+ //@todo returns only the last contract for now
+ for (ASTPointer<ASTNode> const& node: m_sourceUnitASTNode->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ m_bytecode.clear();
+ m_compiler = make_shared<Compiler>();
+ m_compiler->compileContract(*contract, m_globalContext->getMagicVariables());
+ m_bytecode = m_compiler->getAssembledBytecode(_optimize);
+ }
+ return m_bytecode;
}
bytes const& CompilerStack::compile(string const& _sourceCode, bool _optimize)
@@ -87,40 +97,45 @@ string const& CompilerStack::getInterface()
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
if (m_interface.empty())
{
- stringstream interface;
- interface << '[';
- vector<FunctionDefinition const*> exportedFunctions = m_contractASTNode->getInterfaceFunctions();
- unsigned functionsCount = exportedFunctions.size();
- for (FunctionDefinition const* f: exportedFunctions)
- {
- auto streamVariables = [&](vector<ASTPointer<VariableDeclaration>> const& _vars)
+ //@todo returns only the last contract for now
+ for (ASTPointer<ASTNode> const& node: m_sourceUnitASTNode->getNodes())
+ if (ContractDefinition const* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
- unsigned varCount = _vars.size();
- for (ASTPointer<VariableDeclaration> const& var: _vars)
+ stringstream interface;
+ interface << '[';
+ vector<FunctionDefinition const*> exportedFunctions = contract->getInterfaceFunctions();
+ unsigned functionsCount = exportedFunctions.size();
+ for (FunctionDefinition const* f: exportedFunctions)
{
- interface << "{"
- << "\"name\":" << escaped(var->getName(), false) << ","
- << "\"type\":" << escaped(var->getType()->toString(), false)
+ auto streamVariables = [&](vector<ASTPointer<VariableDeclaration>> const& _vars)
+ {
+ unsigned varCount = _vars.size();
+ for (ASTPointer<VariableDeclaration> const& var: _vars)
+ {
+ interface << "{"
+ << "\"name\":" << escaped(var->getName(), false) << ","
+ << "\"type\":" << escaped(var->getType()->toString(), false)
+ << "}";
+ if (--varCount > 0)
+ interface << ",";
+ }
+ };
+
+ interface << '{'
+ << "\"name\":" << escaped(f->getName(), false) << ","
+ << "\"inputs\":[";
+ streamVariables(f->getParameters());
+ interface << "],"
+ << "\"outputs\":[";
+ streamVariables(f->getReturnParameters());
+ interface << "]"
<< "}";
- if (--varCount > 0)
+ if (--functionsCount > 0)
interface << ",";
}
- };
-
- interface << '{'
- << "\"name\":" << escaped(f->getName(), false) << ","
- << "\"inputs\":[";
- streamVariables(f->getParameters());
- interface << "],"
- << "\"outputs\":[";
- streamVariables(f->getReturnParameters());
- interface << "]"
- << "}";
- if (--functionsCount > 0)
- interface << ",";
- }
- interface << ']';
- m_interface = interface.str();
+ interface << ']';
+ m_interface = interface.str();
+ }
}
return m_interface;
}