JBenda / inkcpp

Inkle Ink C++ Runtime with JSON>Binary Compiler
MIT License
70 stars 13 forks source link

Conditional diverts are not properly handled with whitespace #71

Closed Chaosed0 closed 9 months ago

Chaosed0 commented 9 months ago

When running the following ink, "This displays first" is not displayed:

 Here is some test text

 - (opts)

 + [Continue]

 {opts == 1: This displays first}

 {opts < 2:  -> opts}

 - This is the continuation.

 * [OK]

 -> DONE

However, if you remove the space in the line {opts < 2: -> opts} between the colon and the divert arrow, then it displays correctly.

EDIT: I've been debugging this and I think it has to do with the order of the enum values in ink::runtime::internal::value_type. value_type::value_pointer and value_type::marker have the same value. Unfortunately, I'm not well versed enough in C++ to tell what the real solution should be. If I can get a little guidance I could probably submit a PR.

Chaosed0 commented 9 months ago

I'm almost certain there is something buggy with this code, but it fixes it for me:

diff --git a/inkcpp/output.cpp b/inkcpp/output.cpp
index 09075bf..1985b88 100644
--- a/inkcpp/output.cpp
+++ b/inkcpp/output.cpp
@@ -119,6 +119,11 @@ void basic_stream::copy_string(const char* str, size_t& dataIter, T& output)
 #ifdef INK_ENABLE_STL
 std::string basic_stream::get()
 {
+   if (saved())
+   {
+       forget();
+   }
+
    size_t start = find_start();

    // Move up from marker
diff --git a/inkcpp/value.cpp b/inkcpp/value.cpp
index ec2fc0c..1caad50 100644
--- a/inkcpp/value.cpp
+++ b/inkcpp/value.cpp
@@ -104,7 +104,7 @@ namespace ink::runtime::internal
 #ifdef INK_ENABLE_STL
    template<value_type ty = value_type::PRINT_BEGIN>
    void append(std::ostream& os, const value& val, const list_table* lists) {
-       if constexpr (ty != value_type::PRINT_END) {
+       if constexpr (ty != value_type::value_pointer) {
            if (ty == val.type()) {
                os << val.get<ty>();
            } else {
diff --git a/inkcpp/value.h b/inkcpp/value.h
index e2bc97f..1565c4f 100644
--- a/inkcpp/value.h
+++ b/inkcpp/value.h
@@ -35,8 +35,8 @@ namespace ink::runtime::internal {
        list_flag,                  // a single list flag
        string,                     // Pointer to string
        OP_END,                     // END of types where we can operate on
-       value_pointer,              // a pointer to an value
        newline = OP_END,           // newline symbol
+       value_pointer,              // a pointer to an value
        PRINT_END,                  // END of printable values
        marker = PRINT_END,         // special marker (used in output stream)
        glue,                       // glue symbol
JBenda commented 9 months ago

At first: thank you for figuring that out

The problem you identified sounds reasonable. I will take a look at your change :+1

JBenda commented 9 months ago

Yeah the double ID was not god ^^ but the problem was primarily that the output stream was not forget correctly Thanks for pointing it out, and for doing some digging, saved me a lot of time.

JBenda commented 9 months ago

Solved in #72