Variables in context, fix variable and print node save data in JSON
Some checks failed
Build-StoryEditor / build_linux (push) Has been cancelled
Build-StoryEditor / build_win32 (push) Has been cancelled
Deploy-Documentation / deploy (push) Has been cancelled

This commit is contained in:
anthony@rabine.fr 2025-04-26 21:14:20 +02:00
parent 6ec1f39db7
commit 6a92fe745d
15 changed files with 183 additions and 130 deletions

View file

@ -29,25 +29,39 @@ public:
struct GeneratorContext
{
std::string timestamp;
std::string username;
std::string compiler;
bool debugOutput;
bool optimizeCode;
int stackSize;
std::vector<std::shared_ptr<Variable>> variables;
GeneratorContext(const std::string& ts = "2025-04-08 12:09:01",
const std::string& user = "anonymous",
GeneratorContext(std::vector<std::shared_ptr<Variable>> &v,
const std::string& ts = "2025-04-08 12:09:01",
const std::string& comp = "ost-v1",
bool debug = true,
bool optimize = true,
int stack = 1024)
: timestamp(ts)
, username(user)
int stack = 1024
)
: variables(v)
, compiler(comp)
, debugOutput(debug)
, optimizeCode(optimize)
, stackSize(stack)
, timestamp(ts)
{}
// Find variable by uuid
std::shared_ptr<Variable> FindVariableByUuid(const std::string& uuid) const {
for (const auto& var : variables) {
if (var->GetUuid() == uuid) {
return var;
}
}
return nullptr;
}
};
AssemblyGenerator(const GeneratorContext& context = GeneratorContext())
AssemblyGenerator(const GeneratorContext& context)
: m_context(context)
{
Reset();
@ -56,9 +70,7 @@ public:
void Reset() {
m_assembly.str("");
m_labelCounter = 0;
m_variableAddresses.clear();
m_currentStackOffset = 0;
m_stringLiterals.clear();
m_depth = 0;
m_currentSection = Section::NONE;
}
@ -66,7 +78,7 @@ public:
void GenerateHeader() {
AddComment("Assembly generated by Visual Node Editor");
AddComment("Generation time: " + m_context.timestamp);
AddComment("Generated by: " + m_context.username);
AddComment("Generated by: " + m_context.compiler);
AddComment("Optimization: " + m_context.optimizeCode ? "enabled" : "disabled");
}
@ -99,9 +111,9 @@ public:
m_assembly << "\n\n";
}
void GenerateGlobalVariables(const std::vector<std::shared_ptr<Variable>> &variables)
void GenerateGlobalVariables()
{
for (const auto& v : variables) {
for (const auto& v : m_context.variables) {
GenerateVariable(v);
}
}
@ -129,31 +141,18 @@ protected:
virtual void GenerateVariable(const std::shared_ptr<Variable> v) = 0;
std::string AddStringLiteral(const std::string& text)
{
std::string label = "str_" + std::to_string(m_stringLiterals.size());
m_stringLiterals.push_back(text);
return label;
}
std::string GenerateUniqueLabel(const std::string& prefix)
{
return prefix + "_" + std::to_string(m_labelCounter++);
}
protected:
GeneratorContext m_context;
std::stringstream m_assembly;
int m_labelCounter;
std::unordered_map<std::string, std::string> m_variableAddresses;
int m_currentStackOffset;
std::vector<std::string> m_stringLiterals;
int m_depth{0};
Section m_currentSection;
private:

View file

@ -6,7 +6,7 @@
class AssemblyGeneratorChip32 : public AssemblyGenerator
{
public:
AssemblyGeneratorChip32(const GeneratorContext& context = GeneratorContext())
AssemblyGeneratorChip32(const GeneratorContext& context)
: AssemblyGenerator(context)
{
@ -117,8 +117,10 @@ private:
{
auto* varNode = inputNode->GetAs<VariableNode>();
if (varNode) {
auto var = varNode->GetVariable();
std::string varUuid = varNode->GetVariableUuid();
// Find variable in the context
auto var = m_context.FindVariableByUuid(varUuid);
if (var)
{
// Generate code to load the variable value
@ -230,18 +232,7 @@ private:
m_assembly << "$" << v->GetLabel() << " DVB, " << (v->GetValue<bool>() ? "1" : "0") << " ; " << v->GetVariableName() << "\n";
}
}
/*
void GenerateVariableNode(std::shared_ptr<ASTNode> node) {
auto* varNode = node->GetAs<VariableNode>();
if (!varNode) return;
std::string varName = varNode->GetVariableName();
AddComment("Load variable: " + varName);
m_assembly << " mov eax, [" << m_variableAddresses[varName] << "]\n"
<< " push eax\n";
}
*/
};

View file

@ -11,15 +11,6 @@ FunctionNode::FunctionNode(const std::string &type)
SetInternalData(j);
}
void FunctionNode::StoreInternalData()
{
nlohmann::json j;
// j["image"] = m_image;
// j["sound"] = m_sound;
SetInternalData(j);
}
void FunctionNode::Initialize()
{
nlohmann::json j = GetInternalData();

View file

@ -13,8 +13,6 @@ public:
virtual void Initialize() override;
void StoreInternalData();
private:
};

View file

@ -9,14 +9,34 @@ PrintNode::PrintNode(const std::string &type)
{
// Create empty variable in memory
auto v = std::make_shared<Variable>(m_label);
v->SetTextValue("");
v->SetConstant(true);
m_label = v->GetLabel();
m_variables[m_label] = v;
}
SetText("");
}
void PrintNode::Initialize()
{
nlohmann::json j = GetInternalData();
m_variables.at(m_label)->SetTextValue(j["text"].get<std::string>());
}
void PrintNode::SetText(const std::string &text)
{
m_variables.at(m_label)->SetValue<std::string>(text);
SetInternalData({{"text", text}});
}
std::string PrintNode::GetLabel() const
{
return m_label;
}
std::string PrintNode::GetText() const
{
return m_variables.at(m_label)->GetValue<std::string>();
}

View file

@ -13,18 +13,9 @@ public:
virtual void Initialize() override;
void SetText(const std::string &text) {
m_variables.at(m_label)->SetValue<std::string>(text);
}
std::string GetLabel() const {
return m_label;
}
std::string GetText() const {
return m_variables.at(m_label)->GetValue<std::string>();
}
void SetText(const std::string &text);
std::string GetLabel() const;
std::string GetText() const;
private:
std::string m_label; // Label for the string literal

View file

@ -0,0 +1,22 @@
#include "syscall_node.h"
#include "story_project.h"
#include "connection.h"
#include "sys_lib.h"
SyscallNode::SyscallNode(const std::string &type)
: BaseNode(type, "Syscall Node")
{
nlohmann::json j{ {"uuid", ""} };
SetInternalData(j);
}
void SyscallNode::Initialize()
{
nlohmann::json j = GetInternalData();
// m_variableUuid = j["uuid"].get<std::string>();
}

View file

@ -0,0 +1,23 @@
#pragma once
#include <string>
#include "variable.h"
#include "i_story_manager.h"
#include "base_node.h"
#include "i_script_node.h"
#include "i_story_project.h"
class SyscallNode : public BaseNode
{
public:
SyscallNode(const std::string &type = "syscall-node");
virtual void Initialize() override;
private:
};

View file

@ -7,24 +7,29 @@
VariableNode::VariableNode(const std::string &type)
: BaseNode(type, "Variable Node")
{
nlohmann::json j{ {"name", "i"}, {"value", 3} };
SetInternalData(j);
}
void VariableNode::StoreInternalData()
{
nlohmann::json j;
// j["image"] = m_image;
// j["sound"] = m_sound;
nlohmann::json j{ {"uuid", ""} };
SetInternalData(j);
}
void VariableNode::Initialize()
{
nlohmann::json j = GetInternalData();
// m_image = j["image"].get<std::string>();
// m_sound = j["sound"].get<std::string>();
m_variableUuid = j["uuid"].get<std::string>();
}
void VariableNode::SetVariableUuid(const std::string &uuid)
{
m_variableUuid = uuid;
nlohmann::json j;
j["uuid"] = m_variableUuid;
SetInternalData(j);
}
std::string VariableNode::GetVariableUuid() const
{
return m_variableUuid;
}

View file

@ -16,18 +16,12 @@ public:
virtual void Initialize() override;
void StoreInternalData();
void SetVariableUuid(const std::string &uuid);
void SetVariable(const std::shared_ptr<Variable> v) {
m_variable = v;
}
std::shared_ptr<Variable> GetVariable() const {
return m_variable;
}
std::string GetVariableUuid() const;
private:
std::shared_ptr<Variable> m_variable;
std::string m_variableUuid;
};

View file

@ -394,6 +394,7 @@ bool StoryProject::GenerateScript(std::string &codeStr)
// Create generator context with current time and user
AssemblyGenerator::GeneratorContext context(
m_variables,
"2025-04-08 12:09:01", // Current UTC time
"story-editor", // Current user
true, // Enable debug output
@ -424,7 +425,7 @@ bool StoryProject::GenerateScript(std::string &codeStr)
{
p->BuildNodesVariables(generator);
}
generator.GenerateGlobalVariables(m_variables);
generator.GenerateGlobalVariables();
generator.GenerateExit();

View file

@ -9,67 +9,67 @@ Size=400,400
Collapsed=0
[Window][Library Manager]
Pos=900,26
Size=380,322
Pos=731,26
Size=549,268
Collapsed=0
DockId=0x00000003,0
[Window][Console]
Pos=60,545
Size=1220,175
Pos=60,467
Size=610,253
Collapsed=0
DockId=0x00000008,0
DockId=0x00000004,0
[Window][Emulator]
Pos=398,26
Size=500,517
Pos=731,26
Size=549,268
Collapsed=0
DockId=0x00000005,1
DockId=0x00000003,5
[Window][Code viewer]
Pos=398,26
Size=500,517
Pos=731,26
Size=549,268
Collapsed=0
DockId=0x00000005,0
DockId=0x00000003,4
[Window][Resources]
Pos=900,26
Size=380,322
Pos=731,26
Size=549,268
Collapsed=0
DockId=0x00000003,1
[Window][Node editor]
Pos=60,26
Size=336,517
Size=669,439
Collapsed=0
DockId=0x00000004,0
DockId=0x00000001,0
[Window][TOOLBAR]
Pos=92,208
Pos=76,71
Size=79,42
Collapsed=0
[Window][Variables]
Pos=398,26
Size=500,517
Pos=672,467
Size=608,253
Collapsed=0
DockId=0x00000005,2
DockId=0x00000005,0
[Window][CPU]
Pos=900,26
Size=380,322
Pos=731,26
Size=549,268
Collapsed=0
DockId=0x00000003,2
[Window][RAM view]
Pos=900,26
Size=380,322
Pos=731,26
Size=549,268
Collapsed=0
DockId=0x00000003,3
[Window][Properties]
Pos=900,350
Size=380,193
Pos=731,296
Size=549,169
Collapsed=0
DockId=0x00000006,0
@ -83,6 +83,11 @@ Pos=479,312
Size=321,96
Collapsed=0
[Window][ProjectPropertiesPopup]
Pos=381,236
Size=518,248
Collapsed=0
[Table][0x7728942D,5]
RefScale=20
Column 0 Width=44 Sort=0v
@ -104,12 +109,12 @@ Column 2 Width=124
[Docking][Data]
DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=60,26 Size=1220,694 Split=Y
DockNode ID=0x00000007 Parent=0x08BD597D SizeRef=1220,517 Split=X
DockNode ID=0x00000001 Parent=0x00000007 SizeRef=838,694 Split=X
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=336,600 CentralNode=1 Selected=0xBB79A587
DockNode ID=0x00000005 Parent=0x00000001 SizeRef=500,600 Selected=0x52EB28B5
DockNode ID=0x00000002 Parent=0x00000007 SizeRef=380,694 Split=Y Selected=0xE5897A33
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=608,322 Selected=0x63869CAF
DockNode ID=0x00000006 Parent=0x00000002 SizeRef=608,193 Selected=0x8C72BEA8
DockNode ID=0x00000008 Parent=0x08BD597D SizeRef=1220,175 Selected=0xEA83D666
DockNode ID=0x00000007 Parent=0x08BD597D SizeRef=1220,439 Split=X
DockNode ID=0x00000001 Parent=0x00000007 SizeRef=669,694 CentralNode=1 Selected=0xBB79A587
DockNode ID=0x00000002 Parent=0x00000007 SizeRef=549,694 Split=Y Selected=0x52EB28B5
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=718,268 Selected=0x63869CAF
DockNode ID=0x00000006 Parent=0x00000002 SizeRef=718,169 Selected=0x8C72BEA8
DockNode ID=0x00000008 Parent=0x08BD597D SizeRef=1220,253 Split=X Selected=0xEA83D666
DockNode ID=0x00000004 Parent=0x00000008 SizeRef=610,192 Selected=0xEA83D666
DockNode ID=0x00000005 Parent=0x00000008 SizeRef=608,192 Selected=0x6DE9B20C

View file

@ -21,8 +21,16 @@ VariableNodeWidget::VariableNodeWidget(IStoryManager &manager, std::shared_ptr<B
void VariableNodeWidget::Initialize()
{
BaseNodeWidget::Initialize();
}
m_selectedVariableUuid = m_variableNode->GetVariableUuid();
m_manager.ScanVariable([this] (std::shared_ptr<Variable> var) {
if (var->GetUuid() == m_selectedVariableUuid)
{
m_selectedVariableName = var->GetVariableName();
}
});
}
void VariableNodeWidget::DrawProperties()
@ -30,7 +38,7 @@ void VariableNodeWidget::DrawProperties()
ImGui::AlignTextToFramePadding();
static ImGuiComboFlags flags = 0;
if (ImGui::BeginCombo("Variables list", m_selectedVariable.c_str(), flags))
if (ImGui::BeginCombo("Variables list", m_selectedVariableName.c_str(), flags))
{
int i = 0;
m_manager.ScanVariable([&i, this] (std::shared_ptr<Variable> var) {
@ -42,13 +50,15 @@ void VariableNodeWidget::DrawProperties()
if (ImGui::Selectable(l.c_str(), is_selected))
{
m_selectedIndex = i;
m_selectedVariable = l;
m_variableNode->SetVariable(var);
m_selectedVariableName = l;
m_variableNode->SetVariableUuid(var->GetUuid());
}
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
i++;
});
ImGui::EndCombo();
}
@ -59,7 +69,7 @@ void VariableNodeWidget::Draw()
{
BaseNodeWidget::FrameStart();
ImGui::TextUnformatted(m_selectedVariable.c_str());
ImGui::TextUnformatted(m_selectedVariableName.c_str());
DrawPins();

View file

@ -27,5 +27,6 @@ private:
IStoryManager &m_manager;
std::shared_ptr<VariableNode> m_variableNode;
int m_selectedIndex{-1};
std::string m_selectedVariable;
std::string m_selectedVariableUuid;
std::string m_selectedVariableName;
};

View file

@ -924,11 +924,13 @@ void MainWindow::RefreshProjectInformation()
void MainWindow::CloseProject()
{
if (m_story)
{
m_story->Clear();
m_story.reset();
}
// FIXME: not sure but if present, we lost some information in the library manager
// if (m_story)
// {
// m_story->Clear();
// m_story.reset();
// }
m_resources.Clear();