InFact
Interpreter and factory for easily creating C++ objects at run-time
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
interpreter-test.cc
Go to the documentation of this file.
1 // Copyright 2014, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
34 
35 #include <iostream>
36 #include <memory>
37 
38 #include "interpreter.h"
39 
40 using namespace std;
41 using namespace infact;
42 
43 int
44 main(int argc, char **argv) {
45  cout << "Here is a list of abstract types and the concrete implementations\n"
46  << "you can construct:" << endl;
47 
48  int debug = 0;
49  cout << "Debug level: " << debug << endl;
50 
51  Interpreter interpreter(debug);
52 
53  cout << endl;
54  interpreter.PrintFactories(cout);
55 
56  cout << "\nHello! Please type assignment statements.\n" << endl;
57 
58  if (argc >= 2) {
59  interpreter.Eval(argv[1]);
60  } else {
61  interpreter.Eval(cin);
62  }
63 
64  cout << "\nNow doing some hard-coded testing, looking to see if you\n"
65  << "set a variable named \"f\" to have a boolean value." << endl;
66 
67  bool value_for_f;
68  bool success = interpreter.Get("f", &value_for_f);
69  if (success) {
70  cout << "Success! f=" << (value_for_f ? "true" : "false") << endl;
71  } else {
72  cout << ":( ... no boolean value for variable f" << endl;
73  }
74 
75  cout << "\n\nEnvironment: " << endl;
76  interpreter.PrintEnv(cout);
77 
78  cout << "\nHave a nice day!\n" << endl;
79 }
80 
void PrintFactories(ostream &os) const
Definition: interpreter.h:256
Provides an interpreter for assigning primitives and Factory-constructible objects to named variables...
void Eval(const string &filename)
Evaluates the statements in the specified text file.
Definition: interpreter.cc:91
bool Get(const string &varname, T *value) const
Retrieves the value of the specified variable.
Definition: interpreter.h:317
void PrintEnv(ostream &os) const
Definition: interpreter.h:252
int main(int argc, char **argv)
Provides an interpreter for assigning primitives and Factory-constructible objects to named variables...
Definition: interpreter.h:212