Skip to content

Commit

Permalink
Merge pull request #130 from berdal84/nodable-v1.0-alpha
Browse files Browse the repository at this point in the history
refactor(tools::type): naming
  • Loading branch information
berdal84 authored Oct 14, 2024
2 parents b957b34 + 8555781 commit 598ea91
Show file tree
Hide file tree
Showing 57 changed files with 617 additions and 712 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Author: Bérenger Dalle-Cort, 2017-2024
### v1.0:
- graph is now always abstract
- interpreter (build, run, debug etc) is disabled by default and is considered out of scope, but can be enabled via the Developer>Experimental menu,
- improved the Graph user interface to create graph more easily,
- improved the Graph user interface to create_new graph more easily,
- implemented an optimized StateMachine to simplify code (it avoids an OOP version of it! Thanks to Rémi ;)),
- refactor the whole app (uses init/get/shutdown pattern for managers),
- fix memory leaks,
Expand Down
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ add_library(
src/tools/core/reflection/Operator_t.h
src/tools/core/reflection/Initializer.h
src/tools/core/reflection/enum.h
src/tools/core/reflection/FuncType.cpp
src/tools/core/reflection/FuncType.h
src/tools/core/reflection/qword.cpp
src/tools/core/reflection/qword.h
src/tools/core/reflection/reflection
Expand Down Expand Up @@ -199,7 +197,7 @@ target_link_libraries(
add_executable(
test-tools-core
src/tools/core/reflection/reflection.specs.cpp
src/tools/core/reflection/FuncType.builder.specs.cpp
src/tools/core/reflection/Type.specs.cpp
src/tools/core/memory/Pool.specs.cpp
src/tools/core/memory/MemoryManager.specs.cpp
)
Expand Down Expand Up @@ -356,7 +354,7 @@ add_library(
src/ndbl/core/ForLoopNode.cpp
src/ndbl/core/Graph.cpp
src/ndbl/core/GraphUtil.cpp
src/ndbl/core/InvokableNode.cpp
src/ndbl/core/FunctionNode.cpp
src/ndbl/core/LiteralNode.cpp
src/ndbl/core/Node.cpp
src/ndbl/core/NodeFactory.cpp
Expand Down
2 changes: 1 addition & 1 deletion HOW-TO-BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ Few details about the commands above:

- `--recurse-submodules` is important when cloning since *Nodable* needs other git repositories to be built.
- `--branch v<major>.<minor>.<patch>` is to target a specific tag, it is recommended to get a stable version. You can try a more recent if you wish. Browse [tags list](https://github.com/berdal84/nodable/tags).
- `--target install` is to create a clean `out/app` directory with only the necessary files to run the software.
- `--target install` is to create_new a clean `out/app` directory with only the necessary files to run the software.

10 changes: 5 additions & 5 deletions src/ndbl/cli/CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void CLI::update()
}

// Priority 1: call a static function immediately
const ClassDesc* api_class = type::get_class<PublicApi>();
const ClassDescriptor* api_class = type::get_class<PublicApi>();
if( auto static_fct = api_class->get_static(user_input.c_str()) )
{
try
Expand Down Expand Up @@ -96,7 +96,7 @@ void CLI::clear()
NodableHeadless::clear();
}

void CLI::log_function_call(const variant &result, const FuncType *type)
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<std::string>().c_str())
}
Expand Down Expand Up @@ -167,7 +167,7 @@ void CLI::PublicApi::help()
{
std::vector<std::string> command_names;

const ClassDesc* api_class = type::get_class<PublicApi>();
const ClassDescriptor* api_class = type::get_class<PublicApi>();

for ( const IInvokable* invokable : api_class->get_statics() )
{
Expand Down Expand Up @@ -222,7 +222,7 @@ bool CLI::PublicApi::run()
return m_cli->run();
}

variant CLI::invoke_static(const FuncType* _func_type, std::vector<variant>&& _args) const
variant CLI::invoke_static(const FunctionDescriptor* _func_type, std::vector<variant>&& _args) const
{
variant result;

Expand All @@ -232,7 +232,7 @@ variant CLI::invoke_static(const FuncType* _func_type, std::vector<variant>&& _a
return result;
}

variant CLI::invoke_method(const FuncType* _func_type, std::vector<variant>&& _args) const
variant CLI::invoke_method(const FunctionDescriptor* _func_type, std::vector<variant>&& _args) const
{
variant result;

Expand Down
6 changes: 3 additions & 3 deletions src/ndbl/cli/CLI.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ namespace ndbl
~CLI() override = default;

private:
tools::variant invoke_static(const tools::FuncType* _func_type, std::vector<tools::variant>&& _args) const;
tools::variant invoke_method(const tools::FuncType* _func_type, std::vector<tools::variant>&& _args) const;
tools::variant invoke_static(const tools::FunctionDescriptor* _func_type, std::vector<tools::variant>&& _args) const;
tools::variant invoke_method(const tools::FunctionDescriptor* _func_type, std::vector<tools::variant>&& _args) const;

static std::string get_line() ;
static void log_function_call(const tools::variant &result, const tools::FuncType *type) ;
static void log_function_call(const tools::variant &result, const tools::FunctionDescriptor *type) ;
};
}
10 changes: 5 additions & 5 deletions src/ndbl/core/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "ndbl/core/ForLoopNode.h"
#include "ndbl/core/Graph.h"
#include "ndbl/core/IfNode.h"
#include "ndbl/core/InvokableNode.h"
#include "ndbl/core/FunctionNode.h"
#include "ndbl/core/LiteralNode.h"
#include "ndbl/core/Scope.h"
#include "ndbl/core/VariableNode.h"
Expand Down Expand Up @@ -65,7 +65,7 @@ bool Compiler::is_syntax_tree_valid(const Graph* _graph)

case NodeType_OPERATOR:
{
auto* invokable = static_cast<const InvokableNode*>(each_node);
auto* invokable = static_cast<const FunctionNode*>(each_node);
if ( !language->find_operator_fct(invokable->get_func_type()) )
{
std::string signature;
Expand All @@ -76,7 +76,7 @@ bool Compiler::is_syntax_tree_valid(const Graph* _graph)
}
case NodeType_FUNCTION:
{
auto* invokable = static_cast<const InvokableNode*>(each_node);
auto* invokable = static_cast<const FunctionNode*>(each_node);
if ( !language->find_function(invokable->get_func_type()) )
{
std::string signature;
Expand Down Expand Up @@ -198,7 +198,7 @@ void Compiler::compile_node( const Node* _node )
case NodeType_OPERATOR:
{
Instruction* instr = m_temp_code->push_instr(OpCode_call);
const FuncType* func_type = static_cast<const InvokableNode*>(_node)->get_func_type();
const FunctionDescriptor* func_type = static_cast<const FunctionNode*>(_node)->get_func_type();

const IInvokable* invokable = get_language()->find_function( func_type ); // Get exact OR fallback function (in case of arg cast)
VERIFY(invokable != nullptr, "Unable to find this function")
Expand Down Expand Up @@ -345,7 +345,7 @@ const Code* Compiler::compile_syntax_tree(const Graph* _graph)
{
delete m_temp_code;
m_temp_code = nullptr;
LOG_ERROR("Compiler", "Unable to create assembly code for program. Reason: %s\n", e.what());
LOG_ERROR("Compiler", "Unable to create_new assembly code for program. Reason: %s\n", e.what());
}
return m_temp_code;
}
Expand Down
35 changes: 15 additions & 20 deletions src/ndbl/core/InvokableNode.cpp → src/ndbl/core/FunctionNode.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#include "InvokableNode.h"
#include "FunctionNode.h"

#include "tools/core/log.h"
#include "tools/core/memory/memory.h"
#include "tools/core/reflection/FuncType.h"
#include "tools/core/reflection/Type.h"

using namespace ndbl;
using namespace tools;

REFLECT_STATIC_INIT
{
type::Initializer<InvokableNode>("InvokableNode").extends<Node>();
type::Initializer<FunctionNode>("FunctionNode").extends<Node>();
}

void InvokableNode::init(NodeType _type, tools::FuncType&& _func_type )
void FunctionNode::init(NodeType _type, const tools::FunctionDescriptor* _func_type )
{
Node::init(_type, _func_type.get_identifier());
Node::init(_type, _func_type->get_identifier());

m_func_type = _func_type;
m_identifier_token = {
Token_t::identifier,
_func_type.get_identifier()
_func_type->get_identifier()
};
m_argument_slot.resize(_func_type.get_arg_count());
m_argument_props.resize(_func_type.get_arg_count());
m_argument_slot.resize(_func_type->get_arg_count());
m_argument_props.resize(_func_type->get_arg_count());

add_slot( SlotFlag_PREV, Slot::MAX_CAPACITY );
add_slot(SlotFlag_OUTPUT, 1);

switch ( _type )
{
case NodeType_OPERATOR:
set_name(_func_type.get_identifier());
set_name(_func_type->get_identifier());
break;
case NodeType_FUNCTION:
{
const std::string& id = _func_type.get_identifier();
const std::string& id = _func_type->get_identifier();
std::string label = id + "()";
std::string short_label = id.substr(0, 2) + "..()"; // ------- improve, not great.
set_name(label.c_str());
Expand All @@ -45,19 +45,19 @@ void InvokableNode::init(NodeType _type, tools::FuncType&& _func_type )
}

// Create a result/value
Property* value = add_prop(_func_type.get_return_type(), VALUE_PROPERTY );
Property* value = add_prop(_func_type->get_return_type(), VALUE_PROPERTY );
add_slot(SlotFlag_OUTPUT, Slot::MAX_CAPACITY, value);

// Create arguments
if ( _type == NodeType_OPERATOR )
{
VERIFY(_func_type.get_arg_count() >= 1, "An operator must have one argument minimum");
VERIFY(_func_type.get_arg_count() <= 2, "An operator cannot have more than 2 arguments");
VERIFY(_func_type->get_arg_count() >= 1, "An operator must have one argument minimum");
VERIFY(_func_type->get_arg_count() <= 2, "An operator cannot have more than 2 arguments");
}

for (size_t i = 0; i < _func_type.get_arg_count(); i++ )
for (size_t i = 0; i < _func_type->get_arg_count(); i++ )
{
const FuncArg& arg = _func_type.get_arg(i);
const FuncArg& arg = _func_type->get_arg(i);

const char* name;
// TODO: this could be done in the NodeView instead...
Expand All @@ -81,9 +81,4 @@ void InvokableNode::init(NodeType _type, tools::FuncType&& _func_type )
m_argument_slot[i] = add_slot(SlotFlag_INPUT, 1, property);
m_argument_props[i] = property;
}
}

const std::vector<Slot*>& InvokableNode::get_arg_slots() const
{
return m_argument_slot;
}
14 changes: 5 additions & 9 deletions src/ndbl/core/InvokableNode.h → src/ndbl/core/FunctionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@

namespace ndbl
{
/**
* @brief ComputeFunction extends Compute base to provide a Component that represents a Function.
* This function has_flags some arguments.
*/
class InvokableNode : public Node
class FunctionNode : public Node
{
public:
void init(NodeType node_type, tools::FuncType&& func_type);
const std::vector<Slot*>& get_arg_slots() const;
const tools::FuncType* get_func_type()const { return &m_func_type; }
void init(NodeType node_type, const tools::FunctionDescriptor* func_type);
const std::vector<Slot*>& get_arg_slots() const { return m_argument_slot; }
const tools::FunctionDescriptor* get_func_type()const { return m_func_type; }
const Token& get_identifier_token() const { return m_identifier_token; }
void set_identifier_token(const Token& tok) { m_identifier_token = tok; }
Slot* get_lvalue() const { return m_argument_slot[0]; }
Slot* get_rvalue() const { return m_argument_slot[1]; }
protected:
Token m_identifier_token = Token::s_null;
tools::FuncType m_func_type;
const tools::FunctionDescriptor* m_func_type; // not owned
std::vector<Slot*> m_argument_slot;
std::vector<Property*> m_argument_props;

Expand Down
26 changes: 13 additions & 13 deletions src/ndbl/core/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ void Graph::ensure_has_root()
}
}

VariableNode* Graph::create_variable(const TypeDesc *_type, const std::string& _name, Scope* _scope)
VariableNode* Graph::create_variable(const TypeDescriptor *_type, const std::string& _name, Scope* _scope)
{
VariableNode* node = m_factory->create_variable(_type, _name, _scope);
add(node);
return node;
}

InvokableNode* Graph::create_function(FuncType&& _type)
FunctionNode* Graph::create_function(const FunctionDescriptor* _type)
{
InvokableNode* node = m_factory->create_function(std::move(_type), NodeType_FUNCTION);
FunctionNode* node = m_factory->create_function(_type, NodeType_FUNCTION);
add(node);
return node;
}

InvokableNode* Graph::create_operator(FuncType&& _type)
FunctionNode* Graph::create_operator(const FunctionDescriptor* _type)
{
InvokableNode* node = m_factory->create_function(std::move(_type), NodeType_OPERATOR);
FunctionNode* node = m_factory->create_function(_type, NodeType_OPERATOR);
add(node);
return node;
}
Expand Down Expand Up @@ -477,14 +477,14 @@ Node* Graph::create_node()
return node;
}

LiteralNode* Graph::create_literal(const TypeDesc *_type)
LiteralNode* Graph::create_literal(const TypeDescriptor *_type)
{
LiteralNode* node = m_factory->create_literal(_type);
add(node);
return node;
}

Node* Graph::create_node( CreateNodeType _type, const FuncType* _signature )
Node* Graph::create_node( CreateNodeType _type, const FunctionDescriptor* _signature )
{
switch ( _type )
{
Expand All @@ -508,23 +508,23 @@ Node* Graph::create_node( CreateNodeType _type, const FuncType* _signature )
case CreateNodeType_LITERAL_INTEGER: return create_literal<int>();
case CreateNodeType_LITERAL_STRING: return create_literal<std::string>();

case CreateNodeType_INVOKABLE:
case CreateNodeType_FUNCTION:
{
VERIFY(_signature != nullptr, "_signature is expected when dealing with functions or operators")
Nodlang* language = get_language();
// Currently, we handle operators and functions the exact same way
FuncType signature = *language->find_function(_signature)->get_sig();
bool is_operator = language->find_operator_fct( &signature ) != nullptr;
const FunctionDescriptor* signature = language->find_function(_signature)->get_sig();
bool is_operator = language->find_operator_fct( signature ) != nullptr;
if ( is_operator )
return create_operator(std::move(signature));
return create_function(std::move(signature));
return create_operator(signature);
return create_function(signature);
}
default:
VERIFY(false, "Unhandled CreateNodeType.");
}
}

VariableNode* Graph::create_variable_decl(const TypeDesc* _type, const char* _name, Scope* _scope)
VariableNode* Graph::create_variable_decl(const TypeDescriptor* _type, const char* _name, Scope* _scope)
{
if( !_scope)
{
Expand Down
15 changes: 7 additions & 8 deletions src/ndbl/core/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "tools/core/types.h"

#include "IScope.h"
#include "tools/core/reflection/FuncType.h"

namespace ndbl
{
Expand Down Expand Up @@ -43,7 +42,7 @@ namespace ndbl
CreateNodeType_LITERAL_DOUBLE,
CreateNodeType_LITERAL_INTEGER,
CreateNodeType_LITERAL_STRING,
CreateNodeType_INVOKABLE,
CreateNodeType_FUNCTION,
};

/**
Expand All @@ -63,19 +62,19 @@ namespace ndbl
// node related

Node* create_node(); // Create a raw node.
Node* create_node(CreateNodeType, const tools::FuncType* _signature = nullptr); // Create a given node type in a simple way.
Node* create_node(CreateNodeType, const tools::FunctionDescriptor* _signature = nullptr); // Create a given node type in a simple way.
Node* create_root();
VariableNode* create_variable(const tools::TypeDesc *_type, const std::string &_name, Scope* _scope);
VariableNode* create_variable_decl(const tools::TypeDesc* _type, const char* _name, Scope* _scope);
VariableNode* create_variable(const tools::TypeDescriptor *_type, const std::string &_name, Scope* _scope);
VariableNode* create_variable_decl(const tools::TypeDescriptor* _type, const char* _name, Scope* _scope);
template<typename T>
VariableNode* create_variable_decl(const char* _name = "var", Scope* _scope = {})
{ return create_variable_decl(tools::type::get<T>(), _name, _scope); }

LiteralNode* create_literal(const tools::TypeDesc *_type);
LiteralNode* create_literal(const tools::TypeDescriptor *_type);
template<typename T>
LiteralNode* create_literal() { return create_literal( tools::type::get<T>()); }
InvokableNode* create_function(tools::FuncType&&);
InvokableNode* create_operator(tools::FuncType&&);
FunctionNode* create_function(const tools::FunctionDescriptor*);
FunctionNode* create_operator(const tools::FunctionDescriptor*);
Node* create_scope();
IfNode* create_cond_struct();
ForLoopNode* create_for_loop();
Expand Down
Loading

0 comments on commit 598ea91

Please sign in to comment.