diff --git a/executable_semantics/ast/ast.h b/executable_semantics/ast/ast.h index 646c863f64e0..eded57f578bf 100644 --- a/executable_semantics/ast/ast.h +++ b/executable_semantics/ast/ast.h @@ -5,7 +5,7 @@ #ifndef EXECUTABLE_SEMANTICS_AST_AST_H_ #define EXECUTABLE_SEMANTICS_AST_AST_H_ -#include +#include #include "executable_semantics/ast/declaration.h" #include "executable_semantics/ast/library_name.h" @@ -22,7 +22,7 @@ struct AST { // Import directives. std::vector imports; // The file's ordered declarations. - std::list> declarations; + std::vector> declarations; }; } // namespace Carbon diff --git a/executable_semantics/ast/class_definition.h b/executable_semantics/ast/class_definition.h index 18b1e6ae171e..a4d13db00168 100644 --- a/executable_semantics/ast/class_definition.h +++ b/executable_semantics/ast/class_definition.h @@ -5,8 +5,8 @@ #ifndef EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_ #define EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_ -#include #include +#include #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> members; + std::vector> members; }; } // namespace Carbon diff --git a/executable_semantics/ast/declaration.h b/executable_semantics/ast/declaration.h index aeac1114467a..e9eb959a0bde 100644 --- a/executable_semantics/ast/declaration.h +++ b/executable_semantics/ast/declaration.h @@ -5,8 +5,8 @@ #ifndef EXECUTABLE_SEMANTICS_AST_DECLARATION_H_ #define EXECUTABLE_SEMANTICS_AST_DECLARATION_H_ -#include #include +#include #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> members) + std::vector> 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>> alternatives) + std::vector>> 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>>& { + -> const std::vector>>& { return alternatives; } private: std::string name; - std::list>> alternatives; + std::vector>> alternatives; }; // Global variable definition implements the Declaration concept. diff --git a/executable_semantics/ast/statement.h b/executable_semantics/ast/statement.h index ac15a69fdfdd..c94cdb4c35cd 100644 --- a/executable_semantics/ast/statement.h +++ b/executable_semantics/ast/statement.h @@ -5,7 +5,7 @@ #ifndef EXECUTABLE_SEMANTICS_AST_STATEMENT_H_ #define EXECUTABLE_SEMANTICS_AST_STATEMENT_H_ -#include +#include #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 exp, - std::list, Ptr>> clauses) + Match( + SourceLocation loc, Ptr exp, + std::vector, Ptr>> 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 { return exp; } - auto Clauses() const - -> const std::list, Ptr>>& { + auto Clauses() const -> const + std::vector, Ptr>>& { return clauses; } private: Ptr exp; - std::list, Ptr>> clauses; + std::vector, Ptr>> clauses; }; // A continuation statement. diff --git a/executable_semantics/interpreter/exec_program.cpp b/executable_semantics/interpreter/exec_program.cpp index e1afcde17547..90bd42e15d2c 100644 --- a/executable_semantics/interpreter/exec_program.cpp +++ b/executable_semantics/interpreter/exec_program.cpp @@ -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>* declarations) { +static void AddIntrinsics(std::vector>* declarations) { SourceLocation loc("", 0); std::vector print_fields = {TuplePattern::Field( "0", global_arena->New( @@ -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> new_decls; + std::vector> new_decls; for (const auto decl : ast.declarations) { new_decls.push_back(type_checker.MakeTypeChecked(decl, top, ct_top)); } diff --git a/executable_semantics/interpreter/exec_program.h b/executable_semantics/interpreter/exec_program.h index c4f42781ea45..51b3e6aca709 100644 --- a/executable_semantics/interpreter/exec_program.h +++ b/executable_semantics/interpreter/exec_program.h @@ -9,8 +9,6 @@ #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_EXEC_PROGRAM_H_ #define EXECUTABLE_SEMANTICS_INTERPRETER_EXEC_PROGRAM_H_ -#include - #include "executable_semantics/ast/ast.h" namespace Carbon { diff --git a/executable_semantics/interpreter/frame.h b/executable_semantics/interpreter/frame.h index 599527135d38..b8d2b123d66c 100644 --- a/executable_semantics/interpreter/frame.h +++ b/executable_semantics/interpreter/frame.h @@ -5,8 +5,8 @@ #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_FRAME_H_ #define EXECUTABLE_SEMANTICS_INTERPRETER_FRAME_H_ -#include #include +#include #include "common/ostream.h" #include "executable_semantics/interpreter/action.h" @@ -20,12 +20,12 @@ namespace Carbon { using Env = Dictionary; struct Scope { - explicit Scope(Env values) : Scope(values, std::list()) {} - Scope(Env values, std::list l) + explicit Scope(Env values) : Scope(values, std::vector()) {} + Scope(Env values, std::vector l) : values(values), locals(std::move(l)) {} Env values; - std::list locals; + std::vector locals; }; // A frame represents either a function call or a delimited continuation. diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 6a0e26b1b4b6..a61238460aa0 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -5,7 +5,6 @@ #include "executable_semantics/interpreter/interpreter.h" #include -#include #include #include #include @@ -174,7 +173,7 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) { } } -void Interpreter::InitGlobals(const std::list>& fs) { +void Interpreter::InitGlobals(const std::vector>& 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(c->first)}; + return Spawn{global_arena->New(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(c->second)}; + return Spawn{global_arena->New(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 params; + std::vector 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>& fs) +auto Interpreter::InterpProgram(const std::vector>& fs) -> int { // Check that the interpreter is in a clean state. CHECK(globals.IsEmpty()); diff --git a/executable_semantics/interpreter/interpreter.h b/executable_semantics/interpreter/interpreter.h index a38cc7c4c5b5..6a2aba777abc 100644 --- a/executable_semantics/interpreter/interpreter.h +++ b/executable_semantics/interpreter/interpreter.h @@ -5,7 +5,6 @@ #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_ #define EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_ -#include #include #include #include @@ -26,7 +25,7 @@ using Env = Dictionary; class Interpreter { public: // Interpret the whole program. - auto InterpProgram(const std::list>& fs) -> int; + auto InterpProgram(const std::vector>& fs) -> int; // Interpret an expression at compile-time. auto InterpExp(Env values, Ptr e) -> Ptr; @@ -124,7 +123,7 @@ class Interpreter { // State transition for statements. auto StepStmt() -> Transition; - void InitGlobals(const std::list>& fs); + void InitGlobals(const std::vector>& fs); auto CurrentEnv() -> Env; auto GetFromEnv(SourceLocation loc, const std::string& name) -> Address; diff --git a/executable_semantics/interpreter/type_checker.cpp b/executable_semantics/interpreter/type_checker.cpp index be4416d2861d..b90e9c4f029c 100644 --- a/executable_semantics/interpreter/type_checker.cpp +++ b/executable_semantics/interpreter/type_checker.cpp @@ -658,7 +658,7 @@ auto TypeChecker::TypeCheckStmt(Ptr s, TypeEnv types, const auto& match = cast(*s); auto res = TypeCheckExp(match.Exp(), types, values); auto res_type = res.type; - std::list, Ptr>> + std::vector, Ptr>> 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> opt_stmt, switch (stmt->Tag()) { case Statement::Kind::Match: { const auto& match = cast(*stmt); - std::list, Ptr>> + std::vector, Ptr>> 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 d, case Declaration::Kind::ClassDeclaration: { const ClassDefinition& class_def = cast(*d).Definition(); - std::list> fields; + std::vector> fields; for (Ptr 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>& fs) +auto TypeChecker::TopLevel(const std::vector>& fs) -> TypeCheckContext { TypeCheckContext tops; bool found_main = false; diff --git a/executable_semantics/interpreter/type_checker.h b/executable_semantics/interpreter/type_checker.h index f1dff7d9c473..186d682d5f27 100644 --- a/executable_semantics/interpreter/type_checker.h +++ b/executable_semantics/interpreter/type_checker.h @@ -30,7 +30,7 @@ class TypeChecker { auto MakeTypeChecked(const Ptr d, const TypeEnv& types, const Env& values) -> Ptr; - auto TopLevel(const std::list>& fs) + auto TopLevel(const std::vector>& fs) -> TypeCheckContext; private: diff --git a/executable_semantics/interpreter/value.h b/executable_semantics/interpreter/value.h index 22de1039d2f9..9cb3cf89d4a0 100644 --- a/executable_semantics/interpreter/value.h +++ b/executable_semantics/interpreter/value.h @@ -5,7 +5,6 @@ #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_ #define EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_ -#include #include #include #include @@ -86,7 +85,7 @@ class Value { const Kind tag; }; -using VarValues = std::list>>; +using VarValues = std::vector>>; auto FindInVarValues(const std::string& field, const VarValues& inits) -> std::optional>; diff --git a/executable_semantics/syntax/parser.ypp b/executable_semantics/syntax/parser.ypp index b03aebeb3b33..ff2ed05238ce 100644 --- a/executable_semantics/syntax/parser.ypp +++ b/executable_semantics/syntax/parser.ypp @@ -54,7 +54,6 @@ #include #include #include - #include #include #include "common/check.h" @@ -101,7 +100,7 @@ %type >> declaration %type >> function_declaration %type >> function_definition -%type >> declaration_list +%type >> declaration_list %type >> statement %type >> if_statement %type >> optional_else @@ -120,7 +119,7 @@ %type > binding_lhs %type >> variable_declaration %type >> member -%type >> member_list +%type >> member_list %type ::Element>> paren_expression_element %type > paren_expression_base %type > paren_expression_contents @@ -131,9 +130,10 @@ %type ::Element>> paren_pattern_element %type > paren_pattern_contents %type >>> alternative -%type >>> alternative_list +%type >>> alternative_list +%type >>> alternative_list_contents %type , Ptr>>> clause -%type , Ptr>>> clause_list +%type , Ptr>>> clause_list %token // Most tokens have their spelling defined in lexer.lpp. @@ -516,11 +516,11 @@ clause: ; clause_list: // Empty - { $$ = std::list, Ptr>>(); } -| 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>(); } -| 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>>(); } -| alternative + { $$ = {}; } +| alternative_list_contents + { $$ = $1; } +| alternative_list_contents COMMA + { $$ = $1; } +; +alternative_list_contents: + alternative + { $$ = {$1}; } +| alternative_list_contents COMMA alternative { - $$ = std::list>>(); - $$.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>(); } -| declaration declaration_list + { $$ = {}; } +| declaration_list declaration { - $$ = $2; - $$.push_front(Ptr($1)); + $$ = $1; + $$.push_back(Ptr($2)); } ; %%