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,8 +46,11 @@ namespace
|
||||
bytes compileContract(const string& _sourceCode)
|
||||
{
|
||||
Parser parser;
|
||||
ASTPointer<ContractDefinition> contract;
|
||||
BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
|
||||
ASTPointer<SourceUnit> sourceUnit;
|
||||
BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
|
||||
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
|
||||
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
|
||||
{
|
||||
NameAndTypeResolver resolver({});
|
||||
BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
|
||||
|
||||
@ -57,6 +60,9 @@ bytes compileContract(const string& _sourceCode)
|
||||
//compiler.streamAssembly(cout);
|
||||
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.
|
||||
/// This is necessary since the compiler will add boilerplate add the beginning that is not
|
||||
|
@ -86,8 +86,11 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _
|
||||
vector<vector<string>> _localVariables = {})
|
||||
{
|
||||
Parser parser;
|
||||
ASTPointer<ContractDefinition> contract;
|
||||
BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
|
||||
ASTPointer<SourceUnit> sourceUnit;
|
||||
BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
|
||||
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
|
||||
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
|
||||
{
|
||||
NameAndTypeResolver resolver({});
|
||||
BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
|
||||
FirstExpressionExtractor extractor(*contract);
|
||||
@ -108,6 +111,8 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _
|
||||
// cout << eth::disassemble(instructions) << endl;
|
||||
return instructions;
|
||||
}
|
||||
BOOST_FAIL("No contract found in source.");
|
||||
}
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
|
@ -41,12 +41,15 @@ namespace
|
||||
void parseTextAndResolveNames(std::string const& _source)
|
||||
{
|
||||
Parser parser;
|
||||
ASTPointer<ContractDefinition> contract = 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 (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
|
||||
{
|
||||
NameAndTypeResolver resolver({});
|
||||
resolver.resolveNamesAndTypes(*contract);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(SolidityNameAndTypeResolution)
|
||||
|
||||
|
@ -21,13 +21,15 @@
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <memory>
|
||||
#include <libdevcore/Log.h>
|
||||
#include <libsolidity/Scanner.h>
|
||||
#include <libsolidity/Parser.h>
|
||||
#include <libsolidity/Exceptions.h>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace dev
|
||||
{
|
||||
namespace solidity
|
||||
@ -40,7 +42,12 @@ namespace
|
||||
ASTPointer<ContractDefinition> parseText(std::string const& _source)
|
||||
{
|
||||
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_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()
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user