src/exprtest.cc

Go to the documentation of this file.
00001 // $Id: exprtest.cc 18 2007-08-19 19:51:52Z tb $
00002 
00003 #include <iostream>
00004 #include <fstream>
00005 
00006 #include "driver.h"
00007 #include "expression.h"
00008 
00009 int main(int argc, char *argv[])
00010 {
00011     CalcContext calc;
00012     example::Driver driver(calc);
00013     bool readfile = false;
00014 
00015     for(int ai = 1; ai < argc; ++ai)
00016     {
00017         if (argv[ai] == std::string ("-p")) {
00018             driver.trace_parsing = true;
00019         }
00020         else if (argv[ai] == std::string ("-s")) {
00021             driver.trace_scanning = true;
00022         }
00023         else
00024         {
00025             // read a file with expressions
00026 
00027             std::fstream infile(argv[ai]);
00028             if (!infile.good())
00029             {
00030                 std::cerr << "Could not open file: " << argv[ai] << std::endl;
00031                 return 0;
00032             }
00033 
00034             calc.clearExpressions();
00035             bool result = driver.parse_stream(infile, argv[ai]);
00036             if (result)
00037             {
00038                 std::cout << "Expressions:" << std::endl;
00039                 for (unsigned int ei = 0; ei < calc.expressions.size(); ++ei)
00040                 {
00041                     std::cout << "[" << ei << "]:" << std::endl;
00042                     std::cout << "tree:" << std::endl;
00043                     calc.expressions[ei]->print(std::cout);
00044                     std::cout << "evaluated: "
00045                               << calc.expressions[ei]->evaluate()
00046                               << std::endl;
00047                 }
00048             }
00049 
00050             readfile = true;
00051         }
00052     }
00053 
00054     if (readfile) return 0;
00055     
00056     std::cout << "Reading expressions from stdin" << std::endl;
00057 
00058     std::string line;
00059     while( std::cout << "input: " &&
00060            std::getline(std::cin, line) &&
00061            !line.empty() )
00062     {
00063         calc.clearExpressions();
00064         bool result = driver.parse_string(line, "input");
00065 
00066         if (result)
00067         {
00068             for (unsigned int ei = 0; ei < calc.expressions.size(); ++ei)
00069             {
00070                 std::cout << "tree:" << std::endl;
00071                 calc.expressions[ei]->print(std::cout);
00072                 std::cout << "evaluated: "
00073                           << calc.expressions[ei]->evaluate()
00074                           << std::endl;
00075             }
00076         }
00077     }
00078 }

Generated on Mon Aug 20 13:34:21 2007 for Flex Bison C++ Example by  doxygen 1.5.2