Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Wrong parent type when computing type of child expressions(ITypeComputer)
Wrong parent type when computing type of child expressions [message #1096476] Wed, 28 August 2013 12:52 Go to next message
Thomas Goossens is currently offline Thomas GoossensFriend
Messages: 32
Registered: August 2013
Member
I continue on: http://www.eclipse.org/forums/index.php/t/513601/

The goal
Create a matrix literal (matlab style)

Examples:
val x = a.[*,1:2]

val c = 1

val y = a.[c;1:2]



This should be converted to
final Matrix x = new MatrixBuilder().columnRange(1,2).build();

final int c = 1;

final Matrix y = new MatrixBuilder().rowSelect(c).columnRange(1,2).build()



I made a custom xbase compiler that does that correctly for me. The problem however is in my type converter.

The Problem
When I ONLY have the following in my typecomputer:


	def void _computeTypes(OMatrixRangeLiteral object, ITypeComputationState state) {

		val LightweightTypeReference type = getTypeForName(
			typeof(OMatrix),
			state
		);

		state.acceptActualType(type)
}


Then everything works fine (except that I cannot reference to other variables of course ie.: a.[c,*] won't work)

When I change my typecomputer as follows (as said in the documentation of ITypeComputer 2.4)
	def void _computeTypes(OMatrixRangeLiteral object, ITypeComputationState state) {

		val LightweightTypeReference type = getTypeForName(
			typeof(OMatrix),
			state
		);

		state.acceptActualType(type)

		if (object.columns.lower != null) {
			state.computeTypes(object.columns.lower)
		}
}


Then it will correctly resolve that membercall to 'c' but the actual type is NOT correct anymore.

ie:

final Object x = new MatrixBuilder().columnRange(1,2).build();

final int c = 1;

final Object y = new MatrixBuilder().rowSelect(c).columnRange(1,2).build()




My relevant grammar
XLiteral returns XExpression:
	XCollectionLiteral |
	XClosure |
	XBooleanLiteral |
	XNumberLiteral |
	XNullLiteral |
	XStringLiteral |
	XTypeLiteral |
	OMatrixRangeLiteral;

MatrixRangeLiteral returns XExpression:
	{MatrixRangeLiteral}
	id=(ValidID) '.' '[' rows=RangeNotation ';' columns=RangeNotation ']';

RangeNotation returns RangeNotation:
	{RangeNotation}
	(lower=XExpression (isrange?=':' upper=XExpression)?) |
	=>(isselection?='(' selections+=XExpression (',' selections+=XExpression)* ')') |
	all?='*';	


My ITypeComputer

 
class OlimathTypeComputer extends XbaseWithAnnotationsTypeComputer {

	override def computeTypes(XExpression expression, ITypeComputationState state) {
		if (expression instanceof OMatrixRangeLiteral) {
			_computeTypes(expression as OMatrixRangeLiteral, state)
		} else {
			super.computeTypes(expression, state)
		}
	}

	def void _computeTypes(OMatrixRangeLiteral object, ITypeComputationState state) {

		val LightweightTypeReference type = getTypeForName(
			typeof(OMatrix),
			state
		);

		state.acceptActualType(type)

		// columns
		/// lower
		if (object.columns.lower != null) {
			state.computeTypes(object.columns.lower)
		}
 

[Updated on: Wed, 28 August 2013 12:56]

Report message to a moderator

Re: Wrong parent type when computing type of child expressions [message #1096484 is a reply to message #1096476] Wed, 28 August 2013 13:05 Go to previous message
Thomas Goossens is currently offline Thomas GoossensFriend
Messages: 32
Registered: August 2013
Member
I misread the documentation and it explains why it is object (it is the common type of int and matrix)


The type of an expression depends on its children.
Expression types may be inferred from the types of contained children. Therefore the given computation state is directly used to compute their types. The framework will automatically propagate the common type of all children to the parent. If- or Switch expression fall into this category. A simplified implementation would again look like this:

   void computeTypes(XIfExpression expression, ITypeComputationState state) {
      state.withExpectations(getTypeForName(Boolean.TYPE, state).computeTypes(expression.getIfExpression());
      state.computeTypes(expression.getThen());
      state.computeTypes(expression.getElse());
     }

Previous Topic:parsing arguments without separating whitespace
Next Topic:Comma separated Unordered group
Goto Forum:
  


Current Time: Fri Apr 26 20:07:03 GMT 2024

Powered by FUDForum. Page generated in 0.02845 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top