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

How to list available functions? #5

Closed
hippyau opened this issue Jul 5, 2021 · 1 comment
Closed

How to list available functions? #5

hippyau opened this issue Jul 5, 2021 · 1 comment

Comments

@hippyau
Copy link

hippyau commented Jul 5, 2021

I must be doing something fundamentaly wrong here...

        Interpreter *pIr = this;

        // help
        addFunction("help", [pIr](const vector<string> &args) -> string
                    {                        
                        std::string result = "Available Functions:\n";
                        for (auto & a : pIr->allEntities()){                          
                            if (a.type == Interpreter::EntityType::FUNCTION) {
                                result + a.name;
                                result + "\n";
                            }   
                        }
                        
                        return  result;
                    });

returns ""

I want to list the functions defined in Interpreter... I have several, but help(); always returns empty.

@Tyill
Copy link
Owner

Tyill commented Jul 5, 2021

Only functions that are used in the script are returned.
Example:

int main(int argc, char* argv[])
{  
  Interpreter ir;

  ir.addFunction("summ", [](const vector<string>& args) ->string {
    int res = 0;
    for (auto& v : args) {
      if (isNumber(v)) res += stoi(v);
    }
    return to_string(res);
  });

  ir.addFunction("switch", [](const vector<string>& args) ->string {
    
    if (!args.empty()) {
      string key = args[0];
      for (size_t i = 1; i < args.size(); ++i) {
        if (key == args[i]) {
          return "1";
        }
      }
    }
    return "0";
  });


  ir.addFunction("print", [](const vector<string>& args) ->string {
    for (auto& v : args) {
      printf("%s\n", v.c_str());
    }
    return "";
  });

  ir.addFunction("help", [&ir](const vector<string>& args) -> string
    {
      std::string result = "Available Functions: ";
      for (auto& a : ir.allEntities()) {
        if (a.type == Interpreter::EntityType::FUNCTION) {
          result += a.name + ",";
        }
      }
      result.pop_back();

      return  result;
    });

  string scenar = "$a; $b; summ($b, $a); switch($a, 5); help();";
  string res = ir.cmd(scenar); // summ,switch,help
 
  return 0; 
}

Debug like this:

4

@hippyau hippyau closed this as completed Jul 6, 2021
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