Replace all lists with vectors (#815)

This commit is contained in:
Geoff Romer
2021-09-07 09:20:10 -07:00
committed by GitHub
parent 225eda7f49
commit f931a8cead
13 changed files with 65 additions and 67 deletions
+2 -2
View File
@@ -5,7 +5,7 @@
#ifndef EXECUTABLE_SEMANTICS_AST_AST_H_
#define EXECUTABLE_SEMANTICS_AST_AST_H_
#include <list>
#include <vector>
#include "executable_semantics/ast/declaration.h"
#include "executable_semantics/ast/library_name.h"
@@ -22,7 +22,7 @@ struct AST {
// Import directives.
std::vector<LibraryName> imports;
// The file's ordered declarations.
std::list<Ptr<const Declaration>> declarations;
std::vector<Ptr<const Declaration>> declarations;
};
} // namespace Carbon
+2 -2
View File
@@ -5,8 +5,8 @@
#ifndef EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_
#define EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_
#include <list>
#include <string>
#include <vector>
#include "executable_semantics/ast/member.h"
#include "executable_semantics/ast/source_location.h"
@@ -16,7 +16,7 @@ namespace Carbon {
struct ClassDefinition {
SourceLocation loc;
std::string name;
std::list<Ptr<Member>> members;
std::vector<Ptr<Member>> members;
};
} // namespace Carbon
+5 -5
View File
@@ -5,8 +5,8 @@
#ifndef EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
#define EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
#include <list>
#include <string>
#include <vector>
#include "common/ostream.h"
#include "executable_semantics/ast/class_definition.h"
@@ -77,7 +77,7 @@ class FunctionDeclaration : public Declaration {
class ClassDeclaration : public Declaration {
public:
ClassDeclaration(SourceLocation loc, std::string name,
std::list<Ptr<Member>> members)
std::vector<Ptr<Member>> members)
: Declaration(Kind::ClassDeclaration, loc),
definition({.loc = loc,
.name = std::move(name),
@@ -97,7 +97,7 @@ class ChoiceDeclaration : public Declaration {
public:
ChoiceDeclaration(
SourceLocation loc, std::string name,
std::list<std::pair<std::string, Ptr<const Expression>>> alternatives)
std::vector<std::pair<std::string, Ptr<const Expression>>> alternatives)
: Declaration(Kind::ChoiceDeclaration, loc),
name(std::move(name)),
alternatives(std::move(alternatives)) {}
@@ -108,13 +108,13 @@ class ChoiceDeclaration : public Declaration {
auto Name() const -> const std::string& { return name; }
auto Alternatives() const
-> const std::list<std::pair<std::string, Ptr<const Expression>>>& {
-> const std::vector<std::pair<std::string, Ptr<const Expression>>>& {
return alternatives;
}
private:
std::string name;
std::list<std::pair<std::string, Ptr<const Expression>>> alternatives;
std::vector<std::pair<std::string, Ptr<const Expression>>> alternatives;
};
// Global variable definition implements the Declaration concept.
+7 -6
View File
@@ -5,7 +5,7 @@
#ifndef EXECUTABLE_SEMANTICS_AST_STATEMENT_H_
#define EXECUTABLE_SEMANTICS_AST_STATEMENT_H_
#include <list>
#include <vector>
#include "common/ostream.h"
#include "executable_semantics/ast/expression.h"
@@ -225,8 +225,9 @@ class Continue : public Statement {
class Match : public Statement {
public:
Match(SourceLocation loc, Ptr<const Expression> exp,
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>> clauses)
Match(
SourceLocation loc, Ptr<const Expression> exp,
std::vector<std::pair<Ptr<const Pattern>, Ptr<const Statement>>> clauses)
: Statement(Kind::Match, loc), exp(exp), clauses(std::move(clauses)) {}
static auto classof(const Statement* stmt) -> bool {
@@ -234,14 +235,14 @@ class Match : public Statement {
}
auto Exp() const -> Ptr<const Expression> { return exp; }
auto Clauses() const
-> const std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>& {
auto Clauses() const -> const
std::vector<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>& {
return clauses;
}
private:
Ptr<const Expression> exp;
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>> clauses;
std::vector<std::pair<Ptr<const Pattern>, Ptr<const Statement>>> clauses;
};
// A continuation statement.
@@ -15,7 +15,7 @@ namespace Carbon {
// Adds builtins, currently only Print(). Note Print() is experimental, not
// standardized, but is made available for printing state in tests.
static void AddIntrinsics(std::list<Ptr<const Declaration>>* declarations) {
static void AddIntrinsics(std::vector<Ptr<const Declaration>>* declarations) {
SourceLocation loc("<intrinsic>", 0);
std::vector<TuplePattern::Field> print_fields = {TuplePattern::Field(
"0", global_arena->New<BindingPattern>(
@@ -50,7 +50,7 @@ void ExecProgram(AST ast) {
TypeChecker::TypeCheckContext p = type_checker.TopLevel(ast.declarations);
TypeEnv top = p.types;
Env ct_top = p.values;
std::list<Ptr<const Declaration>> new_decls;
std::vector<Ptr<const Declaration>> new_decls;
for (const auto decl : ast.declarations) {
new_decls.push_back(type_checker.MakeTypeChecked(decl, top, ct_top));
}
@@ -9,8 +9,6 @@
#ifndef EXECUTABLE_SEMANTICS_INTERPRETER_EXEC_PROGRAM_H_
#define EXECUTABLE_SEMANTICS_INTERPRETER_EXEC_PROGRAM_H_
#include <list>
#include "executable_semantics/ast/ast.h"
namespace Carbon {
+4 -4
View File
@@ -5,8 +5,8 @@
#ifndef EXECUTABLE_SEMANTICS_INTERPRETER_FRAME_H_
#define EXECUTABLE_SEMANTICS_INTERPRETER_FRAME_H_
#include <list>
#include <string>
#include <vector>
#include "common/ostream.h"
#include "executable_semantics/interpreter/action.h"
@@ -20,12 +20,12 @@ namespace Carbon {
using Env = Dictionary<std::string, Address>;
struct Scope {
explicit Scope(Env values) : Scope(values, std::list<std::string>()) {}
Scope(Env values, std::list<std::string> l)
explicit Scope(Env values) : Scope(values, std::vector<std::string>()) {}
Scope(Env values, std::vector<std::string> l)
: values(values), locals(std::move(l)) {}
Env values;
std::list<std::string> locals;
std::vector<std::string> locals;
};
// A frame represents either a function call or a delimited continuation.
@@ -5,7 +5,6 @@
#include "executable_semantics/interpreter/interpreter.h"
#include <iterator>
#include <list>
#include <map>
#include <optional>
#include <utility>
@@ -174,7 +173,7 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
}
}
void Interpreter::InitGlobals(const std::list<Ptr<const Declaration>>& fs) {
void Interpreter::InitGlobals(const std::vector<Ptr<const Declaration>>& fs) {
for (const auto d : fs) {
InitEnv(*d, &globals);
}
@@ -772,14 +771,13 @@ auto Interpreter::StepStmt() -> Transition {
frame->scopes.Pop();
return Done{};
}
auto c = match_stmt.Clauses().begin();
std::advance(c, clause_num);
auto c = match_stmt.Clauses()[clause_num];
if (act->Pos() % 2 == 1) {
// start interpreting the pattern of the clause
// { {v :: (match ([]) ...) :: C, E, F} :: S, H}
// -> { {pi :: (match ([]) ...) :: C, E, F} :: S, H}
return Spawn{global_arena->New<PatternAction>(c->first)};
return Spawn{global_arena->New<PatternAction>(c.first)};
} else { // try to match
auto v = act->Results()[0];
auto pat = act->Results()[clause_num + 1];
@@ -792,7 +790,7 @@ auto Interpreter::StepStmt() -> Transition {
frame->scopes.Top()->values.Set(name, value);
frame->scopes.Top()->locals.push_back(name);
}
return Spawn{global_arena->New<StatementAction>(c->second)};
return Spawn{global_arena->New<StatementAction>(c.second)};
} else {
return RunAgain{};
}
@@ -1090,7 +1088,7 @@ class Interpreter::DoTransition {
<< "internal error in call_function, pattern match failed";
// Create the new frame and push it on the stack
Env values = interpreter->globals;
std::list<std::string> params;
std::vector<std::string> params;
for (const auto& [name, value] : *matches) {
values.Set(name, value);
params.push_back(name);
@@ -1134,7 +1132,7 @@ void Interpreter::Step() {
} // switch
}
auto Interpreter::InterpProgram(const std::list<Ptr<const Declaration>>& fs)
auto Interpreter::InterpProgram(const std::vector<Ptr<const Declaration>>& fs)
-> int {
// Check that the interpreter is in a clean state.
CHECK(globals.IsEmpty());
@@ -5,7 +5,6 @@
#ifndef EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
#define EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
#include <list>
#include <optional>
#include <utility>
#include <vector>
@@ -26,7 +25,7 @@ using Env = Dictionary<std::string, Address>;
class Interpreter {
public:
// Interpret the whole program.
auto InterpProgram(const std::list<Ptr<const Declaration>>& fs) -> int;
auto InterpProgram(const std::vector<Ptr<const Declaration>>& fs) -> int;
// Interpret an expression at compile-time.
auto InterpExp(Env values, Ptr<const Expression> e) -> Ptr<const Value>;
@@ -124,7 +123,7 @@ class Interpreter {
// State transition for statements.
auto StepStmt() -> Transition;
void InitGlobals(const std::list<Ptr<const Declaration>>& fs);
void InitGlobals(const std::vector<Ptr<const Declaration>>& fs);
auto CurrentEnv() -> Env;
auto GetFromEnv(SourceLocation loc, const std::string& name) -> Address;
@@ -658,7 +658,7 @@ auto TypeChecker::TypeCheckStmt(Ptr<const Statement> s, TypeEnv types,
const auto& match = cast<Match>(*s);
auto res = TypeCheckExp(match.Exp(), types, values);
auto res_type = res.type;
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>
std::vector<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>
new_clauses;
for (auto& clause : match.Clauses()) {
new_clauses.push_back(TypeCheckCase(res_type, clause.first,
@@ -816,7 +816,7 @@ static auto CheckOrEnsureReturn(std::optional<Ptr<const Statement>> opt_stmt,
switch (stmt->Tag()) {
case Statement::Kind::Match: {
const auto& match = cast<Match>(*stmt);
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>
std::vector<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>
new_clauses;
for (const auto& clause : match.Clauses()) {
auto s = CheckOrEnsureReturn(clause.second, omitted_ret_type,
@@ -995,7 +995,7 @@ auto TypeChecker::MakeTypeChecked(const Ptr<const Declaration> d,
case Declaration::Kind::ClassDeclaration: {
const ClassDefinition& class_def =
cast<ClassDeclaration>(*d).Definition();
std::list<Ptr<Member>> fields;
std::vector<Ptr<Member>> fields;
for (Ptr<Member> m : class_def.members) {
switch (m->Tag()) {
case Member::Kind::FieldMember:
@@ -1091,7 +1091,7 @@ void TypeChecker::TopLevel(const Declaration& d, TypeCheckContext* tops) {
}
}
auto TypeChecker::TopLevel(const std::list<Ptr<const Declaration>>& fs)
auto TypeChecker::TopLevel(const std::vector<Ptr<const Declaration>>& fs)
-> TypeCheckContext {
TypeCheckContext tops;
bool found_main = false;
@@ -30,7 +30,7 @@ class TypeChecker {
auto MakeTypeChecked(const Ptr<const Declaration> d, const TypeEnv& types,
const Env& values) -> Ptr<const Declaration>;
auto TopLevel(const std::list<Ptr<const Declaration>>& fs)
auto TopLevel(const std::vector<Ptr<const Declaration>>& fs)
-> TypeCheckContext;
private:
+1 -2
View File
@@ -5,7 +5,6 @@
#ifndef EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_
#define EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_
#include <list>
#include <optional>
#include <string>
#include <variant>
@@ -86,7 +85,7 @@ class Value {
const Kind tag;
};
using VarValues = std::list<std::pair<std::string, Ptr<const Value>>>;
using VarValues = std::vector<std::pair<std::string, Ptr<const Value>>>;
auto FindInVarValues(const std::string& field, const VarValues& inits)
-> std::optional<Ptr<const Value>>;
+29 -26
View File
@@ -54,7 +54,6 @@
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <vector>
#include "common/check.h"
@@ -101,7 +100,7 @@
%type <BisonWrap<Ptr<const Declaration>>> declaration
%type <BisonWrap<Ptr<const FunctionDefinition>>> function_declaration
%type <BisonWrap<Ptr<const FunctionDefinition>>> function_definition
%type <std::list<Ptr<const Declaration>>> declaration_list
%type <std::vector<Ptr<const Declaration>>> declaration_list
%type <BisonWrap<Ptr<const Statement>>> statement
%type <BisonWrap<Ptr<const Statement>>> if_statement
%type <std::optional<Ptr<const Statement>>> optional_else
@@ -120,7 +119,7 @@
%type <std::optional<std::string>> binding_lhs
%type <BisonWrap<Ptr<const BindingPattern>>> variable_declaration
%type <BisonWrap<Ptr<Member>>> member
%type <std::list<Ptr<Member>>> member_list
%type <std::vector<Ptr<Member>>> member_list
%type <BisonWrap<ParenContents<Expression>::Element>> paren_expression_element
%type <ParenContents<Expression>> paren_expression_base
%type <ParenContents<Expression>> paren_expression_contents
@@ -131,9 +130,10 @@
%type <BisonWrap<ParenContents<Pattern>::Element>> paren_pattern_element
%type <ParenContents<Pattern>> paren_pattern_contents
%type <BisonWrap<std::pair<std::string, Ptr<const Expression>>>> alternative
%type <std::list<std::pair<std::string, Ptr<const Expression>>>> alternative_list
%type <std::vector<std::pair<std::string, Ptr<const Expression>>>> alternative_list
%type <std::vector<std::pair<std::string, Ptr<const Expression>>>> alternative_list_contents
%type <BisonWrap<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>> clause
%type <std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>> clause_list
%type <std::vector<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>> clause_list
%token
// Most tokens have their spelling defined in lexer.lpp.
@@ -516,11 +516,11 @@ clause:
;
clause_list:
// Empty
{ $$ = std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>(); }
| clause clause_list
{ $$ = {}; }
| clause_list clause
{
$$ = $2;
$$.push_front($1);
$$ = $1;
$$.push_back($2);
}
;
statement:
@@ -651,11 +651,11 @@ member: VAR variable_declaration SEMICOLON
;
member_list:
// Empty
{ $$ = std::list<Ptr<Member>>(); }
| member member_list
{ $$ = {}; }
| member_list member
{
$$ = $2;
$$.push_front($1);
$$ = $1;
$$.push_back($2);
}
;
alternative:
@@ -669,16 +669,19 @@ alternative:
;
alternative_list:
// Empty
{ $$ = std::list<std::pair<std::string, Ptr<const Expression>>>(); }
| alternative
{ $$ = {}; }
| alternative_list_contents
{ $$ = $1; }
| alternative_list_contents COMMA
{ $$ = $1; }
;
alternative_list_contents:
alternative
{ $$ = {$1}; }
| alternative_list_contents COMMA alternative
{
$$ = std::list<std::pair<std::string, Ptr<const Expression>>>();
$$.push_front($1);
}
| alternative COMMA alternative_list
{
$$ = std::move($3);
$$.push_front($1);
$$ = $1;
$$.push_back($3);
}
;
declaration:
@@ -697,11 +700,11 @@ declaration:
;
declaration_list:
// Empty
{ $$ = std::list<Ptr<const Declaration>>(); }
| declaration declaration_list
{ $$ = {}; }
| declaration_list declaration
{
$$ = $2;
$$.push_front(Ptr<const Declaration>($1));
$$ = $1;
$$.push_back(Ptr<const Declaration>($2));
}
;
%%