From 66586a1427a519798b0073670a6e0f8680fe8406 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 10 Jan 2024 15:42:30 +0100 Subject: [PATCH] Rename variables to camel case --- src/analysis.cpp | 12 +++--- src/interpreter.cpp | 96 ++++++++++++++++++++++----------------------- src/memory.cpp | 10 ++--- src/parser.cpp | 10 ++--- src/tokenize.cpp | 10 ++--- src/utils.cpp | 24 ++++++------ test/tokenize.cpp | 6 +-- test/variables.cpp | 2 +- 8 files changed, 85 insertions(+), 85 deletions(-) diff --git a/src/analysis.cpp b/src/analysis.cpp index 77db96b..21d5113 100644 --- a/src/analysis.cpp +++ b/src/analysis.cpp @@ -242,10 +242,10 @@ AnalysisResult analyze(Node &ast, Memory &memory) { return type; } break; case NodeType::Assignment: { - Token identifierTok = get(node.children[0]); - string identifier = get(identifierTok.data); + Token identifier_token = get(node.children[0]); + string identifier = get(identifier_token.data); if (!memory.contains(identifier)) - throw TypeError(ErrorType::UnknownIdentifier, identifierTok.pos, identifier); + throw TypeError(ErrorType::UnknownIdentifier, identifier_token.pos, identifier); Type type = memory.get(identifier).type; AnalysisResult res = analyze(node.children[1], memory); @@ -258,11 +258,11 @@ AnalysisResult analyze(Node &ast, Memory &memory) { case NodeType::RIncr: case NodeType::LDecr: case NodeType::RDecr: { - Token identifierTok = get(node.children[0]); - string identifier = get(identifierTok.data); + Token identifier_token = get(node.children[0]); + string identifier = get(identifier_token.data); if (!memory.contains(identifier)) - throw TypeError(ErrorType::UnknownIdentifier, identifierTok.pos, identifier); + throw TypeError(ErrorType::UnknownIdentifier, identifier_token.pos, identifier); return memory.get(identifier).type; } diff --git a/src/interpreter.cpp b/src/interpreter.cpp index 1ee7abd..5181935 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -148,9 +148,9 @@ EvalResult eval(Node &ast, Memory &memory, vector history) { return {}; } break; case NodeType::FunctionPrototype: { - Token retTypeTok = get(node.children[0]); - Token identifierTok = get(node.children[1]); - string identifier = get(identifierTok.data); + Token ret_type_token = get(node.children[0]); + Token identifier_token = get(node.children[1]); + string identifier = get(identifier_token.data); FunctionPrototype prototype = make_fn_prototype(node); @@ -162,9 +162,9 @@ EvalResult eval(Node &ast, Memory &memory, vector history) { return {}; } break; case NodeType::FunctionDeclaration: { - Token retTypeTok = get(node.children[0]); - Token identifierTok = get(node.children[1]); - string identifier = get(identifierTok.data); + Token ret_type_token = get(node.children[0]); + Token identifier_token = get(node.children[1]); + string identifier = get(identifier_token.data); FunctionPrototype prototype = make_fn_prototype(node); @@ -180,35 +180,35 @@ EvalResult eval(Node &ast, Memory &memory, vector history) { return {}; } break; case NodeType::FunctionCall: { - Token identifierTok = get(node.children[0]); - string identifier = get(identifierTok.data); + Token identifier_token = get(node.children[0]); + string identifier = get(identifier_token.data); vector args = get(node.children[1]).children; MemoryVar& var = memory.get(identifier); if (!var.initialized) - throw RuntimeError(ErrorType::UninitializedIdentifier, identifierTok.pos, memory.get_trace(identifierTok.pos), identifier); + throw RuntimeError(ErrorType::UninitializedIdentifier, identifier_token.pos, memory.get_trace(identifier_token.pos), identifier); Function& fn = get(var.value); FunctionPrototype& prototype = get(var.type.data); - vector argValues = {}; + vector args_values = {}; for (Node arg : args) { - argValues.push_back(eval(arg, memory)); + args_values.push_back(eval(arg, memory)); } EvalResult res = {}; if (holds_alternative(fn)) { - memory.add_scope(ScopeType::Function, &var, identifierTok.pos); + memory.add_scope(ScopeType::Function, &var, identifier_token.pos); try { for (size_t i = 1; i < prototype.size(); i++) { - ArgDefinition argDef = prototype[i]; - Type argType = get<0>(argDef); - string argName = get<1>(argDef); - memory.declare(argName, argType); - memory.update(argName, cast_from_type(argType, argValues[i - 1])); + ArgDefinition arg_definition = prototype[i]; + Type arg_type = get<0>(arg_definition); + string arg_name = get<1>(arg_definition); + memory.declare(arg_name, arg_type); + memory.update(arg_name, cast_from_type(arg_type, args_values[i - 1])); } try { @@ -216,7 +216,7 @@ EvalResult eval(Node &ast, Memory &memory, vector history) { // Tmp: no flow control if (get<0>(prototype[0]).type != TypeType::Void) { - throw ControlError(ErrorType::ControlReachesEndOfNonVoidFn, identifierTok.pos); + throw ControlError(ErrorType::ControlReachesEndOfNonVoidFn, identifier_token.pos); } } catch (const ReturnException& e) { @@ -429,24 +429,24 @@ EvalResult eval(Node &ast, Memory &memory, vector history) { } } break; case NodeType::Declaration: { - Token typeTok = get(node.children[0]); - Token identifierTok = get(node.children[1]); - string typeName = get(typeTok.data); - string identifier = get(identifierTok.data); + Token type_token = get(node.children[0]); + Token identifier_token = get(node.children[1]); + string type_name = get(type_token.data); + string identifier = get(identifier_token.data); - Type type = string_to_type(typeName); + Type type = string_to_type(type_name); memory.declare(identifier, type); return {}; } break; case NodeType::AssignedDeclaration: { - Token typeTok = get(node.children[0]); - Token identifierTok = get(node.children[1]); - string typeName = get(typeTok.data); - string identifier = get(identifierTok.data); + Token type_token = get(node.children[0]); + Token identifier_token = get(node.children[1]); + string type_name = get(type_token.data); + string identifier = get(identifier_token.data); EvalResult value = eval(node.children[2], memory); - Type type = string_to_type(typeName); + Type type = string_to_type(type_name); memory.declare(identifier, type); if (type.type == TypeType::Int) { @@ -459,8 +459,8 @@ EvalResult eval(Node &ast, Memory &memory, vector history) { return value; } break; case NodeType::Assignment: { - Token identifierTok = get(node.children[0]); - string identifier = get(identifierTok.data); + Token identifier_token = get(node.children[0]); + string identifier = get(identifier_token.data); EvalResult value = eval(node.children[1], memory); Type type = memory.get(identifier).type; @@ -478,13 +478,13 @@ EvalResult eval(Node &ast, Memory &memory, vector history) { return value; } break; case NodeType::LIncr: { - Token identifierTok = get(node.children[0]); - string identifier = get(identifierTok.data); + Token identifier_token = get(node.children[0]); + string identifier = get(identifier_token.data); MemoryVar& var = memory.get(identifier); if (!var.initialized) { - throw RuntimeError(ErrorType::UninitializedIdentifier, identifierTok.pos, memory.get_trace(identifierTok.pos), identifier); + throw RuntimeError(ErrorType::UninitializedIdentifier, identifier_token.pos, memory.get_trace(identifier_token.pos), identifier); } else if (var.type.type == TypeType::Int) { memory.update(identifier, get(var.value) + 1); @@ -495,31 +495,31 @@ EvalResult eval(Node &ast, Memory &memory, vector history) { } } case NodeType::RIncr: { - Token identifierTok = get(node.children[0]); - string identifier = get(identifierTok.data); + Token identifier_token = get(node.children[0]); + string identifier = get(identifier_token.data); MemoryVar& var = memory.get(identifier); if (!var.initialized) { - throw RuntimeError(ErrorType::UninitializedIdentifier, identifierTok.pos, memory.get_trace(identifierTok.pos), identifier); + throw RuntimeError(ErrorType::UninitializedIdentifier, identifier_token.pos, memory.get_trace(identifier_token.pos), identifier); } else if (var.type.type == TypeType::Int) { - int oldVal = get(var.value); - memory.update(identifier, oldVal + 1); - return oldVal; + int old_value = get(var.value); + memory.update(identifier, old_value + 1); + return old_value; } else { return var.value; } } case NodeType::LDecr: { - Token identifierTok = get(node.children[0]); - string identifier = get(identifierTok.data); + Token identifier_token = get(node.children[0]); + string identifier = get(identifier_token.data); MemoryVar& var = memory.get(identifier); if (!var.initialized) { - throw RuntimeError(ErrorType::UninitializedIdentifier, identifierTok.pos, memory.get_trace(identifierTok.pos), identifier); + throw RuntimeError(ErrorType::UninitializedIdentifier, identifier_token.pos, memory.get_trace(identifier_token.pos), identifier); } else if (var.type.type == TypeType::Int) { memory.update(identifier, get(var.value) - 1); @@ -530,18 +530,18 @@ EvalResult eval(Node &ast, Memory &memory, vector history) { } } case NodeType::RDecr: { - Token identifierTok = get(node.children[0]); - string identifier = get(identifierTok.data); + Token identifier_token = get(node.children[0]); + string identifier = get(identifier_token.data); MemoryVar& var = memory.get(identifier); if (!var.initialized) { - throw RuntimeError(ErrorType::UninitializedIdentifier, identifierTok.pos, memory.get_trace(identifierTok.pos), identifier); + throw RuntimeError(ErrorType::UninitializedIdentifier, identifier_token.pos, memory.get_trace(identifier_token.pos), identifier); } else if (var.type.type == TypeType::Int) { - int oldVal = get(var.value); - memory.update(identifier, oldVal - 1); - return oldVal; + int old_value = get(var.value); + memory.update(identifier, old_value - 1); + return old_value; } else { return var.value; diff --git a/src/memory.cpp b/src/memory.cpp index 90c4d03..6de4257 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -61,11 +61,11 @@ bool Memory::contains_top(string identifier) { void Memory::add_scope(ScopeType type, MemoryVar* fn, CodePosition entry_pos) { Scope& top = scopes.back(); - Scope& newScope = scopes.emplace_back(); - newScope.depth = top.depth + 1; - newScope.type = type; - newScope.fn = fn; - newScope.entry_pos = entry_pos; + Scope& new_scope = scopes.emplace_back(); + new_scope.depth = top.depth + 1; + new_scope.type = type; + new_scope.fn = fn; + new_scope.entry_pos = entry_pos; } void Memory::remove_scope(void) { diff --git a/src/parser.cpp b/src/parser.cpp index 23534ba..d9bee39 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -23,9 +23,9 @@ const char* _debug_ast_node_names[] = { }; void _debug_print_tree(const Node& node, int depth, const string& prefix) { if (holds_alternative(node)) { - const InnerNode& innerNode = get(node); + const InnerNode& inner_node = get(node); - cout << prefix << _debug_ast_node_names[int(innerNode.type)] << "\n"; + cout << prefix << _debug_ast_node_names[int(inner_node.type)] << "\n"; string new_prefix = prefix; size_t pos = new_prefix.find("└──"); @@ -40,9 +40,9 @@ void _debug_print_tree(const Node& node, int depth, const string& prefix) { pos = new_prefix.find("├──", pos + 6); } - for (size_t i = 0; i < innerNode.children.size(); ++i) { - string childPrefix = (i == innerNode.children.size() - 1) ? "└── " : "├── "; - _debug_print_tree(innerNode.children[i], depth + 1, new_prefix + childPrefix); + for (size_t i = 0; i < inner_node.children.size(); ++i) { + string child_prefix = (i == inner_node.children.size() - 1) ? "└── " : "├── "; + _debug_print_tree(inner_node.children[i], depth + 1, new_prefix + child_prefix); } } else { const Token& token = get(node); diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 3d0c943..d872616 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -10,7 +10,7 @@ regex INT_REGEX ("\\d+"); regex DOUBLE_REGEX ("\\d+\\.\\d*|\\d*\\.\\d+"); regex IDENTIFIER_REGEX ("[A-Za-z_]\\w*"); -vector> simpleTokens = { +vector> simple_tokens = { { "if", TokenType::If }, { "else", TokenType::Else }, { "while", TokenType::While }, @@ -236,11 +236,11 @@ vector tokenize(vector input, int initial_line) { } bool matched = false; - for (auto simpleToken: simpleTokens) { - if (str.starts_with(get<0>(simpleToken))) { - Token token = { .type = get<1>(simpleToken), .pos = pos }; + for (auto simple_token: simple_tokens) { + if (str.starts_with(get<0>(simple_token))) { + Token token = { .type = get<1>(simple_token), .pos = pos }; tokens.emplace_back(token); - j += get<0>(simpleToken).length(); + j += get<0>(simple_token).length(); matched = true; break; } diff --git a/src/utils.cpp b/src/utils.cpp index 022154d..569dfb5 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -36,20 +36,20 @@ Type string_to_type(string type_name) { } FunctionPrototype make_fn_prototype(InnerNode node) { - Token retTypeTok = get(node.children[0]); - string retTypeName = get(retTypeTok.data); - Type retType = string_to_type(retTypeName); + Token ret_type_token = get(node.children[0]); + string ret_type_name = get(ret_type_token.data); + Type ret_type = string_to_type(ret_type_name); vector args = get(node.children[2]).children; - FunctionPrototype prototype = { { retType, "" } }; + FunctionPrototype prototype = { { ret_type, "" } }; for (Node arg : args) { - InnerNode argInner = get(arg); - Token typeTok = get(argInner.children[0]); - Token identifierTok = get(argInner.children[1]); - string typeName = get(typeTok.data); - string identifier = get(identifierTok.data); - Type type = string_to_type(typeName); + InnerNode arg_inner = get(arg); + Token type_token = get(arg_inner.children[0]); + Token identifier_token = get(arg_inner.children[1]); + string type_name = get(type_token.data); + string identifier = get(identifier_token.data); + Type type = string_to_type(type_name); prototype.push_back({ type, identifier }); } @@ -59,9 +59,9 @@ FunctionPrototype make_fn_prototype(InnerNode node) { vector split_string(const string& input, char delimiter) { vector tokens; string token; - istringstream tokenStream(input); + istringstream token_stream(input); - while (getline(tokenStream, token, delimiter)) { + while (getline(token_stream, token, delimiter)) { tokens.push_back(token); } diff --git a/test/tokenize.cpp b/test/tokenize.cpp index b40aebb..95fc9bf 100644 --- a/test/tokenize.cpp +++ b/test/tokenize.cpp @@ -14,7 +14,7 @@ int main() { "int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else", "while", "for", "break", "continue", "return", "," }; - vector expectedTypes = { + vector expected_types = { TokenType::Identifier, TokenType::Identifier, TokenType::Equal, TokenType::Identifier, TokenType::DoublePlus, TokenType::DoubleMinus, TokenType::DoubleEqual, TokenType::Land, TokenType::Lor, TokenType::Lt, TokenType::Gt, TokenType::Leq, TokenType::Geq, TokenType::NotEqual, @@ -26,8 +26,8 @@ int main() { for (size_t i = 0; i < inputs.size(); i++) { vector tokens = tokenize({ inputs[i] }); - _TEST_ASSERT(tokens.size() == 1, _debug_get_token_type_name(expectedTypes[i]).c_str(), false); - _TEST_ASSERT(tokens[0].type == expectedTypes[i], _debug_get_token_type_name(expectedTypes[i]).c_str(), true); + _TEST_ASSERT(tokens.size() == 1, _debug_get_token_type_name(expected_types[i]).c_str(), false); + _TEST_ASSERT(tokens[0].type == expected_types[i], _debug_get_token_type_name(expected_types[i]).c_str(), true); } /* Complex input */ diff --git a/test/variables.cpp b/test/variables.cpp index 6938560..3b1e6cc 100644 --- a/test/variables.cpp +++ b/test/variables.cpp @@ -12,7 +12,7 @@ int main() { _TEST_PRESENTATION("Variables"); _TEST_ASSERT( - _TEST_NO_EXCEPTION(get(execute("int uneVariableFarfelue__ = 12;")) == 12), + _TEST_NO_EXCEPTION(get(execute("int __une_variable_farfelue__ = 12;")) == 12), "Déclaration avec assignement", true );