-
Notifications
You must be signed in to change notification settings - Fork 18
/
accept_no_visitors.cpp
115 lines (84 loc) · 2.93 KB
/
accept_no_visitors.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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2018-2021 Jean-Louis Leroy
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <memory>
#include <string>
#include <yorel/yomm2/keywords.hpp>
using std::cout;
using std::make_shared;
using std::shared_ptr;
using std::string;
struct Node {
virtual ~Node() {
}
};
struct Plus : Node {
Plus(shared_ptr<const Node> left, shared_ptr<const Node> right)
: left(left), right(right) {
}
shared_ptr<const Node> left, right;
};
struct Times : Node {
Times(shared_ptr<const Node> left, shared_ptr<const Node> right)
: left(left), right(right) {
}
shared_ptr<const Node> left, right;
};
struct Integer : Node {
explicit Integer(int value) : value(value) {
}
int value;
};
// =============================================================================
// add behavior to existing classes, without changing them
register_classes(Node, Plus, Times, Integer);
// -----------------------------------------------------------------------------
// evaluate
declare_method(int, value, (virtual_<const Node&>));
define_method(int, value, (const Plus& expr)) {
return value(*expr.left) + value(*expr.right);
}
define_method(int, value, (const Times& expr)) {
return value(*expr.left) * value(*expr.right);
}
define_method(int, value, (const Integer& expr)) {
return expr.value;
}
// -----------------------------------------------------------------------------
// render as Forth
declare_method(string, as_forth, (virtual_<const Node&>));
define_method(string, as_forth, (const Plus& expr)) {
return as_forth(*expr.left) + " " + as_forth(*expr.right) + " +";
}
define_method(string, as_forth, (const Times& expr)) {
return as_forth(*expr.left) + " " + as_forth(*expr.right) + " *";
}
define_method(string, as_forth, (const Integer& expr)) {
return std::to_string(expr.value);
}
// -----------------------------------------------------------------------------
// render as Lisp
declare_method(string, as_lisp, (virtual_<const Node&>));
define_method(string, as_lisp, (const Plus& expr)) {
return "(plus " + as_lisp(*expr.left) + " " + as_lisp(*expr.right) + ")";
}
define_method(string, as_lisp, (const Times& expr)) {
return "(times " + as_lisp(*expr.left) + " " + as_lisp(*expr.right) + ")";
}
define_method(string, as_lisp, (const Integer& expr)) {
return std::to_string(expr.value);
}
// -----------------------------------------------------------------------------
int main() {
yorel::yomm2::update();
shared_ptr<Node> expr = make_shared<Times>(
make_shared<Integer>(2),
make_shared<Plus>(make_shared<Integer>(3), make_shared<Integer>(4)));
cout << as_forth(*expr) << " = " << as_lisp(*expr) << " = " << value(*expr)
<< "\n";
// error_output:
// 2 3 4 + * = (times 2 (plus 3 4)) = 14
return 0;
}