Skip to content

Commit

Permalink
Grammar: add support for type aliasing
Browse files Browse the repository at this point in the history
The rule is: TYPE Identifier = Type

Refs sealangdotorg/sea#16
  • Loading branch information
emmanuel099 committed Jul 17, 2017
1 parent d798226 commit 5e51e1b
Show file tree
Hide file tree
Showing 19 changed files with 8,649 additions and 8,416 deletions.
13 changes: 13 additions & 0 deletions src/GrammarParser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ END 0 "end of file"
%type <DerivedDefinition::Ptr> DerivedDefinition
%type <RuleDefinition::Ptr> RuleDefinition
%type <EnumerationDefinition::Ptr> EnumerationDefinition
%type <TypeDefinition::Ptr> TypeDefinition

// expressions
%type <Expression::Ptr> Expression Term Atom
Expand Down Expand Up @@ -297,6 +298,10 @@ Definition
{
$$ = $1;
}
| TypeDefinition
{
$$ = $1;
}
| error // error recovery
{
$$ = nullptr;
Expand Down Expand Up @@ -524,6 +529,14 @@ EnumerationDefinition
;


TypeDefinition
: TYPE Identifier EQUAL Type

This comment has been minimized.

Copy link
@ppaulweber

ppaulweber Jul 17, 2017

Member

finally 😃

{
$$ = Ast::make< TypeDefinition >( @$, $2, $4 );
}
;


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 @@ -30,6 +30,7 @@ INIT "init" { return Parser::make_INIT(loc); }
DERIVED "derived" { return Parser::make_DERIVED(loc); }
ENUM "enum" { return Parser::make_ENUM(loc); }
RULE "rule" { return Parser::make_RULE(loc); }
TYPE "type" { return Parser::make_TYPE(loc); }

FUNCTION "function" { return Parser::make_FUNCTION(loc); }
INITIALLY "initially" { return Parser::make_INITIALLY(loc); }
Expand Down
17 changes: 17 additions & 0 deletions src/ast/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,20 @@ void EnumerationDefinition::accept( Visitor& visitor )
{
visitor.visit( *this );
}

TypeDefinition::TypeDefinition(
const Identifier::Ptr& identifier, const Type::Ptr& type )
: Definition( Node::ID::TYPE_DEFINITION, identifier )
, m_type( type )
{
}

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

void TypeDefinition::accept( Visitor& visitor )
{
visitor.visit( *this );
}
16 changes: 16 additions & 0 deletions src/ast/Definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ namespace libcasm_fe
private:
const Identifiers::Ptr m_enumerators;
};

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

TypeDefinition(
const Identifier::Ptr& identifier, const Type::Ptr& type );

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

void accept( Visitor& visitor ) override final;

private:
const Type::Ptr m_type;
};
}
}

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

void EmptyVisitor::visit( TypeDefinition& )
{
}

void EmptyVisitor::visit( ValueAtom& )
{
}
Expand Down
1 change: 1 addition & 0 deletions src/ast/EmptyVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace libcasm_fe
void visit( DerivedDefinition& node ) override;
void visit( RuleDefinition& node ) override;
void visit( EnumerationDefinition& node ) override;
void visit( TypeDefinition& node ) override;

void visit( ValueAtom& node ) override;
void visit( ReferenceAtom& 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 @@ -77,6 +77,10 @@ std::string Node::description( void ) const
{
return "enumeration";
}
case ID::TYPE_DEFINITION:
{
return "type";
}
case ID::VALUE_ATOM:
{
return "value";
Expand Down
1 change: 1 addition & 0 deletions src/ast/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace libcasm_fe
DERIVED_DEFINITION,
RULE_DEFINITION,
ENUMERATION_DEFINITION,
TYPE_DEFINITION,

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

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

void RecursiveVisitor::visit( ValueAtom& node )
{
}
Expand Down
1 change: 1 addition & 0 deletions src/ast/RecursiveVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace libcasm_fe
void visit( DerivedDefinition& node ) override;
void visit( RuleDefinition& node ) override;
void visit( EnumerationDefinition& node ) override;
void visit( TypeDefinition& node ) override;

void visit( ValueAtom& node ) override;
void visit( ReferenceAtom& 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 @@ -37,6 +37,7 @@ namespace libcasm_fe
class DerivedDefinition;
class RuleDefinition;
class EnumerationDefinition;
class TypeDefinition;

class ValueAtom;
class ReferenceAtom;
Expand Down Expand Up @@ -94,6 +95,7 @@ namespace libcasm_fe
virtual void visit( DerivedDefinition& node ) = 0;
virtual void visit( RuleDefinition& node ) = 0;
virtual void visit( EnumerationDefinition& node ) = 0;
virtual void visit( TypeDefinition& node ) = 0;

virtual void visit( ValueAtom& node ) = 0;
virtual void visit( ReferenceAtom& node ) = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/transform/AstDumpDotPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class AstDumpDotVisitor final : public RecursiveVisitor
void visit( DerivedDefinition& node ) override;
void visit( RuleDefinition& node ) override;
void visit( EnumerationDefinition& node ) override;
void visit( TypeDefinition& node ) override;

void visit( ValueAtom& node ) override;
void visit( ReferenceAtom& node ) override;
Expand Down Expand Up @@ -201,6 +202,13 @@ void AstDumpDotVisitor::visit( EnumerationDefinition& node )
RecursiveVisitor::visit( node );
}

void AstDumpDotVisitor::visit( TypeDefinition& node )
{
DotLink link( this, &node );
dumpNode( node, "TypeDefinition" );
RecursiveVisitor::visit( node );
}

void AstDumpDotVisitor::visit( ValueAtom& node )
{
DotLink link( this, &node );
Expand Down
9 changes: 9 additions & 0 deletions src/transform/AstDumpSourcePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class AstDumpSourceVisitor final : public Visitor
void visit( DerivedDefinition& node ) override;
void visit( RuleDefinition& node ) override;
void visit( EnumerationDefinition& node ) override;
void visit( TypeDefinition& node ) override;

void visit( ValueAtom& node ) override;
void visit( ReferenceAtom& node ) override;
Expand Down Expand Up @@ -323,6 +324,14 @@ void AstDumpSourceVisitor::visit( EnumerationDefinition& node )
m_stream << "}";
}

void AstDumpSourceVisitor::visit( TypeDefinition& node )
{
m_stream << "type ";
node.identifier()->accept( *this );
m_stream << " = ";
node.type()->accept( *this );
}

void AstDumpSourceVisitor::visit( ValueAtom& node )
{
m_stream << node.value()->name();
Expand Down
7 changes: 7 additions & 0 deletions src/transform/AstToCasmIRPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class AstToCasmIRVisitor final : public RecursiveVisitor
void visit( DerivedDefinition& node ) override;
void visit( RuleDefinition& node ) override;
void visit( EnumerationDefinition& node ) override;
void visit( TypeDefinition& node ) override;

void visit( ValueAtom& node ) override;
void visit( ReferenceAtom& node ) override;
Expand Down Expand Up @@ -262,6 +263,12 @@ void AstToCasmIRVisitor::visit( EnumerationDefinition& node )
"%s:%i: TODO %s", __FILE__, __LINE__, node.description().c_str() );
}

void AstToCasmIRVisitor::visit( TypeDefinition& node )
{
m_log.info(
"%s:%i: TODO %s", __FILE__, __LINE__, node.description().c_str() );
}

void AstToCasmIRVisitor::visit( ValueAtom& node )
{
assert( node.type() );
Expand Down
4 changes: 4 additions & 0 deletions src/various/Grammar.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Definition
| DerivedDefinition
| RuleDefinition
| EnumerationDefinition
| TypeDefinition
| error // error recovery

AttributedDefinition
Expand Down Expand Up @@ -60,6 +61,9 @@ DerivedDefinition
EnumerationDefinition
: ENUM Identifier EQUAL LCURPAREN Identifiers RCURPAREN

TypeDefinition
: TYPE Identifier EQUAL Type

Identifier
: IDENTIFIER
| IN // allow in keyword as identifier
Expand Down
Loading

0 comments on commit 5e51e1b

Please sign in to comment.