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
23 namespace SeExpr2 {
24
25 inline const ExprVarNode* isVariable(const ExprNode* testee) {
26 return dynamic_cast<const ExprVarNode*>(testee);
27 };
28
29 inline const ExprNumNode* isScalar(const ExprNode* testee) {
30 return dynamic_cast<const ExprNumNode*>(testee);
31 };
32
33 inline const ExprVecNode* isVector(const ExprNode* testee) {
34 return dynamic_cast<const ExprVecNode*>(testee);
35 };
36
37 inline 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
44 inline const ExprStrNode* isString(const ExprNode* testee) {
45 return dynamic_cast<const ExprStrNode*>(testee);
46 };
47
48 inline const ExprAssignNode* isAssign(const ExprNode* testee) {
49 return dynamic_cast<const ExprAssignNode*>(testee);
50 };
51
52 inline const ExprFuncNode* isFunc(const ExprNode* testee) {
53 return dynamic_cast<const ExprFuncNode*>(testee);
54 };
55
56 inline 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
63 inline 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
73 inline bool hasCurveNumArgs(const ExprFuncNode* testee) {
75 return !((testee->numChildren() - 1) % 3);
76 };
77
78 inline 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
96 inline 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
114 inline 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
122 inline 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
130 inline 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
138 inline 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
146 inline 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
SeExpr2::isString
const ExprStrNode * isString(const ExprNode *testee)
Definition: ExprPatterns.h:44
SeExpr2::isVectorAssign
const ExprAssignNode * isVectorAssign(const ExprNode *testee)
Definition: ExprPatterns.h:122
SeExpr2::ExprNode::numChildren
int numChildren() const
Number of children.
Definition: ExprNode.h:114
SeExpr2::ExprNumNode
Node that stores a numeric constant.
Definition: ExprNode.h:486
SeExpr2::ExprNode
Definition: ExprNode.h:72
SeExpr2::hasCurveNumArgs
bool hasCurveNumArgs(const ExprFuncNode *testee)
Definition: ExprPatterns.h:73
SeExpr2::isVector
const ExprVecNode * isVector(const ExprNode *testee)
Definition: ExprPatterns.h:33
SeExpr2::isFunc
const ExprFuncNode * isFunc(const ExprNode *testee)
Definition: ExprPatterns.h:52
SeExpr2::isCcurveFunc
const ExprFuncNode * isCcurveFunc(const ExprNode *testee)
Definition: ExprPatterns.h:96
SeExpr2::isCurveAssign
const ExprAssignNode * isCurveAssign(const ExprNode *testee)
Definition: ExprPatterns.h:138
SeExpr2::isScalar
const ExprNumNode * isScalar(const ExprNode *testee)
Definition: ExprPatterns.h:29
SeExpr2::ExprFuncNode
Node that calls a function.
Definition: ExprNode.h:517
SeExpr2::ExprAssignNode
Node that compute a local variable assignment.
Definition: ExprNode.h:355
SeExpr2::isStrFuncAssign
const ExprAssignNode * isStrFuncAssign(const ExprNode *testee)
Definition: ExprPatterns.h:130
SeExpr2::isLitVec
const ExprVecNode * isLitVec(const ExprNode *testee)
Definition: ExprPatterns.h:37
SeExpr2
Definition: Context.h:22
SeExpr2::isAssign
const ExprAssignNode * isAssign(const ExprNode *testee)
Definition: ExprPatterns.h:48
ExprNode.h
SeExpr2::isScalarAssign
const ExprAssignNode * isScalarAssign(const ExprNode *testee)
Definition: ExprPatterns.h:114
SeExpr2::ExprVecNode
Node that constructs a vector from three scalars.
Definition: ExprNode.h:381
SeExpr2::max
double max(double x, double y)
Definition: ExprBuiltins.h:42
SeExpr2::ExprVarNode
Node that references a variable.
Definition: ExprNode.h:465
SeExpr2::isCurveFunc
const ExprFuncNode * isCurveFunc(const ExprNode *testee)
Definition: ExprPatterns.h:78
SeExpr2::ExprStrNode
Node that stores a string.
Definition: ExprNode.h:502
SeExpr2::ExprNode::child
const ExprNode * child(size_t i) const
Get 0 indexed child.
Definition: ExprNode.h:117
SeExpr2::isVariable
const ExprVarNode * isVariable(const ExprNode *testee)
Definition: ExprPatterns.h:25
SeExpr2::isCcurveAssign
const ExprAssignNode * isCcurveAssign(const ExprNode *testee)
Definition: ExprPatterns.h:146
SeExpr2::isNamedFunc
const ExprFuncNode * isNamedFunc(const ExprNode *testee, const std::string &name)
Definition: ExprPatterns.h:56
SeExpr2::isStrFunc
const ExprFuncNode * isStrFunc(const ExprNode *testee)
Definition: ExprPatterns.h:63