Skip to content

Commit

Permalink
Added ability to use C++ references with QF ints
Browse files Browse the repository at this point in the history
Signed-off-by: Vedant <[email protected]>
  • Loading branch information
vrnimje committed Sep 19, 2023
1 parent b214d0a commit 6f16699
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
17 changes: 8 additions & 9 deletions cpp_examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ using namespace std;
using namespace quick_ftxui;

int main() {

set_int_var("x", 5);
set_str_var("y", "");
string source_code = R"(Vertical{
int x = 5;
set_int_var("x", &x);
set_str_var("y");
string source_code = R"(DashedBorder Vertical{
str z = "init"
str a
int o = 0
Input {
"Type something...",
Password,
y
}
RedLight Slider {
Expand All @@ -26,12 +25,12 @@ int main() {
2
}
Magenta Button{
"ls",
"Chrome",
System("/usr/bin/google-chrome-stable"),
Ascii,
Animated,
z
}
Menu{
Green Menu{
[ "Physics", "Maths", "Chemistry", "Biology",],
VerticalAnimated,
o
Expand All @@ -44,7 +43,7 @@ int main() {

parse_qf(source_code);

cout << "x is: " << get_int("x") << "\n";
cout << "x is: " << x << "\n";
cout << "y is: " << get_str("y") << "\n";
cout << "o is: " << get_int("o") << "\n";
cout << "z is: " << get_str("z") << "\n";
Expand Down
37 changes: 37 additions & 0 deletions include/quick-ftxui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace quick_ftxui_ast {
std::map<std::string, int> numbers;
std::map<std::string, std::string> strings;

std::map<int *, std::string> ref_nums;

///////////////////////////////////////////////////////////////////////////
// The AST
///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1087,6 +1089,10 @@ void parse_qf(std::string source_code) {
}

screen.Loop(main_renderer);

for (auto It : quick_ftxui_ast::ref_nums) {
*(It.first) = quick_ftxui_ast::numbers[It.second];
}
}
} else {
throw std::runtime_error("Parsing failed\n");
Expand Down Expand Up @@ -1114,6 +1120,27 @@ void set_int_var(std::string var_name, int init_value) {
}
}

void set_int_var(std::string var_name, int *init_value) {
if (auto It = quick_ftxui_ast::numbers.find(var_name);
It != quick_ftxui_ast::numbers.end()) {
throw std::runtime_error("Integer variable with name " + var_name +
" already exists, please use another name");
} else {
quick_ftxui_ast::numbers.insert({var_name, (*init_value)});
quick_ftxui_ast::ref_nums.insert({init_value, var_name});
}
}

void set_int_var(std::string var_name) {
if (auto It = quick_ftxui_ast::numbers.find(var_name);
It != quick_ftxui_ast::numbers.end()) {
throw std::runtime_error("Integer variable with name " + var_name +
" already exists, please use another name");
} else {
quick_ftxui_ast::numbers.insert({var_name, 0});
}
}

std::string get_str(std::string var_name) {
if (auto It = quick_ftxui_ast::strings.find(std::string(var_name));
It != quick_ftxui_ast::strings.end()) {
Expand All @@ -1135,6 +1162,16 @@ void set_str_var(std::string var_name, std::string init_value) {
}
}

void set_str_var(std::string var_name) {
if (auto It = quick_ftxui_ast::strings.find(var_name);
It != quick_ftxui_ast::strings.end()) {
throw std::runtime_error("Integer variable with name " + var_name +
" already exists, please use another name");
} else {
quick_ftxui_ast::strings.insert({var_name, ""});
}
}

} // namespace quick_ftxui

#endif // QUICK_FTXUI_HPP

0 comments on commit 6f16699

Please sign in to comment.