Fix type errors in function calls (#308)

* Make `field_list` always a list.

* Create separate `paren_expression` and `tuple` nonterminals

* Rename expression_or_field_list.* to field_list.*

Co-authored-by: Jeremy G. Siek <jsiek@indiana.edu>
This commit is contained in:
Geoff Romer
2021-03-01 10:13:53 -05:00
committed by GitHub
co-authored by Jeremy G. Siek
parent bf6bb800c4
commit 8b3bb7c5e9
7 changed files with 83 additions and 107 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ cc_library(
deps = [
"//executable_semantics/ast:declaration",
"//executable_semantics/ast:expression",
"//executable_semantics/ast:expression_or_field_list",
"//executable_semantics/ast:field_list",
"//executable_semantics/interpreter",
],
)
+3 -3
View File
@@ -24,9 +24,9 @@ cc_library(
)
cc_library(
name = "expression_or_field_list",
srcs = ["expression_or_field_list.cpp"],
hdrs = ["expression_or_field_list.h"],
name = "field_list",
srcs = ["field_list.cpp"],
hdrs = ["field_list.h"],
deps = [":expression"],
)
@@ -1,49 +0,0 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "executable_semantics/ast/expression_or_field_list.h"
namespace Carbon {
auto MakeExp(Expression* exp) -> ExpOrFieldList* {
auto e = new ExpOrFieldList();
e->tag = ExpOrFieldListKind::Exp;
e->u.exp = exp;
return e;
}
auto MakeFieldList(std::list<std::pair<std::string, Expression*>>* fields)
-> ExpOrFieldList* {
auto e = new ExpOrFieldList();
e->tag = ExpOrFieldListKind::FieldList;
e->u.fields = fields;
return e;
}
auto MakeConsField(ExpOrFieldList* e1, ExpOrFieldList* e2) -> ExpOrFieldList* {
auto fields = new std::list<std::pair<std::string, Expression*>>();
switch (e1->tag) {
case ExpOrFieldListKind::Exp:
fields->push_back(std::make_pair("", e1->u.exp));
break;
case ExpOrFieldListKind::FieldList:
for (auto& field : *e1->u.fields) {
fields->push_back(field);
}
break;
}
switch (e2->tag) {
case ExpOrFieldListKind::Exp:
fields->push_back(std::make_pair("", e2->u.exp));
break;
case ExpOrFieldListKind::FieldList:
for (auto& field : *e2->u.fields) {
fields->push_back(field);
}
break;
}
return MakeFieldList(fields);
}
} // namespace Carbon
@@ -1,32 +0,0 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef EXECUTABLE_SEMANTICS_AST_EXPRESSION_OR_FIELD_LIST_H_
#define EXECUTABLE_SEMANTICS_AST_EXPRESSION_OR_FIELD_LIST_H_
#include <list>
#include "executable_semantics/ast/expression.h"
namespace Carbon {
enum class ExpOrFieldListKind { Exp, FieldList };
// This is used in the parsing of tuples and parenthesized expressions.
struct ExpOrFieldList {
ExpOrFieldListKind tag;
union {
Expression* exp;
std::list<std::pair<std::string, Expression*>>* fields;
} u;
};
auto MakeExp(Expression* exp) -> ExpOrFieldList*;
auto MakeFieldList(std::list<std::pair<std::string, Expression*>>* fields)
-> ExpOrFieldList*;
auto MakeConsField(ExpOrFieldList* e1, ExpOrFieldList* e2) -> ExpOrFieldList*;
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_OR_FIELD_LIST_H_
+29
View File
@@ -0,0 +1,29 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "executable_semantics/ast/field_list.h"
namespace Carbon {
auto MakeFieldList(std::list<std::pair<std::string, Expression*>>* fields)
-> FieldList* {
auto e = new FieldList();
e->fields = fields;
return e;
}
auto MakeConsField(FieldList* e1, FieldList* e2) -> FieldList* {
auto fields = new std::list<std::pair<std::string, Expression*>>();
for (auto& field : *e1->fields) {
fields->push_back(field);
}
for (auto& field : *e2->fields) {
fields->push_back(field);
}
auto result = MakeFieldList(fields);
result->has_explicit_comma = true;
return result;
}
} // namespace Carbon
+26
View File
@@ -0,0 +1,26 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef EXECUTABLE_SEMANTICS_AST_FIELD_LIST_H_
#define EXECUTABLE_SEMANTICS_AST_FIELD_LIST_H_
#include <list>
#include "executable_semantics/ast/expression.h"
namespace Carbon {
// This is used in the parsing of tuples and parenthesized expressions.
struct FieldList {
std::list<std::pair<std::string, Expression*>>* fields;
bool has_explicit_comma = false;
};
auto MakeFieldList(std::list<std::pair<std::string, Expression*>>* fields)
-> FieldList*;
auto MakeConsField(FieldList* e1, FieldList* e2) -> FieldList*;
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_AST_FIELD_LIST_H_
+24 -22
View File
@@ -15,7 +15,7 @@
%code requires {
#include "executable_semantics/ast/declaration.h"
#include "executable_semantics/ast/expression_or_field_list.h"
#include "executable_semantics/ast/field_list.h"
#include "executable_semantics/ast/function_definition.h"
}
@@ -41,7 +41,7 @@ void yyerror(char* error) {
std::list<Carbon::Declaration>* declaration_list;
Carbon::Member* member;
std::list<Carbon::Member*>* member_list;
Carbon::ExpOrFieldList* field_list;
Carbon::FieldList* field_list;
std::pair<std::string, Carbon::Expression*>* alternative;
std::list<std::pair<std::string, Carbon::Expression*>>* alternative_list;
std::pair<Carbon::Expression*, Carbon::Statement*>* clause;
@@ -62,6 +62,7 @@ void yyerror(char* error) {
%type <expression> expression
%type <expression> pattern
%type <expression> return_type
%type <expression> paren_expression
%type <expression> tuple
%type <member> member
%type <member_list> member_list
@@ -137,7 +138,7 @@ expression:
{ $$ = Carbon::MakeTypeType(yylineno); }
| AUTO
{ $$ = Carbon::MakeAutoType(yylineno); }
| tuple { $$ = $1; }
| paren_expression { $$ = $1; }
| expression EQUAL expression
{ $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Eq, $1, $3); }
| expression '+' expression
@@ -153,38 +154,39 @@ expression:
| '-' expression
{ $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Neg, $2); }
| expression tuple
{
if ($2->tag == Carbon::ExpressionKind::Tuple) {
$$ = Carbon::MakeCall(yylineno, $1, $2);
} else {
auto vec =
new std::vector<std::pair<std::string, Carbon::Expression*>>();
vec->push_back(std::make_pair("", $2));
$$ = Carbon::MakeCall(yylineno, $1, Carbon::MakeTuple(yylineno, vec));
}
}
{ $$ = Carbon::MakeCall(yylineno, $1, $2); }
| FNTY tuple return_type
{ $$ = Carbon::MakeFunType(yylineno, $2, $3); }
;
designator: '.' identifier { $$ = $2; }
;
tuple: '(' field_list ')'
paren_expression: '(' field_list ')'
{
switch ($2->tag) {
case Carbon::ExpOrFieldListKind::Exp:
$$ = $2->u.exp;
break;
case Carbon::ExpOrFieldListKind::FieldList:
if ($2->fields->size() == 1 &&
$2->fields->front().first == "" &&
!$2->has_explicit_comma) {
$$ = $2->fields->front().second;
} else {
auto vec = new std::vector<std::pair<std::string,Carbon::Expression*>>(
$2->u.fields->begin(), $2->u.fields->end());
$2->fields->begin(), $2->fields->end());
$$ = Carbon::MakeTuple(yylineno, vec);
break;
}
}
;
tuple: '(' field_list ')'
{
auto vec = new std::vector<std::pair<std::string,Carbon::Expression*>>(
$2->fields->begin(), $2->fields->end());
$$ = Carbon::MakeTuple(yylineno, vec);
}
field:
pattern
{ $$ = Carbon::MakeExp($1); }
{
auto fields =
new std::list<std::pair<std::string, Carbon::Expression*>>();
fields->push_back(std::make_pair("", $1));
$$ = Carbon::MakeFieldList(fields);
}
| designator '=' pattern
{
auto fields =