SeExpr
ExprPatterns.h
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#ifndef ExprPatterns_h
19#define ExprPatterns_h
20
21#include "ExprNode.h"
22
23namespace SeExpr2 {
24
25inline const ExprVarNode* isVariable(const ExprNode* testee) {
26 return dynamic_cast<const ExprVarNode*>(testee);
27};
28
29inline const ExprNumNode* isScalar(const ExprNode* testee) {
30 return dynamic_cast<const ExprNumNode*>(testee);
31};
32
33inline const ExprVecNode* isVector(const ExprNode* testee) {
34 return dynamic_cast<const ExprVecNode*>(testee);
35};
36
37inline const ExprVecNode* isLitVec(const ExprNode* testee) {
38 if (const ExprVecNode* vec = isVector(testee))
39 if (isScalar(vec->child(0)) && isScalar(vec->child(1)) && isScalar(vec->child(2))) return vec;
40
41 return 0;
42};
43
44inline const ExprStrNode* isString(const ExprNode* testee) {
45 return dynamic_cast<const ExprStrNode*>(testee);
46};
47
48inline const ExprAssignNode* isAssign(const ExprNode* testee) {
49 return dynamic_cast<const ExprAssignNode*>(testee);
50};
51
52inline const ExprFuncNode* isFunc(const ExprNode* testee) {
53 return dynamic_cast<const ExprFuncNode*>(testee);
54};
55
56inline const ExprFuncNode* isNamedFunc(const ExprNode* testee, const std::string& name) {
57 if (const ExprFuncNode* func = isFunc(testee))
58 if (name.compare(func->name()) == 0) return func;
59
60 return 0;
61};
62
63inline const ExprFuncNode* isStrFunc(const ExprNode* testee) {
64 if (const ExprFuncNode* func = isFunc(testee)) {
65 int max = testee->numChildren();
66 for (int i = 0; i < max; ++i)
67 if (isString(testee->child(i))) return func;
68 };
69
70 return 0;
71};
72
73inline bool hasCurveNumArgs(const ExprFuncNode* testee) {
75 return !((testee->numChildren() - 1) % 3);
76};
77
78inline const ExprFuncNode* isCurveFunc(const ExprNode* testee) {
79 const ExprFuncNode* curveFunc = isNamedFunc(testee, "curve");
80
81 if (curveFunc && hasCurveNumArgs(curveFunc)) {
82 int numChildren = curveFunc->numChildren() - 2;
83 for (int i = 1; i < numChildren && curveFunc; i += 3) {
84 if (!isScalar(curveFunc->child(i)))
85 curveFunc = 0;
86 else if (!isScalar(curveFunc->child(i + 1)))
87 curveFunc = 0;
88 else if (!isScalar(curveFunc->child(i + 2)))
89 curveFunc = 0;
90 };
91 };
92
93 return curveFunc;
94};
95
96inline const ExprFuncNode* isCcurveFunc(const ExprNode* testee) {
97 const ExprFuncNode* ccurveFunc = isNamedFunc(testee, "ccurve");
98
99 if (ccurveFunc && hasCurveNumArgs(ccurveFunc)) {
100 int numChildren = ccurveFunc->numChildren() - 2;
101 for (int i = 1; i < numChildren && ccurveFunc; i += 3) {
102 if (!isScalar(ccurveFunc->child(i)))
103 ccurveFunc = 0;
104 else if (!isScalar(ccurveFunc->child(i + 1)) && !isLitVec(ccurveFunc->child(i + 1)))
105 ccurveFunc = 0;
106 else if (!isScalar(ccurveFunc->child(i + 2)))
107 ccurveFunc = 0;
108 };
109 };
110
111 return ccurveFunc;
112};
113
114inline const ExprAssignNode* isScalarAssign(const ExprNode* testee) {
116 if (const ExprAssignNode* assign = isAssign(testee))
117 if (isScalar(assign->child(0))) return assign;
118
119 return 0;
120};
121
122inline const ExprAssignNode* isVectorAssign(const ExprNode* testee) {
124 if (const ExprAssignNode* assign = isAssign(testee))
125 if (isLitVec(assign->child(0))) return assign;
126
127 return 0;
128};
129
130inline const ExprAssignNode* isStrFuncAssign(const ExprNode* testee) {
132 if (const ExprAssignNode* assign = isAssign(testee))
133 if (isStrFunc(assign->child(0))) return assign;
134
135 return 0;
136};
137
138inline const ExprAssignNode* isCurveAssign(const ExprNode* testee) {
140 if (const ExprAssignNode* assign = isAssign(testee))
141 if (isCurveFunc(assign->child(0))) return assign;
142
143 return 0;
144};
145
146inline const ExprAssignNode* isCcurveAssign(const ExprNode* testee) {
148 if (const ExprAssignNode* assign = isAssign(testee))
149 if (isCcurveFunc(assign->child(0))) return assign;
150
151 return 0;
152};
153}
154#endif
Node that compute a local variable assignment.
Definition ExprNode.h:355
Node that calls a function.
Definition ExprNode.h:517
int numChildren() const
Number of children.
Definition ExprNode.h:114
const ExprNode * child(size_t i) const
Get 0 indexed child.
Definition ExprNode.h:117
Node that stores a numeric constant.
Definition ExprNode.h:486
Node that stores a string.
Definition ExprNode.h:502
Node that references a variable.
Definition ExprNode.h:465
Node that constructs a vector from three scalars.
Definition ExprNode.h:381
const ExprFuncNode * isCcurveFunc(const ExprNode *testee)
const ExprVecNode * isVector(const ExprNode *testee)
const ExprStrNode * isString(const ExprNode *testee)
const ExprAssignNode * isAssign(const ExprNode *testee)
bool hasCurveNumArgs(const ExprFuncNode *testee)
const ExprFuncNode * isNamedFunc(const ExprNode *testee, const std::string &name)
const ExprAssignNode * isVectorAssign(const ExprNode *testee)
const ExprAssignNode * isCcurveAssign(const ExprNode *testee)
const ExprFuncNode * isStrFunc(const ExprNode *testee)
const ExprNumNode * isScalar(const ExprNode *testee)
const ExprAssignNode * isScalarAssign(const ExprNode *testee)
const ExprFuncNode * isFunc(const ExprNode *testee)
const ExprAssignNode * isCurveAssign(const ExprNode *testee)
const ExprAssignNode * isStrFuncAssign(const ExprNode *testee)
const ExprVecNode * isLitVec(const ExprNode *testee)
const ExprVarNode * isVariable(const ExprNode *testee)
double max(double x, double y)
const ExprFuncNode * isCurveFunc(const ExprNode *testee)