Skip to content

Commit

Permalink
Feature Definition and Declarations
Browse files Browse the repository at this point in the history
* added new AST feature definition node and the new syntax to define a
  named feature
  - related to ref #35
* added new AST declaration definition node (needed by feature etc.)
  • Loading branch information
ppaulweber committed Jul 29, 2017
1 parent 6997e6d commit fce06e4
Show file tree
Hide file tree
Showing 17 changed files with 9,353 additions and 8,562 deletions.
64 changes: 64 additions & 0 deletions src/GrammarParser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ END 0 "end of file"
%type <RuleDefinition::Ptr> RuleDefinition
%type <EnumerationDefinition::Ptr> EnumerationDefinition
%type <StructureDefinition::Ptr> StructureDefinition
%type <FeatureDefinition::Ptr> FeatureDefinition
%type <Definition::Ptr> FeatureDeclarationOrDefinition
%type <Definitions::Ptr> FeatureDeclarationsAndDefinitions
%type <DeclarationDefinition::Ptr> DeclarationDefinition

// expressions
%type <Expression::Ptr> Expression Term Atom
Expand Down Expand Up @@ -303,6 +307,10 @@ Definition
{
$$ = $1;
}
| FeatureDefinition
{
$$ = $1;
}
| error // error recovery
{
$$ = nullptr;
Expand Down Expand Up @@ -554,6 +562,62 @@ StructureDefinition
;


FeatureDefinition
: FEATURE Identifier EQUAL LCURPAREN FeatureDeclarationsAndDefinitions RCURPAREN
{
$$ = Ast::make< FeatureDefinition >( @$, $2, $5 );
}
;


FeatureDeclarationOrDefinition

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

It should also be possible to add attributes to them. (see grammar rule AttributedDefinition)

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


FeatureDeclarationsAndDefinitions
: FeatureDeclarationsAndDefinitions COMMA FeatureDeclarationOrDefinition

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

I think the COMMA should be removed (btw. the AstDumpSourceVisitor dumps it without commas)

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

Ah already fixed in 452957a :)

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


DeclarationDefinition

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

Please rename it to Declaration (only in the grammar) because it sounds a little bit strange ;)

: DERIVED Identifier COLON MaybeFunctionParameters MAPS Type
{
auto declaration = Ast::make< DeclarationDefinition >( @$, $2, $4, $6 );
declaration->setKind( DeclarationDefinition::Kind::DERIVED );
$$ = declaration;
}
| RULE Identifier COLON MaybeFunctionParameters MAPS Type
{
auto declaration = Ast::make< DeclarationDefinition >( @$, $2, $4, $6 );
declaration->setKind( DeclarationDefinition::Kind::RULE );
$$ = declaration;
}
;


Identifier
: IDENTIFIER
{
Expand Down
1 change: 1 addition & 0 deletions src/GrammarToken.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ INITIALLY "initially" { return Parser::make_INITIALLY(loc); }
DEFINED "defined" { return Parser::make_DEFINED(loc); }

STRUCTURE "structure" { return Parser::make_STRUCTURE(loc); }
FEATURE "feature" { return Parser::make_FEATURE(loc); }

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

//
//
// FeatureDefinition
//

FeatureDefinition::FeatureDefinition(
const Identifier::Ptr& identifier, const Definitions::Ptr& definitions )
: Definition( Node::ID::FEATURE_DEFINITION, identifier )
, m_definitions( definitions )
{
}

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

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

//
//
// DeclarationDefinition
//

DeclarationDefinition::DeclarationDefinition( const Identifier::Ptr& identifier,
const Types::Ptr& argumentTypes,
const Type::Ptr& returnType )
: Definition( Node::ID::DECLARATION_DEFINITION, identifier )
, m_argumentTypes( argumentTypes )
, m_returnType( returnType )
{
}

const Types::Ptr& DeclarationDefinition::argumentTypes( void ) const
{
return m_argumentTypes;
}

const Type::Ptr& DeclarationDefinition::returnType( void ) const
{
return m_returnType;
}

void DeclarationDefinition::setKind( const Kind kind )
{
m_kind = kind;
}

DeclarationDefinition::Kind DeclarationDefinition::kind( void ) const
{
return m_kind;
}

std::string DeclarationDefinition::kindName( void ) const
{
switch( kind() )
{
case DeclarationDefinition::Kind::DERIVED:
{
return "derived";
}
case DeclarationDefinition::Kind::RULE:
{
return "rule";
}
}

assert( !" internal error! " );
return std::string();
}

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

//
// Local variables:
// mode: c++
Expand Down
51 changes: 51 additions & 0 deletions src/ast/Definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,57 @@ namespace libcasm_fe
private:
const NodeList< FunctionDefinition >::Ptr m_functions;
};

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

FeatureDefinition( const Identifier::Ptr& identifier,
const Definitions::Ptr& definitions );

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

void accept( Visitor& visitor ) override final;

private:
const Definitions::Ptr m_definitions;
};

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

enum class Kind
{
DERIVED = 1,

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

Please remove the misleading = 1. (I think you have added it to catch the no-kind-set case, but please note that it's not necessarily true that an uninitialized m_kind is equal to 0)

RULE
};

DeclarationDefinition( const Identifier::Ptr& identifier,
const Types::Ptr& argumentTypes,
const Type::Ptr& returnType );

const Types::Ptr& argumentTypes( void ) const;

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

void setKind( const Kind kind );

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

Make this a ctor argument. Otherwise you may end up with an uninitialized m_kind, because m_kind as no default value.

(Please don't forget to make m_kind const after this change ;)


Kind kind( void ) const;

std::string kindName( void ) const;

void accept( Visitor& visitor ) override final;

private:
const Types::Ptr m_argumentTypes;
const Type::Ptr m_returnType;
Kind m_kind;
};

using DeclarationDefinitions = NodeList< DeclarationDefinition >;
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/ast/EmptyVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ void EmptyVisitor::visit( StructureDefinition& )
{
}

void EmptyVisitor::visit( FeatureDefinition& )
{
}

void EmptyVisitor::visit( DeclarationDefinition& )
{
}

void EmptyVisitor::visit( ValueAtom& )
{
}
Expand Down
2 changes: 2 additions & 0 deletions src/ast/EmptyVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace libcasm_fe
void visit( RuleDefinition& node ) override;
void visit( EnumerationDefinition& node ) override;
void visit( StructureDefinition& node ) override;
void visit( FeatureDefinition& node ) override;
void visit( DeclarationDefinition& node ) override;

void visit( ValueAtom& node ) override;
void visit( ReferenceAtom& node ) override;
Expand Down
8 changes: 8 additions & 0 deletions src/ast/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ std::string Node::description( void ) const
{
return "structure";
}
case ID::FEATURE_DEFINITION:
{
return "feature";
}
case ID::DECLARATION_DEFINITION:
{
return "declaration";
}
case ID::VALUE_ATOM:
{
return "value";
Expand Down
2 changes: 2 additions & 0 deletions src/ast/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ namespace libcasm_fe
RULE_DEFINITION,
ENUMERATION_DEFINITION,
STRUCTURE_DEFINITION,
FEATURE_DEFINITION,
DECLARATION_DEFINITION,

// expressions
VALUE_ATOM,
Expand Down
13 changes: 13 additions & 0 deletions src/ast/RecursiveVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ void RecursiveVisitor::visit( StructureDefinition& node )
node.functions()->accept( *this );
}

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

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

node.attributes()->accept( *this ); is missing (every definition has attributes)


void RecursiveVisitor::visit( DeclarationDefinition& node )
{
node.identifier()->accept( *this );
node.argumentTypes()->accept( *this );
node.returnType()->accept( *this );
}

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

node.attributes()->accept( *this ); is missing (every definition has attributes)


void RecursiveVisitor::visit( ValueAtom& node )
{
}
Expand Down
2 changes: 2 additions & 0 deletions src/ast/RecursiveVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace libcasm_fe
void visit( RuleDefinition& node ) override;
void visit( EnumerationDefinition& node ) override;
void visit( StructureDefinition& node ) override;
void visit( FeatureDefinition& node ) override;
void visit( DeclarationDefinition& node ) override;

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

class ValueAtom;
class ReferenceAtom;
Expand Down Expand Up @@ -96,6 +98,8 @@ namespace libcasm_fe
virtual void visit( RuleDefinition& node ) = 0;
virtual void visit( EnumerationDefinition& node ) = 0;
virtual void visit( StructureDefinition& node ) = 0;
virtual void visit( FeatureDefinition& node ) = 0;
virtual void visit( DeclarationDefinition& node ) = 0;

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

void visit( ValueAtom& node ) override;
void visit( ReferenceAtom& node ) override;
Expand Down Expand Up @@ -337,6 +339,38 @@ void AstDumpSourceVisitor::visit( StructureDefinition& node )
m_stream << "}";
}

void AstDumpSourceVisitor::visit( FeatureDefinition& node )
{
m_stream << "feature ";
node.identifier()->accept( *this );
m_stream << " =\n{";
for( auto& e : *node.definitions() )
{
m_stream << "\n ";

This comment has been minimized.

Copy link
@emmanuel099

emmanuel099 Jul 30, 2017

Member

please make use of the Indentation feature

e->accept( *this );
}
m_stream << "}";
}

void AstDumpSourceVisitor::visit( DeclarationDefinition& node )
{
m_stream << " ";
node.identifier()->accept( *this );
m_stream << " : ";
bool firstArgType = true;
for( auto& t : *node.argumentTypes() )
{
if( not firstArgType )
{
m_stream << " * ";
}
t->accept( *this );
firstArgType = false;
}
m_stream << " -> ";
node.returnType()->accept( *this );
}

void AstDumpSourceVisitor::visit( ValueAtom& node )
{
m_stream << node.value()->name();
Expand Down
Loading

2 comments on commit fce06e4

@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.