Adds basic support for class functions and methods. (#1057)

* adding methods to the ast

* pre commit stuff?

* implementation of class functions

* implemented methods

* some cleanup

* more cleanup

* add newlines in test programs

* pre-commit fixups

* added include of return_term.h

* a test of a method calling another method

* replacing Member with Declaration

* removing the member.h etc files

* clarify a type annotation

* update uses of FunctionDeclaration

* remove ReturnTarget, no longer needed

* more cleanup

* more cleanup

* yet more cleanup, playing with pre-commit

* did a pre-commit run --all-files

* fixed const issue

* remove comment

* checking dependencies in BUILD files and headers

* pre-commit working now

* refactor NominalClassType to just hold a pointer to the class declaration

* remove Member from rtti

* responding to Geoffreys review

* change field_types to a non-member function
This commit is contained in:
Jeremy G. Siek
2022-02-05 12:30:13 -05:00
committed by GitHub
parent 4479c55305
commit ac0b810bf3
27 changed files with 739 additions and 351 deletions
+32 -24
View File
@@ -110,8 +110,8 @@
%type <std::vector<Nonnull<Statement*>>> statement_list
%type <Nonnull<Expression*>> expression
%type <Nonnull<GenericBinding*>> generic_binding
%type <std::vector<Nonnull<GenericBinding*>>> deduced_params
%type <std::vector<Nonnull<GenericBinding*>>> deduced_param_list
%type <std::vector<Nonnull<AstNode*>>> deduced_params
%type <std::vector<Nonnull<AstNode*>>> deduced_param_list
%type <Nonnull<Pattern*>> pattern
%type <Nonnull<Pattern*>> non_expression_pattern
%type <BisonWrap<ReturnTerm>> return_term
@@ -122,9 +122,8 @@
%type <std::vector<FieldInitializer>> struct_type_literal_contents
%type <Nonnull<TupleLiteral*>> tuple
%type <std::string> binding_lhs
%type <std::optional<Nonnull<BindingPattern*>>> receiver
%type <Nonnull<BindingPattern*>> variable_declaration
%type <Nonnull<Member*>> member
%type <std::vector<Nonnull<Member*>>> member_list
%type <ParenContents<Expression>> paren_expression_base
%type <ParenContents<Expression>> paren_expression_contents
%type <Nonnull<Pattern*>> paren_pattern
@@ -653,10 +652,10 @@ generic_binding:
;
deduced_param_list:
// Empty
{ $$ = std::vector<Nonnull<GenericBinding*>>(); }
{ $$ = std::vector<Nonnull<AstNode*>>(); }
| generic_binding
{
$$ = std::vector<Nonnull<GenericBinding*>>();
$$ = std::vector<Nonnull<AstNode*>>();
$$.push_back($1);
}
| generic_binding COMMA deduced_param_list
@@ -664,40 +663,44 @@ deduced_param_list:
$$ = $3;
$$.push_back($1);
}
| variable_declaration
{
$$ = std::vector<Nonnull<AstNode*>>();
$$.push_back($1);
}
| variable_declaration COMMA deduced_param_list
{
$$ = $3;
$$.push_back($1);
}
;
deduced_params:
// Empty
{ $$ = std::vector<Nonnull<GenericBinding*>>(); }
{ $$ = std::vector<Nonnull<AstNode*>>(); }
| LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
{ $$ = $2; }
;
receiver:
// Empty
{ $$ = std::nullopt; }
| LEFT_CURLY_BRACE variable_declaration RIGHT_CURLY_BRACE
{ $$ = $2; }
;
function_declaration:
FN identifier deduced_params maybe_empty_tuple_pattern return_term block
FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term block
{
$$ = arena->New<FunctionDeclaration>(context.source_loc(), $2, $3, $4, $5,
$6);
$6, $7);
}
| FN identifier deduced_params maybe_empty_tuple_pattern return_term SEMICOLON
| FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term SEMICOLON
{
$$ = arena->New<FunctionDeclaration>(context.source_loc(), $2, $3, $4, $5,
std::nullopt);
$6, std::nullopt);
}
;
variable_declaration: identifier COLON pattern
{ $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3); }
;
member: VAR variable_declaration SEMICOLON
{ $$ = arena->New<FieldMember>(context.source_loc(), $2); }
;
member_list:
// Empty
{ $$ = {}; }
| member_list member
{
$$ = $1;
$$.push_back($2);
}
;
alternative:
identifier tuple
{ $$ = arena->New<AlternativeSignature>(context.source_loc(), $1, $2); }
@@ -728,10 +731,15 @@ alternative_list_contents:
declaration:
function_declaration
{ $$ = $1; }
| CLASS identifier LEFT_CURLY_BRACE member_list RIGHT_CURLY_BRACE
| CLASS identifier LEFT_CURLY_BRACE declaration_list RIGHT_CURLY_BRACE
{ $$ = arena->New<ClassDeclaration>(context.source_loc(), $2, $4); }
| CHOICE identifier LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
{ $$ = arena->New<ChoiceDeclaration>(context.source_loc(), $2, $4); }
| VAR variable_declaration SEMICOLON
{
$$ = arena->New<VariableDeclaration>(context.source_loc(), $2,
std::nullopt);
}
| VAR variable_declaration EQUAL expression SEMICOLON
{ $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4); }
;