mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Import directive.
This commit is contained in:
parent
e7201a0324
commit
5b6a211b8a
@ -46,16 +46,22 @@ namespace
|
|||||||
bytes compileContract(const string& _sourceCode)
|
bytes compileContract(const string& _sourceCode)
|
||||||
{
|
{
|
||||||
Parser parser;
|
Parser parser;
|
||||||
ASTPointer<ContractDefinition> contract;
|
ASTPointer<SourceUnit> sourceUnit;
|
||||||
BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
|
BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
|
||||||
NameAndTypeResolver resolver({});
|
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
|
||||||
BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
|
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
|
||||||
|
{
|
||||||
|
NameAndTypeResolver resolver({});
|
||||||
|
BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
|
||||||
|
|
||||||
Compiler compiler;
|
Compiler compiler;
|
||||||
compiler.compileContract(*contract, {});
|
compiler.compileContract(*contract, {});
|
||||||
// debug
|
// debug
|
||||||
//compiler.streamAssembly(cout);
|
//compiler.streamAssembly(cout);
|
||||||
return compiler.getAssembledBytecode();
|
return compiler.getAssembledBytecode();
|
||||||
|
}
|
||||||
|
BOOST_FAIL("No contract found in source.");
|
||||||
|
return bytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks that @a _compiledCode is present starting from offset @a _offset in @a _expectation.
|
/// Checks that @a _compiledCode is present starting from offset @a _offset in @a _expectation.
|
||||||
|
@ -86,27 +86,32 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _
|
|||||||
vector<vector<string>> _localVariables = {})
|
vector<vector<string>> _localVariables = {})
|
||||||
{
|
{
|
||||||
Parser parser;
|
Parser parser;
|
||||||
ASTPointer<ContractDefinition> contract;
|
ASTPointer<SourceUnit> sourceUnit;
|
||||||
BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
|
BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
|
||||||
NameAndTypeResolver resolver({});
|
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
|
||||||
BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
|
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
|
||||||
FirstExpressionExtractor extractor(*contract);
|
{
|
||||||
BOOST_REQUIRE(extractor.getExpression() != nullptr);
|
NameAndTypeResolver resolver({});
|
||||||
|
BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
|
||||||
|
FirstExpressionExtractor extractor(*contract);
|
||||||
|
BOOST_REQUIRE(extractor.getExpression() != nullptr);
|
||||||
|
|
||||||
CompilerContext context;
|
CompilerContext context;
|
||||||
for (vector<string> const& function: _functions)
|
for (vector<string> const& function: _functions)
|
||||||
context.addFunction(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
|
context.addFunction(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
|
||||||
for (vector<string> const& variable: _localVariables)
|
for (vector<string> const& variable: _localVariables)
|
||||||
context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)));
|
context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)));
|
||||||
|
|
||||||
ExpressionCompiler::compileExpression(context, *extractor.getExpression());
|
ExpressionCompiler::compileExpression(context, *extractor.getExpression());
|
||||||
|
|
||||||
for (vector<string> const& function: _functions)
|
for (vector<string> const& function: _functions)
|
||||||
context << context.getFunctionEntryLabel(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
|
context << context.getFunctionEntryLabel(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
|
||||||
bytes instructions = context.getAssembledBytecode();
|
bytes instructions = context.getAssembledBytecode();
|
||||||
// debug
|
// debug
|
||||||
// cout << eth::disassemble(instructions) << endl;
|
// cout << eth::disassemble(instructions) << endl;
|
||||||
return instructions;
|
return instructions;
|
||||||
|
}
|
||||||
|
BOOST_FAIL("No contract found in source.");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end anonymous namespace
|
} // end anonymous namespace
|
||||||
|
@ -41,10 +41,13 @@ namespace
|
|||||||
void parseTextAndResolveNames(std::string const& _source)
|
void parseTextAndResolveNames(std::string const& _source)
|
||||||
{
|
{
|
||||||
Parser parser;
|
Parser parser;
|
||||||
ASTPointer<ContractDefinition> contract = parser.parse(
|
ASTPointer<SourceUnit> sourceUnit = parser.parse(std::make_shared<Scanner>(CharStream(_source)));
|
||||||
std::make_shared<Scanner>(CharStream(_source)));
|
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
|
||||||
NameAndTypeResolver resolver({});
|
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
|
||||||
resolver.resolveNamesAndTypes(*contract);
|
{
|
||||||
|
NameAndTypeResolver resolver({});
|
||||||
|
resolver.resolveNamesAndTypes(*contract);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,13 +21,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
#include <libdevcore/Log.h>
|
#include <libdevcore/Log.h>
|
||||||
#include <libsolidity/Scanner.h>
|
#include <libsolidity/Scanner.h>
|
||||||
#include <libsolidity/Parser.h>
|
#include <libsolidity/Parser.h>
|
||||||
#include <libsolidity/Exceptions.h>
|
#include <libsolidity/Exceptions.h>
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
namespace dev
|
namespace dev
|
||||||
{
|
{
|
||||||
namespace solidity
|
namespace solidity
|
||||||
@ -40,7 +42,12 @@ namespace
|
|||||||
ASTPointer<ContractDefinition> parseText(std::string const& _source)
|
ASTPointer<ContractDefinition> parseText(std::string const& _source)
|
||||||
{
|
{
|
||||||
Parser parser;
|
Parser parser;
|
||||||
return parser.parse(std::make_shared<Scanner>(CharStream(_source)));
|
ASTPointer<SourceUnit> sourceUnit = parser.parse(std::make_shared<Scanner>(CharStream(_source)));
|
||||||
|
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
|
||||||
|
if (ASTPointer<ContractDefinition> contract = dynamic_pointer_cast<ContractDefinition>(node))
|
||||||
|
return contract;
|
||||||
|
BOOST_FAIL("No contract found in source.");
|
||||||
|
return ASTPointer<ContractDefinition>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -380,6 +387,50 @@ BOOST_AUTO_TEST_CASE(statement_starting_with_type_conversion)
|
|||||||
BOOST_CHECK_NO_THROW(parseText(text));
|
BOOST_CHECK_NO_THROW(parseText(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(import_directive)
|
||||||
|
{
|
||||||
|
char const* text = "import \"abc\";\n"
|
||||||
|
"contract test {\n"
|
||||||
|
" function fun() {\n"
|
||||||
|
" uint64(2);\n"
|
||||||
|
" }\n"
|
||||||
|
"}\n";
|
||||||
|
BOOST_CHECK_NO_THROW(parseText(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(multiple_contracts)
|
||||||
|
{
|
||||||
|
char const* text = "contract test {\n"
|
||||||
|
" function fun() {\n"
|
||||||
|
" uint64(2);\n"
|
||||||
|
" }\n"
|
||||||
|
"}\n"
|
||||||
|
"contract test2 {\n"
|
||||||
|
" function fun() {\n"
|
||||||
|
" uint64(2);\n"
|
||||||
|
" }\n"
|
||||||
|
"}\n";
|
||||||
|
BOOST_CHECK_NO_THROW(parseText(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(multiple_contracts_and_imports)
|
||||||
|
{
|
||||||
|
char const* text = "import \"abc\";\n"
|
||||||
|
"contract test {\n"
|
||||||
|
" function fun() {\n"
|
||||||
|
" uint64(2);\n"
|
||||||
|
" }\n"
|
||||||
|
"}\n"
|
||||||
|
"import \"def\";\n"
|
||||||
|
"contract test2 {\n"
|
||||||
|
" function fun() {\n"
|
||||||
|
" uint64(2);\n"
|
||||||
|
" }\n"
|
||||||
|
"}\n"
|
||||||
|
"import \"ghi\";\n";
|
||||||
|
BOOST_CHECK_NO_THROW(parseText(text));
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user