mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Expression compiler.
This commit is contained in:
parent
fa256b7f95
commit
221975bce8
41
main.cpp
41
main.cpp
@ -10,6 +10,7 @@
|
||||
#include <libsolidity/ASTPrinter.h>
|
||||
#include <libsolidity/NameAndTypeResolver.h>
|
||||
#include <libsolidity/Exceptions.h>
|
||||
#include <libsolidity/Compiler.h>
|
||||
#include <libsolidity/SourceReferenceFormatter.h>
|
||||
|
||||
using namespace dev;
|
||||
@ -34,6 +35,35 @@ void version()
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
/// Helper class that extracts the first expression in an AST.
|
||||
class FirstExpressionExtractor: private ASTVisitor
|
||||
{
|
||||
public:
|
||||
FirstExpressionExtractor(ASTNode& _node): m_expression(nullptr) { _node.accept(*this); }
|
||||
Expression* getExpression() const { return m_expression; }
|
||||
private:
|
||||
virtual bool visit(Expression& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(Assignment& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(UnaryOperation& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(BinaryOperation& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(FunctionCall& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(MemberAccess& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(IndexAccess& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(PrimaryExpression& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(Identifier& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(ElementaryTypeNameExpression& _expression) override { return checkExpression(_expression); }
|
||||
virtual bool visit(Literal& _expression) override { return checkExpression(_expression); }
|
||||
bool checkExpression(Expression& _expression)
|
||||
{
|
||||
if (m_expression == nullptr)
|
||||
m_expression = &_expression;
|
||||
return false;
|
||||
}
|
||||
private:
|
||||
Expression* m_expression;
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
std::string infile;
|
||||
@ -92,5 +122,16 @@ int main(int argc, char** argv)
|
||||
std::cout << "Syntax tree for the contract:" << std::endl;
|
||||
dev::solidity::ASTPrinter printer(ast, sourceCode);
|
||||
printer.print(std::cout);
|
||||
|
||||
FirstExpressionExtractor extractor(*ast);
|
||||
|
||||
CompilerContext context;
|
||||
ExpressionCompiler compiler(context);
|
||||
compiler.compile(*extractor.getExpression());
|
||||
bytes instructions = compiler.getAssembledBytecode();
|
||||
// debug
|
||||
std::cout << "Bytecode for the first expression: " << std::endl;
|
||||
std::cout << eth::disassemble(instructions) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user