Adding Main program
[obcplusplus.git] / Main.C
blob5d1a68169dee84b88227c31baa4d06aa2603e974
1 // $Id: exprtest.cc 18 2007-08-19 19:51:52Z tb $
3 #include <iostream>
4 #include <fstream>
6 #include "ObCompiler.H"
7 #include "expression.h"
9 int main(int argc, char *argv[])
11     CalcContext calc;
12     objectbuilder::ObCompiler obCompiler(calc);
13     bool readfile = false;
15     for(int ai = 1; ai < argc; ++ai)
16     {
17         if (argv[ai] == std::string ("-p")) {
18             obCompiler.trace_parsing = true;
19         }
20         else if (argv[ai] == std::string ("-s")) {
21             obCompiler.trace_scanning = true;
22         }
23         else
24         {
25             // read a file with expressions
27             std::fstream infile(argv[ai]);
28             if (!infile.good())
29             {
30                 std::cerr << "Could not open file: " << argv[ai] << std::endl;
31                 return 0;
32             }
34             calc.clearExpressions();
35             bool result = obCompiler.parse_stream(infile, argv[ai]);
36             if (result)
37             {
38                 std::cout << "Expressions:" << std::endl;
39                 for (unsigned int ei = 0; ei < calc.expressions.size(); ++ei)
40                 {
41                     std::cout << "[" << ei << "]:" << std::endl;
42                     std::cout << "tree:" << std::endl;
43                     calc.expressions[ei]->print(std::cout);
44                     std::cout << "evaluated: "
45                               << calc.expressions[ei]->evaluate()
46                               << std::endl;
47                 }
48             }
50             readfile = true;
51         }
52     }
54     if (readfile) return 0;
55     
56     std::cout << "Reading expressions from stdin" << std::endl;
58     std::string line;
59     while( std::cout << "input: " &&
60            std::getline(std::cin, line) &&
61            !line.empty() )
62     {
63         calc.clearExpressions();
64         bool result = obCompiler.parse_string(line, "input");
66         if (result)
67         {
68             for (unsigned int ei = 0; ei < calc.expressions.size(); ++ei)
69             {
70                 std::cout << "tree:" << std::endl;
71                 calc.expressions[ei]->print(std::cout);
72                 std::cout << "evaluated: "
73                           << calc.expressions[ei]->evaluate()
74                           << std::endl;
75             }
76         }
77     }