~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to libslparse/parsenode.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////---------------------------------------------------------------------
 
2
////    Class definition file:  PARSENODE.CPP
 
3
////    Associated header file: PARSENODE.H
 
4
////
 
5
////    Author:                                 Paul C. Gregory
 
6
////    Creation date:                  18/07/99
 
7
////---------------------------------------------------------------------
 
8
 
 
9
#include        "aqsis.h"
 
10
#include        "parsenode.h"
 
11
#include        "funcdef.h"
 
12
#include        "vardef.h"
 
13
 
 
14
#if defined (AQSIS_SYSTEM_WIN32)
 
15
        #define tolower(x) _tolower(x)
 
16
#endif
 
17
 
 
18
START_NAMESPACE( Aqsis )
 
19
 
 
20
static char* gVariableTypeNames[] =
 
21
    {
 
22
        "invalid",
 
23
        "float",
 
24
        "integer",
 
25
        "point",
 
26
        "string",
 
27
        "color",
 
28
        "triple",
 
29
        "hpoint",
 
30
        "normal",
 
31
        "vector",
 
32
        "void",
 
33
        "matrix",
 
34
        "hextuple",
 
35
    };
 
36
static TqInt gcVariableTypeNames = sizeof( gVariableTypeNames ) / sizeof( gVariableTypeNames[ 0 ] );
 
37
 
 
38
char* gShaderTypeNames[] =
 
39
    {
 
40
        "surface",
 
41
        "lightsource",
 
42
        "volume",
 
43
        "displacement",
 
44
        "transformation",
 
45
        "imager",
 
46
    };
 
47
TqInt gcShaderTypeNames = sizeof( gShaderTypeNames ) / sizeof( gShaderTypeNames[ 0 ] );
 
48
 
 
49
 
 
50
//---------------------------------------------------------------------
 
51
// Static data on CqParseNode
 
52
 
 
53
TqInt   CqParseNode::m_cLabels = 0;
 
54
TqInt   CqParseNode::m_aaTypePriorities[ Type_Last ][ Type_Last ] =
 
55
    {
 
56
        //                                 @  f  i      p  s  c  t      h  n  v  x      m
 
57
        /*Type_Nil*/     { 99,   00,   00,   00,   00,   00,   00,   00,   00,   00,   00,   00,   00 },
 
58
        /*Type_Float*/   { 00,   99,   98,   02,   00,   01,   02,   02,   02,   02,   00,   01,   00 },
 
59
        /*Type_Integer*/ { 00,   98,   99,   00,   00,   00,   00,   00,   00,   00,   00,   00,   00 },
 
60
        /*Type_Point*/   { 00,   00,   00,   99,   00,   96,   98,   98,   97,   97,   00,   00,   00 },
 
61
        /*Type_String*/  { 00,   00,   00,   00,   99,   00,   00,   00,   00,   00,   00,   00,   00 },
 
62
        /*Type_Color*/   { 00,   00,   00,   01,   00,   99,   02,   01,   01,   01,   00,   00,   00 },
 
63
        /*Type_Triple*/  { 00,   00,   00,   98,   00,   98,   99,   97,   98,   98,   00,   00,   00 },
 
64
        /*Type_hPoint*/  { 00,   00,   00,   97,   00,   96,   98,   99,   98,   98,   00,   00,   00 },
 
65
        /*Type_Normal*/  { 00,   00,   00,   98,   00,   96,   98,   97,   99,   98,   00,   00,   00 },
 
66
        /*Type_Vector*/  { 00,   00,   00,   98,   00,   96,   98,   97,   98,   99,   00,   00,   00 },
 
67
        /*Type_Void*/    { 00,   00,   00,   00,   00,   00,   00,   00,   00,   00,   99,   00,   00 },
 
68
        /*Type_Matrix*/  { 00,   00,   00,   00,   00,   00,   00,   00,   00,   00,   00,   99,   00 },
 
69
        /*Type_HexTuple*/{ 00,   00,   00,   00,   00,   00,   00,   00,   00,   00,   00,   99,   98 },
 
70
    };
 
71
TqInt   CqParseNode::m_aAllTypes[ Type_Last - 1 ] =
 
72
    {
 
73
        Type_Float,
 
74
        Type_Integer,
 
75
        Type_Point,
 
76
        Type_String,
 
77
        Type_Color,
 
78
        Type_Triple,
 
79
        Type_hPoint,
 
80
        Type_Normal,
 
81
        Type_Vector,
 
82
        Type_Void,
 
83
        Type_Matrix,
 
84
        Type_HexTuple,
 
85
    };
 
86
 
 
87
 
 
88
const EqParseNodeType IqParseNode::m_ID = ParseNode_Base;
 
89
const EqParseNodeType IqParseNodeShader::m_ID = ParseNode_Shader;
 
90
const EqParseNodeType IqParseNodeFunctionCall::m_ID = ParseNode_FunctionCall;
 
91
const EqParseNodeType IqParseNodeUnresolvedCall::m_ID = ParseNode_UnresolvedCall;
 
92
const EqParseNodeType IqParseNodeVariable::m_ID = ParseNode_Variable;
 
93
const EqParseNodeType IqParseNodeArrayVariable::m_ID = ParseNode_ArrayVariable;
 
94
const EqParseNodeType IqParseNodeVariableAssign::m_ID = ParseNode_VariableAssign;
 
95
const EqParseNodeType IqParseNodeArrayVariableAssign::m_ID = ParseNode_ArrayVariableAssign;
 
96
const EqParseNodeType IqParseNodeOperator::m_ID = ParseNode_Operator;
 
97
const EqParseNodeType IqParseNodeMathOp::m_ID = ParseNode_MathOp;
 
98
const EqParseNodeType IqParseNodeRelationalOp::m_ID = ParseNode_RelationalOp;
 
99
const EqParseNodeType IqParseNodeUnaryOp::m_ID = ParseNode_UnaryOp;
 
100
const EqParseNodeType IqParseNodeLogicalOp::m_ID = ParseNode_LogicalOp;
 
101
const EqParseNodeType IqParseNodeDiscardResult::m_ID = ParseNode_DiscardResult;
 
102
const EqParseNodeType IqParseNodeConstantFloat::m_ID = ParseNode_ConstantFloat;
 
103
const EqParseNodeType IqParseNodeConstantString::m_ID = ParseNode_ConstantString;
 
104
const EqParseNodeType IqParseNodeWhileConstruct::m_ID = ParseNode_WhileConstruct;
 
105
const EqParseNodeType IqParseNodeIlluminateConstruct::m_ID = ParseNode_IlluminateConstruct;
 
106
const EqParseNodeType IqParseNodeIlluminanceConstruct::m_ID = ParseNode_IlluminanceConstruct;
 
107
const EqParseNodeType IqParseNodeSolarConstruct::m_ID = ParseNode_SolarConstruct;
 
108
const EqParseNodeType IqParseNodeConditional::m_ID = ParseNode_Conditional;
 
109
const EqParseNodeType IqParseNodeConditionalExpression::m_ID = ParseNode_ConditionalExpression;
 
110
const EqParseNodeType IqParseNodeTypeCast::m_ID = ParseNode_TypeCast;
 
111
const EqParseNodeType IqParseNodeTriple::m_ID = ParseNode_Triple;
 
112
const EqParseNodeType IqParseNodeSixteenTuple::m_ID = ParseNode_SixteenTuple;
 
113
const EqParseNodeType IqParseNodeMessagePassingFunction::m_ID = ParseNode_MessagePassingFunction;
 
114
 
 
115
 
 
116
///---------------------------------------------------------------------
 
117
/** Find the shader node, to aid in identifying the shader type.
 
118
 */
 
119
 
 
120
CqParseNodeShader* CqParseNode::pShaderNode()
 
121
{
 
122
    // Search up in the hierarchy.
 
123
    CqParseNode* pShader = this;
 
124
    while( ( NULL != pShader ) && ( pShader->NodeType() != IqParseNodeShader::m_ID ) )
 
125
        pShader = pShader->m_pParent;
 
126
    return( static_cast<CqParseNodeShader*>( pShader ) );
 
127
}
 
128
 
 
129
 
 
130
///---------------------------------------------------------------------
 
131
/** Return a string type identifier for the specified type.
 
132
 */
 
133
 
 
134
char* CqParseNode::TypeIdentifier( int Type )
 
135
{
 
136
    return ( gVariableTypeIdentifiers[ Type & Type_Mask ] );
 
137
}
 
138
 
 
139
 
 
140
///---------------------------------------------------------------------
 
141
/** Return a type for the specified type identifier.
 
142
 */
 
143
 
 
144
TqInt CqParseNode::TypeFromIdentifier( char Id )
 
145
{
 
146
    TqInt i;
 
147
    for ( i = 0; i < Type_Last; i++ )
 
148
    {
 
149
        if ( gVariableTypeIdentifiers[ i ][ 0 ] == Id || gVariableTypeIdentifiers[ i ][ 0 ] == tolower( Id ) )
 
150
            return ( i );
 
151
    }
 
152
    return ( Type_Nil );
 
153
}
 
154
 
 
155
 
 
156
///---------------------------------------------------------------------
 
157
/** Return a string type name for the specified type.
 
158
 */
 
159
 
 
160
char* CqParseNode::TypeName( int Type )
 
161
{
 
162
    return ( gVariableTypeNames[ Type & Type_Mask ] );
 
163
}
 
164
 
 
165
 
 
166
///---------------------------------------------------------------------
 
167
/** Find a valid cast type from the list of available options.
 
168
 */
 
169
 
 
170
TqInt   CqParseNode::FindCast( TqInt CurrType, TqInt* pTypes, TqInt Count )
 
171
{
 
172
    // Check if the current type exists in the list first.
 
173
    TqInt i;
 
174
    for ( i = 0; i < Count; i++ )
 
175
        if ( ( pTypes[ i ] & Type_Mask ) == ( CurrType & Type_Mask ) ) return ( static_cast<TqInt>( CurrType & Type_Mask ) );
 
176
 
 
177
    // Else search for the best option.
 
178
    TqInt Ret = Type_Nil;
 
179
    TqInt Pri = 0;
 
180
    for ( i = 0; i < Count; i++ )
 
181
    {
 
182
        if ( m_aaTypePriorities[ CurrType & Type_Mask ][ ( pTypes[ i ] & Type_Mask ) ] > Pri )
 
183
        {
 
184
            Ret = pTypes[ i ];
 
185
            Pri = m_aaTypePriorities[ CurrType & Type_Mask ][ ( pTypes[ i ] & Type_Mask ) ];
 
186
        }
 
187
    }
 
188
    return ( Ret );
 
189
}
 
190
 
 
191
 
 
192
///---------------------------------------------------------------------
 
193
/// CqParseNodeFunctionCall::ResType
 
194
 
 
195
TqInt CqParseNodeFunctionCall::ResType() const
 
196
{
 
197
    // The return type of a function depends on its arguments.
 
198
    return ( CqFuncDef::GetFunctionPtr( m_aFuncRef[ 0 ] ) ->Type() );
 
199
}
 
200
 
 
201
 
 
202
const char*     CqParseNodeFunctionCall::strName() const
 
203
{
 
204
    CqFuncDef * pFuncDef = CqFuncDef::GetFunctionPtr( m_aFuncRef[ 0 ] );
 
205
    if ( pFuncDef != 0 )
 
206
        return ( pFuncDef->strName() );
 
207
    else
 
208
        return ( "" );
 
209
}
 
210
 
 
211
 
 
212
const IqFuncDef* CqParseNodeFunctionCall::pFuncDef() const
 
213
{
 
214
    CqFuncDef * pFuncDef = CqFuncDef::GetFunctionPtr( m_aFuncRef[ 0 ] );
 
215
    if ( pFuncDef != 0 )
 
216
        return ( pFuncDef );
 
217
    else
 
218
        return ( 0 );
 
219
}
 
220
 
 
221
 
 
222
IqFuncDef* CqParseNodeFunctionCall::pFuncDef()
 
223
{
 
224
    CqFuncDef * pFuncDef = CqFuncDef::GetFunctionPtr( m_aFuncRef[ 0 ] );
 
225
    if ( pFuncDef != 0 )
 
226
        return ( pFuncDef );
 
227
    else
 
228
        return ( 0 );
 
229
}
 
230
 
 
231
 
 
232
///---------------------------------------------------------------------
 
233
/// CqParseNodeUnresolvedCall::ResType
 
234
 
 
235
TqInt CqParseNodeUnresolvedCall::ResType() const
 
236
{
 
237
    // The return type of a function depends on its arguments.
 
238
    return ( m_aFuncDef.Type() );
 
239
}
 
240
 
 
241
 
 
242
const char*     CqParseNodeUnresolvedCall::strName() const
 
243
{
 
244
    return ( m_aFuncDef.strName() );
 
245
}
 
246
 
 
247
 
 
248
const IqFuncDef* CqParseNodeUnresolvedCall::pFuncDef() const
 
249
{
 
250
    return ( &m_aFuncDef );
 
251
}
 
252
 
 
253
IqFuncDef* CqParseNodeUnresolvedCall::pFuncDef()
 
254
{
 
255
    return ( &m_aFuncDef );
 
256
}
 
257
 
 
258
 
 
259
///---------------------------------------------------------------------
 
260
/// CqParseNodeVariable::CqParseNodeVariable
 
261
 
 
262
CqParseNodeVariable::CqParseNodeVariable( SqVarRef VarRef ) :
 
263
        CqParseNode(),
 
264
        m_VarRef( VarRef )
 
265
{
 
266
    m_fVarying = ( CqVarDef::GetVariablePtr( VarRef ) ->Type() & Type_Varying ) != 0;
 
267
}
 
268
 
 
269
 
 
270
///---------------------------------------------------------------------
 
271
/// CqParseNodeVariable::CqParseNodeVariable
 
272
 
 
273
CqParseNodeVariable::CqParseNodeVariable( CqParseNodeVariable* pVar ) :
 
274
        CqParseNode(),
 
275
        m_VarRef( pVar->m_VarRef )
 
276
{
 
277
    m_fVarying = ( CqVarDef::GetVariablePtr( m_VarRef ) ->Type() & Type_Varying ) != 0;
 
278
}
 
279
 
 
280
 
 
281
///---------------------------------------------------------------------
 
282
/// CqParseNodeVariable::strName
 
283
/// Returnt the variable name.
 
284
 
 
285
const char* CqParseNodeVariable::strName() const
 
286
{
 
287
    return ( CqVarDef::GetVariablePtr( m_VarRef ) ->strName() );
 
288
}
 
289
 
 
290
 
 
291
///---------------------------------------------------------------------
 
292
/// CqParseNodeVariable::ResType
 
293
/// Return a string type identifier for this variable.
 
294
 
 
295
TqInt CqParseNodeVariable::ResType() const
 
296
{
 
297
    IqVarDef * pVD = IqVarDef::GetVariablePtr( m_VarRef );
 
298
    if ( pVD )
 
299
        return ( pVD->Type() );
 
300
    else
 
301
        return ( Type_Nil );
 
302
}
 
303
 
 
304
 
 
305
///---------------------------------------------------------------------
 
306
/// CqParseNodeVariableArray::CqParseNodeVariableArray
 
307
/// Constructor from a variable reference.
 
308
 
 
309
CqParseNodeVariableArray::CqParseNodeVariableArray( SqVarRef VarRef ) :
 
310
        CqParseNodeVariable( VarRef )
 
311
{}
 
312
 
 
313
 
 
314
///---------------------------------------------------------------------
 
315
/// CqParseNodeVariableArray::CqParseNodeVariableArray
 
316
/// Construct from another variable
 
317
 
 
318
 
 
319
 
 
320
 
 
321
CqParseNodeVariableArray::CqParseNodeVariableArray( CqParseNodeVariableArray* pVar ) :
 
322
        CqParseNodeVariable( pVar )
 
323
{
 
324
    m_fVarying = ( CqVarDef::GetVariablePtr( m_VarRef ) ->Type() & Type_Varying ) != 0;
 
325
    if ( pVar->m_pChild )
 
326
        m_pChild = pVar->m_pChild->Clone( this );
 
327
}
 
328
 
 
329
 
 
330
///---------------------------------------------------------------------
 
331
/// CqParseNodeAssign::ResType
 
332
/// Return a string type identifier for this variable.
 
333
 
 
334
TqInt CqParseNodeAssign::ResType() const
 
335
{
 
336
    return ( CqVarDef::GetVariablePtr( m_VarRef ) ->Type() );
 
337
}
 
338
 
 
339
 
 
340
///---------------------------------------------------------------------
 
341
/// CqParseNodeMathOp::ResType
 
342
/// Return a string type identifier for this variable.
 
343
 
 
344
TqInt CqParseNodeMathOp::ResType() const
 
345
{
 
346
    CqParseNode * pOperandA = m_pChild;
 
347
    CqParseNode* pOperandB = m_pChild->pNext();
 
348
    assert( pOperandA != 0 && pOperandB != 0 );
 
349
 
 
350
    TqInt ResAType = pOperandA->ResType();
 
351
    TqInt ResBType = pOperandB->ResType();
 
352
 
 
353
    switch ( m_Operator )
 
354
    {
 
355
    case Op_Dot:
 
356
        return ( Type_Float );
 
357
    case Op_Mul:
 
358
    case Op_Div:
 
359
    case Op_Add:
 
360
    case Op_Sub:
 
361
    default:
 
362
        // TODO: Should check here for valid types.
 
363
        if ( ( ResAType & Type_Mask ) == Type_Point ||
 
364
                ( ResAType & Type_Mask ) == Type_Color )
 
365
            return ( ResAType );
 
366
        else
 
367
            return ( ResBType );
 
368
        break;
 
369
    }
 
370
}
 
371
 
 
372
 
 
373
END_NAMESPACE( Aqsis )
 
374
//---------------------------------------------------------------------