Rename variables to camel case

This commit is contained in:
augustin64 2024-01-10 15:42:30 +01:00
parent df22a7f065
commit 66586a1427
8 changed files with 85 additions and 85 deletions

View File

@ -242,10 +242,10 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
return type;
} break;
case NodeType::Assignment: {
Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data);
Token identifier_token = get<Token>(node.children[0]);
string identifier = get<string>(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<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data);
Token identifier_token = get<Token>(node.children[0]);
string identifier = get<string>(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;
}

View File

@ -148,9 +148,9 @@ EvalResult eval(Node &ast, Memory &memory, vector<string> history) {
return {};
} break;
case NodeType::FunctionPrototype: {
Token retTypeTok = get<Token>(node.children[0]);
Token identifierTok = get<Token>(node.children[1]);
string identifier = get<string>(identifierTok.data);
Token ret_type_token = get<Token>(node.children[0]);
Token identifier_token = get<Token>(node.children[1]);
string identifier = get<string>(identifier_token.data);
FunctionPrototype prototype = make_fn_prototype(node);
@ -162,9 +162,9 @@ EvalResult eval(Node &ast, Memory &memory, vector<string> history) {
return {};
} break;
case NodeType::FunctionDeclaration: {
Token retTypeTok = get<Token>(node.children[0]);
Token identifierTok = get<Token>(node.children[1]);
string identifier = get<string>(identifierTok.data);
Token ret_type_token = get<Token>(node.children[0]);
Token identifier_token = get<Token>(node.children[1]);
string identifier = get<string>(identifier_token.data);
FunctionPrototype prototype = make_fn_prototype(node);
@ -180,35 +180,35 @@ EvalResult eval(Node &ast, Memory &memory, vector<string> history) {
return {};
} break;
case NodeType::FunctionCall: {
Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data);
Token identifier_token = get<Token>(node.children[0]);
string identifier = get<string>(identifier_token.data);
vector<Node> args = get<InnerNode>(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<Function>(var.value);
FunctionPrototype& prototype = get<FunctionPrototype>(var.type.data);
vector<EvalResult> argValues = {};
vector<EvalResult> args_values = {};
for (Node arg : args) {
argValues.push_back(eval(arg, memory));
args_values.push_back(eval(arg, memory));
}
EvalResult res = {};
if (holds_alternative<UserFunction>(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<string> 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<string> history) {
}
} break;
case NodeType::Declaration: {
Token typeTok = get<Token>(node.children[0]);
Token identifierTok = get<Token>(node.children[1]);
string typeName = get<string>(typeTok.data);
string identifier = get<string>(identifierTok.data);
Token type_token = get<Token>(node.children[0]);
Token identifier_token = get<Token>(node.children[1]);
string type_name = get<string>(type_token.data);
string identifier = get<string>(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<Token>(node.children[0]);
Token identifierTok = get<Token>(node.children[1]);
string typeName = get<string>(typeTok.data);
string identifier = get<string>(identifierTok.data);
Token type_token = get<Token>(node.children[0]);
Token identifier_token = get<Token>(node.children[1]);
string type_name = get<string>(type_token.data);
string identifier = get<string>(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<string> history) {
return value;
} break;
case NodeType::Assignment: {
Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data);
Token identifier_token = get<Token>(node.children[0]);
string identifier = get<string>(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<string> history) {
return value;
} break;
case NodeType::LIncr: {
Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data);
Token identifier_token = get<Token>(node.children[0]);
string identifier = get<string>(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<int>(var.value) + 1);
@ -495,31 +495,31 @@ EvalResult eval(Node &ast, Memory &memory, vector<string> history) {
}
}
case NodeType::RIncr: {
Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data);
Token identifier_token = get<Token>(node.children[0]);
string identifier = get<string>(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<int>(var.value);
memory.update(identifier, oldVal + 1);
return oldVal;
int old_value = get<int>(var.value);
memory.update(identifier, old_value + 1);
return old_value;
}
else {
return var.value;
}
}
case NodeType::LDecr: {
Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data);
Token identifier_token = get<Token>(node.children[0]);
string identifier = get<string>(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<int>(var.value) - 1);
@ -530,18 +530,18 @@ EvalResult eval(Node &ast, Memory &memory, vector<string> history) {
}
}
case NodeType::RDecr: {
Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data);
Token identifier_token = get<Token>(node.children[0]);
string identifier = get<string>(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<int>(var.value);
memory.update(identifier, oldVal - 1);
return oldVal;
int old_value = get<int>(var.value);
memory.update(identifier, old_value - 1);
return old_value;
}
else {
return var.value;

View File

@ -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) {

View File

@ -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<InnerNode>(node)) {
const InnerNode& innerNode = get<InnerNode>(node);
const InnerNode& inner_node = get<InnerNode>(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<Token>(node);

View File

@ -10,7 +10,7 @@ regex INT_REGEX ("\\d+");
regex DOUBLE_REGEX ("\\d+\\.\\d*|\\d*\\.\\d+");
regex IDENTIFIER_REGEX ("[A-Za-z_]\\w*");
vector<tuple<string, TokenType>> simpleTokens = {
vector<tuple<string, TokenType>> simple_tokens = {
{ "if", TokenType::If },
{ "else", TokenType::Else },
{ "while", TokenType::While },
@ -236,11 +236,11 @@ vector<Token> tokenize(vector<string> 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;
}

View File

@ -36,20 +36,20 @@ Type string_to_type(string type_name) {
}
FunctionPrototype make_fn_prototype(InnerNode node) {
Token retTypeTok = get<Token>(node.children[0]);
string retTypeName = get<string>(retTypeTok.data);
Type retType = string_to_type(retTypeName);
Token ret_type_token = get<Token>(node.children[0]);
string ret_type_name = get<string>(ret_type_token.data);
Type ret_type = string_to_type(ret_type_name);
vector<Node> args = get<InnerNode>(node.children[2]).children;
FunctionPrototype prototype = { { retType, "" } };
FunctionPrototype prototype = { { ret_type, "" } };
for (Node arg : args) {
InnerNode argInner = get<InnerNode>(arg);
Token typeTok = get<Token>(argInner.children[0]);
Token identifierTok = get<Token>(argInner.children[1]);
string typeName = get<string>(typeTok.data);
string identifier = get<string>(identifierTok.data);
Type type = string_to_type(typeName);
InnerNode arg_inner = get<InnerNode>(arg);
Token type_token = get<Token>(arg_inner.children[0]);
Token identifier_token = get<Token>(arg_inner.children[1]);
string type_name = get<string>(type_token.data);
string identifier = get<string>(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<string> split_string(const string& input, char delimiter) {
vector<string> 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);
}

View File

@ -14,7 +14,7 @@ int main() {
"int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else", "while", "for", "break", "continue", "return", ","
};
vector<TokenType> expectedTypes = {
vector<TokenType> 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<Token> 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 */

View File

@ -12,7 +12,7 @@ int main() {
_TEST_PRESENTATION("Variables");
_TEST_ASSERT(
_TEST_NO_EXCEPTION(get<int>(execute("int uneVariableFarfelue__ = 12;")) == 12),
_TEST_NO_EXCEPTION(get<int>(execute("int __une_variable_farfelue__ = 12;")) == 12),
"Déclaration avec assignement",
true
);