From 475d4332aab6ba2998c3f9034aa0a9c75ef70944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9renger=20Dalle-Cort?= Date: Tue, 22 Oct 2024 09:14:51 -0400 Subject: [PATCH 1/5] refactor(Node): remove unused UpdateResult enum --- src/ndbl/core/Node.h | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/ndbl/core/Node.h b/src/ndbl/core/Node.h index 2f3475e77..cef8ca517 100644 --- a/src/ndbl/core/Node.h +++ b/src/ndbl/core/Node.h @@ -24,15 +24,6 @@ namespace ndbl // forward declarations class Graph; - /** - * Distinguish between all possible update_world_matrix result - */ - enum class UpdateResult - { - SUCCES_WITHOUT_CHANGES, - SUCCESS_WITH_CHANGES, - }; - typedef int NodeFlags; enum NodeFlag_ { From 080c3bbfb829d9053b215579006b4e7bbed2cad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9renger=20Dalle-Cort?= Date: Tue, 22 Oct 2024 19:48:03 -0400 Subject: [PATCH 2/5] feat(tools::core): add Optional struct --- src/tools/core/Optional.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/tools/core/Optional.h diff --git a/src/tools/core/Optional.h b/src/tools/core/Optional.h new file mode 100644 index 000000000..a3206b509 --- /dev/null +++ b/src/tools/core/Optional.h @@ -0,0 +1,36 @@ +#pragma once + +#include "assertions.h" + +namespace tools +{ + // + // Struct to deal with optional returned values + // Implementation is very simple, it tries to follow std::optional interface, + // We only support pointers, and consider nullptr as an absence of value + // + template< typename TPtr, typename T = std::remove_pointer_t > + struct Optional + { + + static_assert( std::is_pointer_v, "Implemented for pointers only" ); + + inline constexpr Optional(): _value(nullptr) {} + inline constexpr Optional(nullptr_t): _value(nullptr){} + inline constexpr Optional(TPtr ptr): _value(ptr){} + inline constexpr Optional(const Optional& other): _value(other._value){} + inline constexpr TPtr operator->() { return value(); } + inline constexpr T& operator*() { return *value(); } + inline constexpr operator bool () { return has_value(); } + inline constexpr TPtr value_or_null() { return has_value() ? _value : nullptr; } + inline constexpr TPtr raw_ptr() { return _value; } + inline constexpr TPtr value() { VERIFY(has_value(), "no value hold"); return _value; } + inline constexpr bool has_value() const { return _value != nullptr; } + inline constexpr bool is_empty() const { return !has_value(); } + private: + TPtr _value; + }; + + struct S { }; + static_assert(sizeof(Optional) == 8, "Result size is incorrect" ); +} From 7429872c7b1d6092f05f70464834520fdb0b0185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9renger=20Dalle-Cort?= Date: Tue, 22 Oct 2024 19:48:37 -0400 Subject: [PATCH 3/5] refactor(Nodlang): use tools::Optional --- src/ndbl/core/language/Nodlang.cpp | 221 +++++++++--------- src/ndbl/core/language/Nodlang.h | 33 +-- .../Nodlang.parse_function_call.specs.cpp | 9 +- 3 files changed, 135 insertions(+), 128 deletions(-) diff --git a/src/ndbl/core/language/Nodlang.cpp b/src/ndbl/core/language/Nodlang.cpp index 567864d23..f2be36cff 100644 --- a/src/ndbl/core/language/Nodlang.cpp +++ b/src/ndbl/core/language/Nodlang.cpp @@ -192,9 +192,9 @@ bool Nodlang::parse(const std::string &_source_code, Graph *_graphNode) return false; } - Node* program = parse_program(); + Optional program = parse_program(); - if ( program == nullptr ) + if ( !program ) { LOG_WARNING("Parser", "Unable to generate program tree.\n") return false; @@ -241,7 +241,7 @@ int Nodlang::to_int(const std::string &_str) return stoi(_str); } -Slot* Nodlang::token_to_slot(Token _token) +Optional Nodlang::token_to_slot(Token _token) { if (_token.m_type == Token_t::identifier) { @@ -255,7 +255,7 @@ Slot* Nodlang::token_to_slot(Token _token) if ( !m_strict_mode ) { // Insert a VariableNodeRef with "any" type - LOG_WARNING( "Parser", "%s is not declared (strict mode), abstract graph can be generated but compilation will fail.\n", + LOG_WARNING( "Parser", "%s is not declared (strict mode), abstract graph can be generated but compilation will failure.\n", _token.word_to_string().c_str() ) VariableRefNode* ref = parser_state.graph->create_variable_ref(); ref->value()->set_token(_token ); @@ -288,10 +288,11 @@ Slot* Nodlang::token_to_slot(Token _token) return literal->value_out(); } -Slot *Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot& _left) +Optional Nodlang::parse_binary_operator_expression(u8_t _precedence, Optional _left) { LOG_VERBOSE("Parser", "parse binary operation expr...\n") LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) + VERIFY(_left.has_value(), "It is required that _left is not empty") if (!parser_state.ribbon.can_eat(2)) { @@ -333,9 +334,9 @@ Slot *Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot& _left) // Parse right expression - Slot* right = parse_expression(ope->precedence); + Optional right = parse_expression(ope->precedence); - if ( right == nullptr ) + if ( !right ) { LOG_VERBOSE("Parser", "parseBinaryOperationExpression... " KO " (right expression is nullptr)\n") rollback_transaction(); @@ -344,15 +345,15 @@ Slot *Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot& _left) // Create a function signature according to ltype, rtype and operator word FunctionDescriptor* type = FunctionDescriptor::create(ope->identifier.c_str()); - type->push_arg( _left.property->get_type()); + type->push_arg( _left->property->get_type()); type->push_arg(right->property->get_type()); FunctionNode* binary_op = parser_state.graph->create_operator(type); binary_op->set_identifier_token( operator_token ); - binary_op->lvalue_in()->property->token().m_type = _left.property->token().m_type; + binary_op->lvalue_in()->property->token().m_type = _left->property->token().m_type; binary_op->rvalue_in()->property->token().m_type = right->property->token().m_type; - parser_state.graph->connect_or_merge( _left, *binary_op->lvalue_in()); + parser_state.graph->connect_or_merge( *_left, *binary_op->lvalue_in()); parser_state.graph->connect_or_merge( *right, *binary_op->rvalue_in() ); commit_transaction(); @@ -360,7 +361,7 @@ Slot *Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot& _left) return binary_op->value_out(); } -Slot *Nodlang::parse_unary_operator_expression(u8_t _precedence) +Optional Nodlang::parse_unary_operator_expression(u8_t _precedence) { LOG_VERBOSE("Parser", "parseUnaryOperationExpression...\n") LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) @@ -383,14 +384,14 @@ Slot *Nodlang::parse_unary_operator_expression(u8_t _precedence) } // Parse expression after the operator - Slot* out_atomic = parse_atomic_expression(); + Optional out_atomic = parse_atomic_expression(); - if ( out_atomic == nullptr ) + if ( !out_atomic ) { out_atomic = parse_parenthesis_expression(); } - if ( out_atomic == nullptr ) + if ( !out_atomic ) { LOG_VERBOSE("Parser", "parseUnaryOperationExpression... " KO " (right expression is nullptr)\n") rollback_transaction(); @@ -413,7 +414,7 @@ Slot *Nodlang::parse_unary_operator_expression(u8_t _precedence) return node->value_out(); } -Slot* Nodlang::parse_atomic_expression() +Optional Nodlang::parse_atomic_expression() { LOG_VERBOSE("Parser", "parse atomic expr... \n") @@ -433,7 +434,7 @@ Slot* Nodlang::parse_atomic_expression() return nullptr; } - if ( Slot* result = token_to_slot(token) ) + if ( Optional result = token_to_slot(token) ) { commit_transaction(); LOG_VERBOSE("Parser", "parse atomic expr... " OK "\n") @@ -446,7 +447,7 @@ Slot* Nodlang::parse_atomic_expression() return nullptr; } -Slot *Nodlang::parse_parenthesis_expression() +Optional Nodlang::parse_parenthesis_expression() { LOG_VERBOSE("Parser", "parse parenthesis expr...\n") LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) @@ -466,8 +467,8 @@ Slot *Nodlang::parse_parenthesis_expression() return nullptr; } - Slot* result = parse_expression(); - if ( result != nullptr ) + Optional result = parse_expression(); + if ( result ) { Token token = parser_state.ribbon.eat(); if (token.m_type != Token_t::parenthesis_close) @@ -491,11 +492,11 @@ Slot *Nodlang::parse_parenthesis_expression() return result; } -Node* Nodlang::parse_instr() +Optional Nodlang::parse_instr() { start_transaction(); - Slot* expression_out = parse_expression(); + Optional expression_out = parse_expression(); if ( !expression_out ) { @@ -541,7 +542,7 @@ Node* Nodlang::parse_instr() return output_node; } -Node* Nodlang::parse_program() +Optional Nodlang::parse_program() { start_transaction(); @@ -566,7 +567,7 @@ Node* Nodlang::parse_program() return root; } -Node* Nodlang::parse_scope( Slot& _parent_scope_slot ) +Optional Nodlang::parse_scope(Optional _parent_scope_slot ) { auto scope_begin_token = parser_state.ribbon.eat_if(Token_t::scope_begin); if ( !scope_begin_token ) @@ -574,23 +575,25 @@ Node* Nodlang::parse_scope( Slot& _parent_scope_slot ) return {}; } + VERIFY(_parent_scope_slot.has_value(), "Required!") + start_transaction(); - Node* curr_scope_node = get_current_scope_node(); - Node* new_scope_node = parser_state.graph->create_scope(); - Scope* new_scope = new_scope_node->get_component(); + Optional curr_scope_node = get_current_scope_node(); + Optional new_scope_node = parser_state.graph->create_scope(); + Optional new_scope = new_scope_node->get_component(); // Handle nested scopes - parser_state.graph->connect( _parent_scope_slot, *new_scope_node->find_slot( SlotFlag_PARENT ), ConnectFlag_ALLOW_SIDE_EFFECTS ); + parser_state.graph->connect( *_parent_scope_slot, *new_scope_node->find_slot( SlotFlag_PARENT ), ConnectFlag_ALLOW_SIDE_EFFECTS ); - parser_state.scope.push( new_scope ); + parser_state.scope.push( new_scope.value() ); parse_code_block(); parser_state.scope.pop(); auto scope_end_token = parser_state.ribbon.eat_if(Token_t::scope_end); if ( !scope_end_token ) { - parser_state.graph->destroy( new_scope_node ); + parser_state.graph->destroy( new_scope_node.value() ); rollback_transaction(); return {}; } @@ -609,7 +612,7 @@ void Nodlang::parse_code_block() bool block_end_reached = false; while ( parser_state.ribbon.can_eat() && !block_end_reached ) { - if ( Node* instruction = parse_instr() ) + if (Optional instruction = parse_instr() ) { Slot* child_slot = get_current_scope_node()->find_slot( SlotFlag_CHILD | SlotFlag_NOT_FULL ); ASSERT(child_slot); @@ -623,7 +626,7 @@ void Nodlang::parse_code_block() parse_conditional_structure() || parse_for_loop() || parse_while_loop() || - parse_scope( *get_current_scope_node()->find_slot( SlotFlag_CHILD ) ) ) + parse_scope( get_current_scope_node()->find_slot( SlotFlag_CHILD ) ) ) {} else { @@ -636,7 +639,7 @@ void Nodlang::parse_code_block() : commit_transaction(); } -Slot* Nodlang::parse_expression(u8_t _precedence, Slot* _left_override) +Optional Nodlang::parse_expression(u8_t _precedence, Optional _left_override) { LOG_VERBOSE("Parser", "parse expr...\n") LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) @@ -644,13 +647,13 @@ Slot* Nodlang::parse_expression(u8_t _precedence, Slot* _left_override) if (!parser_state.ribbon.can_eat()) { LOG_VERBOSE("Parser", "parse expr..." KO " (unable to eat a single token)\n") - return _left_override; + return _left_override.value_or_null(); } /* Get the left-handed operand */ - Slot* left = _left_override; + Optional left = _left_override; if( !left ) { @@ -667,26 +670,30 @@ Slot* Nodlang::parse_expression(u8_t _precedence, Slot* _left_override) return left; } + if ( !left ) + { + LOG_VERBOSE("Parser", "parse expr... left is null, we return it\n") + return left; + } + /* Get the right-handed operand */ - if ( left ) + LOG_VERBOSE("Parser", "parse expr... left parsed, we parse right\n") + Optional expression_out = parse_binary_operator_expression(_precedence, left); + if ( expression_out ) { - LOG_VERBOSE("Parser", "parse expr... left parsed, we parse right\n") - Slot* expression_out = parse_binary_operator_expression(_precedence, *left); - if ( expression_out ) + if (!parser_state.ribbon.can_eat()) { - if (!parser_state.ribbon.can_eat()) - { - LOG_VERBOSE("Parser", "parse expr... " OK " right parsed (last token reached)\n") - return expression_out; - } - LOG_VERBOSE("Parser", "parse expr... " OK " right parsed, recursive call...\n") - return parse_expression(_precedence, expression_out); + LOG_VERBOSE("Parser", "parse expr... " OK " right parsed (last token reached)\n") + return expression_out.raw_ptr(); } + LOG_VERBOSE("Parser", "parse expr... " OK " right parsed, recursive call...\n") + return parse_expression(_precedence, expression_out); } - LOG_VERBOSE("Parser", "parse expr... left is nullptr, we return it\n") + LOG_VERBOSE("Parser", "parse expr... left only " OK "\n") + return left; } @@ -971,7 +978,7 @@ Token Nodlang::parse_token(const char* buffer, size_t buffer_size, size_t& globa return Token::s_null; } -Slot* Nodlang::parse_function_call() +Optional Nodlang::parse_function_call() { LOG_VERBOSE("Parser", "parse function call...\n") @@ -1018,10 +1025,10 @@ Slot* Nodlang::parse_function_call() while (!parsingError && parser_state.ribbon.can_eat() && parser_state.ribbon.peek().m_type != Token_t::parenthesis_close) { - Slot* expression_out = parse_expression(); - if ( expression_out != nullptr ) + Optional expression_out = parse_expression(); + if ( expression_out ) { - result_slots.push_back( expression_out ); + result_slots.push_back( expression_out.value() ); signature->push_arg( expression_out->property->get_type() ); parser_state.ribbon.eat_if(Token_t::list_separator); } @@ -1055,28 +1062,28 @@ Slot* Nodlang::parse_function_call() return fct_node->value_out(); } -Scope* Nodlang::get_current_scope() +Optional Nodlang::get_current_scope() { ASSERT( parser_state.scope.top() ); return parser_state.scope.top(); } -Node* Nodlang::get_current_scope_node() +Optional Nodlang::get_current_scope_node() { ASSERT( parser_state.scope.top() ); return parser_state.scope.top()->get_owner(); } -IfNode* Nodlang::parse_conditional_structure() +Optional Nodlang::parse_conditional_structure() { LOG_VERBOSE("Parser", "try to parse conditional structure...\n") start_transaction(); bool success = false; - Node* condition = nullptr; - Node* condition_true_scope_node = nullptr; - IfNode* if_node = nullptr; - IfNode* else_node = nullptr; + Optional condition; + Optional condition_true_scope_node; + Optional if_node; + Optional else_node; Token if_token = parser_state.ribbon.eat_if(Token_t::keyword_if); if ( !if_token ) @@ -1107,7 +1114,7 @@ IfNode* Nodlang::parse_conditional_structure() if ( empty_condition || condition && parser_state.ribbon.eat_if(Token_t::parenthesis_close) ) { - condition_true_scope_node = parse_scope( if_node->child_slot_at(Branch_TRUE) ); + condition_true_scope_node = parse_scope( &if_node->child_slot_at(Branch_TRUE) ); if ( condition_true_scope_node ) { if ( parser_state.ribbon.eat_if(Token_t::keyword_else) ) @@ -1115,7 +1122,7 @@ IfNode* Nodlang::parse_conditional_structure() if_node->token_else = parser_state.ribbon.get_eaten(); /* parse "else" scope */ - if ( parse_scope( if_node->child_slot_at( Branch_FALSE ) ) ) + if ( parse_scope( &if_node->child_slot_at( Branch_FALSE ) ) ) { LOG_VERBOSE("Parser", "parse IF {...} ELSE {...} block... " OK "\n") success = true; @@ -1155,32 +1162,32 @@ IfNode* Nodlang::parse_conditional_structure() return if_node; } - parser_state.graph->destroy( else_node ); - parser_state.graph->destroy( condition_true_scope_node ); - parser_state.graph->destroy( condition ); - parser_state.graph->destroy( if_node ); + parser_state.graph->destroy( else_node.value() ); + parser_state.graph->destroy( condition_true_scope_node.value() ); + parser_state.graph->destroy( condition.value() ); + parser_state.graph->destroy( if_node.value() ); rollback_transaction(); return nullptr; } -ForLoopNode* Nodlang::parse_for_loop() +Optional Nodlang::parse_for_loop() { - bool success = false; - ForLoopNode* for_loop_node{}; + Optional result; + Optional _temp_for_loop_node; start_transaction(); Token token_for = parser_state.ribbon.eat_if(Token_t::keyword_for); if (!token_for.is_null()) { - for_loop_node = parser_state.graph->create_for_loop(); + _temp_for_loop_node = parser_state.graph->create_for_loop(); parser_state.graph->connect( *get_current_scope()->get_owner()->find_slot( SlotFlag_CHILD ), - *for_loop_node->find_slot( SlotFlag_PARENT ), + *_temp_for_loop_node->find_slot(SlotFlag_PARENT ), ConnectFlag_ALLOW_SIDE_EFFECTS ); - parser_state.scope.push(for_loop_node->get_component()); + parser_state.scope.push(_temp_for_loop_node->get_component()); - for_loop_node->token_for = token_for; + _temp_for_loop_node->token_for = token_for; LOG_VERBOSE("Parser", "parse FOR (...) block...\n") Token open_bracket = parser_state.ribbon.eat_if(Token_t::parenthesis_open); @@ -1190,7 +1197,7 @@ ForLoopNode* Nodlang::parse_for_loop() } else { - Node* init_instr = parse_instr(); + Optional init_instr = parse_instr(); if (!init_instr) { LOG_ERROR("Parser", "Unable to find initial instruction.\n") @@ -1199,10 +1206,10 @@ ForLoopNode* Nodlang::parse_for_loop() { parser_state.graph->connect_or_merge( *init_instr->find_slot( SlotFlag_OUTPUT ), - for_loop_node->initialization_slot() ); + _temp_for_loop_node->initialization_slot() ); - Node* condition = parse_instr(); - if (!condition ) + Optional condition = parse_instr(); + if ( !condition ) { LOG_ERROR("Parser", "Unable to find condition instruction.\n") } @@ -1210,10 +1217,10 @@ ForLoopNode* Nodlang::parse_for_loop() { parser_state.graph->connect_or_merge( *condition->find_slot( SlotFlag_OUTPUT ), - for_loop_node->condition_slot(Branch_TRUE) ); + _temp_for_loop_node->condition_slot(Branch_TRUE) ); - Node* iter_instr = parse_instr(); - if (!iter_instr) + Optional iter_instr = parse_instr(); + if ( !iter_instr ) { LOG_ERROR("Parser", "Unable to find iterative instruction.\n") } @@ -1221,20 +1228,20 @@ ForLoopNode* Nodlang::parse_for_loop() { parser_state.graph->connect_or_merge( *iter_instr->find_slot( SlotFlag_OUTPUT ), - for_loop_node->iteration_slot() ); + _temp_for_loop_node->iteration_slot() ); Token close_bracket = parser_state.ribbon.eat_if(Token_t::parenthesis_close); if (close_bracket.is_null()) { LOG_ERROR("Parser", "Unable to find close bracket after iterative instruction.\n") } - else if (!parse_scope( for_loop_node->child_slot_at( Branch_TRUE ) ) ) + else if (!parse_scope( &_temp_for_loop_node->child_slot_at(Branch_TRUE ) ) ) { LOG_ERROR("Parser", "Unable to parse a scope after for(...).\n") } else { - success = true; + result = _temp_for_loop_node; } } } @@ -1243,38 +1250,38 @@ ForLoopNode* Nodlang::parse_for_loop() parser_state.scope.pop(); } - if (success) + if ( result ) { commit_transaction(); } else { rollback_transaction(); - parser_state.graph->destroy( for_loop_node ); - for_loop_node = {}; + parser_state.graph->destroy(_temp_for_loop_node.value() ); } - return for_loop_node; + return result; } -WhileLoopNode* Nodlang::parse_while_loop() +Optional Nodlang::parse_while_loop() { - bool success = false; - WhileLoopNode* while_loop_node = nullptr; + Optional result; + Optional _temp_while_loop_node; + start_transaction(); Token token_while = parser_state.ribbon.eat_if(Token_t::keyword_while); if (!token_while.is_null()) { - while_loop_node = parser_state.graph->create_while_loop(); + _temp_while_loop_node = parser_state.graph->create_while_loop(); parser_state.graph->connect( *get_current_scope()->get_owner()->find_slot( SlotFlag_CHILD ), - *while_loop_node->find_slot( SlotFlag_PARENT ), + *_temp_while_loop_node->find_slot(SlotFlag_PARENT ), ConnectFlag_ALLOW_SIDE_EFFECTS ); - parser_state.scope.push(while_loop_node->get_component() ); + parser_state.scope.push(_temp_while_loop_node->get_component() ); - while_loop_node->token_while = token_while; + _temp_while_loop_node->token_while = token_while; LOG_VERBOSE("Parser", "parse WHILE (...) { /* block */ }\n") Token open_bracket = parser_state.ribbon.eat_if(Token_t::parenthesis_open); @@ -1282,41 +1289,43 @@ WhileLoopNode* Nodlang::parse_while_loop() { LOG_ERROR("Parser", "Unable to find open bracket after \"while\"\n") } - else if( Node* cond_instr = parse_instr() ) + else if( Optional cond_instr = parse_instr() ) { parser_state.graph->connect_or_merge( *cond_instr->find_slot( SlotFlag_OUTPUT ), - while_loop_node->condition_slot(Branch_TRUE) ); + _temp_while_loop_node->condition_slot(Branch_TRUE) ); Token close_bracket = parser_state.ribbon.eat_if(Token_t::parenthesis_close); if ( close_bracket.is_null() ) { LOG_ERROR("Parser", "Unable to find close bracket after condition instruction.\n") } - else if (!parse_scope( while_loop_node->child_slot_at( Branch_TRUE ) ) ) + else if (!parse_scope(&_temp_while_loop_node->child_slot_at(Branch_TRUE ) ) ) { LOG_ERROR("Parser", "Unable to parse a scope after \"while(\".\n") } else { - success = true; + result = _temp_while_loop_node; } } parser_state.scope.pop(); } - if (success) + if (result) { commit_transaction(); - return while_loop_node; + } + else + { + rollback_transaction(); + parser_state.graph->destroy( _temp_while_loop_node.value() ); } - rollback_transaction(); - parser_state.graph->destroy( while_loop_node ); - return {}; + return result; } -Slot* Nodlang::parse_variable_declaration() +Optional Nodlang::parse_variable_declaration() { if (!parser_state.ribbon.can_eat(2)) @@ -1332,9 +1341,8 @@ Slot* Nodlang::parse_variable_declaration() if (type_token.is_keyword_type() && identifier_token.m_type == Token_t::identifier) { const TypeDescriptor* variable_type = get_type(type_token.m_type); - auto* scope = get_current_scope(); - ASSERT(scope != nullptr ); // There must always be a scope! - VariableNode* variable_node = parser_state.graph->create_variable(variable_type, identifier_token.word_to_string(), scope ); + Optional scope = get_current_scope(); + VariableNode* variable_node = parser_state.graph->create_variable(variable_type, identifier_token.word_to_string(), scope.value() ); variable_node->set_flags(VariableFlag_DECLARED); variable_node->set_type_token( type_token ); variable_node->set_identifier_token( identifier_token ); @@ -1342,10 +1350,7 @@ Slot* Nodlang::parse_variable_declaration() Token operator_token = parser_state.ribbon.eat_if(Token_t::operator_); if (!operator_token.is_null() && operator_token.word_size() == 1 && *operator_token.word_ptr() == '=') { - Slot* expression_out = parse_expression(); - if (expression_out != nullptr //&& - // type::is_implicitly_convertible(expression_out->get_property()->get_type(), variable_type ) - ) + if ( Optional expression_out = parse_expression() ) { parser_state.graph->connect_to_variable( *expression_out, *variable_node ); variable_node->set_operator_token( operator_token ); diff --git a/src/ndbl/core/language/Nodlang.h b/src/ndbl/core/language/Nodlang.h index 8da3eb4ad..653de0ff7 100644 --- a/src/ndbl/core/language/Nodlang.h +++ b/src/ndbl/core/language/Nodlang.h @@ -12,6 +12,7 @@ #include "ndbl/core/VariableNode.h" #include "ndbl/core/Token.h" #include "ndbl/core/TokenRibbon.h" +#include "tools/core/Optional.h" namespace ndbl{ @@ -53,21 +54,21 @@ namespace ndbl{ bool parse(const std::string& _in, Graph *_out); // Try to convert a source code (input string) to a program tree (output graph). Return true if evaluation went well and false otherwise. Token parse_token(const char *buffer, size_t buffer_size, size_t &global_cursor) const; // parse a single token from position _cursor in _string. Token parse_token(const std::string& _string) const; - Node* parse_scope( Slot& _parent_scope_slot ); - Node* parse_instr(); - Slot* parse_variable_declaration(); // Try to parse a variable declaration (ex: "int a = 10;"). + tools::Optional parse_scope(tools::Optional _parent_scope_slot ); + tools::Optional parse_instr(); + tools::Optional parse_variable_declaration(); // Try to parse a variable declaration (ex: "int a = 10;"). void parse_code_block(); // Try to parse a code block with the option to create a scope or not (reusing the current one). - IfNode* parse_conditional_structure(); // Try to parse a conditional structure (if/else if/.else) recursively. - ForLoopNode* parse_for_loop(); - WhileLoopNode* parse_while_loop(); - Node* parse_program(); - Slot* parse_function_call(); - Slot* parse_parenthesis_expression(); - Slot* parse_unary_operator_expression(u8_t _precedence = 0); - Slot* parse_binary_operator_expression(u8_t _precedence, Slot& _left); - Slot* parse_atomic_expression(); - Slot* parse_expression(u8_t _precedence = 0, Slot* _left_override = nullptr); - Slot* token_to_slot(Token _token); + tools::Optional parse_conditional_structure(); // Try to parse a conditional structure (if/else if/.else) recursively. + tools::Optional parse_for_loop(); + tools::Optional parse_while_loop(); + tools::Optional parse_program(); + tools::Optional parse_function_call(); + tools::Optional parse_parenthesis_expression(); + tools::Optional parse_unary_operator_expression(u8_t _precedence = 0); + tools::Optional parse_binary_operator_expression(u8_t _precedence, tools::Optional _left); + tools::Optional parse_atomic_expression(); + tools::Optional parse_expression(u8_t _precedence = 0, tools::Optional _left_override = nullptr); + tools::Optional token_to_slot(Token _token); bool to_bool(const std::string& ); std::string to_unquoted_string(const std::string& _quoted_str); double to_double(const std::string& ); @@ -79,8 +80,8 @@ namespace ndbl{ void rollback_transaction(); // Rollback the pending transaction (revert cursor to parse again from the transaction start). void commit_transaction(); // Commit the pending transaction bool is_syntax_valid(); // Check if the syntax of the token ribbon is correct. (ex: ["12", "-"] is incorrect) - Scope* get_current_scope(); - Node* get_current_scope_node(); + tools::Optional get_current_scope(); + tools::Optional get_current_scope_node(); public: struct ParserState diff --git a/src/ndbl/core/language/Nodlang.parse_function_call.specs.cpp b/src/ndbl/core/language/Nodlang.parse_function_call.specs.cpp index 227aaf59a..a536ee02e 100644 --- a/src/ndbl/core/language/Nodlang.parse_function_call.specs.cpp +++ b/src/ndbl/core/language/Nodlang.parse_function_call.specs.cpp @@ -6,6 +6,7 @@ using namespace ndbl; typedef ::testing::Core Language_parse_function_call; +using tools::Optional; TEST_F(Language_parse_function_call, dna_to_protein) { @@ -28,10 +29,10 @@ TEST_F(Language_parse_function_call, dna_to_protein) EXPECT_EQ( parser_state.ribbon.at(3).m_type, Token_t::parenthesis_close); // parse - Slot* function_out = language->parse_function_call(); + Optional function_out = language->parse_function_call(); // check - EXPECT_TRUE(function_out); + EXPECT_TRUE(function_out.has_value()); EXPECT_TRUE(function_out->node->type() == NodeType_FUNCTION); } @@ -55,9 +56,9 @@ TEST_F(Language_parse_function_call, operator_add) EXPECT_EQ( parser_state.ribbon.at(2).m_type, Token_t::literal_int); // parse - Slot* result = language->parse_expression(); + Optional result = language->parse_expression(); // check - EXPECT_TRUE(result); + EXPECT_TRUE(result.has_value()); EXPECT_TRUE(result->node->type() == NodeType_OPERATOR); } \ No newline at end of file From effcbb0a76495180aef6d26236541abd2fe2317d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9renger=20Dalle-Cort?= Date: Tue, 22 Oct 2024 21:36:40 -0400 Subject: [PATCH 4/5] fixup! refactor(Nodlang): use tools::Optional --- src/ndbl/core/language/Nodlang.cpp | 16 +++++++------- src/ndbl/core/language/Nodlang.h | 34 +++++++++++++++--------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/ndbl/core/language/Nodlang.cpp b/src/ndbl/core/language/Nodlang.cpp index f2be36cff..72e9d089d 100644 --- a/src/ndbl/core/language/Nodlang.cpp +++ b/src/ndbl/core/language/Nodlang.cpp @@ -255,7 +255,7 @@ Optional Nodlang::token_to_slot(Token _token) if ( !m_strict_mode ) { // Insert a VariableNodeRef with "any" type - LOG_WARNING( "Parser", "%s is not declared (strict mode), abstract graph can be generated but compilation will failure.\n", + LOG_WARNING( "Parser", "%s is not declared (strict mode), abstract graph can be generated but compilation will fail.\n", _token.word_to_string().c_str() ) VariableRefNode* ref = parser_state.graph->create_variable_ref(); ref->value()->set_token(_token ); @@ -288,11 +288,11 @@ Optional Nodlang::token_to_slot(Token _token) return literal->value_out(); } -Optional Nodlang::parse_binary_operator_expression(u8_t _precedence, Optional _left) +Optional Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot* _left) { LOG_VERBOSE("Parser", "parse binary operation expr...\n") LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) - VERIFY(_left.has_value(), "It is required that _left is not empty") + ASSERT(_left != nullptr); if (!parser_state.ribbon.can_eat(2)) { @@ -567,7 +567,7 @@ Optional Nodlang::parse_program() return root; } -Optional Nodlang::parse_scope(Optional _parent_scope_slot ) +Optional Nodlang::parse_scope(Slot* _parent_scope_slot ) { auto scope_begin_token = parser_state.ribbon.eat_if(Token_t::scope_begin); if ( !scope_begin_token ) @@ -575,7 +575,7 @@ Optional Nodlang::parse_scope(Optional _parent_scope_slot ) return {}; } - VERIFY(_parent_scope_slot.has_value(), "Required!") + VERIFY(_parent_scope_slot != nullptr, "Required!"); start_transaction(); @@ -680,7 +680,7 @@ Optional Nodlang::parse_expression(u8_t _precedence, Optional _lef Get the right-handed operand */ LOG_VERBOSE("Parser", "parse expr... left parsed, we parse right\n") - Optional expression_out = parse_binary_operator_expression(_precedence, left); + Optional expression_out = parse_binary_operator_expression( _precedence, left.value() ); if ( expression_out ) { if (!parser_state.ribbon.can_eat()) @@ -1257,7 +1257,7 @@ Optional Nodlang::parse_for_loop() else { rollback_transaction(); - parser_state.graph->destroy(_temp_for_loop_node.value() ); + parser_state.graph->destroy( _temp_for_loop_node.value_or_null() ); } return result; @@ -1319,7 +1319,7 @@ Optional Nodlang::parse_while_loop() else { rollback_transaction(); - parser_state.graph->destroy( _temp_while_loop_node.value() ); + parser_state.graph->destroy( _temp_while_loop_node.value_or_null() ); } return result; diff --git a/src/ndbl/core/language/Nodlang.h b/src/ndbl/core/language/Nodlang.h index 653de0ff7..2cc224615 100644 --- a/src/ndbl/core/language/Nodlang.h +++ b/src/ndbl/core/language/Nodlang.h @@ -49,15 +49,15 @@ namespace ndbl{ ~Nodlang(); // Parser --------------------------------------------------------------------- - bool tokenize(const std::string& _string); // Tokenize a string, return true for success. Tokens are stored in the token ribbon. - bool tokenize(const char* buffer, size_t buffer_size); // Tokenize a buffer of a certain length, return true for success. Tokens are stored in the token ribbon. - bool parse(const std::string& _in, Graph *_out); // Try to convert a source code (input string) to a program tree (output graph). Return true if evaluation went well and false otherwise. - Token parse_token(const char *buffer, size_t buffer_size, size_t &global_cursor) const; // parse a single token from position _cursor in _string. - Token parse_token(const std::string& _string) const; - tools::Optional parse_scope(tools::Optional _parent_scope_slot ); + bool tokenize(const std::string& _string); // Tokenize a string, return true for success. Tokens are stored in the token ribbon. + bool tokenize(const char* buffer, size_t buffer_size); // Tokenize a buffer of a certain length, return true for success. Tokens are stored in the token ribbon. + bool parse(const std::string& _in, Graph *_out); // Try to convert a source code (input string) to a program tree (output graph). Return true if evaluation went well and false otherwise. + Token parse_token(const char *buffer, size_t buffer_size, size_t &global_cursor) const; // parse a single token from position _cursor in _string. + Token parse_token(const std::string& _string) const; + tools::Optional parse_scope(Slot* _parent_scope_slot ); tools::Optional parse_instr(); tools::Optional parse_variable_declaration(); // Try to parse a variable declaration (ex: "int a = 10;"). - void parse_code_block(); // Try to parse a code block with the option to create a scope or not (reusing the current one). + void parse_code_block(); // Try to parse a code block with the option to create a scope or not (reusing the current one). tools::Optional parse_conditional_structure(); // Try to parse a conditional structure (if/else if/.else) recursively. tools::Optional parse_for_loop(); tools::Optional parse_while_loop(); @@ -65,21 +65,21 @@ namespace ndbl{ tools::Optional parse_function_call(); tools::Optional parse_parenthesis_expression(); tools::Optional parse_unary_operator_expression(u8_t _precedence = 0); - tools::Optional parse_binary_operator_expression(u8_t _precedence, tools::Optional _left); + tools::Optional parse_binary_operator_expression(u8_t _precedence, Slot* _left); tools::Optional parse_atomic_expression(); tools::Optional parse_expression(u8_t _precedence = 0, tools::Optional _left_override = nullptr); tools::Optional token_to_slot(Token _token); - bool to_bool(const std::string& ); - std::string to_unquoted_string(const std::string& _quoted_str); - double to_double(const std::string& ); - int to_int(const std::string& ); + bool to_bool(const std::string& ); + std::string to_unquoted_string(const std::string& _quoted_str); + double to_double(const std::string& ); + int to_int(const std::string& ); private: - bool allow_to_attach_suffix(Token_t type) const; - void start_transaction(); // Start a parsing transaction. Must be followed by rollback_transaction or commit_transaction. - void rollback_transaction(); // Rollback the pending transaction (revert cursor to parse again from the transaction start). - void commit_transaction(); // Commit the pending transaction - bool is_syntax_valid(); // Check if the syntax of the token ribbon is correct. (ex: ["12", "-"] is incorrect) + bool allow_to_attach_suffix(Token_t type) const; + void start_transaction(); // Start a parsing transaction. Must be followed by rollback_transaction or commit_transaction. + void rollback_transaction(); // Rollback the pending transaction (revert cursor to parse again from the transaction start). + void commit_transaction(); // Commit the pending transaction + bool is_syntax_valid(); // Check if the syntax of the token ribbon is correct. (ex: ["12", "-"] is incorrect) tools::Optional get_current_scope(); tools::Optional get_current_scope_node(); From f42d40c207816290724879b9ee95aa6777e13abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9renger=20Dalle-Cort?= Date: Tue, 22 Oct 2024 21:51:00 -0400 Subject: [PATCH 5/5] refactor: remove ";" from macros --- src/ndbl/cli/CLI.cpp | 10 +- src/ndbl/core/Graph.cpp | 10 +- src/ndbl/core/Interpreter.cpp | 22 +-- src/ndbl/core/NodableHeadless.cpp | 2 +- src/ndbl/core/NodeFactory.cpp | 2 +- src/ndbl/core/Scope.cpp | 6 +- src/ndbl/core/TokenRibbon.cpp | 8 +- src/ndbl/core/language/Nodlang.cpp | 159 +++++++++++---------- src/ndbl/gui/File.cpp | 16 +-- src/ndbl/gui/FileView.cpp | 8 +- src/ndbl/gui/GraphView.cpp | 2 +- src/ndbl/gui/Nodable.cpp | 6 +- src/tools/core/System.cpp | 2 +- src/tools/core/assertions.h | 2 +- src/tools/core/log.h | 10 +- src/tools/core/memory/MemoryManager.cpp | 2 +- src/tools/core/reflection/TypeRegister.cpp | 4 +- src/tools/gui/FontManager.cpp | 6 +- src/tools/gui/TextureManager.cpp | 8 +- 19 files changed, 143 insertions(+), 142 deletions(-) diff --git a/src/ndbl/cli/CLI.cpp b/src/ndbl/cli/CLI.cpp index a0953abb9..db5140f4f 100644 --- a/src/ndbl/cli/CLI.cpp +++ b/src/ndbl/cli/CLI.cpp @@ -98,7 +98,7 @@ void CLI::clear() void CLI::log_function_call(const variant &result, const FunctionDescriptor *type) { - LOG_MESSAGE("CLI", "CLI::%s() done (result: %s)\n",type->get_identifier(), result.to().c_str()) + LOG_MESSAGE("CLI", "CLI::%s() done (result: %s)\n",type->get_identifier(), result.to().c_str()); } std::string CLI::get_line() @@ -124,7 +124,7 @@ bool CLI::PublicApi::serialize() return true; } - LOG_WARNING("CLI", "unable to serialize! Are you sure you entered an expression earlier?\n") + LOG_WARNING("CLI", "unable to serialize! Are you sure you entered an expression earlier?\n"); return false; } @@ -132,7 +132,7 @@ bool CLI::PublicApi::compile() { if( m_cli->compile() == nullptr) { - LOG_ERROR("CLI", "unable to compile!\n") + LOG_ERROR("CLI", "unable to compile!\n"); return false; } return true; @@ -204,13 +204,13 @@ bool CLI::run() if( !load_program(m_asm_code) ) { - LOG_ERROR("CLI", "Unable to run program!\n") + LOG_ERROR("CLI", "Unable to run program!\n"); return false; } if( !run_program() ) { - LOG_ERROR("CLI", "Unable to run program!\n") + LOG_ERROR("CLI", "Unable to run program!\n"); return false; } diff --git a/src/ndbl/core/Graph.cpp b/src/ndbl/core/Graph.cpp index a53d1679a..2ea224307 100644 --- a/src/ndbl/core/Graph.cpp +++ b/src/ndbl/core/Graph.cpp @@ -31,12 +31,12 @@ void Graph::clear() return; } - LOG_VERBOSE( "Graph", "Clearing graph ...\n") + LOG_VERBOSE( "Graph", "Clearing graph ...\n"); m_root = nullptr; while ( !m_node_registry.empty() ) { Node* node = m_node_registry[0]; - LOG_VERBOSE("Graph", "destroying node \"%s\" (id: %zu)\n", node->name().c_str(), (u64_t)node ) + LOG_VERBOSE("Graph", "destroying node \"%s\" (id: %zu)\n", node->name().c_str(), (u64_t)node ); destroy(node); } @@ -53,7 +53,7 @@ void Graph::clear() } #endif - LOG_VERBOSE("Graph", "Graph cleared.\n") + LOG_VERBOSE("Graph", "Graph cleared.\n"); } bool Graph::update() @@ -99,7 +99,7 @@ void Graph::add(Node* _node) set_dirty(); - LOG_VERBOSE("Graph", "add node %s (%s)\n", _node->name().c_str(), _node->get_class()->get_name()) + LOG_VERBOSE("Graph", "add node %s (%s)\n", _node->name().c_str(), _node->get_class()->get_name()); } void Graph::remove(Node* _node) @@ -247,7 +247,7 @@ void Graph::remove(DirectedEdge edge) } else { - LOG_WARNING("Graph", "Unable to unregister edge\n") + LOG_WARNING("Graph", "Unable to unregister edge\n"); } set_dirty(); // To express this graph changed } diff --git a/src/ndbl/core/Interpreter.cpp b/src/ndbl/core/Interpreter.cpp index a5a68bf1b..9906cd894 100644 --- a/src/ndbl/core/Interpreter.cpp +++ b/src/ndbl/core/Interpreter.cpp @@ -25,14 +25,14 @@ void CPU::clear_registers() qword CPU::read(Register _id)const { ASSERT(_id < Register_COUNT); - LOG_VERBOSE("CPU", "read register %s (value: %s)\n", Register_to_string(_id), m_register[_id].to_string().c_str() ) + LOG_VERBOSE("CPU", "read register %s (value: %s)\n", Register_to_string(_id), m_register[_id].to_string().c_str() ); return m_register[_id]; } qword& CPU::read_write(Register _id) { ASSERT(_id < Register_COUNT); - LOG_VERBOSE("CPU", "read_write register %s (value: %s)\n", Register_to_string(_id), m_register[_id].to_string().c_str() ) + LOG_VERBOSE("CPU", "read_write register %s (value: %s)\n", Register_to_string(_id), m_register[_id].to_string().c_str() ); return m_register[_id]; } @@ -41,7 +41,7 @@ void CPU::write(Register _id, qword _data) ASSERT(_id < Register_COUNT); qword& mem_dst = read_write(_id); mem_dst = _data; - LOG_VERBOSE("CPU", "write register %s (value: %s)\n", Register_to_string(_id), mem_dst.to_string().c_str()) + LOG_VERBOSE("CPU", "write register %s (value: %s)\n", Register_to_string(_id), mem_dst.to_string().c_str()); } void Interpreter::advance_cursor(i64_t _amount) @@ -54,7 +54,7 @@ void Interpreter::advance_cursor(i64_t _amount) void Interpreter::run_program() { ASSERT(m_code); - LOG_MESSAGE("Interpreter", "Running program ...\n") + LOG_MESSAGE("Interpreter", "Running program ...\n"); m_is_program_running = true; m_cpu.clear_registers(); m_visited_nodes.clear(); @@ -65,7 +65,7 @@ void Interpreter::run_program() step_over(); } stop_program(); - LOG_MESSAGE("Interpreter", "Program terminated\n") + LOG_MESSAGE("Interpreter", "Program terminated\n"); } void Interpreter::stop_program() @@ -75,11 +75,11 @@ void Interpreter::stop_program() m_is_program_running = false; m_is_debugging = false; m_next_node = {}; - LOG_MESSAGE("Interpreter", "Program stopped\n") + LOG_MESSAGE("Interpreter", "Program stopped\n"); } else { - LOG_ERROR("Interpreter", "stop_program() failed, program is not running\n") + LOG_ERROR("Interpreter", "stop_program() failed, program is not running\n"); } } @@ -92,9 +92,9 @@ const Code* Interpreter::release_program() } m_cpu.clear_registers(); // will also clear reset instruction pointer (stored in a register Register_eip) - LOG_VERBOSE("Interpreter", "registers cleared\n") + LOG_VERBOSE("Interpreter", "registers cleared\n"); - LOG_VERBOSE("Interpreter", "program released\n") + LOG_VERBOSE("Interpreter", "program released\n"); const Code* copy = m_code; m_code = nullptr; return copy; @@ -325,7 +325,7 @@ bool Interpreter::debug_step_over() default: break; } - LOG_MESSAGE("Interpreter", "Step over (current line %#1llx)\n", next_instr->line) + LOG_MESSAGE("Interpreter", "Step over (current line %#1llx)\n", next_instr->line); } return continue_execution; @@ -340,7 +340,7 @@ void Interpreter::debug_program() m_visited_nodes.clear(); m_next_node = m_code->get_meta_data().graph->get_root(); - LOG_MESSAGE("Interpreter", "Debugging program ...\n") + LOG_MESSAGE("Interpreter", "Debugging program ...\n"); } bool Interpreter::is_there_a_next_instr() const diff --git a/src/ndbl/core/NodableHeadless.cpp b/src/ndbl/core/NodableHeadless.cpp index d7c83df1c..6fc68630f 100644 --- a/src/ndbl/core/NodableHeadless.cpp +++ b/src/ndbl/core/NodableHeadless.cpp @@ -46,7 +46,7 @@ bool NodableHeadless::run_program() const } catch ( std::runtime_error& error) { - LOG_ERROR("NodableHeadless", "Unable to run the program! %s\n", error.what()) + LOG_ERROR("NodableHeadless", "Unable to run the program! %s\n", error.what()); return false; } diff --git a/src/ndbl/core/NodeFactory.cpp b/src/ndbl/core/NodeFactory.cpp index af8eabc21..2dfa7103a 100644 --- a/src/ndbl/core/NodeFactory.cpp +++ b/src/ndbl/core/NodeFactory.cpp @@ -67,7 +67,7 @@ VariableNode* NodeFactory::create_variable(const TypeDescriptor* _type, const st } else { - LOG_WARNING("HeadlessNodeFactory", "Variable %s has_flags been created without defining its scope.\n", _name.c_str()) + LOG_WARNING("HeadlessNodeFactory", "Variable %s has_flags been created without defining its scope.\n", _name.c_str()); } m_post_process(node); diff --git a/src/ndbl/core/Scope.cpp b/src/ndbl/core/Scope.cpp index 316525ed9..0b2990a97 100644 --- a/src/ndbl/core/Scope.cpp +++ b/src/ndbl/core/Scope.cpp @@ -48,15 +48,15 @@ void Scope::add_variable(VariableNode* _variable) { if (find_variable(_variable->get_identifier()) != nullptr ) { - LOG_ERROR("Scope", "Unable to add variable '%s', already exists in the same scope.\n", _variable->get_identifier().c_str()) + LOG_ERROR("Scope", "Unable to add variable '%s', already exists in the same scope.\n", _variable->get_identifier().c_str()); } else if ( _variable->get_scope() ) { - LOG_ERROR("Scope", "Unable to add variable '%s', already declared in another scope. Remove it first.\n", _variable->get_identifier().c_str()) + LOG_ERROR("Scope", "Unable to add variable '%s', already declared in another scope. Remove it first.\n", _variable->get_identifier().c_str()); } else { - LOG_VERBOSE("Scope", "Add variable '%s' to the scope\n", _variable->get_identifier().c_str() ) + LOG_VERBOSE("Scope", "Add variable '%s' to the scope\n", _variable->get_identifier().c_str() ); m_variables.push_back(_variable); _variable->reset_scope(this); } diff --git a/src/ndbl/core/TokenRibbon.cpp b/src/ndbl/core/TokenRibbon.cpp index 4faec7826..4e113f0ce 100644 --- a/src/ndbl/core/TokenRibbon.cpp +++ b/src/ndbl/core/TokenRibbon.cpp @@ -87,26 +87,26 @@ Token TokenRibbon::eat_if(Token_t expectedType) Token TokenRibbon::eat() { - LOG_VERBOSE("Parser", "Eat token (idx %i) %s \n", m_cursor, peek().buffer_to_string().c_str() ) + LOG_VERBOSE("Parser", "Eat token (idx %i) %s \n", m_cursor, peek().buffer_to_string().c_str() ); return m_tokens.at(m_cursor++); } void TokenRibbon::transaction_start() { m_transaction.push(m_cursor); - LOG_VERBOSE("Parser", "Start Transaction (idx %i)\n", m_cursor) + LOG_VERBOSE("Parser", "Start Transaction (idx %i)\n", m_cursor); } void TokenRibbon::transaction_rollback() { m_cursor = m_transaction.top(); - LOG_VERBOSE("Parser", "Rollback transaction (idx %i)\n", m_cursor) + LOG_VERBOSE("Parser", "Rollback transaction (idx %i)\n", m_cursor); m_transaction.pop(); } void TokenRibbon::transaction_commit() { - LOG_VERBOSE("Parser", "Commit transaction (idx %i)\n", m_cursor) + LOG_VERBOSE("Parser", "Commit transaction (idx %i)\n", m_cursor); m_transaction.pop(); } diff --git a/src/ndbl/core/language/Nodlang.cpp b/src/ndbl/core/language/Nodlang.cpp index 72e9d089d..c29cfd39e 100644 --- a/src/ndbl/core/language/Nodlang.cpp +++ b/src/ndbl/core/language/Nodlang.cpp @@ -179,8 +179,8 @@ bool Nodlang::parse(const std::string &_source_code, Graph *_graphNode) parser_state.set_source_buffer(_source_code.c_str(), _source_code.size()); parser_state.graph = _graphNode; - LOG_VERBOSE("Parser", "Trying to evaluate evaluated: %s\"\n", _source_code.c_str()) - LOG_MESSAGE("Parser", "Tokenization ...\n") + LOG_VERBOSE("Parser", "Trying to evaluate evaluated: %s\"\n", _source_code.c_str()); + LOG_MESSAGE("Parser", "Tokenization ...\n"); if (!tokenize(parser_state.source_buffer, parser_state.source_buffer_size)) { @@ -196,14 +196,14 @@ bool Nodlang::parse(const std::string &_source_code, Graph *_graphNode) if ( !program ) { - LOG_WARNING("Parser", "Unable to generate program tree.\n") + LOG_WARNING("Parser", "Unable to generate program tree.\n"); return false; } if (parser_state.ribbon.can_eat()) { parser_state.graph->clear(); - LOG_WARNING("Parser", "Unable to generate a full program tree.\n") + LOG_WARNING("Parser", "Unable to generate a full program tree.\n"); LOG_MESSAGE("Parser", "%s", format::title("TokenRibbon").c_str()); for (const Token& each_token : parser_state.ribbon) { @@ -211,7 +211,7 @@ bool Nodlang::parse(const std::string &_source_code, Graph *_graphNode) } LOG_MESSAGE("Parser", "%s", format::title("TokenRibbon end").c_str()); auto curr_token = parser_state.ribbon.peek(); - LOG_ERROR("Parser", "Couldn't parse token %llu and above: %s\n", curr_token.m_index, curr_token.json().c_str()) + LOG_ERROR("Parser", "Couldn't parse token %llu and above: %s\n", curr_token.m_index, curr_token.json().c_str()); return false; } parser_state.graph->set_dirty(); @@ -256,13 +256,13 @@ Optional Nodlang::token_to_slot(Token _token) { // Insert a VariableNodeRef with "any" type LOG_WARNING( "Parser", "%s is not declared (strict mode), abstract graph can be generated but compilation will fail.\n", - _token.word_to_string().c_str() ) + _token.word_to_string().c_str() ); VariableRefNode* ref = parser_state.graph->create_variable_ref(); ref->value()->set_token(_token ); return ref->value_out(); } - LOG_ERROR( "Parser", "%s is not declared (strict mode) \n", _token.word_to_string().c_str() ) + LOG_ERROR( "Parser", "%s is not declared (strict mode) \n", _token.word_to_string().c_str() ); return nullptr; } @@ -280,7 +280,7 @@ Optional Nodlang::token_to_slot(Token _token) if ( literal == nullptr) { - LOG_VERBOSE("Parser", "Unable to perform token_to_property for token %s!\n", _token.word_to_string().c_str()) + LOG_VERBOSE("Parser", "Unable to perform token_to_property for token %s!\n", _token.word_to_string().c_str()); return nullptr; } @@ -290,13 +290,13 @@ Optional Nodlang::token_to_slot(Token _token) Optional Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot* _left) { - LOG_VERBOSE("Parser", "parse binary operation expr...\n") - LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) + LOG_VERBOSE("Parser", "parse binary operation expr...\n"); + LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()); ASSERT(_left != nullptr); if (!parser_state.ribbon.can_eat(2)) { - LOG_VERBOSE("Parser", "parse binary operation expr...... " KO " (not enought tokens)\n") + LOG_VERBOSE("Parser", "parse binary operation expr...... " KO " (not enought tokens)\n"); return nullptr; } @@ -311,7 +311,7 @@ Optional Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot if (!isValid) { rollback_transaction(); - LOG_VERBOSE("Parser", "parse binary operation expr... " KO " (Structure)\n") + LOG_VERBOSE("Parser", "parse binary operation expr... " KO " (Structure)\n"); return nullptr; } @@ -319,7 +319,7 @@ Optional Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot const Operator *ope = find_operator(word, Operator_t::Binary); if (ope == nullptr) { - LOG_VERBOSE("Parser", "parse binary operation expr... " KO " (unable to find operator %s)\n", word.c_str()) + LOG_VERBOSE("Parser", "parse binary operation expr... " KO " (unable to find operator %s)\n", word.c_str()); rollback_transaction(); return nullptr; } @@ -327,7 +327,7 @@ Optional Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot // Precedence check if (ope->precedence <= _precedence && _precedence > 0) {// always update the first operation if they have the same precedence or less. - LOG_VERBOSE("Parser", "parse binary operation expr... " KO " (Precedence)\n") + LOG_VERBOSE("Parser", "parse binary operation expr... " KO " (Precedence)\n"); rollback_transaction(); return nullptr; } @@ -338,7 +338,7 @@ Optional Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot if ( !right ) { - LOG_VERBOSE("Parser", "parseBinaryOperationExpression... " KO " (right expression is nullptr)\n") + LOG_VERBOSE("Parser", "parseBinaryOperationExpression... " KO " (right expression is nullptr)\n"); rollback_transaction(); return nullptr; } @@ -357,18 +357,18 @@ Optional Nodlang::parse_binary_operator_expression(u8_t _precedence, Slot parser_state.graph->connect_or_merge( *right, *binary_op->rvalue_in() ); commit_transaction(); - LOG_VERBOSE("Parser", "parse binary operation expr... " OK "\n") + LOG_VERBOSE("Parser", "parse binary operation expr... " OK "\n"); return binary_op->value_out(); } Optional Nodlang::parse_unary_operator_expression(u8_t _precedence) { - LOG_VERBOSE("Parser", "parseUnaryOperationExpression...\n") - LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) + LOG_VERBOSE("Parser", "parseUnaryOperationExpression...\n"); + LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()); if (!parser_state.ribbon.can_eat(2)) { - LOG_VERBOSE("Parser", "parseUnaryOperationExpression... " KO " (not enough tokens)\n") + LOG_VERBOSE("Parser", "parseUnaryOperationExpression... " KO " (not enough tokens)\n"); return nullptr; } @@ -379,7 +379,7 @@ Optional Nodlang::parse_unary_operator_expression(u8_t _precedence) if (operator_token.m_type != Token_t::operator_) { rollback_transaction(); - LOG_VERBOSE("Parser", "parseUnaryOperationExpression... " KO " (operator not found)\n") + LOG_VERBOSE("Parser", "parseUnaryOperationExpression... " KO " (operator not found)\n"); return nullptr; } @@ -393,7 +393,7 @@ Optional Nodlang::parse_unary_operator_expression(u8_t _precedence) if ( !out_atomic ) { - LOG_VERBOSE("Parser", "parseUnaryOperationExpression... " KO " (right expression is nullptr)\n") + LOG_VERBOSE("Parser", "parseUnaryOperationExpression... " KO " (right expression is nullptr)\n"); rollback_transaction(); return nullptr; } @@ -408,7 +408,7 @@ Optional Nodlang::parse_unary_operator_expression(u8_t _precedence) parser_state.graph->connect_or_merge( *out_atomic, *node->lvalue_in() ); - LOG_VERBOSE("Parser", "parseUnaryOperationExpression... " OK "\n") + LOG_VERBOSE("Parser", "parseUnaryOperationExpression... " OK "\n"); commit_transaction(); return node->value_out(); @@ -416,11 +416,11 @@ Optional Nodlang::parse_unary_operator_expression(u8_t _precedence) Optional Nodlang::parse_atomic_expression() { - LOG_VERBOSE("Parser", "parse atomic expr... \n") + LOG_VERBOSE("Parser", "parse atomic expr... \n"); if (!parser_state.ribbon.can_eat()) { - LOG_VERBOSE("Parser", "parse atomic expr... " KO "(not enough tokens)\n") + LOG_VERBOSE("Parser", "parse atomic expr... " KO "(not enough tokens)\n"); return nullptr; } @@ -429,7 +429,7 @@ Optional Nodlang::parse_atomic_expression() if (token.m_type == Token_t::operator_) { - LOG_VERBOSE("Parser", "parse atomic expr... " KO "(token is an operator)\n") + LOG_VERBOSE("Parser", "parse atomic expr... " KO "(token is an operator)\n"); rollback_transaction(); return nullptr; } @@ -437,24 +437,24 @@ Optional Nodlang::parse_atomic_expression() if ( Optional result = token_to_slot(token) ) { commit_transaction(); - LOG_VERBOSE("Parser", "parse atomic expr... " OK "\n") + LOG_VERBOSE("Parser", "parse atomic expr... " OK "\n"); return result; } rollback_transaction(); - LOG_VERBOSE("Parser", "parse atomic expr... " KO " (result is nullptr)\n") + LOG_VERBOSE("Parser", "parse atomic expr... " KO " (result is nullptr)\n"); return nullptr; } Optional Nodlang::parse_parenthesis_expression() { - LOG_VERBOSE("Parser", "parse parenthesis expr...\n") - LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) + LOG_VERBOSE("Parser", "parse parenthesis expr...\n"); + LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()); if (!parser_state.ribbon.can_eat()) { - LOG_VERBOSE("Parser", "parse parenthesis expr..." KO " no enough tokens.\n") + LOG_VERBOSE("Parser", "parse parenthesis expr..." KO " no enough tokens.\n"); return nullptr; } @@ -462,7 +462,7 @@ Optional Nodlang::parse_parenthesis_expression() Token currentToken = parser_state.ribbon.eat(); if (currentToken.m_type != Token_t::parenthesis_open) { - LOG_VERBOSE("Parser", "parse parenthesis expr..." KO " open bracket not found.\n") + LOG_VERBOSE("Parser", "parse parenthesis expr..." KO " open bracket not found.\n"); rollback_transaction(); return nullptr; } @@ -473,20 +473,20 @@ Optional Nodlang::parse_parenthesis_expression() Token token = parser_state.ribbon.eat(); if (token.m_type != Token_t::parenthesis_close) { - LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) + LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()); LOG_VERBOSE("Parser", "parse parenthesis expr..." KO " ( \")\" expected instead of %s )\n", - token.word_to_string().c_str()) + token.word_to_string().c_str()); rollback_transaction(); } else { - LOG_VERBOSE("Parser", "parse parenthesis expr..." OK "\n") + LOG_VERBOSE("Parser", "parse parenthesis expr..." OK "\n"); commit_transaction(); } } else { - LOG_VERBOSE("Parser", "parse parenthesis expr..." KO ", expression in parenthesis is nullptr.\n") + LOG_VERBOSE("Parser", "parse parenthesis expr..." KO ", expression in parenthesis is nullptr.\n"); rollback_transaction(); } return result; @@ -500,7 +500,7 @@ Optional Nodlang::parse_instr() if ( !expression_out ) { - LOG_VERBOSE("Parser", "parse instruction " KO " (parsed is nullptr)\n") + LOG_VERBOSE("Parser", "parse instruction " KO " (parsed is nullptr)\n"); rollback_transaction(); return {}; } @@ -530,13 +530,13 @@ Optional Nodlang::parse_instr() } else if (parser_state.ribbon.peek().m_type != Token_t::parenthesis_close) { - LOG_VERBOSE("Parser", "parse instruction " KO " (end of instruction not found)\n") + LOG_VERBOSE("Parser", "parse instruction " KO " (end of instruction not found)\n"); rollback_transaction(); return {}; } } - LOG_VERBOSE("Parser", "parse instruction " OK "\n") + LOG_VERBOSE("Parser", "parse instruction " OK "\n"); commit_transaction(); return output_node; @@ -641,12 +641,12 @@ void Nodlang::parse_code_block() Optional Nodlang::parse_expression(u8_t _precedence, Optional _left_override) { - LOG_VERBOSE("Parser", "parse expr...\n") - LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()) + LOG_VERBOSE("Parser", "parse expr...\n"); + LOG_VERBOSE("Parser", "%s \n", parser_state.ribbon.to_string().c_str()); if (!parser_state.ribbon.can_eat()) { - LOG_VERBOSE("Parser", "parse expr..." KO " (unable to eat a single token)\n") + LOG_VERBOSE("Parser", "parse expr..." KO " (unable to eat a single token)\n"); return _left_override.value_or_null(); } @@ -666,33 +666,33 @@ Optional Nodlang::parse_expression(u8_t _precedence, Optional _lef if (!parser_state.ribbon.can_eat()) { - LOG_VERBOSE("Parser", "parse expr... " OK " (last token reached)\n") + LOG_VERBOSE("Parser", "parse expr... " OK " (last token reached)\n"); return left; } if ( !left ) { - LOG_VERBOSE("Parser", "parse expr... left is null, we return it\n") + LOG_VERBOSE("Parser", "parse expr... left is null, we return it\n"); return left; } /* Get the right-handed operand */ - LOG_VERBOSE("Parser", "parse expr... left parsed, we parse right\n") + LOG_VERBOSE("Parser", "parse expr... left parsed, we parse right\n"); Optional expression_out = parse_binary_operator_expression( _precedence, left.value() ); if ( expression_out ) { if (!parser_state.ribbon.can_eat()) { - LOG_VERBOSE("Parser", "parse expr... " OK " right parsed (last token reached)\n") + LOG_VERBOSE("Parser", "parse expr... " OK " right parsed (last token reached)\n"); return expression_out.raw_ptr(); } - LOG_VERBOSE("Parser", "parse expr... " OK " right parsed, recursive call...\n") + LOG_VERBOSE("Parser", "parse expr... " OK " right parsed, recursive call...\n"); return parse_expression(_precedence, expression_out); } - LOG_VERBOSE("Parser", "parse expr... left only " OK "\n") + LOG_VERBOSE("Parser", "parse expr... left only " OK "\n"); return left; } @@ -722,7 +722,7 @@ bool Nodlang::is_syntax_valid() "Syntax Error: Unexpected close bracket after \"... %s\" (position %llu)\n", parser_state.ribbon.concat_token_buffers(token->m_index, -10).c_str(), token->m_string_start_pos - ) + ); success = false; } opened--; @@ -737,7 +737,7 @@ bool Nodlang::is_syntax_valid() if (opened > 0)// same opened/closed parenthesis count required. { - LOG_ERROR("Parser", "Syntax Error: Bracket count mismatch, %i still opened.\n", opened) + LOG_ERROR("Parser", "Syntax Error: Bracket count mismatch, %i still opened.\n", opened); success = false; } @@ -762,7 +762,7 @@ bool Nodlang::tokenize(const char* buffer, size_t buffer_size) { char buffer_portion[40]; snprintf(buffer_portion, 40, "%s", &buffer[global_cursor]); - LOG_WARNING("Parser", "Scanner Error: unable to tokenize \"%s...\" at index %llu\n", buffer_portion, global_cursor) + LOG_WARNING("Parser", "Scanner Error: unable to tokenize \"%s...\" at index %llu\n", buffer_portion, global_cursor); return false; } @@ -772,7 +772,7 @@ bool Nodlang::tokenize(const char* buffer, size_t buffer_size) if( ignored_chars_size == 0) ignored_chars_start_pos = new_token.m_string_start_pos; ignored_chars_size += new_token.m_string_length; - LOG_VERBOSE("Parser", "Append \"%s\" to ignored chars\n", new_token.buffer_to_string().c_str()) + LOG_VERBOSE("Parser", "Append \"%s\" to ignored chars\n", new_token.buffer_to_string().c_str()); } else // handle ignored_chars_accumulator then push the token in the ribbon and handle ignored_chars_accumulator { @@ -799,7 +799,7 @@ bool Nodlang::tokenize(const char* buffer, size_t buffer_size) } ignored_chars_size = 0; } - LOG_VERBOSE("Parser", "Push token \"%s\" to ribbon\n", new_token.buffer_to_string().c_str()) + LOG_VERBOSE("Parser", "Push token \"%s\" to ribbon\n", new_token.buffer_to_string().c_str()); parser_state.ribbon.push(new_token); } } @@ -980,12 +980,12 @@ Token Nodlang::parse_token(const char* buffer, size_t buffer_size, size_t& globa Optional Nodlang::parse_function_call() { - LOG_VERBOSE("Parser", "parse function call...\n") + LOG_VERBOSE("Parser", "parse function call...\n"); // Check if the minimum token count required is available ( 0: identifier, 1: open parenthesis, 2: close parenthesis) if (!parser_state.ribbon.can_eat(3)) { - LOG_VERBOSE("Parser", "parse function call... " KO " aborted, not enough tokens.\n") + LOG_VERBOSE("Parser", "parse function call... " KO " aborted, not enough tokens.\n"); return nullptr; } @@ -999,7 +999,7 @@ Optional Nodlang::parse_function_call() token_1.m_type == Token_t::parenthesis_open) { fct_id = token_0.word_to_string(); - LOG_VERBOSE("Parser", "parse function call... " OK " regular function pattern detected.\n") + LOG_VERBOSE("Parser", "parse function call... " OK " regular function pattern detected.\n"); } else// Try to parse operator like (ex: operator==(..,..)) { @@ -1008,10 +1008,11 @@ Optional Nodlang::parse_function_call() if (token_0.m_type == Token_t::keyword_operator && token_1.m_type == Token_t::operator_ && token_2.m_type == Token_t::parenthesis_open) { fct_id = token_1.word_to_string();// operator - LOG_VERBOSE("Parser", "parse function call... " OK " operator function-like pattern detected.\n") - } else + LOG_VERBOSE("Parser", "parse function call... " OK " operator function-like pattern detected.\n"); + } + else { - LOG_VERBOSE("Parser", "parse function call... " KO " abort, this is not a function.\n") + LOG_VERBOSE("Parser", "parse function call... " KO " abort, this is not a function.\n"); rollback_transaction(); return nullptr; } @@ -1041,7 +1042,7 @@ Optional Nodlang::parse_function_call() // eat "close bracket supposed" token if (parser_state.ribbon.eat_if(Token_t::parenthesis_close).is_null()) { - LOG_WARNING("Parser", "parse function call... " KO " abort, close parenthesis expected. \n") + LOG_WARNING("Parser", "parse function call... " KO " abort, close parenthesis expected. \n"); rollback_transaction(); return nullptr; } @@ -1057,7 +1058,7 @@ Optional Nodlang::parse_function_call() } commit_transaction(); - LOG_VERBOSE("Parser", "parse function call... " OK "\n") + LOG_VERBOSE("Parser", "parse function call... " OK "\n"); return fct_node->value_out(); } @@ -1076,7 +1077,7 @@ Optional Nodlang::get_current_scope_node() Optional Nodlang::parse_conditional_structure() { - LOG_VERBOSE("Parser", "try to parse conditional structure...\n") + LOG_VERBOSE("Parser", "try to parse conditional structure...\n"); start_transaction(); bool success = false; @@ -1124,33 +1125,33 @@ Optional Nodlang::parse_conditional_structure() /* parse "else" scope */ if ( parse_scope( &if_node->child_slot_at( Branch_FALSE ) ) ) { - LOG_VERBOSE("Parser", "parse IF {...} ELSE {...} block... " OK "\n") + LOG_VERBOSE("Parser", "parse IF {...} ELSE {...} block... " OK "\n"); success = true; } /* or parse "else if" conditional structure */ else if ( parse_conditional_structure() ) { - LOG_VERBOSE("Parser", "parse IF {...} ELSE IF {...} block... " OK "\n") + LOG_VERBOSE("Parser", "parse IF {...} ELSE IF {...} block... " OK "\n"); success = true; } else { - LOG_VERBOSE("Parser", "parse IF {...} ELSE {...} block... " KO "\n") + LOG_VERBOSE("Parser", "parse IF {...} ELSE {...} block... " KO "\n"); } } else { - LOG_VERBOSE("Parser", "parse IF {...} block... " OK "\n") + LOG_VERBOSE("Parser", "parse IF {...} block... " OK "\n"); success = true; } } else { - LOG_VERBOSE("Parser", "parse IF {...} block... " KO "\n") + LOG_VERBOSE("Parser", "parse IF {...} block... " KO "\n"); } } else { - LOG_VERBOSE("Parser", "parse IF (...) <--- close bracket missing { ... } " KO "\n") + LOG_VERBOSE("Parser", "parse IF (...) <--- close bracket missing { ... } " KO "\n"); success = false; } } @@ -1189,18 +1190,18 @@ Optional Nodlang::parse_for_loop() _temp_for_loop_node->token_for = token_for; - LOG_VERBOSE("Parser", "parse FOR (...) block...\n") + LOG_VERBOSE("Parser", "parse FOR (...) block...\n"); Token open_bracket = parser_state.ribbon.eat_if(Token_t::parenthesis_open); if (open_bracket.is_null()) { - LOG_ERROR("Parser", "Unable to find open bracket after for keyword.\n") + LOG_ERROR("Parser", "Unable to find open bracket after for keyword.\n"); } else { Optional init_instr = parse_instr(); if (!init_instr) { - LOG_ERROR("Parser", "Unable to find initial instruction.\n") + LOG_ERROR("Parser", "Unable to find initial instruction.\n"); } else { @@ -1211,7 +1212,7 @@ Optional Nodlang::parse_for_loop() Optional condition = parse_instr(); if ( !condition ) { - LOG_ERROR("Parser", "Unable to find condition instruction.\n") + LOG_ERROR("Parser", "Unable to find condition instruction.\n"); } else { @@ -1222,7 +1223,7 @@ Optional Nodlang::parse_for_loop() Optional iter_instr = parse_instr(); if ( !iter_instr ) { - LOG_ERROR("Parser", "Unable to find iterative instruction.\n") + LOG_ERROR("Parser", "Unable to find iterative instruction.\n"); } else { @@ -1233,11 +1234,11 @@ Optional Nodlang::parse_for_loop() Token close_bracket = parser_state.ribbon.eat_if(Token_t::parenthesis_close); if (close_bracket.is_null()) { - LOG_ERROR("Parser", "Unable to find close bracket after iterative instruction.\n") + LOG_ERROR("Parser", "Unable to find close bracket after iterative instruction.\n"); } else if (!parse_scope( &_temp_for_loop_node->child_slot_at(Branch_TRUE ) ) ) { - LOG_ERROR("Parser", "Unable to parse a scope after for(...).\n") + LOG_ERROR("Parser", "Unable to parse a scope after for(...).\n"); } else { @@ -1283,11 +1284,11 @@ Optional Nodlang::parse_while_loop() _temp_while_loop_node->token_while = token_while; - LOG_VERBOSE("Parser", "parse WHILE (...) { /* block */ }\n") + LOG_VERBOSE("Parser", "parse WHILE (...) { /* block */ }\n"); Token open_bracket = parser_state.ribbon.eat_if(Token_t::parenthesis_open); if (open_bracket.is_null()) { - LOG_ERROR("Parser", "Unable to find open bracket after \"while\"\n") + LOG_ERROR("Parser", "Unable to find open bracket after \"while\"\n"); } else if( Optional cond_instr = parse_instr() ) { @@ -1298,11 +1299,11 @@ Optional Nodlang::parse_while_loop() Token close_bracket = parser_state.ribbon.eat_if(Token_t::parenthesis_close); if ( close_bracket.is_null() ) { - LOG_ERROR("Parser", "Unable to find close bracket after condition instruction.\n") + LOG_ERROR("Parser", "Unable to find close bracket after condition instruction.\n"); } else if (!parse_scope(&_temp_while_loop_node->child_slot_at(Branch_TRUE ) ) ) { - LOG_ERROR("Parser", "Unable to parse a scope after \"while(\".\n") + LOG_ERROR("Parser", "Unable to parse a scope after \"while(\".\n"); } else { @@ -1357,7 +1358,7 @@ Optional Nodlang::parse_variable_declaration() } else { - LOG_ERROR("Parser", "Unable to parse expression to assign %s\n", identifier_token.word_to_string().c_str()) + LOG_ERROR("Parser", "Unable to parse expression to assign %s\n", identifier_token.word_to_string().c_str()); rollback_transaction(); parser_state.graph->destroy(variable_node ); return nullptr; diff --git a/src/ndbl/gui/File.cpp b/src/ndbl/gui/File.cpp index 1cfaf9deb..d60deae70 100644 --- a/src/ndbl/gui/File.cpp +++ b/src/ndbl/gui/File.cpp @@ -21,19 +21,19 @@ File::File() , view() , history() { - LOG_VERBOSE( "File", "Constructor being called ...\n") + LOG_VERBOSE( "File", "Constructor being called ...\n"); // FileView view.init(*this); - LOG_VERBOSE( "File", "View built, creating History ...\n") + LOG_VERBOSE( "File", "View built, creating History ...\n"); // History TextEditor* text_editor = view.get_text_editor(); TextEditorBuffer* text_editor_buf = history.configure_text_editor_undo_buffer(text_editor); view.set_undo_buffer(text_editor_buf); - LOG_VERBOSE( "File", "History built, creating graph ...\n") + LOG_VERBOSE( "File", "History built, creating graph ...\n"); // Graph _graph = new Graph(get_node_factory()); @@ -45,7 +45,7 @@ File::File() if ( auto create_node_action = dynamic_cast(action)) _graph->get_view()->add_action_to_node_menu(create_node_action); - LOG_VERBOSE( "File", "Constructor being called.\n") + LOG_VERBOSE( "File", "Constructor being called.\n"); } File::~File() @@ -139,17 +139,17 @@ bool File::write( File& file, const tools::Path& path) bool File::read( File& file, const tools::Path& path) { - LOG_MESSAGE("File", "\"%s\" loading... (%s).\n", path.filename().c_str(), path.c_str()) + LOG_MESSAGE("File", "\"%s\" loading... (%s).\n", path.filename().c_str(), path.c_str()); if(path.empty() ) { - LOG_ERROR("File", "Path is empty \"%s\"\n", path.c_str()) + LOG_ERROR("File", "Path is empty \"%s\"\n", path.c_str()); return false; } std::ifstream file_stream(path.string()); if (!file_stream.is_open()) { - LOG_ERROR("File", "Unable to load \"%s\"\n", path.c_str()) + LOG_ERROR("File", "Unable to load \"%s\"\n", path.c_str()); return false; } @@ -158,7 +158,7 @@ bool File::read( File& file, const tools::Path& path) file.dirty = false; file.path = path; - LOG_MESSAGE("File", "\"%s\" loaded (%s).\n", path.filename().c_str(), path.c_str()) + LOG_MESSAGE("File", "\"%s\" loaded (%s).\n", path.filename().c_str(), path.c_str()); return true; } diff --git a/src/ndbl/gui/FileView.cpp b/src/ndbl/gui/FileView.cpp index a77cfee65..1c766562d 100644 --- a/src/ndbl/gui/FileView.cpp +++ b/src/ndbl/gui/FileView.cpp @@ -221,8 +221,8 @@ void FileView::set_text(const std::string& text, Isolation mode) { m_text_editor.SetSelection(selectionStart, selectionEnd); } - LOG_MESSAGE("FileView", "Selected text updated from graph.\n") - LOG_VERBOSE("FileView", "%s \n", text.c_str()) + LOG_MESSAGE("FileView", "Selected text updated from graph.\n"); + LOG_VERBOSE("FileView", "%s \n", text.c_str()); } else { @@ -230,8 +230,8 @@ void FileView::set_text(const std::string& text, Isolation mode) // auto cmd = std::make_shared(current_content, text, &m_text_editor); // m_file->get_history()->push_command(cmd); - LOG_MESSAGE("FileView", "Whole text updated from graph.\n") - LOG_VERBOSE("FileView", "%s \n", text.c_str()) + LOG_MESSAGE("FileView", "Whole text updated from graph.\n"); + LOG_VERBOSE("FileView", "%s \n", text.c_str()); } } diff --git a/src/ndbl/gui/GraphView.cpp b/src/ndbl/gui/GraphView.cpp index f8cf4617b..2a1c15b9f 100644 --- a/src/ndbl/gui/GraphView.cpp +++ b/src/ndbl/gui/GraphView.cpp @@ -398,7 +398,7 @@ void GraphView::frame_views(const std::vector& _views, bool _align_to { if (_views.empty()) { - LOG_VERBOSE("GraphView", "Unable to frame views vector. Reason: is empty.\n") + LOG_VERBOSE("GraphView", "Unable to frame views vector. Reason: is empty.\n"); return; } diff --git a/src/ndbl/gui/Nodable.cpp b/src/ndbl/gui/Nodable.cpp index da69290a1..6b5fed472 100644 --- a/src/ndbl/gui/Nodable.cpp +++ b/src/ndbl/gui/Nodable.cpp @@ -356,7 +356,7 @@ void Nodable::update() if ( !complementary_slot ) { // TODO: this case should not happens, instead we should check ahead of time whether or not this not can be attached - LOG_ERROR( "GraphView", "unable to connect this node" ) + LOG_ERROR( "GraphView", "unable to connect this node" ); } else { @@ -390,7 +390,7 @@ void Nodable::update() default: { - LOG_VERBOSE("App", "Ignoring and event, this case is not handled\n") + LOG_VERBOSE("App", "Ignoring and event, this case is not handled\n"); } } @@ -405,7 +405,7 @@ void Nodable::shutdown() for( File* each_file : m_loaded_files ) { - LOG_VERBOSE("ndbl::App", "Delete file %s ...\n", each_file->path.c_str()) + LOG_VERBOSE("ndbl::App", "Delete file %s ...\n", each_file->path.c_str()); delete each_file; } diff --git a/src/tools/core/System.cpp b/src/tools/core/System.cpp index eb87796e8..d150cdbc8 100644 --- a/src/tools/core/System.cpp +++ b/src/tools/core/System.cpp @@ -26,7 +26,7 @@ void System::open_url_async(std::string _URL) if (result != 0) { LOG_ERROR( "tools::system", "Unable to open %s. Because the command %s is not available on your system.", - _URL.c_str(), command.c_str()) + _URL.c_str(), command.c_str()); } return result; diff --git a/src/tools/core/assertions.h b/src/tools/core/assertions.h index 07927a18e..3d4132f05 100644 --- a/src/tools/core/assertions.h +++ b/src/tools/core/assertions.h @@ -34,7 +34,7 @@ static_assert(false, "VERIFY_ is reserved for tools, it should not be defined he #endif #define VERIFY_(expression, message_if_fails )\ -if(!(expression)) { LOG_FLUSH() throw tools::runtime_error(message_if_fails); } +if(!(expression)) { LOG_FLUSH(); throw tools::runtime_error(message_if_fails); } #define ASSERT(expression) VERIFY_( (expression), "Assertion failed: " #expression" is false" ) #define VERIFY(expression, message) VERIFY_( (expression), message ) diff --git a/src/tools/core/log.h b/src/tools/core/log.h index 25e99ef63..54b712344 100644 --- a/src/tools/core/log.h +++ b/src/tools/core/log.h @@ -32,13 +32,13 @@ # define LOG_ERROR(...) \ tools::log::push_message( tools::log::Verbosity_Error , ##__VA_ARGS__ ); \ - tools::log::flush(); -# define LOG_WARNING(...) tools::log::push_message( tools::log::Verbosity_Warning, ##__VA_ARGS__ ); -# define LOG_MESSAGE(...) tools::log::push_message( tools::log::Verbosity_Message, ##__VA_ARGS__ ); -# define LOG_FLUSH() tools::log::flush(); + tools::log::flush() +# define LOG_WARNING(...) tools::log::push_message( tools::log::Verbosity_Warning, ##__VA_ARGS__ ) +# define LOG_MESSAGE(...) tools::log::push_message( tools::log::Verbosity_Message, ##__VA_ARGS__ ) +# define LOG_FLUSH() tools::log::flush() #ifdef TOOLS_DEBUG -# define LOG_VERBOSE(...) tools::log::push_message( tools::log::Verbosity_Verbose, ##__VA_ARGS__ ); +# define LOG_VERBOSE(...) tools::log::push_message( tools::log::Verbosity_Verbose, ##__VA_ARGS__ ) #else # define LOG_VERBOSE(...) #endif diff --git a/src/tools/core/memory/MemoryManager.cpp b/src/tools/core/memory/MemoryManager.cpp index ce3f0a279..c59b44f45 100644 --- a/src/tools/core/memory/MemoryManager.cpp +++ b/src/tools/core/memory/MemoryManager.cpp @@ -29,7 +29,7 @@ void tools::shutdown_memory_manager() MemoryStats stats = *g_memory_stats; // copy locally before to delete if(stats.alloc_count() != 0) { - LOG_WARNING("tools", "shutdown_memory_manager() - %zu B is still in use (%zu alloc(s))\n", g_memory_stats->mem_usage(), g_memory_stats->alloc_count() ) + LOG_WARNING("tools", "shutdown_memory_manager() - %zu B is still in use (%zu alloc(s))\n", g_memory_stats->mem_usage(), g_memory_stats->alloc_count() ); } delete g_memory_stats; // this would decrease counters diff --git a/src/tools/core/reflection/TypeRegister.cpp b/src/tools/core/reflection/TypeRegister.cpp index 9682565fd..f11924a19 100644 --- a/src/tools/core/reflection/TypeRegister.cpp +++ b/src/tools/core/reflection/TypeRegister.cpp @@ -51,7 +51,7 @@ TypeDescriptor* TypeRegister::merge(TypeDescriptor* existing, const TypeDescript "Merge existing: \"%s\" (%s), with: \"%s\" (%s)\n", existing->m_name.c_str(), existing->m_compiler_name, other->m_name.c_str(), other->m_compiler_name - ) + ); if( existing->m_name[0] == '\0' ) { existing->m_name = other->m_name; @@ -72,7 +72,7 @@ TypeDescriptor* TypeRegister::merge(TypeDescriptor* existing, const TypeDescript void TypeRegister::log_statistics() { LOG_MESSAGE("reflection", "Logging reflected types ...\n"); - LOG_MESSAGE("reflection", " %-16s %-25s %-60s\n", "-- type hash --", "-- user name --", "-- compiler name --" ) + LOG_MESSAGE("reflection", " %-16s %-25s %-60s\n", "-- type hash --", "-- user name --", "-- compiler name --" ); for ( const auto& [type_hash, type] : by_index() ) { diff --git a/src/tools/gui/FontManager.cpp b/src/tools/gui/FontManager.cpp index 510558787..aaada5bc1 100644 --- a/src/tools/gui/FontManager.cpp +++ b/src/tools/gui/FontManager.cpp @@ -68,7 +68,7 @@ ImFont* FontManager::load_font(const FontConfig& font_config) imfont_cfg.OversampleH = 2; imfont_cfg.OversampleV = 3; tools::Path absolute_path = App::get_absolute_asset_path(font_config.path); - LOG_VERBOSE("NodableView", "Adding text_font from file ... %s\n", absolute_path.c_str()) + LOG_VERBOSE("NodableView", "Adding text_font from file ... %s\n", absolute_path.c_str()); font = io.Fonts->AddFontFromFileTTF(absolute_path.string().c_str(), font_config.size * m_config->subsamples, &imfont_cfg); } @@ -93,13 +93,13 @@ ImFont* FontManager::load_font(const FontConfig& font_config) imfont_cfg.GlyphMinAdvanceX = font_config.icons_size * m_config->subsamples; // monospace to fix text alignment in drop down menus. tools::Path absolute_path = App::get_absolute_asset_path(m_config->icon.path); font = io.Fonts->AddFontFromFileTTF(absolute_path.string().c_str(), font_config.icons_size * m_config->subsamples, &imfont_cfg, icons_ranges); - LOG_VERBOSE("NodableView", "Merging icons font ...\n") + LOG_VERBOSE("NodableView", "Merging icons font ...\n"); } font->Scale = 1.0f / m_config->subsamples; m_loaded_fonts.insert_or_assign(font_config.id, font); - LOG_MESSAGE("NodableView", "Font %s added: \"%s\"\n", font_config.id, font_config.path ) + LOG_MESSAGE("NodableView", "Font %s added: \"%s\"\n", font_config.id, font_config.path ); return font; } diff --git a/src/tools/gui/TextureManager.cpp b/src/tools/gui/TextureManager.cpp index ede8962c3..4a9eabb50 100644 --- a/src/tools/gui/TextureManager.cpp +++ b/src/tools/gui/TextureManager.cpp @@ -57,11 +57,11 @@ bool TextureManager::release_all() if( GL_NO_ERROR != glGetError()) { success = false; - LOG_WARNING("TextureManager", "Unable to release: %s (code: %i)\n", key.c_str(), glGetError()) + LOG_WARNING("TextureManager", "Unable to release: %s (code: %i)\n", key.c_str(), glGetError()); } else { - LOG_MESSAGE("TextureManager", "Released %s\n", key.c_str()) + LOG_MESSAGE("TextureManager", "Released %s\n", key.c_str()); } } delete texture; @@ -87,12 +87,12 @@ Texture *TextureManager::load_png_to_gpu(const Path &path) if ( error ) { delete texture; - LOG_ERROR("TextureManager", "Unable to load texture to GPU (code %u): %s\n", error, path.c_str()) + LOG_ERROR("TextureManager", "Unable to load texture to GPU (code %u): %s\n", error, path.c_str()); return nullptr; } m_register.emplace(path.string(), texture); - LOG_MESSAGE("TextureManager", "File loaded to GPU: %s\n", path.c_str()) + LOG_MESSAGE("TextureManager", "File loaded to GPU: %s\n", path.c_str()); return texture; }