SomeRanDev / reflaxe.CPP

An alternative C++ target for Haxe that generates dependent-less, GC-less C++17 code.
MIT License
72 stars 5 forks source link

improve the generation of CPP code that can be easily read by humans. #27

Open sonygod opened 1 year ago

sonygod commented 1 year ago

for example

template<typename T>
    static std::shared_ptr<std::deque<T>> concat(std::deque<T>* a, std::deque<T>* other) {
        std::shared_ptr<std::deque<T>> tempArray;

        {
            std::shared_ptr<std::deque<T>> result = std::make_shared<std::deque<T>>(std::deque<T>{});

            {
                int _g = 0;
                std::deque<T>* _g1 = a;

                while(_g < (int)(_g1->size())) {
                    T obj = (*_g1)[_g];

                    ++_g;

                    {
                        result->push_back(obj);
                    };
                };
            };

            tempArray = result;
        };

        int _g_current = 0;
        std::deque<T>* _g_array = other;

        while(_g_current < (int)(_g_array->size())) {
            T o = (*_g_array)[_g_current++];

            tempArray->push_back(o);
        };

        return tempArray;
    }

why not like here

#include <deque>
#include <memory>

template<typename T>
static std::shared_ptr<std::deque<T>> concat(std::deque<T>* a, std::deque<T>* other) {
    std::shared_ptr<std::deque<T>> result = std::make_shared<std::deque<T>>();

    for (const auto& item : *a) {
        result->push_back(item);
    }

    for (const auto& item : *other) {
        result->push_back(item);
    }

    return result;
}

Your library is already doing a great job, but there is always room for improvement. Thank you for your efforts :)

sonygod commented 1 year ago

oh,I see ,you write code in Array.cross.hx

public static function concat<T>(a: cxx.Ptr<Array<T>>, other: cxx.Ptr<Array<T>>): Array<T> {
        final result = a.copy();
        for(o in other) {
            result.push(o);
        }
        return result;
    }

and what I want to know how to replace this code look like more native cpp?

use
untyped __cpp__?
sonygod commented 1 year ago

I'm planning to simulate all the fundamental classes of Haxe using native C++, and then combine it with your library to generate code that is more human-readable and easier to write. What do you think about this? Currently, the generated base library from your model is not very readable for humans.

SomeRanDev commented 1 year ago

Hello! Please feel free to keep this open, this is something I hope to fix eventually.

The issue is actually a bit complicated. The reason it generates that way is because of how Haxe works.

Reflaxe uses the "typed" expressions provided by the Haxe compiler to generate the code. The issue is Haxe injects its own optimizations/changes that change how the original Haxe code looks (for example, it converts all for loops into while loops). If you place the same code in other targets, you may notice they all get converted into while loops afaik.

This can be fixed by analyzing the Haxe typed expressions and working backwards to make them look a bit cleaner. It hasn't been a priority yet since it's just an aesthetics issue (the code still works the same) and Reflaxe/C++ still has functional issues and is incomplete. But I want to get to this eventually!