
#include <iostream>

using namespace std;

#include "fstringMR.hpp"

//Lsys

const char * tree = "{a:ab;b:aa;xx:xyx;}a";

funcString * getKey(funcString * rules, int n)
{
    int start = 0;
    if (n)
        start = find(rules,';',n-1)+1;
    int end = find(rules,':',n);
    return new fsubstr(rules,start,end-start);

}

funcString * getValue(funcString * rules, int n)
{
    int start = find(rules,':',n)+1,
        end = find(rules,';',n);
    return new fsubstr(rules,start,end-start);
}

/*now do a functor for those three function*/

//main

void show(funcString * fs)
{
    for (int i=0;i<len(fs);i++)
    {
        cout << (*fs)(i);
    }
    cout << endl;
}

void show(funcString & fs)
{
    show(&fs);
}

int main()
{
    cout << "Lsystem using fucking functional stack" << endl;

    fstr ftree(tree);
    fbetween rules(ftree,'{','}');

    show(rules);

    funcString * temp = 0;
    for (int i =0 ;i<count(&rules,';');i++)
    {
        temp = getKey(&rules,i);
        show(temp);
        if (temp)
            delete temp;
        temp = getValue(&rules,i);
        show(temp);
        if (temp)
            delete temp;
    }

//     fstart serie(ftree,'}');
//     show(serie);
//
//     int i=0;
//     while (i<len(&serie))
//     {
//         for (int j=0;j<count(&rules,':');j++)
//         {
//             int start= find(&rules,';',j-1)+1;
//             fsubstr key(rules, start , find(&rules,':',j)-start);
//             show (&key);
//
//             start = find(&rules,':',j)+1;
//             fsubstr value(rules, start, find(&rules,';',j)-start );
//             show (&value);
//
//
//         }
//         i++;
//     }

    return 0;
}
