Java Code Examples for org.eclipse.xtext.xbase.XBasicForLoopExpression#getInitExpressions()

The following examples show how to use org.eclipse.xtext.xbase.XBasicForLoopExpression#getInitExpressions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(final XBasicForLoopExpression object, final ITypeComputationState state) {
	for(XExpression initExpression : object.getInitExpressions()) {
		ITypeComputationState expressionState = state.withoutExpectation();
		expressionState.computeTypes(initExpression);
		addLocalToCurrentScope(initExpression, state);
	}
	state.withinScope(object);
	XExpression expression = object.getExpression();
	if (expression != null) {
		LightweightTypeReference booleanType = getRawTypeForName(Boolean.TYPE, state);
		ITypeComputationState conditionExpectation = state.withExpectation(booleanType);
		conditionExpectation.computeTypes(expression);
	}
	
	XExpression eachExpression = object.getEachExpression();
	ITypeComputationState stateWithoutExpectation = state.withoutExpectation();
	ITypeComputationState eachExpressionState = reassignCheckedType(expression, eachExpression, stateWithoutExpectation);
	eachExpressionState.computeTypes(eachExpression);
	
	for (XExpression updateExpression : object.getUpdateExpressions()) {
		eachExpressionState.computeTypes(updateExpression);
	}
	
	LightweightTypeReference primitiveVoid = getPrimitiveVoid(state);
	state.acceptActualType(primitiveVoid);
}
 
Example 2
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected Object _doEvaluate(XBasicForLoopExpression forLoop, IEvaluationContext context, CancelIndicator indicator) {
	IEvaluationContext forkedContext = context.fork();
	for (XExpression initExpression : forLoop.getInitExpressions()) {
		internalEvaluate(initExpression, forkedContext, indicator);
	}
	XExpression expression = forLoop.getExpression();
	Object condition = expression == null ? Boolean.TRUE : internalEvaluate(expression, forkedContext, indicator);
	while (Boolean.TRUE.equals(condition)) {
		internalEvaluate(forLoop.getEachExpression(), forkedContext, indicator);
		for (XExpression updateExpression : forLoop.getUpdateExpressions()) {
			internalEvaluate(updateExpression, forkedContext, indicator);
		}
		condition = expression == null ? Boolean.TRUE : internalEvaluate(expression, forkedContext, indicator);
	}
	return null;
}
 
Example 3
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBasicForLoopExpression_0() throws Exception {
	XBasicForLoopExpression loop = (XBasicForLoopExpression) expression("for(var i = 0; i < 10; i = i + 1) {}");
	for (XExpression initExpression : loop.getInitExpressions()) {
		assertExpected(null, initExpression);	
	}
	assertExpected("boolean", loop.getExpression());
	for (XExpression updateExpression : loop.getUpdateExpressions()) {
		assertExpected(null, updateExpression);	
	}
}
 
Example 4
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBasicForLoopExpression_1() throws Exception {
	XBasicForLoopExpression loop = (XBasicForLoopExpression) expression("for(null; null; null) {}");
	for (XExpression initExpression : loop.getInitExpressions()) {
		assertExpected(null, initExpression);	
	}
	assertExpected("boolean", loop.getExpression());
	for (XExpression updateExpression : loop.getUpdateExpressions()) {
		assertExpected(null, updateExpression);	
	}
}
 
Example 5
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBasicForLoopExpression_2() throws Exception {
	XBasicForLoopExpression loop = (XBasicForLoopExpression) expression("for(;;) {}");
	for (XExpression initExpression : loop.getInitExpressions()) {
		assertExpected(null, initExpression);	
	}
	assertExpected(null, loop.getExpression());
	for (XExpression updateExpression : loop.getUpdateExpressions()) {
		assertExpected(null, updateExpression);	
	}
}
 
Example 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature 
 */
protected void toJavaBasicForStatement(XBasicForLoopExpression expr, ITreeAppendable b, boolean isReferenced) {
	ITreeAppendable loopAppendable = b.trace(expr);
	loopAppendable.openPseudoScope();
	loopAppendable.newLine().append("for (");
	
	EList<XExpression> initExpressions = expr.getInitExpressions();
	XExpression firstInitExpression = IterableExtensions.head(initExpressions);
	if (firstInitExpression instanceof XVariableDeclaration) {
		XVariableDeclaration variableDeclaration = (XVariableDeclaration) firstInitExpression;
		LightweightTypeReference type = appendVariableTypeAndName(variableDeclaration, loopAppendable);
		loopAppendable.append(" = ");
		if (variableDeclaration.getRight() != null) {
			compileAsJavaExpression(variableDeclaration.getRight(), loopAppendable, type);
		} else {
			appendDefaultLiteral(loopAppendable, type);
		}
	} else {
		for (int i = 0; i < initExpressions.size(); i++) {
			if (i != 0) {
				loopAppendable.append(", ");
			}
			XExpression initExpression = initExpressions.get(i);
			compileAsJavaExpression(initExpression, loopAppendable, getLightweightType(initExpression));
		}
	}
	
	loopAppendable.append(";");
	
	XExpression expression = expr.getExpression();
	if (expression != null) {
		loopAppendable.append(" ");
		internalToJavaExpression(expression, loopAppendable);
	}
	loopAppendable.append(";");
	
	EList<XExpression> updateExpressions = expr.getUpdateExpressions();
	for (int i = 0; i < updateExpressions.size(); i++) {
		if (i != 0) {
			loopAppendable.append(",");
		}
		loopAppendable.append(" ");
		XExpression updateExpression = updateExpressions.get(i);
		internalToJavaExpression(updateExpression, loopAppendable);
	}
	loopAppendable.append(") {").increaseIndentation();
	
	XExpression eachExpression = expr.getEachExpression();
	internalToJavaStatement(eachExpression, loopAppendable, false);
	
	loopAppendable.decreaseIndentation().newLine().append("}");
	loopAppendable.closeScope();
}
 
Example 7
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void toJavaWhileStatement(XBasicForLoopExpression expr, ITreeAppendable b, boolean isReferenced) {
	ITreeAppendable loopAppendable = b.trace(expr);
	
	boolean needBraces = !bracesAreAddedByOuterStructure(expr);
	if (needBraces) {
		loopAppendable.newLine().increaseIndentation().append("{");
		loopAppendable.openPseudoScope();
	}
	
	EList<XExpression> initExpressions = expr.getInitExpressions();
	for (int i = 0; i < initExpressions.size(); i++) {
		XExpression initExpression = initExpressions.get(i);
		if (i < initExpressions.size() - 1) {
			internalToJavaStatement(initExpression, loopAppendable, false);
		} else {
			internalToJavaStatement(initExpression, loopAppendable, isReferenced);
			if (isReferenced) {
				loopAppendable.newLine().append(getVarName(expr, loopAppendable)).append(" = (");
				internalToConvertedExpression(initExpression, loopAppendable, getLightweightType(expr));
				loopAppendable.append(");");
			}
		}
	}

	final String varName = loopAppendable.declareSyntheticVariable(expr, "_while");
	
	XExpression expression = expr.getExpression();
	if (expression != null) {
		internalToJavaStatement(expression, loopAppendable, true);
		loopAppendable.newLine().append("boolean ").append(varName).append(" = ");
		internalToJavaExpression(expression, loopAppendable);
		loopAppendable.append(";");
	} else {
		loopAppendable.newLine().append("boolean ").append(varName).append(" = true;");
	}
	loopAppendable.newLine();
	loopAppendable.append("while (");
	loopAppendable.append(varName);
	loopAppendable.append(") {").increaseIndentation();
	loopAppendable.openPseudoScope();
	
	XExpression eachExpression = expr.getEachExpression();
	internalToJavaStatement(eachExpression, loopAppendable, false);
	
	EList<XExpression> updateExpressions = expr.getUpdateExpressions();
	if (!updateExpressions.isEmpty()) {
		for (XExpression updateExpression : updateExpressions) {
			internalToJavaStatement(updateExpression, loopAppendable, false);
		}
	}
	
	if (!isEarlyExit(eachExpression)) {
		if (expression != null) {
			internalToJavaStatement(expression, loopAppendable, true);
			loopAppendable.newLine().append(varName).append(" = ");
			internalToJavaExpression(expression, loopAppendable);
			loopAppendable.append(";");
		} else {
			loopAppendable.newLine().append(varName).append(" = true;");
		}
	}
	
	loopAppendable.closeScope();
	loopAppendable.decreaseIndentation().newLine().append("}");
	
	if (needBraces) {
		loopAppendable.closeScope();
		loopAppendable.decreaseIndentation().newLine().append("}");
	}
}