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 Jun 10, 2021
1 parent 8940f03 commit 86010cf
Show file tree
Hide file tree
Showing 19 changed files with 34,436 additions and 31,864 deletions.
59 changes: 55 additions & 4 deletions src/GrammarParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ END 0 "end of file"
%type <VariableDefinition::Ptr> Variable TypedVariable AttributedVariable TypedAttributedVariable
%type <VariableDefinitions::Ptr> TypedVariables
%type <FunctionDefinition::Ptr> FunctionDefinition
%type <FunctionDefinitions::Ptr> FunctionDefinitions
%type <DerivedDefinition::Ptr> DerivedDefinition
%type <RuleDefinition::Ptr> RuleDefinition
%type <EnumeratorDefinition::Ptr> EnumeratorDefinition
Expand All @@ -148,6 +147,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

// literals
Expand Down Expand Up @@ -369,6 +371,14 @@ Definition
{
$$ = $1;
}
| FeatureDefinition
{
$$ = $1;
}
| ImplementationDefinition
{
$$ = $1;
}
;


Expand Down Expand Up @@ -572,11 +582,10 @@ FeatureDeclarationOrDefinition


FeatureDeclarationsAndDefinitions
: FeatureDeclarationsAndDefinitions COMMA FeatureDeclarationOrDefinition
: FeatureDeclarationsAndDefinitions FeatureDeclarationOrDefinition
{
// TODO: FIXME: @ppaulweber: handle AST keyword tokens $2
auto definitions = $1;
definitions->add( $3 );
definitions->add( $2 );
$$ = definitions;
}
| FeatureDeclarationOrDefinition
Expand All @@ -588,6 +597,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
: 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
3 changes: 3 additions & 0 deletions src/GrammarToken.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ INVARIANT "invariant"
IMPORT "import"
STRUCTURE "structure"
FEATURE "feature"
IMPLEMENTS "implements"
FOR "for"
THIS "this"

FUNCTION "function"
DEFINED "defined"
Expand Down
34 changes: 34 additions & 0 deletions src/ast/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,40 @@ 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
24 changes: 24 additions & 0 deletions src/ast/Definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,30 @@ 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 @@ -116,6 +116,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 @@ -71,6 +71,7 @@ namespace libcasm_fe
void visit( ImportDefinition& node ) override;
void visit( StructureDefinition& node ) override;
void visit( FeatureDefinition& node ) override;
void visit( ImplementationDefinition& node ) override;
void visit( DeclarationDefinition& node ) override;

void visit( UndefLiteral& 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 @@ -135,6 +135,10 @@ std::string Node::description( void ) const
{
return "feature";
}
case ID::IMPLEMENTATION_DEFINITION:
{
return "implementation";
}
case ID::DECLARATION_DEFINITION:
{
return "declaration";
Expand Down
1 change: 1 addition & 0 deletions src/ast/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace libcasm_fe
IMPORT_DEFINITION,
STRUCTURE_DEFINITION,
FEATURE_DEFINITION,
IMPLEMENTATION_DEFINITION,
DECLARATION_DEFINITION,

// literals
Expand Down
7 changes: 7 additions & 0 deletions src/ast/RecursiveVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,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 );
}

void RecursiveVisitor::visit( DeclarationDefinition& node )
{
// TODO: FIXME: @ppaulweber: call token methods to accept this visitor
Expand Down
1 change: 1 addition & 0 deletions src/ast/RecursiveVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace libcasm_fe
void visit( ImportDefinition& node ) override;
void visit( StructureDefinition& node ) override;
void visit( FeatureDefinition& node ) override;
void visit( ImplementationDefinition& node ) override;
void visit( DeclarationDefinition& node ) override;

void visit( UndefLiteral& 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 @@ -66,6 +66,7 @@ namespace libcasm_fe
class ImportDefinition;
class StructureDefinition;
class FeatureDefinition;
class ImplementationDefinition;
class DeclarationDefinition;

class UndefLiteral;
Expand Down Expand Up @@ -151,6 +152,7 @@ namespace libcasm_fe
virtual void visit( ImportDefinition& 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( UndefLiteral& node ) = 0;
Expand Down
72 changes: 72 additions & 0 deletions src/various/Grammar.org
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Definition ::= InitDefinition
| InvariantDefinition
| ImportDefinition
| StructureDefinition
| FeatureDefinition
| ImplementationDefinition
#+end_src

#+html: {{page>.:grammar:Definition&noheader&nofooter}}
Expand Down Expand Up @@ -179,6 +181,76 @@ StructureDefinition ::= "structure" Identifier "=" "{" FunctionDefinition "}"
#+html: {{page>.:grammar:StructureDefinition&noheader&nofooter}}


** FeatureDefinition

#+begin_src
FeatureDefinition ::= "feature" Identifier "=" "{" FeatureDeclarationsAndDefinitions "}"
#+end_src

#+html: {{page>.:grammar:FeatureDefinition&noheader&nofooter}}


** FeatureDeclarationOrDefinition

#+begin_src
FeatureDeclarationOrDefinition ::= DeclarationDefinition
| DerivedDefinition
| RuleDefinition
#+end_src

#+html: {{page>.:grammar:FeatureDeclarationOrDefinition&noheader&nofooter}}


** FeatureDeclarationsAndDefinitions

#+begin_src
FeatureDeclarationsAndDefinitions ::= FeatureDeclarationsAndDefinitions FeatureDeclarationOrDefinition
| FeatureDeclarationOrDefinition
#+end_src

#+html: {{page>.:grammar:FeatureDeclarationsAndDefinitions&noheader&nofooter}}


** ImplementationDefinition

#+begin_src
ImplementationDefinition ::= "implements" IdentifierPath "for" Type "=" "{" ImplementationDefinitionDefinitions "}"
| "implements" Type "=" "{" ImplementationDefinitionDefinitions "}"
#+end_src

#+html: {{page>.:grammar:ImplementationDefinition&noheader&nofooter}}


** ImplementationDefinitionDefinition

#+begin_src
ImplementationDefinitionDefinition ::= DerivedDefinition
| RuleDefinition
#+end_src

#+html: {{page>.:grammar:ImplementationDefinitionDefinition&noheader&nofooter}}


** ImplementationDefinitionDefinitions

#+begin_src
ImplementationDefinitionDefinitions ::= ImplementationDefinitionDefinitions ImplementationDefinitionDefinition
| ImplementationDefinitionDefinition
#+end_src

#+html: {{page>.:grammar:ImplementationDefinitionDefinitions&noheader&nofooter}}


** DeclarationDefinition

#+begin_src
DeclarationDefinition ::= "derived" Identifier ":" MaybeFunctionParameters "->" Type
| "rule" Identifier ":" MaybeFunctionParameters "->" Type
#+end_src

#+html: {{page>.:grammar:DeclarationDefinition&noheader&nofooter}}


** Rules

#+begin_src
Expand Down
Loading

0 comments on commit 86010cf

Please sign in to comment.