-
Notifications
You must be signed in to change notification settings - Fork 66
/
interpreter.cpp
35 lines (32 loc) · 960 Bytes
/
interpreter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <string>
#include <iostream>
#include <stdexcept>
class Interpreter
{
public:
template<typename It>
void run(It from, It to)
{
for(It i=from; i!=to; ++i)
{
switch(*i)
{
case 'h': std::cout << "Hello"; break;
case ' ': std::cout << ' '; break;
case 'w': std::cout << "world"; break;
case '!': std::cout << '!'; break;
case 'n': std::cout << std::endl; break;
default: throw std::runtime_error("Unknown command");
}
}
}
};
void hello_world(const std::string & script)
{
Interpreter().run(script.begin(), script.end());
}
int main()
{
hello_world("h w!n");
return 0;
}