Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa17291a408d7e65c8a329c375aa130ca732091d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Created on Jun 26, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.eclipse.cdt.internal.core.parser.ast.quick;

import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.ast.ASTExpressionEvaluationException;
import org.eclipse.cdt.core.parser.ast.ASTNotImplementedException;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
import org.eclipse.cdt.core.parser.ast.IASTNode;
import org.eclipse.cdt.core.parser.ast.IASTTypeId;



/**
 * @author jcamelon
 */
public class ASTExpression implements IASTExpression {

	private final Kind kind;
	private static final String EMPTY_STRING = "";  //$NON-NLS-1$

	/**
	 * @param kind
	 * @param id
	 */
	public ASTExpression(Kind kind ) {
		this.kind = kind; 
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#getExpressionKind()
	 */
	public Kind getExpressionKind() {
		return kind;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#getLHSExpression()
	 */
	public IASTExpression getLHSExpression() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#getRHSExpression()
	 */
	public IASTExpression getRHSExpression() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#getLiteralString()
	 */
	public String getLiteralString() {
		return EMPTY_STRING;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#getTypeId()
	 */
	public IASTTypeId getTypeId() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#getNewExpressionDescriptor()
	 */
	public IASTNewExpressionDescriptor getNewExpressionDescriptor() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#getThirdExpression()
	 */
	public IASTExpression getThirdExpression() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#evaluateExpression()
	 */
	public long evaluateExpression() throws ASTExpressionEvaluationException {
		// primary expressions
		if( getExpressionKind() == IASTExpression.Kind.PRIMARY_INTEGER_LITERAL )
		{
			try
			{
				if( getLiteralString().startsWith( "0x") || getLiteralString().startsWith( "0x") ) //$NON-NLS-1$ //$NON-NLS-2$
				{
                    return Integer.parseInt( getLiteralString().substring(2), 16 );
				}
				if( getLiteralString().startsWith( "0") && getLiteralString().length() > 1 ) //$NON-NLS-1$
					return Integer.parseInt( getLiteralString().substring(1), 8 );
				return Integer.parseInt( getLiteralString() );
			}
			catch( NumberFormatException nfe )
			{
				throw new ASTExpressionEvaluationException();
			}
		}	
		
		if( getExpressionKind() == IASTExpression.Kind.PRIMARY_BRACKETED_EXPRESSION ) 
			return getLHSExpression().evaluateExpression();
		// unary not 
		if( getExpressionKind() == IASTExpression.Kind.UNARY_NOT_CASTEXPRESSION ) 
			return ( ( getLHSExpression().evaluateExpression() == 0 ) ? 1 : 0 ); 
		
		// multiplicative expressions 
		if( getExpressionKind() == IASTExpression.Kind.MULTIPLICATIVE_MULTIPLY )
			return ( getLHSExpression().evaluateExpression() * getRHSExpression().evaluateExpression()) ; 
		if( getExpressionKind() == IASTExpression.Kind.MULTIPLICATIVE_DIVIDE )
			return ( getLHSExpression().evaluateExpression() / getRHSExpression().evaluateExpression()) ; 
		if( getExpressionKind() == IASTExpression.Kind.MULTIPLICATIVE_MODULUS )
			return ( getLHSExpression().evaluateExpression() % getRHSExpression().evaluateExpression()) ;
		// additives 
		if( getExpressionKind() == IASTExpression.Kind.ADDITIVE_PLUS )
			return ( getLHSExpression().evaluateExpression() + getRHSExpression().evaluateExpression()) ; 
		if( getExpressionKind() == IASTExpression.Kind.ADDITIVE_MINUS )
			return ( getLHSExpression().evaluateExpression() - getRHSExpression().evaluateExpression()) ; 
		// shift expression 
		if( getExpressionKind() == IASTExpression.Kind.SHIFT_LEFT )
			return ( getLHSExpression().evaluateExpression() << getRHSExpression().evaluateExpression()) ; 
		if( getExpressionKind() == IASTExpression.Kind.SHIFT_RIGHT )
			return ( getLHSExpression().evaluateExpression() >> getRHSExpression().evaluateExpression()) ;
		// relational 
		if( getExpressionKind() == IASTExpression.Kind.RELATIONAL_LESSTHAN )
			return ( getLHSExpression().evaluateExpression() < getRHSExpression().evaluateExpression() ? 1 : 0 ) ; 
		if( getExpressionKind() == IASTExpression.Kind.RELATIONAL_GREATERTHAN )
			return ( getLHSExpression().evaluateExpression() > getRHSExpression().evaluateExpression() ? 1 : 0 ) ; 
		if( getExpressionKind() == IASTExpression.Kind.RELATIONAL_LESSTHANEQUALTO )
			return ( getLHSExpression().evaluateExpression() <= getRHSExpression().evaluateExpression() ? 1 : 0 ) ; 
		if( getExpressionKind() == IASTExpression.Kind.RELATIONAL_GREATERTHANEQUALTO )
			return ( getLHSExpression().evaluateExpression() >= getRHSExpression().evaluateExpression() ? 1 : 0 ) ;
		// equality 
		if( getExpressionKind() == IASTExpression.Kind.EQUALITY_EQUALS )
			return ( getLHSExpression().evaluateExpression() == getRHSExpression().evaluateExpression() ? 1 : 0 ) ;  
		if( getExpressionKind() == IASTExpression.Kind.EQUALITY_NOTEQUALS )
			return ( getLHSExpression().evaluateExpression() != getRHSExpression().evaluateExpression() ? 1 : 0 ) ; 
		 // and  
		if( getExpressionKind() == IASTExpression.Kind.ANDEXPRESSION )
			return ( getLHSExpression().evaluateExpression() & getRHSExpression().evaluateExpression() ) ;
		 // xor
		if( getExpressionKind() == IASTExpression.Kind.EXCLUSIVEOREXPRESSION )
			return ( getLHSExpression().evaluateExpression() ^ getRHSExpression().evaluateExpression() ) ;
		// or 
		if( getExpressionKind() == IASTExpression.Kind.INCLUSIVEOREXPRESSION )
			return ( getLHSExpression().evaluateExpression() | getRHSExpression().evaluateExpression() ) ;
		// logical and
		if( getExpressionKind() == IASTExpression.Kind.LOGICALANDEXPRESSION )
			return( ( getLHSExpression().evaluateExpression() != 0 ) &&  ( getRHSExpression().evaluateExpression() != 0 ) ) ? 1 : 0 ;	 
		// logical or  
		if( getExpressionKind() == IASTExpression.Kind.LOGICALOREXPRESSION )
			return( ( getLHSExpression().evaluateExpression() != 0 ) || ( getRHSExpression().evaluateExpression() != 0 ) ) ? 1 : 0 ;
		
		if( getExpressionKind() == IASTExpression.Kind.CONDITIONALEXPRESSION )
		{
			return ( getLHSExpression().evaluateExpression() != 0 ) ? getRHSExpression().evaluateExpression() : getThirdExpression().evaluateExpression(); 
		}

		throw new ASTExpressionEvaluationException();
	}

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate#acceptElement(org.eclipse.cdt.core.parser.ISourceElementRequestor)
     */
    public void acceptElement(ISourceElementRequestor requestor)
    {
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate#enterScope(org.eclipse.cdt.core.parser.ISourceElementRequestor)
     */
    public void enterScope(ISourceElementRequestor requestor)
    {
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate#exitScope(org.eclipse.cdt.core.parser.ISourceElementRequestor)
     */
    public void exitScope(ISourceElementRequestor requestor)
    {
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.parser.ast.IASTExpression#getIdExpression()
     */
    public String getIdExpression()
    {
    	return null;
    }
    public char[] getIdExpressionCharArray(){
    	return null;
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.parser.ast.IASTExpression#reconcileReferences()
     */
    public void reconcileReferences() throws ASTNotImplementedException
    {
    	throw new ASTNotImplementedException();
    }
	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#purgeReferences()
	 */
	public void purgeReferences() throws ASTNotImplementedException
	{
		throw new ASTNotImplementedException();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTNode#lookup(java.lang.String, org.eclipse.cdt.core.parser.ast.IASTNode.LookupKind[], org.eclipse.cdt.core.parser.ast.IASTNode)
	 */
	public ILookupResult lookup(String prefix, LookupKind[] k, IASTNode context, IASTExpression functionParameters) throws LookupError, ASTNotImplementedException {
		// Not provided in this mode
		throw new ASTNotImplementedException();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTExpression#freeReferences()
	 */
	public void freeReferences() {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#setStartingOffsetAndLineNumber(int, int)
	 */
	public void setStartingOffsetAndLineNumber(int offset, int lineNumber) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#setEndingOffsetAndLineNumber(int, int)
	 */
	public void setEndingOffsetAndLineNumber(int offset, int lineNumber) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getStartingOffset()
	 */
	public int getStartingOffset() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getEndingOffset()
	 */
	public int getEndingOffset() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getStartingLine()
	 */
	public int getStartingLine() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getEndingLine()
	 */
	public int getEndingLine() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#getFilename()
	 */
	public char[] getFilename() {
		// TODO Auto-generated method stub
		return null;
	}


}

Back to the top