interpreter_example.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // (C) Copyright Tobias Schwinger
  2. //
  3. // Use modification and distribution are subject to the boost Software License,
  4. // Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt).
  5. //------------------------------------------------------------------------------
  6. #include <string>
  7. #include <iostream>
  8. #include <stdexcept>
  9. #include "interpreter.hpp"
  10. void echo(std::string const & s)
  11. {
  12. std::cout << s << std::endl;
  13. }
  14. void add(int a, int b)
  15. {
  16. std::cout << a + b << std::endl;
  17. }
  18. void repeat(std::string const & s, int n)
  19. {
  20. while (--n >= 0) std::cout << s;
  21. std::cout << std::endl;
  22. }
  23. int main()
  24. {
  25. example::interpreter interpreter;
  26. interpreter.register_function("echo", & echo);
  27. interpreter.register_function("add", & add);
  28. interpreter.register_function("repeat", & repeat);
  29. std::string line = "nonempty";
  30. while (! line.empty())
  31. {
  32. std::cout << std::endl << "] ", std::getline(std::cin,line);
  33. try
  34. {
  35. interpreter.parse_input(line);
  36. }
  37. catch (std::runtime_error &error)
  38. {
  39. std::cerr << error.what() << std::endl;
  40. }
  41. }
  42. return 0;
  43. }