1/*-------------------------------------------------------------------------
2 * drawElements Quality Program Random Shader Generator
3 * ----------------------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Expression generator.
22 *//*--------------------------------------------------------------------*/
23
24#include "rsgExpressionGenerator.hpp"
25
26namespace rsg
27{
28
29ExpressionGenerator::ExpressionGenerator (GeneratorState& state)
30	: m_state(state)
31{
32}
33
34ExpressionGenerator::~ExpressionGenerator (void)
35{
36}
37
38Expression* ExpressionGenerator::generate (const ValueRange& valueRange, int initialDepth)
39{
40	// Create root
41	m_state.setExpressionDepth(initialDepth);
42	Expression* root = Expression::createRandom(m_state, valueRange);
43
44	try
45	{
46		// Generate full expression
47		generate(root);
48	}
49	catch (const std::exception&)
50	{
51		delete root;
52		m_expressionStack.clear();
53		throw;
54	}
55
56	return root;
57}
58
59void ExpressionGenerator::generate (Expression* root)
60{
61	DE_ASSERT(m_expressionStack.empty());
62
63	// Initialize stack
64	m_expressionStack.push_back(root);
65	m_state.setExpressionDepth(m_state.getExpressionDepth()+1);
66
67	// Process until done
68	while (!m_expressionStack.empty())
69	{
70		DE_ASSERT(m_state.getExpressionDepth() <= m_state.getShaderParameters().maxExpressionDepth);
71
72		Expression* curExpr = m_expressionStack[m_expressionStack.size()-1];
73		Expression*	child	= curExpr->createNextChild(m_state);
74
75		if (child)
76		{
77			m_expressionStack.push_back(child);
78			m_state.setExpressionDepth(m_state.getExpressionDepth()+1);
79		}
80		else
81		{
82			m_expressionStack.pop_back();
83			m_state.setExpressionDepth(m_state.getExpressionDepth()-1);
84		}
85	}
86}
87
88} // rsg
89