Skip to content

Commit

Permalink
Merge branch 'feature/19_new_ast' into feature/22_symbol_resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ppaulweber committed Apr 19, 2017
2 parents a6fcd16 + f2e44c1 commit 418a260
Show file tree
Hide file tree
Showing 17 changed files with 7,678 additions and 3,645 deletions.
68 changes: 43 additions & 25 deletions src/GrammarParser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,19 @@
return Ast::make< FunctionDefinition >( sourceLocation, program, argTypes, ruleRefType );
}

static IdentifierPath::Ptr asIdentifierPath( const Identifier::Ptr& identifier )
static FunctionDefinition::Ptr createSelfFunction( Location& sourceLocation )
{
const auto& location = identifier->sourceLocation();
return Ast::make< IdentifierPath >( location, identifier );
const auto resType = createAgentType( sourceLocation );
const auto argTypes = Ast::make< Types >( sourceLocation );

const auto program = Ast::make< Identifier >( sourceLocation, "self" );
return Ast::make< FunctionDefinition >( sourceLocation, program, argTypes, resType );
}

static DirectCallExpression::Ptr createSelfBuiltinCall( Location& sourceLocation )
static IdentifierPath::Ptr asIdentifierPath( const Identifier::Ptr& identifier )
{
const auto self = libcasm_fe::Ast::make< Identifier >( sourceLocation, "self" );
const auto arguments = libcasm_fe::Ast::make< Expressions >( sourceLocation );
return libcasm_fe::Ast::make< DirectCallExpression >(
sourceLocation, asIdentifierPath( self ), arguments );
const auto& location = identifier->sourceLocation();
return Ast::make< IdentifierPath >( location, identifier );
}

static Rule::Ptr wrapInBlockRule( const Rule::Ptr& rule )
Expand Down Expand Up @@ -195,6 +196,7 @@ END 0 "end of file"
%type <CaseRule::Ptr> CaseRule
%type <LetRule::Ptr> LetRule
%type <ForallRule::Ptr> ForallRule
%type <ChooseRule::Ptr> ChooseRule
%type <IterateRule::Ptr> IterateRule
%type <BlockRule::Ptr> BlockRule
%type <SequenceRule::Ptr> SequenceRule
Expand Down Expand Up @@ -238,7 +240,7 @@ END 0 "end of file"
%precedence UPDATE

%left DOT
%left IMPLIES
%left IMPLIES ARROW
%left OR
%left XOR
%left AND
Expand Down Expand Up @@ -327,7 +329,7 @@ Definitions


FunctionDefinition
: FUNCTION Identifier COLON MaybeFunctionParameters ARROW Type MaybeDefined MaybeInitially
: FUNCTION Identifier COLON MaybeFunctionParameters MAPS Type MaybeDefined MaybeInitially
{
const auto identifier = $2;

Expand Down Expand Up @@ -405,16 +407,16 @@ MaybeFunctionParameters
ProgramFunctionDefinition
: INIT IdentifierPath
{
auto programDefinition = createProgramFunction( @$ );

auto arguments = libcasm_fe::Ast::make< Expressions >( @$ );

// single execution agent case, use 'self' built-in!
const auto self = createSelfBuiltinCall( @$ );
arguments->add( self );
auto selfDefinition = createSelfFunction( @$ );
auto selfArguments = libcasm_fe::Ast::make< Expressions >( @$ );
const auto self = libcasm_fe::Ast::make< DirectCallExpression >(
@$, asIdentifierPath( selfDefinition->identifier() ), selfArguments );

auto programDefinition = createProgramFunction( @$ );
auto programArguments = libcasm_fe::Ast::make< Expressions >( @$ );
programArguments->add( self );
const auto program = libcasm_fe::Ast::make< DirectCallExpression >(
@$, asIdentifierPath( programDefinition->identifier() ), arguments );
@$, asIdentifierPath( programDefinition->identifier() ), programArguments );

const auto ruleReference = Ast::make< RuleReferenceAtom >( @$, $2 );

Expand Down Expand Up @@ -449,7 +451,7 @@ Initializer
const auto function = Ast::make< DirectCallExpression >( @$, nullptr, arguments );
$$ = Ast::make< UpdateRule >( @$, function, $1 );
}
| Term ARROW Term
| Term MAPS Term
{
auto arguments = Ast::make< Expressions >( @$ );
arguments->add( $1 );
Expand All @@ -458,7 +460,7 @@ Initializer
const auto function = Ast::make< DirectCallExpression >( @$, nullptr, arguments );
$$ = Ast::make< UpdateRule >( @$, function, $3 );
}
| TwoOrMoreArguments ARROW Term // the rule above can be (arg)->... so force >=2 args here to avoid a shift/reduce conflict
| TwoOrMoreArguments MAPS Term // the rule above can be (arg)->... so force >=2 args here to avoid a shift/reduce conflict
{
// the unknown function identifier will be replaced in FunctionDefinition
const auto function = Ast::make< DirectCallExpression >( @$, nullptr, $1 );
Expand Down Expand Up @@ -496,7 +498,7 @@ MaybeInitializers


DerivedDefinition
: DERIVED Identifier MaybeParameters ARROW Type EQUAL Term
: DERIVED Identifier MaybeParameters MAPS Type EQUAL Term
{
$$ = Ast::make< DerivedDefinition >( @$, $2, $3, $5, $7 );
}
Expand Down Expand Up @@ -641,7 +643,7 @@ ComposedType


RelationType
: IdentifierPath LESSER MaybeFunctionParameters ARROW Type GREATER
: IdentifierPath LESSER MaybeFunctionParameters MAPS Type GREATER
{
$$ = Ast::make< RelationType >( @$, $1, $3, $5 );
}
Expand Down Expand Up @@ -905,7 +907,7 @@ Expression
}
| Term CARET Term
{
// TODO call power builtin
$$ = Ast::make< BinaryExpression >( @$, $1, $3, libcasm_ir::Value::POW_INSTRUCTION );
}
| Term NEQUAL Term
{
Expand Down Expand Up @@ -943,9 +945,13 @@ Expression
{
$$ = Ast::make< BinaryExpression >( @$, $1, $3, libcasm_ir::Value::AND_INSTRUCTION );
}
| Term ARROW Term
{
$$ = Ast::make< BinaryExpression >( @$, $1, $3, libcasm_ir::Value::IMP_INSTRUCTION );
}
| Term IMPLIES Term
{
// TODO add implies instruction
$$ = Ast::make< BinaryExpression >( @$, $1, $3, libcasm_ir::Value::IMP_INSTRUCTION );
}
| NOT Term
{
Expand Down Expand Up @@ -1065,7 +1071,7 @@ RuleDefinition
$$ = Ast::make< RuleDefinition >( @$, $2, $3, createVoidType( @$ ),
wrapInBlockRule( $5 ) );
}
| RULE Identifier MaybeParameters ARROW Type EQUAL Rule
| RULE Identifier MaybeParameters MAPS Type EQUAL Rule
{
$$ = Ast::make< RuleDefinition >( @$, $2, $3, $5,
wrapInBlockRule( $7 ) );
Expand Down Expand Up @@ -1094,6 +1100,10 @@ Rule
{
$$ = $1;
}
| ChooseRule
{
$$ = $1;
}
| IterateRule
{
$$ = $1;
Expand Down Expand Up @@ -1209,6 +1219,14 @@ ForallRule
;


ChooseRule
: CHOOSE Variable IN Term DO Rule
{
$$ = Ast::make< ChooseRule >( @$, $2, $4, $6 );
}
;


IterateRule
: ITERATE Rule
{
Expand Down
4 changes: 3 additions & 1 deletion src/GrammarToken.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ SKIP "skip" { return Parser::make_SKIP(loc); }
LET "let" { return Parser::make_LET(loc); }
IN "in" { return Parser::make_IN(loc); }
FORALL "forall" { return Parser::make_FORALL(loc); }
CHOOSE "choose" { return Parser::make_CHOOSE(loc); }
ITERATE "iterate" { return Parser::make_ITERATE(loc); }
DO "do" { return Parser::make_DO(loc); }
CALL "call" { return Parser::make_CALL(loc); }
Expand Down Expand Up @@ -90,7 +91,8 @@ MARK "'" { return Parser::make_MARK(loc); }

DOTDOT ".." { return Parser::make_DOTDOT(loc); }
DOT "." { return Parser::make_DOT(loc); }
ARROW "->" { return Parser::make_ARROW(loc); }
MAPS "->" { return Parser::make_MAPS(loc); }
ARROW "=>" { return Parser::make_ARROW(loc); }
UPDATE ":=" { return Parser::make_UPDATE(loc); }
NEQUAL "!=" { return Parser::make_NEQUAL(loc); }
LESSEQ "<=" { return Parser::make_LESSEQ(loc); }
Expand Down
4 changes: 4 additions & 0 deletions src/ast/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ std::string Node::description( void ) const
{
return "forall";
}
case ID::CHOOSE_RULE:
{
return "choose";
}
case ID::ITERATE_RULE:
{
return "iterate";
Expand Down
1 change: 1 addition & 0 deletions src/ast/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace libcasm_fe
CASE_RULE,
LET_RULE,
FORALL_RULE,
CHOOSE_RULE,
ITERATE_RULE,
BLOCK_RULE,
SEQUENCE_RULE,
Expand Down
7 changes: 7 additions & 0 deletions src/ast/RecursiveVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ void RecursiveVisitor::visit( ForallRule& node )
node.rule()->accept( *this );
}

void RecursiveVisitor::visit( ChooseRule& node )
{
node.variable()->accept( *this );
node.universe()->accept( *this );
node.rule()->accept( *this );
}

void RecursiveVisitor::visit( IterateRule& node )
{
node.rule()->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 @@ -61,6 +61,7 @@ namespace libcasm_fe
void visit( CaseRule& node ) override;
void visit( LetRule& node ) override;
void visit( ForallRule& node ) override;
void visit( ChooseRule& node ) override;
void visit( IterateRule& node ) override;
void visit( BlockRule& node ) override;
void visit( SequenceRule& node ) override;
Expand Down
29 changes: 29 additions & 0 deletions src/ast/Rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,35 @@ void ForallRule::accept( Visitor& visitor )
visitor.visit( *this );
}

ChooseRule::ChooseRule( const VariableDefinition::Ptr& variable,
const Expression::Ptr& universe, const Rule::Ptr& rule )
: Rule( Node::ID::CHOOSE_RULE )
, m_variable( variable )
, m_universe( universe )
, m_rule( rule )
{
}

VariableDefinition::Ptr ChooseRule::variable( void ) const
{
return m_variable;
}

Expression::Ptr ChooseRule::universe( void ) const
{
return m_universe;
}

Rule::Ptr ChooseRule::rule( void ) const
{
return m_rule;
}

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

IterateRule::IterateRule( const Rule::Ptr& rule )
: Rule( Node::ID::ITERATE_RULE )
, m_rule( rule )
Expand Down
20 changes: 20 additions & 0 deletions src/ast/Rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ namespace libcasm_fe
Rule::Ptr m_rule;
};

class ChooseRule : public Rule
{
public:
using Ptr = std::shared_ptr< ChooseRule >;

ChooseRule( const std::shared_ptr< VariableDefinition >& variable,
const Expression::Ptr& universe, const Rule::Ptr& rule );

std::shared_ptr< VariableDefinition > variable( void ) const;
Expression::Ptr universe( void ) const;
Rule::Ptr rule( void ) const;

void accept( Visitor& visitor ) override final;

private:
std::shared_ptr< VariableDefinition > m_variable;
Expression::Ptr m_universe;
Rule::Ptr m_rule;
};

class IterateRule : public Rule
{
public:
Expand Down
2 changes: 2 additions & 0 deletions src/ast/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace libcasm_fe
class CaseRule;
class LetRule;
class ForallRule;
class ChooseRule;
class IterateRule;
class BlockRule;
class SequenceRule;
Expand Down Expand Up @@ -110,6 +111,7 @@ namespace libcasm_fe
virtual void visit( CaseRule& node ) = 0;
virtual void visit( LetRule& node ) = 0;
virtual void visit( ForallRule& node ) = 0;
virtual void visit( ChooseRule& node ) = 0;
virtual void visit( IterateRule& node ) = 0;
virtual void visit( BlockRule& node ) = 0;
virtual void visit( SequenceRule& 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 @@ -104,6 +104,7 @@ class AstDumpDotVisitor final : public RecursiveVisitor
void visit( CaseRule& node ) override;
void visit( LetRule& node ) override;
void visit( ForallRule& node ) override;
void visit( ChooseRule& node ) override;
void visit( IterateRule& node ) override;
void visit( BlockRule& node ) override;
void visit( SequenceRule& node ) override;
Expand Down Expand Up @@ -317,6 +318,13 @@ void AstDumpDotVisitor::visit( ForallRule& node )
RecursiveVisitor::visit( node );
}

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

void AstDumpDotVisitor::visit( IterateRule& node )
{
DotLink link( this, &node );
Expand Down
7 changes: 7 additions & 0 deletions src/transform/AstDumpSourcePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class AstDumpSourceVisitor final : public RecursiveVisitor
void visit( CaseRule& node ) override;
void visit( LetRule& node ) override;
void visit( ForallRule& node ) override;
void visit( ChooseRule& node ) override;
void visit( IterateRule& node ) override;
void visit( BlockRule& node ) override;
void visit( SequenceRule& node ) override;
Expand Down Expand Up @@ -244,6 +245,12 @@ void AstDumpSourceVisitor::visit( ForallRule& node )
RecursiveVisitor::visit( node );
}

void AstDumpSourceVisitor::visit( ChooseRule& node )
{
// TODO
RecursiveVisitor::visit( node );
}

void AstDumpSourceVisitor::visit( IterateRule& node )
{
// TODO
Expand Down
18 changes: 5 additions & 13 deletions src/transform/AstToCasmIRPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ libcasm_ir::Type::Ptr AstToCasmIRPass::getType( Type* type )

switch( type->t )
{
case TypeType::AGENT:
return libstdhl::get< libcasm_ir::AgentType >( m_agent );
case TypeType::RULEREF:
return libstdhl::get< libcasm_ir::RuleReferenceType >();
case TypeType::BOOLEAN:
Expand Down Expand Up @@ -304,14 +302,6 @@ void AstToCasmIRPass::visit_derived_def_pre( DerivedDefNode* node )
auto ir_derived = libstdhl::make< libcasm_ir::Derived >(
node->sym->name.c_str(), ftype );

for( i32 i = 0; i < node->sym->arguments_.size(); i++ )
{
const char* param_ident = node->sym->parameter[ i ];

// ir_derived->addParameter( // PPA: FIXME:
// libcasm_ir::Identifier::create( ftype_args[ i ], param_ident ) );
}

current_scope.push_back( ir_derived );
}

Expand Down Expand Up @@ -1127,10 +1117,12 @@ u1 AstToCasmIRPass::visit_string_atom( StringAtom* node )
u1 AstToCasmIRPass::visit_self_atom( SelfAtom* node )
{
VISIT;
const auto type
= static_cast< const libcasm_ir::EnumerationType& >( m_agent->type() );

const libcasm_ir::Constant::Ptr ir_const
= libstdhl::get< libcasm_ir::AgentConstant >(
libstdhl::get< libcasm_ir::AgentType >( m_agent ),
single_execution_agent );
= libstdhl::get< libcasm_ir::EnumerationConstant >(
type.ptr_kind(), single_execution_agent );

assert( ir_const );
ast2casmir[ node ] = ir_const;
Expand Down
Loading

0 comments on commit 418a260

Please sign in to comment.