-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.cpp
More file actions
61 lines (56 loc) · 1.25 KB
/
interpreter.cpp
File metadata and controls
61 lines (56 loc) · 1.25 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* \file
* \brief
*
* \todo
*/
/*
Interpreter processes commands and performs actions based on its input.
Here we create a simple command language consisting of single characters.
*/
#include <string>
#include <iostream>
#include <stdexcept>
//-------------------------------------------------------------------------------------------------
class Interpreter
{
public:
template<typename T>
void run(T from, T to)
{
for (T 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");
}
} // for
}
};
//-------------------------------------------------------------------------------------------------
void hello_world(const std::string &script)
{
Interpreter interpreter;
interpreter.run(script.begin(), script.end());
}
//-------------------------------------------------------------------------------------------------
int main()
{
hello_world("h w!n");
return 0;
}
//-------------------------------------------------------------------------------------------------