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
23using namespace std;
24using namespace SeExpr2;
25
26class 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.
33int 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}
main expression class
Definition Expression.h:76
llvmexpr(const std::string &expr, const ExprType &type=ExprType().FP(3))
Constructor that takes the expression to parse.
Definition llvmtest.cpp:29
int main(int argc, char *argv[])
Definition llvmtest.cpp:33
</pre >< h3 > Binding our variable reference</h3 > If we now tried to use the variable would still not be found by our expressions To make it bindable we need to override the resolveVar() function as follows</pre >< h3 > Variable setting</h3 > Next we need to make a way of setting the variable As the controlling code will use the expression it will repeatedly alternate between setting the independent variables that are used and calling evaluate(). What it has to do depends very much on the application. In this case we only need to set the independent variable x as</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")