Skip to content

Commit

Permalink
Implementation Definition
Browse files Browse the repository at this point in the history
* added AST node for implementation definition and its new syntax rules
* added new grammar tokens 'implements', 'for', and 'this', which are
  needed to fully describe the definition and implementation details
  about implemented structures and/or features (aka. traits)
* related to ref #35
  • Loading branch information
ppaulweber committed Jul 30, 2017
1 parent fce06e4 commit 452957a
Show file tree
Hide file tree
Showing 17 changed files with 9,536 additions and 8,833 deletions.
57 changes: 55 additions & 2 deletions src/GrammarParser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ END 0 "end of file"
%type <FeatureDefinition::Ptr> FeatureDefinition
%type <Definition::Ptr> FeatureDeclarationOrDefinition
%type <Definitions::Ptr> FeatureDeclarationsAndDefinitions
%type <ImplementationDefinition::Ptr> ImplementationDefinition
%type <Definition::Ptr> ImplementationDefinitionDefinition
%type <Definitions::Ptr> ImplementationDefinitionDefinitions
%type <DeclarationDefinition::Ptr> DeclarationDefinition

// expressions
Expand Down Expand Up @@ -311,6 +314,10 @@ Definition
{
$$ = $1;
}
| ImplementationDefinition
{
$$ = $1;
}
| error // error recovery
{
$$ = nullptr;
Expand Down Expand Up @@ -587,10 +594,10 @@ FeatureDeclarationOrDefinition


FeatureDeclarationsAndDefinitions
: FeatureDeclarationsAndDefinitions COMMA FeatureDeclarationOrDefinition
: FeatureDeclarationsAndDefinitions FeatureDeclarationOrDefinition
{
auto definitions = $1;
definitions->add( $3 );
definitions->add( $2 );
$$ = definitions;
}
| FeatureDeclarationOrDefinition
Expand All @@ -602,6 +609,48 @@ FeatureDeclarationsAndDefinitions
;


ImplementationDefinition
: IMPLEMENTS IdentifierPath FOR Type EQUAL LCURPAREN ImplementationDefinitionDefinitions RCURPAREN
{
$$ = Ast::make< ImplementationDefinition >( @$, $2, $4, $7 );
}
| IMPLEMENTS Type EQUAL LCURPAREN ImplementationDefinitionDefinitions RCURPAREN
{
const auto name = Ast::make< Identifier >( @$, "" );
const auto path = asIdentifierPath( name );
$$ = Ast::make< ImplementationDefinition >( @$, path, $2, $5 );
}
;


ImplementationDefinitionDefinition

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

DefinitionDefinition :O

-> ImplementationMember? ;)

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

Please also consider that rules and deriveds can be attributed.

: DerivedDefinition
{
$$ = $1;
}
| RuleDefinition
{
$$ = $1;
}
;


ImplementationDefinitionDefinitions
: ImplementationDefinitionDefinitions ImplementationDefinitionDefinition
{
auto definitions = $1;
definitions->add( $2 );
$$ = definitions;
}
| ImplementationDefinitionDefinition
{
auto definitions = Ast::make< Definitions >( @$ );
definitions->add( $1 );
$$ = definitions;
}
;


DeclarationDefinition
: DERIVED Identifier COLON MaybeFunctionParameters MAPS Type
{
Expand Down Expand Up @@ -671,6 +720,10 @@ IdentifierPath
{
$$ = Ast::make< IdentifierPath >( @$, $2, IdentifierPath::Type::RELATIVE );
}
| THIS DOT DotSeparatedIdentifiers

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

🥇

This comment has been minimized.

Copy link
@ppaulweber

ppaulweber Jul 31, 2017

Author Member

😆 🤣 🚀

{
$$ = Ast::make< IdentifierPath >( @$, $3, IdentifierPath::Type::THIS );
}
;


Expand Down
3 changes: 3 additions & 0 deletions src/GrammarToken.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ DEFINED "defined" { return Parser::make_DEFINED(loc); }

STRUCTURE "structure" { return Parser::make_STRUCTURE(loc); }
FEATURE "feature" { return Parser::make_FEATURE(loc); }
IMPLEMENTS "implements" { return Parser::make_IMPLEMENTS(loc); }
FOR "for" { return Parser::make_FOR(loc); }
THIS "this" { return Parser::make_THIS(loc); }

SEQ "seq" { return Parser::make_SEQ(loc); }
ENDSEQ "endseq" { return Parser::make_ENDSEQ(loc); }
Expand Down
36 changes: 36 additions & 0 deletions src/ast/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,42 @@ void FeatureDefinition::accept( Visitor& visitor )
visitor.visit( *this );
}

//
//
// ImplementationDefinition
//

ImplementationDefinition::ImplementationDefinition(
const IdentifierPath::Ptr& path,
const Type::Ptr& type,
const Definitions::Ptr& definitions )
: Definition( Node::ID::IMPLEMENTATION_DEFINITION, *path->identifiers()->end() )
, m_path( path )
, m_type( type )
, m_definitions( definitions )
{
}

const IdentifierPath::Ptr& ImplementationDefinition::path( void ) const
{
return m_path;
}

const Type::Ptr& ImplementationDefinition::type( void ) const
{
return m_type;
}

const Definitions::Ptr& ImplementationDefinition::definitions( void ) const
{
return m_definitions;
}

void ImplementationDefinition::accept( Visitor& visitor )
{
visitor.visit( *this );
}

//
//
// DeclarationDefinition
Expand Down
23 changes: 23 additions & 0 deletions src/ast/Definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,29 @@ namespace libcasm_fe
const Definitions::Ptr m_definitions;
};

class ImplementationDefinition final : public Definition
{
public:
using Ptr = std::shared_ptr< ImplementationDefinition >;

ImplementationDefinition( const IdentifierPath::Ptr& path,
const Type::Ptr& type,
const Definitions::Ptr& definitions );

const IdentifierPath::Ptr& path( void ) const;

const Type::Ptr& type( void ) const;

const Definitions::Ptr& definitions( void ) const;

void accept( Visitor& visitor ) override final;

private:
const IdentifierPath::Ptr m_path;
const Type::Ptr m_type;
const Definitions::Ptr m_definitions;
};

class DeclarationDefinition final : public Definition
{
public:
Expand Down
4 changes: 4 additions & 0 deletions src/ast/EmptyVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ void EmptyVisitor::visit( FeatureDefinition& )
{
}

void EmptyVisitor::visit( ImplementationDefinition& )
{
}

void EmptyVisitor::visit( DeclarationDefinition& )
{
}
Expand Down
1 change: 1 addition & 0 deletions src/ast/EmptyVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace libcasm_fe
void visit( EnumerationDefinition& node ) override;
void visit( StructureDefinition& node ) override;
void visit( FeatureDefinition& node ) override;
void visit( ImplementationDefinition& node ) override;
void visit( DeclarationDefinition& node ) override;

void visit( ValueAtom& node ) override;
Expand Down
4 changes: 4 additions & 0 deletions src/ast/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ std::string Node::description( void ) const
{
return "feature";
}
case ID::IMPLEMENTATION_DEFINITION:
{
return "implementation";
}
case ID::DECLARATION_DEFINITION:
{
return "declaration";
Expand Down
3 changes: 3 additions & 0 deletions src/ast/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace libcasm_fe
ENUMERATION_DEFINITION,
STRUCTURE_DEFINITION,
FEATURE_DEFINITION,
IMPLEMENTATION_DEFINITION,
DECLARATION_DEFINITION,

// expressions
Expand Down Expand Up @@ -209,6 +210,8 @@ namespace libcasm_fe
{
ABSOLUTE, /**< absolute namespace + identifier path */
RELATIVE, /**< path started with a dot, needs to be resolved */
THIS, /**< path started with a 'this' and dot, needs to be
resolved */
};

public:
Expand Down
7 changes: 7 additions & 0 deletions src/ast/RecursiveVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ void RecursiveVisitor::visit( FeatureDefinition& node )
node.definitions()->accept( *this );
}

void RecursiveVisitor::visit( ImplementationDefinition& node )
{
node.identifier()->accept( *this );
node.type()->accept( *this );
node.definitions()->accept( *this );
}

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

attribute visitation is missing


void RecursiveVisitor::visit( DeclarationDefinition& node )
{
node.identifier()->accept( *this );
Expand Down
1 change: 1 addition & 0 deletions src/ast/RecursiveVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace libcasm_fe
void visit( EnumerationDefinition& node ) override;
void visit( StructureDefinition& node ) override;
void visit( FeatureDefinition& node ) override;
void visit( ImplementationDefinition& node ) override;
void visit( DeclarationDefinition& node ) override;

void visit( ValueAtom& node ) override;
Expand Down
2 changes: 2 additions & 0 deletions src/ast/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace libcasm_fe
class EnumerationDefinition;
class StructureDefinition;
class FeatureDefinition;
class ImplementationDefinition;
class DeclarationDefinition;

class ValueAtom;
Expand Down Expand Up @@ -99,6 +100,7 @@ namespace libcasm_fe
virtual void visit( EnumerationDefinition& node ) = 0;
virtual void visit( StructureDefinition& node ) = 0;
virtual void visit( FeatureDefinition& node ) = 0;
virtual void visit( ImplementationDefinition& node ) = 0;
virtual void visit( DeclarationDefinition& node ) = 0;

virtual void visit( ValueAtom& node ) = 0;
Expand Down
19 changes: 19 additions & 0 deletions src/transform/AstDumpSourcePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class AstDumpSourceVisitor final : public Visitor
void visit( EnumerationDefinition& node ) override;
void visit( StructureDefinition& node ) override;
void visit( FeatureDefinition& node ) override;
void visit( ImplementationDefinition& node ) override;
void visit( DeclarationDefinition& node ) override;

void visit( ValueAtom& node ) override;
Expand Down Expand Up @@ -352,6 +353,24 @@ void AstDumpSourceVisitor::visit( FeatureDefinition& node )
m_stream << "}";
}

void AstDumpSourceVisitor::visit( ImplementationDefinition& node )
{
m_stream << "implements ";
if( node.identifier()->name() != "" )
{
node.identifier()->accept( *this );
m_stream << " for ";
}
node.type()->accept( *this );
m_stream << " =\n{";
for( auto& e : *node.definitions() )
{
m_stream << "\n ";
e->accept( *this );
}
m_stream << "}";
}

void AstDumpSourceVisitor::visit( DeclarationDefinition& node )
{
m_stream << " ";
Expand Down
16 changes: 15 additions & 1 deletion src/various/Grammar.org
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Definition
| EnumerationDefinition
| StructureDefinition
| FeatureDefinition
| ImplementationDefinition
| error // error recovery

AttributedDefinition
Expand Down Expand Up @@ -78,9 +79,21 @@ FeatureDeclarationOrDefinition
| RuleDefinition

FeatureDeclarationsAndDefinitions
: FeatureDeclarationsAndDefinitions COMMA FeatureDeclarationOrDefinition
: FeatureDeclarationsAndDefinitions FeatureDeclarationOrDefinition
| FeatureDeclarationOrDefinition

ImplementationDefinition
: IMPLEMENTS IdentifierPath FOR Type EQUAL LCURPAREN ImplementationDefinitionDefinitions RCURPAREN
| IMPLEMENTS Type EQUAL LCURPAREN ImplementationDefinitionDefinitions RCURPAREN

ImplementationDefinitionDefinition
: DerivedDefinition
| RuleDefinition

ImplementationDefinitionDefinitions
: ImplementationDefinitionDefinitions ImplementationDefinitionDefinition
| ImplementationDefinitionDefinition

DeclarationDefinition
: DERIVED Identifier COLON MaybeFunctionParameters MAPS Type
| RULE Identifier COLON MaybeFunctionParameters MAPS Type
Expand All @@ -100,6 +113,7 @@ DotSeparatedIdentifiers
IdentifierPath
: DotSeparatedIdentifiers %prec ABSOLUTE_PATH
| DOT DotSeparatedIdentifiers
| THIS DOT DotSeparatedIdentifiers

Variable
: Identifier COLON Type
Expand Down
Loading

2 comments on commit 452957a

@emmanuel099
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AstDumpDotVisitor implementation is missing

@ppaulweber
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed all issues and comments in 452957a...cc64255

Please sign in to comment.