Opened 16 years ago

Closed 16 years ago

#203 closed enhancement (fixed)

Add support for record and array literal expressions to the front-end

Reported by: zirkel Owned by: ywei
Priority: major Milestone: Release 1.0
Component: front Version: 1.0
Keywords: literal, array, record, modelbuilder, modelfactory Cc:

Description

There are methods in the ModelFactory called arrayLiteralExpression and recordLiteralExpression. The front-end support for these expressions needs to be added.

Change History (9)

comment:1 by ywei, 16 years ago

I am confused about how arrayLiteralExpression() works, on line 415 in ModelFactory.java:

TypeIF type = arrayType(literalArray[0].type(), literalArray.length);

I am not quite sure what the literalArray argument should look like. In the front-end, for example, when you have the following declaration:

int x[3][3] = {1, 2, 3};

The initialization {1, 2, 3} will be converted into a literal array which looks like this:

((1, 2, 3), (null, null, null), (null, null, null))

It is a literal array expression with 3 elements, all these 3 elements are of type ArrayLiteral. The first ArrayLiteral is (1, 2, 3), it has 3 elements of type integer. The second and third ArrayLiteral contain only null. (total of 6 nulls). So in model builder, I converted the outermost ArrayLiteral into an array containing 3 LiteralExpressionIF type elements, then I called the arrayLiteralExpression() method. But it looks like the method is trying to first construct the array type from the literal array (the code I quoted above), which I don't think is correct. For example, in the above example, the type generated by this method will be int[][][], but actually the type is int[][].

Do I miss something or should I use another form to represent the array literal? Or does the array literal expression method in model package also need a type argument, just like the record literal expression method?

comment:2 by ywei, 16 years ago

This is a simple example, suppose the following code:

int x[3] = {1, 2, 3};

The model builder should construct an array literal containing 3 elements. Each element has type integer. The array literal should have type int[]. But the arrayLiteralExpression() method returns an array literal expression of type int[][][].

comment:3 by Stephen Siegel, 16 years ago

It is a bug, it should be

TypeIF type = arrayType(literalArray[0].type());

Although I wonder what is supposed to happen if the literal array has length 0.

comment:4 by Stephen Siegel, 16 years ago

To answer the question about multi-dim. array literals: to create array literal expression for {{1,2},{3,4}}:

LiteralExpressionIF a1 = mf.arrayLiteralExpression(new LiteralExpressionIF[]
 { mf.integerLiteralExpression("1"), mf.integerLiteralExpression("2") });
LiteralExpressionIF a2 = mf.arrayLiteralExpression(new LiteralExpressionIF[]
 { mf.integerLiteralExpression("3"), mf.integerLiteralExpression("4") });
LiteralExpressionIF a3 = mf.arrayLiteralExpression(new LiteralExpressionIF[] {a1, a2});

The desired expression is a3.

comment:5 by ywei, 16 years ago

Status: newaccepted

in reply to:  3 comment:6 by ywei, 16 years ago

Replying to siegel:

It is a bug, it should be

TypeIF type = arrayType(literalArray[0].type());

Although I wonder what is supposed to happen if the literal array has length 0.

I think this method needs a type argument because we allow null value in the literal array. So the above code will cause a null pointer exception if element 0 of the array is null.

comment:7 by Stephen Siegel, 16 years ago

Sounds reasonable to me. Someone should go ahead and add the type argument to this method. (Also, the array could be non null but have length 0.) Make sure the documentation states clearly that elements can be null.

comment:8 by ywei, 16 years ago

I will do that.

comment:9 by ywei, 16 years ago

Resolution: fixed
Status: acceptedclosed

Features added. Front-end supports array and struct type initializations now.

Note: See TracTickets for help on using tickets.