Contract compiler and also add ExpressionStatement to AST.

ExpressionStatement functions as glue between Statements and Expressions.

This way it is possible to detect when the border between statements and
expressions is crossed while walking the AST. Note that ExpressionStatement is
not the only border, almost every statement can contains expressions.
This commit is contained in:
Christian 2014-10-30 01:20:32 +01:00
parent 03a6cf0e81
commit 877aa91c57

View File

@ -55,35 +55,6 @@ 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)
{
string infile;
@ -143,13 +114,10 @@ int main(int argc, char** argv)
dev::solidity::ASTPrinter printer(ast, sourceCode);
printer.print(cout);
FirstExpressionExtractor extractor(*ast);
CompilerContext context;
ExpressionCompiler compiler(context);
bytes instructions;
try
{
compiler.compile(*extractor.getExpression());
instructions = Compiler::compile(*ast);
}
catch (CompilerError const& exception)
{
@ -157,10 +125,9 @@ int main(int argc, char** argv)
return -1;
}
bytes instructions = compiler.getAssembledBytecode();
cout << "Bytecode for the first expression: " << endl;
cout << "EVM assembly: " << endl;
cout << eth::disassemble(instructions) << endl;
cout << "Binary: " << toHex(instructions) << endl;
return 0;
}