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