From 7e1721dc01db3c285055ae64d2bea5a3ee406f16 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Wed, 17 Mar 2021 13:09:58 -0700 Subject: [PATCH] lowerCamelCase => snake_case (#396) --- executable_semantics/ast/declaration.h | 6 +- .../interpreter/interpreter.cpp | 36 +++--- .../interpreter/typecheck.cpp | 12 +- executable_semantics/main.cpp | 12 +- executable_semantics/syntax/lexer.lpp | 104 +++++++++--------- executable_semantics/syntax/parse.cpp | 20 ++-- executable_semantics/syntax/parse.h | 2 +- .../syntax/parse_and_lex_context.cpp | 3 +- .../syntax/parse_and_lex_context.h | 9 +- executable_semantics/syntax/parser.ypp | 4 +- 10 files changed, 105 insertions(+), 103 deletions(-) diff --git a/executable_semantics/ast/declaration.h b/executable_semantics/ast/declaration.h index 8f416130e591..2e240a569be3 100644 --- a/executable_semantics/ast/declaration.h +++ b/executable_semantics/ast/declaration.h @@ -141,9 +141,9 @@ struct ChoiceDeclaration { // Global variable definition implements the Declaration concept. class VariableDeclaration { public: - VariableDeclaration(int sourceLocation, std::string name, Expression* type, + VariableDeclaration(int source_location, std::string name, Expression* type, Expression* initializer) - : sourceLocation(sourceLocation), + : source_location(source_location), name(name), type(type), initializer(initializer) {} @@ -155,7 +155,7 @@ class VariableDeclaration { auto TopLevel(ExecutionEnvironment&) const -> void; private: - int sourceLocation; + int source_location; std::string name; Expression* type; Expression* initializer; diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index b6bb6ec3c821..c5fcac9b88f4 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -322,15 +322,15 @@ void CallFunction(int line_num, std::vector operas, State* state) { case ValKind::FunV: { // Bind arguments to parameters std::list params; - std::optional envWithMatches = PatternMatch( + std::optional env_with_matches = PatternMatch( operas[0]->u.fun.param, operas[1], globals, ¶ms, line_num); - if (!envWithMatches) { + if (!env_with_matches) { std::cerr << "internal error in call_function, pattern match failed" << std::endl; exit(-1); } // Create the new frame and push it on the stack - auto* scope = new Scope(*envWithMatches, params); + auto* scope = new Scope(*env_with_matches, params); auto* frame = new Frame(*operas[0]->u.fun.name, Stack(scope), Stack(MakeStmtAct(operas[0]->u.fun.body))); state->stack.Push(frame); @@ -420,12 +420,12 @@ auto PatternMatch(Value* p, Value* v, Env env, std::list* vars, std::cerr << std::endl; exit(-1); } - std::optional envWithMatches = PatternMatch( + std::optional env_with_matches = PatternMatch( state->heap[elt.second], state->heap[*a], env, vars, line_num); - if (!envWithMatches) { + if (!env_with_matches) { return std::nullopt; } - env = *envWithMatches; + env = *env_with_matches; } // for return env; } @@ -443,12 +443,12 @@ auto PatternMatch(Value* p, Value* v, Env env, std::list* vars, *p->u.alt.alt_name != *v->u.alt.alt_name) { return std::nullopt; } - std::optional envWithMatches = + std::optional env_with_matches = PatternMatch(p->u.alt.arg, v->u.alt.arg, env, vars, line_num); - if (!envWithMatches) { + if (!env_with_matches) { return std::nullopt; } - return *envWithMatches; + return *env_with_matches; } default: std::cerr @@ -461,13 +461,13 @@ auto PatternMatch(Value* p, Value* v, Env env, std::list* vars, case ValKind::FunctionTV: switch (v->tag) { case ValKind::FunctionTV: { - std::optional envWithMatches = PatternMatch( + std::optional env_with_matches = PatternMatch( p->u.fun_type.param, v->u.fun_type.param, env, vars, line_num); - if (!envWithMatches) { + if (!env_with_matches) { return std::nullopt; } return PatternMatch(p->u.fun_type.ret, v->u.fun_type.ret, - *envWithMatches, vars, line_num); + *env_with_matches, vars, line_num); } default: return std::nullopt; @@ -1165,17 +1165,17 @@ void HandleValue() { Value* v = act->results[0]; Value* p = act->results[1]; - std::optional envWithMatches = + std::optional env_with_matches = PatternMatch(p, v, frame->scopes.Top()->env, &frame->scopes.Top()->locals, stmt->line_num); - if (!envWithMatches) { + if (!env_with_matches) { std::cerr << stmt->line_num << ": internal error in variable definition, match failed" << std::endl; exit(-1); } - frame->scopes.Top()->env = *envWithMatches; + frame->scopes.Top()->env = *env_with_matches; frame->todo.Pop(2); } break; @@ -1259,10 +1259,10 @@ void HandleValue() { auto pat = act->results[clause_num + 1]; auto env = CurrentEnv(state); std::list vars; - std::optional envWithMatches = + std::optional env_with_matches = PatternMatch(pat, v, env, &vars, stmt->line_num); - if (envWithMatches) { // we have a match, start the body - auto* new_scope = new Scope(*envWithMatches, vars); + if (env_with_matches) { // we have a match, start the body + auto* new_scope = new Scope(*env_with_matches, vars); frame->scopes.Push(new_scope); Statement* body_block = MakeBlock(stmt->line_num, c->second); Action* body_act = MakeStmtAct(body_block); diff --git a/executable_semantics/interpreter/typecheck.cpp b/executable_semantics/interpreter/typecheck.cpp index ac1ae248716a..d0161565cc8c 100644 --- a/executable_semantics/interpreter/typecheck.cpp +++ b/executable_semantics/interpreter/typecheck.cpp @@ -650,11 +650,11 @@ auto ChoiceDeclaration::TypeChecked(TypeEnv env, Env ct_env) const // declaration with annotated types. auto VariableDeclaration::TypeChecked(TypeEnv env, Env ct_env) const -> Declaration { - TCResult typeCheckedInitializer = + TCResult type_checked_initializer = TypeCheckExp(initializer, env, ct_env, nullptr, TCContext::ValueContext); - Value* declaredType = ToType(sourceLocation, InterpExp(ct_env, type)); - ExpectType(sourceLocation, "initializer of variable", declaredType, - typeCheckedInitializer.type); + Value* declared_type = ToType(source_location, InterpExp(ct_env, type)); + ExpectType(source_location, "initializer of variable", declared_type, + type_checked_initializer.type); return *this; } @@ -706,8 +706,8 @@ auto ChoiceDeclaration::TopLevel(ExecutionEnvironment& tops) const -> void { // Associate the variable name with it's declared type in the // compile-time symbol table. auto VariableDeclaration::TopLevel(ExecutionEnvironment& tops) const -> void { - Value* declaredType = ToType(sourceLocation, InterpExp(tops.second, type)); - tops.first.Set(Name(), declaredType); + Value* declared_type = ToType(source_location, InterpExp(tops.second, type)); + tops.first.Set(Name(), declared_type); } } // namespace Carbon diff --git a/executable_semantics/main.cpp b/executable_semantics/main.cpp index 555b3c247407..b8bda20a2e13 100644 --- a/executable_semantics/main.cpp +++ b/executable_semantics/main.cpp @@ -17,22 +17,22 @@ int main(int argc, char* argv[]) { using llvm::cl::desc; using llvm::cl::opt; opt trace_option("trace", desc("Enable tracing")); - opt inputFileName(llvm::cl::Positional, desc(""), - llvm::cl::Required); + opt input_file_name(llvm::cl::Positional, desc(""), + llvm::cl::Required); llvm::cl::ParseCommandLineOptions(argc, argv); if (trace_option) { Carbon::tracing_output = true; } - std::variant astOrError = - Carbon::parse(inputFileName); + std::variant ast_or_error = + Carbon::parse(input_file_name); - if (auto* error = std::get_if(&astOrError)) { + if (auto* error = std::get_if(&ast_or_error)) { // Diagnostic already reported to std::cerr; this is just a return code. return *error; } // Typecheck and run the parsed program. - Carbon::ExecProgram(std::get(astOrError)); + Carbon::ExecProgram(std::get(ast_or_error)); } diff --git a/executable_semantics/syntax/lexer.lpp b/executable_semantics/syntax/lexer.lpp index 0ba82dffbd8b..44ba34af5ae5 100644 --- a/executable_semantics/syntax/lexer.lpp +++ b/executable_semantics/syntax/lexer.lpp @@ -55,7 +55,7 @@ horizontal_whitespace [ \t\r] // // Advances the current token position by yyleng columns without changing // the line number. - # define YY_USER_ACTION context.currentTokenPosition.columns(yyleng); + # define YY_USER_ACTION context.current_token_position.columns(yyleng); %} %% @@ -64,92 +64,92 @@ horizontal_whitespace [ \t\r] // Code run each time yylex is called. // Begin with an empty token span starting where its previous end was. - context.currentTokenPosition.step(); + context.current_token_position.step(); %} -{AND} { return yy::parser::make_AND(context.currentTokenPosition); } -{ARROW} { return yy::parser::make_ARROW(context.currentTokenPosition); } -{AUTO} { return yy::parser::make_AUTO(context.currentTokenPosition); } -{BOOL} { return yy::parser::make_BOOL(context.currentTokenPosition); } -{BREAK} { return yy::parser::make_BREAK(context.currentTokenPosition); } -{CASE} { return yy::parser::make_CASE(context.currentTokenPosition); } -{CHOICE} { return yy::parser::make_CHOICE(context.currentTokenPosition); } -{CONTINUE} { return yy::parser::make_CONTINUE(context.currentTokenPosition); } -{DBLARROW} { return yy::parser::make_DBLARROW(context.currentTokenPosition); } -{DEFAULT} { return yy::parser::make_DEFAULT(context.currentTokenPosition); } -{ELSE} { return yy::parser::make_ELSE(context.currentTokenPosition); } -"==" { return yy::parser::make_EQUAL_EQUAL(context.currentTokenPosition); } -{FALSE} { return yy::parser::make_FALSE(context.currentTokenPosition); } -{FN} { return yy::parser::make_FN(context.currentTokenPosition); } -{FNTY} { return yy::parser::make_FNTY(context.currentTokenPosition); } -{IF} { return yy::parser::make_IF(context.currentTokenPosition); } -{INT} { return yy::parser::make_INT(context.currentTokenPosition); } -{MATCH} { return yy::parser::make_MATCH(context.currentTokenPosition); } -{NOT} { return yy::parser::make_NOT(context.currentTokenPosition); } -{OR} { return yy::parser::make_OR(context.currentTokenPosition); } -{RETURN} { return yy::parser::make_RETURN(context.currentTokenPosition); } -{STRUCT} { return yy::parser::make_STRUCT(context.currentTokenPosition); } -{TRUE} { return yy::parser::make_TRUE(context.currentTokenPosition); } -{TYPE} { return yy::parser::make_TYPE(context.currentTokenPosition); } -{VAR} { return yy::parser::make_VAR(context.currentTokenPosition); } -{WHILE} { return yy::parser::make_WHILE(context.currentTokenPosition); } +{AND} { return yy::parser::make_AND(context.current_token_position); } +{ARROW} { return yy::parser::make_ARROW(context.current_token_position); } +{AUTO} { return yy::parser::make_AUTO(context.current_token_position); } +{BOOL} { return yy::parser::make_BOOL(context.current_token_position); } +{BREAK} { return yy::parser::make_BREAK(context.current_token_position); } +{CASE} { return yy::parser::make_CASE(context.current_token_position); } +{CHOICE} { return yy::parser::make_CHOICE(context.current_token_position); } +{CONTINUE} { return yy::parser::make_CONTINUE(context.current_token_position); } +{DBLARROW} { return yy::parser::make_DBLARROW(context.current_token_position); } +{DEFAULT} { return yy::parser::make_DEFAULT(context.current_token_position); } +{ELSE} { return yy::parser::make_ELSE(context.current_token_position); } +"==" { return yy::parser::make_EQUAL_EQUAL(context.current_token_position); } +{FALSE} { return yy::parser::make_FALSE(context.current_token_position); } +{FN} { return yy::parser::make_FN(context.current_token_position); } +{FNTY} { return yy::parser::make_FNTY(context.current_token_position); } +{IF} { return yy::parser::make_IF(context.current_token_position); } +{INT} { return yy::parser::make_INT(context.current_token_position); } +{MATCH} { return yy::parser::make_MATCH(context.current_token_position); } +{NOT} { return yy::parser::make_NOT(context.current_token_position); } +{OR} { return yy::parser::make_OR(context.current_token_position); } +{RETURN} { return yy::parser::make_RETURN(context.current_token_position); } +{STRUCT} { return yy::parser::make_STRUCT(context.current_token_position); } +{TRUE} { return yy::parser::make_TRUE(context.current_token_position); } +{TYPE} { return yy::parser::make_TYPE(context.current_token_position); } +{VAR} { return yy::parser::make_VAR(context.current_token_position); } +{WHILE} { return yy::parser::make_WHILE(context.current_token_position); } -"=" return yy::parser::make_EQUAL(context.currentTokenPosition); -"-" return yy::parser::make_MINUS(context.currentTokenPosition); -"+" return yy::parser::make_PLUS(context.currentTokenPosition); -"*" return yy::parser::make_STAR(context.currentTokenPosition); -"/" return yy::parser::make_SLASH(context.currentTokenPosition); -"(" return yy::parser::make_LEFT_PARENTHESIS(context.currentTokenPosition); -")" return yy::parser::make_RIGHT_PARENTHESIS(context.currentTokenPosition); -"{" return yy::parser::make_LEFT_CURLY_BRACE(context.currentTokenPosition); -"}" return yy::parser::make_RIGHT_CURLY_BRACE(context.currentTokenPosition); -"[" return yy::parser::make_LEFT_SQUARE_BRACKET(context.currentTokenPosition); -"]" return yy::parser::make_RIGHT_SQUARE_BRACKET(context.currentTokenPosition); -"." return yy::parser::make_PERIOD(context.currentTokenPosition); -"," return yy::parser::make_COMMA(context.currentTokenPosition); -";" return yy::parser::make_SEMICOLON(context.currentTokenPosition); -":" return yy::parser::make_COLON(context.currentTokenPosition); +"=" return yy::parser::make_EQUAL(context.current_token_position); +"-" return yy::parser::make_MINUS(context.current_token_position); +"+" return yy::parser::make_PLUS(context.current_token_position); +"*" return yy::parser::make_STAR(context.current_token_position); +"/" return yy::parser::make_SLASH(context.current_token_position); +"(" return yy::parser::make_LEFT_PARENTHESIS(context.current_token_position); +")" return yy::parser::make_RIGHT_PARENTHESIS(context.current_token_position); +"{" return yy::parser::make_LEFT_CURLY_BRACE(context.current_token_position); +"}" return yy::parser::make_RIGHT_CURLY_BRACE(context.current_token_position); +"[" return yy::parser::make_LEFT_SQUARE_BRACKET(context.current_token_position); +"]" return yy::parser::make_RIGHT_SQUARE_BRACKET(context.current_token_position); +"." return yy::parser::make_PERIOD(context.current_token_position); +"," return yy::parser::make_COMMA(context.current_token_position); +";" return yy::parser::make_SEMICOLON(context.current_token_position); +":" return yy::parser::make_COLON(context.current_token_position); {identifier} { int n = strlen(yytext); auto r = reinterpret_cast(malloc((n + 1) * sizeof(char))); strncpy(r, yytext, n + 1); - return yy::parser::make_identifier(r, context.currentTokenPosition); + return yy::parser::make_identifier(r, context.current_token_position); } {integer_literal} { auto r = atof(yytext); - return yy::parser::make_integer_literal(r, context.currentTokenPosition); + return yy::parser::make_integer_literal(r, context.current_token_position); } {ONE_LINE_COMMENT} { // Advance end by 1 line, resetting the column to zero. - context.currentTokenPosition.lines(1); + context.current_token_position.lines(1); // Make the span empty by setting start to end. - context.currentTokenPosition.step(); + context.current_token_position.step(); } {horizontal_whitespace}+ { // Make the span empty by setting start to end. - context.currentTokenPosition.step(); + context.current_token_position.step(); } \n+ { // Advance end by yyleng lines, resetting the column to zero. - context.currentTokenPosition.lines(yyleng); + context.current_token_position.lines(yyleng); // Make the span empty by setting start to end. - context.currentTokenPosition.step(); + context.current_token_position.step(); } . { - std::cerr << context.currentTokenPosition << ": invalid character '" + std::cerr << context.current_token_position << ": invalid character '" << yytext[0] << "' in source file." << std::endl; std::exit(1); } <> { // A more modern Bison would give us make_EOF. - return yy::parser::make_END_OF_FILE(context.currentTokenPosition); + return yy::parser::make_END_OF_FILE(context.current_token_position); } %% diff --git a/executable_semantics/syntax/parse.cpp b/executable_semantics/syntax/parse.cpp index 61a12064b126..bbeb80595c68 100644 --- a/executable_semantics/syntax/parse.cpp +++ b/executable_semantics/syntax/parse.cpp @@ -17,29 +17,29 @@ namespace Carbon { // Returns an abstract representation of the program contained in the // well-formed input file, or if the file was malformed, a description of the // problem. -auto parse(const std::string& inputFileName) +auto parse(const std::string& input_file_name) -> std::variant { - yyin = fopen(inputFileName.c_str(), "r"); + yyin = fopen(input_file_name.c_str(), "r"); if (yyin == nullptr) { - std::cerr << "Error opening '" << inputFileName + std::cerr << "Error opening '" << input_file_name << "': " << std::strerror(errno) << std::endl; exit(1); } - std::optional parsedInput = std::nullopt; - ParseAndLexContext context(inputFileName); + std::optional parsed_input = std::nullopt; + ParseAndLexContext context(input_file_name); - auto syntaxErrorCode = yy::parser(parsedInput, context)(); - if (syntaxErrorCode != 0) { - return syntaxErrorCode; + auto syntax_error_code = yy::parser(parsed_input, context)(); + if (syntax_error_code != 0) { + return syntax_error_code; } - if (parsedInput == std::nullopt) { + if (parsed_input == std::nullopt) { std::cerr << "Internal error: parser validated syntax yet didn't produce " "an AST.\n"; exit(1); } - return *parsedInput; + return *parsed_input; } } // namespace Carbon diff --git a/executable_semantics/syntax/parse.h b/executable_semantics/syntax/parse.h index 592b70b1bd87..1b4936fb54dc 100644 --- a/executable_semantics/syntax/parse.h +++ b/executable_semantics/syntax/parse.h @@ -17,7 +17,7 @@ using SyntaxErrorCode = int; // Returns the AST representing the contents of the named file, or an error code // if parsing fails. -auto parse(const std::string& inputFileName) +auto parse(const std::string& input_file_name) -> std::variant; } // namespace Carbon diff --git a/executable_semantics/syntax/parse_and_lex_context.cpp b/executable_semantics/syntax/parse_and_lex_context.cpp index 99d5a086352f..150abd0b6e56 100644 --- a/executable_semantics/syntax/parse_and_lex_context.cpp +++ b/executable_semantics/syntax/parse_and_lex_context.cpp @@ -13,7 +13,8 @@ // the given line, to standard error. auto Carbon::ParseAndLexContext::PrintDiagnostic(const std::string& message, int line_num) -> void { - std::cerr << inputFileName << ":" << line_num << ": " << message << std::endl; + std::cerr << input_file_name << ":" << line_num << ": " << message + << std::endl; exit(-1); // TODO: do we really want this here? It makes the comment and the // name a lie, and renders some of the other yyparse() result // propagation code moot. diff --git a/executable_semantics/syntax/parse_and_lex_context.h b/executable_semantics/syntax/parse_and_lex_context.h index 247adc3689f2..8fc0965286a5 100644 --- a/executable_semantics/syntax/parse_and_lex_context.h +++ b/executable_semantics/syntax/parse_and_lex_context.h @@ -17,19 +17,20 @@ namespace Carbon { class ParseAndLexContext { public: // Creates an instance analyzing the given input file. - ParseAndLexContext(const std::string& inputFile) : inputFileName(inputFile) {} + ParseAndLexContext(const std::string& input_file) + : input_file_name(input_file) {} // Writes a syntax error diagnostic, containing message, for the input file at // the given line, to standard error. - auto PrintDiagnostic(const std::string& message, int lineNumber) -> void; + auto PrintDiagnostic(const std::string& message, int line_number) -> void; // The source range of the token being (or just) lex'd. - yy::location currentTokenPosition; + yy::location current_token_position; private: // A path to the file processed, relative to the current working directory // when *this is called. - const std::string inputFileName; + const std::string input_file_name; }; } // namespace Carbon diff --git a/executable_semantics/syntax/parser.ypp b/executable_semantics/syntax/parser.ypp index 3706ce436740..cfff4e876c70 100644 --- a/executable_semantics/syntax/parser.ypp +++ b/executable_semantics/syntax/parser.ypp @@ -26,7 +26,7 @@ // thus available to its methods. // "out" parameter passed to the parser, where the AST is written. -%parse-param {std::optional& parsedProgram} +%parse-param {std::optional& parsed_program} // "inout" parameter passed to both the parser and the lexer. %param {Carbon::ParseAndLexContext& context} @@ -151,7 +151,7 @@ void yy::parser::error( %locations %% input: declaration_list - { parsedProgram = $1; } + { parsed_program = $1; } ; pattern: expression