SeExpr
llvmtest.cpp
Go to the documentation of this file.
1 /*
2  Copyright Disney Enterprises, Inc. All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License
6  and the following modification to it: Section 6 Trademarks.
7  deleted and replaced with:
8 
9  6. Trademarks. This License does not grant permission to use the
10  trade names, trademarks, service marks, or product names of the
11  Licensor and its affiliates, except as required for reproducing
12  the content of the NOTICE file.
13 
14  You may obtain a copy of the License at
15  http://www.apache.org/licenses/LICENSE-2.0
16 */
17 
18 #include <Expression.h>
19 #include <fstream>
20 #include <string>
21 #include <sstream>
22 
23 using namespace std;
24 using namespace SeExpr2;
25 
26 class llvmexpr : public Expression {
27  public:
29  llvmexpr(const std::string &expr, const ExprType &type = ExprType().FP(3)) : Expression(expr, type) {}
30 };
31 
32 // TODO: turn off parsing output.
33 int main(int argc, char *argv[]) {
34  if (argc < 2) {
35  std::cerr << "need a filename as argument\n";
36  return 1;
37  }
38 
39  // Test fp
40  std::string line;
41  std::ifstream FSProfileData(argv[1], std::ifstream::in);
42  while (std::getline(FSProfileData, line)) {
43  if (!line.size() || line.at(0) == ';') continue;
44 
45  string::size_type start = line.find_first_of('"');
46  ++start;
47  string::size_type end = line.find_last_of('"');
48  string exprStr(line, start, end - start);
49 
50  std::cout << left << "\n";
51  std::istringstream iss(string(line, end + 1), std::istringstream::in);
52  int dim = 0;
53  iss >> dim;
54  std::cout << "exprStr: " << exprStr << std::endl;
55  std::cout << "dim: " << dim << std::endl;
56 
57  llvmexpr expr(exprStr, ExprType().FP(dim));
58  if (!expr.isValid()) {
59  std::cerr << expr.parseError() << std::endl;
60  assert(false);
61  }
62 
63  const double *result = expr.evalFP();
64 
65  for (int i = 0; i < dim; ++i) std::cout << result[i] << ' ';
66  std::cout << std::endl;
67  }
68 
69  if (!argv[2]) return 0;
70  FSProfileData.close();
71 
72  // Test string
73  FSProfileData.open(argv[2], std::ifstream::in);
74  while (std::getline(FSProfileData, line)) {
75  if (!line.size() || line.at(0) == ';') continue;
76 
77  std::cout << "exprStr: " << line << std::endl;
78  llvmexpr expr(line, ExprType().String());
79  if (!expr.isValid()) {
80  std::cerr << expr.parseError() << std::endl;
81  assert(false);
82  }
83 
84  const char *result = expr.evalStr();
85 
86  std::cout << result;
87  }
88 }
</pre >< h2 > Evaluating expressions</h2 > Evaluating an expression is pretty easy But before we can do that we need to make an instance< pre > GrapherExpr expr("x+x^2")
llvmexpr(const std::string &expr, const ExprType &type=ExprType().FP(3))
Constructor that takes the expression to parse.
Definition: llvmtest.cpp:29
const std::string & parseError() const
Definition: Expression.h:140
main expression class
Definition: Expression.h:76
const double * evalFP(VarBlock *varBlock=nullptr) const
Definition: Expression.cpp:299
const char * evalStr(VarBlock *varBlock=nullptr) const
Definition: Expression.cpp:335
bool isValid() const
Definition: Expression.h:133
int main(int argc, char *argv[])
Definition: EditMain.cpp:24