Compiler for assignments.

This commit is contained in:
Christian 2014-10-25 16:52:22 +02:00
parent f6fabb268b
commit 03a6cf0e81

View File

@ -34,25 +34,24 @@
#include <libsolidity/Compiler.h> #include <libsolidity/Compiler.h>
#include <libsolidity/SourceReferenceFormatter.h> #include <libsolidity/SourceReferenceFormatter.h>
using namespace std;
using namespace dev; using namespace dev;
using namespace solidity; using namespace solidity;
void help() void help()
{ {
std::cout cout << "Usage solc [OPTIONS] <file>" << endl
<< "Usage solc [OPTIONS] <file>" << std::endl << "Options:" << endl
<< "Options:" << std::endl << " -h,--help Show this help message and exit." << endl
<< " -h,--help Show this help message and exit." << std::endl << " -V,--version Show the version and exit." << endl;
<< " -V,--version Show the version and exit." << std::endl;
exit(0); exit(0);
} }
void version() void version()
{ {
std::cout cout << "solc, the solidity complier commandline interface " << dev::Version << endl
<< "solc, the solidity complier commandline interface " << dev::Version << std::endl << " by Christian <c@ethdev.com>, (c) 2014." << endl
<< " by Christian <c@ethdev.com>, (c) 2014." << std::endl << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl;
<< "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << std::endl;
exit(0); exit(0);
} }
@ -87,10 +86,10 @@ private:
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
std::string infile; string infile;
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i)
{ {
std::string arg = argv[i]; string arg = argv[i];
if (arg == "-h" || arg == "--help") if (arg == "-h" || arg == "--help")
help(); help();
else if (arg == "-V" || arg == "--version") else if (arg == "-V" || arg == "--version")
@ -98,13 +97,13 @@ int main(int argc, char** argv)
else else
infile = argv[i]; infile = argv[i];
} }
std::string sourceCode; string sourceCode;
if (infile.empty()) if (infile.empty())
{ {
std::string s; string s;
while (!std::cin.eof()) while (!cin.eof())
{ {
getline(std::cin, s); getline(cin, s);
sourceCode.append(s); sourceCode.append(s);
} }
} }
@ -112,7 +111,7 @@ int main(int argc, char** argv)
sourceCode = asString(dev::contents(infile)); sourceCode = asString(dev::contents(infile));
ASTPointer<ContractDefinition> ast; ASTPointer<ContractDefinition> ast;
std::shared_ptr<Scanner> scanner = std::make_shared<Scanner>(CharStream(sourceCode)); shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(sourceCode));
Parser parser; Parser parser;
try try
{ {
@ -120,39 +119,48 @@ int main(int argc, char** argv)
} }
catch (ParserError const& exception) catch (ParserError const& exception)
{ {
SourceReferenceFormatter::printExceptionInformation(std::cerr, exception, "Parser error", *scanner); SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Parser error", *scanner);
return -1; return -1;
} }
dev::solidity::NameAndTypeResolver resolver; NameAndTypeResolver resolver;
try try
{ {
resolver.resolveNamesAndTypes(*ast.get()); resolver.resolveNamesAndTypes(*ast.get());
} }
catch (DeclarationError const& exception) catch (DeclarationError const& exception)
{ {
SourceReferenceFormatter::printExceptionInformation(std::cerr, exception, "Declaration error", *scanner); SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Declaration error", *scanner);
return -1; return -1;
} }
catch (TypeError const& exception) catch (TypeError const& exception)
{ {
SourceReferenceFormatter::printExceptionInformation(std::cerr, exception, "Type error", *scanner); SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Type error", *scanner);
return -1; return -1;
} }
std::cout << "Syntax tree for the contract:" << std::endl; cout << "Syntax tree for the contract:" << endl;
dev::solidity::ASTPrinter printer(ast, sourceCode); dev::solidity::ASTPrinter printer(ast, sourceCode);
printer.print(std::cout); printer.print(cout);
FirstExpressionExtractor extractor(*ast); FirstExpressionExtractor extractor(*ast);
CompilerContext context; CompilerContext context;
ExpressionCompiler compiler(context); ExpressionCompiler compiler(context);
try
{
compiler.compile(*extractor.getExpression()); compiler.compile(*extractor.getExpression());
}
catch (CompilerError const& exception)
{
SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Compiler error", *scanner);
return -1;
}
bytes instructions = compiler.getAssembledBytecode(); bytes instructions = compiler.getAssembledBytecode();
// debug
std::cout << "Bytecode for the first expression: " << std::endl; cout << "Bytecode for the first expression: " << endl;
std::cout << eth::disassemble(instructions) << std::endl; cout << eth::disassemble(instructions) << endl;
return 0; return 0;
} }