Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator awareness of parent function? #3

Closed
hippyau opened this issue May 20, 2021 · 6 comments
Closed

Operator awareness of parent function? #3

hippyau opened this issue May 20, 2021 · 6 comments

Comments

@hippyau
Copy link

hippyau commented May 20, 2021

Is there a mechanism or suggestion how that that the operator can know it's parent function?

eg: lettuce(1->24);

        ir->addOperator( // a selection
            "->", [](string &leftOpd, string &rightOpd) -> string {
                
                if (isNumber(leftOpd) && isNumber(rightOpd)){
                    uint a = stoi(leftOpd);
                    uint b = stoi(rightOpd);
                    for (int cnt = a; cnt <= b; cnt++){
                        printf("select %s %d \n\r", ___parent_function_name___, cnt);
                    }
                }
                return ""; // return nothing - ignore?
            },
            0);

would output...

select lettuce 1
select lettuce 2
select lettuce 3
...
select lettuce 22
select lettuce 23
select lettuce 24
@Tyill
Copy link
Owner

Tyill commented May 20, 2021

No, there is no such thing.

@Tyill
Copy link
Owner

Tyill commented May 20, 2021

I'll think about how to do it.

@hippyau
Copy link
Author

hippyau commented May 20, 2021

okay cool, thanks 👍

I now wonder how I might go about implementing.

@hippyau hippyau closed this as completed May 20, 2021
@hippyau hippyau reopened this May 20, 2021
@Tyill
Copy link
Owner

Tyill commented May 20, 2021

@Tyill
Copy link
Owner

Tyill commented May 21, 2021

How to get the function name:

ir.addFunction("lettuce", [&ir](const vector<string>& args) ->string {
      return !args.empty() ? args[0] : "";
  });

ir.addOperator( // a selection
    "->", [&ir](string &leftOpd, string &rightOpd) -> string {

    auto entities = ir.allEntities();

    auto curEntity = ir.currentEntity();

    int index = curEntity.beginIndex - 1;
    while ((index >= 0) && (entities[index].type != Interpreter::EntityType::FUNCTION))
        --index;

    string parentFunctionName = "";
    if (index >= 0)
        parentFunctionName = entities[index].name;

    if (isNumber(leftOpd) && isNumber(rightOpd)) {
        int a = stoi(leftOpd);
        int b = stoi(rightOpd);
        for (int cnt = a; cnt <= b; cnt++) {
            printf("select %s %d \n\r", parentFunctionName.c_str(), cnt);
        }
    }
    return parentFunctionName;
}, 0);

string scenar = "lettuce(1->24);";

string res = ir.cmd(scenar); // lettuce

@Tyill Tyill closed this as completed May 21, 2021
@hippyau
Copy link
Author

hippyau commented May 22, 2021

Hi Tyill! Thanks so much, this works, but breaks the multiple instances #1 when using a vector.

This had me stumped, but I think it's because Interpreter *pIr = &ir; is invalidated when its.push_back(test2); is used and the object is moved... Any way around this while still being able to use a dynamic array?

Solved: I should be using a deque instead of a vector, and emplace_back() instead to create the objects in place, instead of using push_back() and moving an object there. facepalm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants