From 5d584aded8bcabaafddf2f61692f8663913be603 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 31 Jan 2017 23:59:41 +0100 Subject: Parsing function definitions. --- libsolidity/inlineasm/AsmCodeGen.cpp | 5 +++- libsolidity/inlineasm/AsmData.h | 5 +++- libsolidity/inlineasm/AsmParser.cpp | 49 +++++++++++++++++++++++++++++++++--- libsolidity/inlineasm/AsmParser.h | 2 ++ 4 files changed, 55 insertions(+), 6 deletions(-) (limited to 'libsolidity/inlineasm') diff --git a/libsolidity/inlineasm/AsmCodeGen.cpp b/libsolidity/inlineasm/AsmCodeGen.cpp index 43c3b27a..1eab5df0 100644 --- a/libsolidity/inlineasm/AsmCodeGen.cpp +++ b/libsolidity/inlineasm/AsmCodeGen.cpp @@ -249,7 +249,10 @@ public: _block.location ); } - + } + void operator()(assembly::FunctionDefinition const&) + { + solAssert(false, "Function definition not removed during desugaring phase."); } private: diff --git a/libsolidity/inlineasm/AsmData.h b/libsolidity/inlineasm/AsmData.h index d622ff54..64dd7b8e 100644 --- a/libsolidity/inlineasm/AsmData.h +++ b/libsolidity/inlineasm/AsmData.h @@ -48,8 +48,9 @@ struct Label { SourceLocation location; std::string name; }; struct Assignment { SourceLocation location; Identifier variableName; }; struct FunctionalAssignment; struct VariableDeclaration; +struct FunctionDefinition; struct Block; -using Statement = boost::variant; +using Statement = boost::variant; /// Functional assignment ("x := mload(20)", expects push-1-expression on the right hand /// side and requires x to occupy exactly one stack slot. struct FunctionalAssignment { SourceLocation location; Identifier variableName; std::shared_ptr value; }; @@ -59,6 +60,8 @@ struct FunctionalInstruction { SourceLocation location; Instruction instruction; struct VariableDeclaration { SourceLocation location; std::string name; std::shared_ptr value; }; /// Block that creates a scope (frees declared stack variables) struct Block { SourceLocation location; std::vector statements; }; +/// Function definition ("function f(a, b) -> (d, e) { ... }") +struct FunctionDefinition { SourceLocation location; std::string name; std::vector arguments; std::vector returns; Block body; }; struct LocationExtractor: boost::static_visitor { diff --git a/libsolidity/inlineasm/AsmParser.cpp b/libsolidity/inlineasm/AsmParser.cpp index 46a2730d..019ec1a3 100644 --- a/libsolidity/inlineasm/AsmParser.cpp +++ b/libsolidity/inlineasm/AsmParser.cpp @@ -62,6 +62,8 @@ assembly::Statement Parser::parseStatement() { case Token::Let: return parseVariableDeclaration(); + case Token::Function: + return parseFunctionDefinition(); case Token::LBrace: return parseBlock(); case Token::Assign: @@ -214,10 +216,7 @@ assembly::VariableDeclaration Parser::parseVariableDeclaration() { VariableDeclaration varDecl = createWithLocation(); expectToken(Token::Let); - varDecl.name = m_scanner->currentLiteral(); - if (instructions().count(varDecl.name)) - fatalParserError("Cannot use instruction names for identifier names."); - expectToken(Token::Identifier); + varDecl.name = expectAsmIdentifier(); expectToken(Token::Colon); expectToken(Token::Assign); varDecl.value.reset(new Statement(parseExpression())); @@ -225,6 +224,39 @@ assembly::VariableDeclaration Parser::parseVariableDeclaration() return varDecl; } +assembly::FunctionDefinition Parser::parseFunctionDefinition() +{ + FunctionDefinition funDef = createWithLocation(); + expectToken(Token::Function); + funDef.name = expectAsmIdentifier(); + expectToken(Token::LParen); + while (m_scanner->currentToken() != Token::RParen) + { + funDef.arguments.push_back(expectAsmIdentifier()); + if (m_scanner->currentToken() == Token::RParen) + break; + expectToken(Token::Comma); + } + expectToken(Token::RParen); + if (m_scanner->currentToken() == Token::Sub) + { + expectToken(Token::Sub); + expectToken(Token::GreaterThan); + expectToken(Token::LParen); + while (true) + { + funDef.returns.push_back(expectAsmIdentifier()); + if (m_scanner->currentToken() == Token::RParen) + break; + expectToken(Token::Comma); + } + expectToken(Token::RParen); + } + funDef.body = parseBlock(); + funDef.location.end = funDef.body.location.end; + return funDef; +} + FunctionalInstruction Parser::parseFunctionalInstruction(assembly::Statement&& _instruction) { if (_instruction.type() != typeid(Instruction)) @@ -266,3 +298,12 @@ FunctionalInstruction Parser::parseFunctionalInstruction(assembly::Statement&& _ expectToken(Token::RParen); return ret; } + +string Parser::expectAsmIdentifier() +{ + string name = m_scanner->currentLiteral(); + if (instructions().count(name)) + fatalParserError("Cannot use instruction names for identifier names."); + expectToken(Token::Identifier); + return name; +} diff --git a/libsolidity/inlineasm/AsmParser.h b/libsolidity/inlineasm/AsmParser.h index 643548dd..1da049f6 100644 --- a/libsolidity/inlineasm/AsmParser.h +++ b/libsolidity/inlineasm/AsmParser.h @@ -67,7 +67,9 @@ protected: std::map const& instructions(); Statement parseElementaryOperation(bool _onlySinglePusher = false); VariableDeclaration parseVariableDeclaration(); + FunctionDefinition parseFunctionDefinition(); FunctionalInstruction parseFunctionalInstruction(Statement&& _instruction); + std::string expectAsmIdentifier(); }; } -- cgit v1.2.3