Skip to content

Commit c9dfe66

Browse files
committed
Some initial progress on arrays.
Problem: we need a default value for the element type of the (nested) arrays.
1 parent 8329eda commit c9dfe66

File tree

1 file changed

+52
-23
lines changed
  • language/inbetween/src/main/scala/org/combinators/ep/language/inbetween/ffi

1 file changed

+52
-23
lines changed

language/inbetween/src/main/scala/org/combinators/ep/language/inbetween/ffi/ArraysAST.scala

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,71 @@ import org.combinators.ep.language.inbetween.polymorphism.ParametricPolymorphism
44

55
trait ArraysAST extends OperatorExpressionOpsAST with ParametricPolymorphismAST {
66
object arraysOps {
7-
trait CreateArray extends any.Type {
7+
trait FinalTypes {
8+
type Array <: arraysOps.Array
9+
type CreateArrayExpression <: arraysOps.CreateArray
10+
}
11+
12+
trait Array extends any.Type {
13+
def getSelfArrayType: arraysOpsFinalTypes.Array
14+
}
15+
16+
trait CreateArray extends any.Expression {
17+
def getSelfCreateArrayExpression: arraysOpsFinalTypes.Array
18+
19+
def elementType: any.Type
820
def dimension: any.Expression
21+
def initialization: Either[any.Expression, Seq[any.Expression]]
22+
23+
def copy(elementType: any.Type, dimension: any.Expression, initialization: Either[any.Expression, Seq[any.Expression]]) =
24+
arraysOpsFactory.createArray(elementType, dimension, initialization)
925
}
1026

1127
trait GetArrayOp extends operatorExpressions.Operator
28+
1229
trait SetArrayOp extends operatorExpressions.Operator
30+
1331
trait LengthArrayOp extends operatorExpressions.Operator
1432

1533
trait Factory {
16-
def createArray(dimension: any.Expression): CreateArray
1734

18-
def createArray(tpe: any.Type, dimensions:Seq[any.Expression], contentSpec:Option[(Seq[Int], Seq[any.Expression])]): any.Expression = {
19-
contentSpec match {
35+
def array(): arraysOps.Array
2036

21-
case Some((dims, values)) =>
22-
val initializers = dimensions.zip(dims).reverse.tail.foldLeft[(any.Type,Seq[any.Expression])] ( {
23-
val arrayTpe = polymorphismFactory.typeApplication(createArray(dimensions.last), Seq(tpe))
24-
val arrayExpr = values.grouped(dims.last).toSeq.map(subSeq =>
25-
factory.applyExpression(polymorphismFactory.typeReferenceExpression(arrayTpe), subSeq))
26-
(arrayTpe, arrayExpr)
27-
}
28-
) { case ((arrayTpe, inits), (dimension, dim)) =>
37+
def array(elementType: any.Type): any.Type = {
38+
polymorphismFactory.typeApplication(array(), Seq(elementType))
39+
}
40+
41+
def createArray(elementType: any.Type, dimension: any.Expression, initialization: Either[any.Expression, Seq[any.Expression]]): CreateArray
2942

30-
val outerArrayTpe = polymorphismFactory.typeApplication(createArray(dimension), Seq(arrayTpe))
31-
val outerArrayExpr = inits.grouped(dim).toSeq.map(subSeq => factory.applyExpression(polymorphismFactory.typeReferenceExpression(arrayTpe), subSeq))
32-
(outerArrayTpe, outerArrayExpr)
33-
}
43+
def createArray(tpe: any.Type, dimensions: Seq[any.Expression], contentSpec: Option[(Seq[Int], Seq[any.Expression])]): any.Expression = {
44+
contentSpec match {
45+
case Some((dims, values)) =>
46+
val initializers = dimensions.zip(dims).reverse.tail.foldLeft[(any.Type, Seq[any.Expression])]({
47+
val arrayTpe = array(tpe)
48+
val arrayExpr = values.grouped(dims.last).toSeq.map(subSeq =>
49+
arraysOpsFactory.createArray(tpe, dimensions.last, Right(subSeq)))
50+
(arrayTpe, arrayExpr)
51+
}) { case ((arrayTpe, inits), (dimension, dim)) =>
52+
val outerArrayTpe = arraysOpsFactory.array(arrayTpe)
53+
val outerArrayExpr = inits.grouped(dim).toSeq.map(subSeq =>
54+
arraysOpsFactory.createArray(arrayTpe, dimension, Right(subSeq)))
55+
(outerArrayTpe, outerArrayExpr)
56+
}
3457

35-
initializers._2.head
58+
initializers._2.head
3659

3760
case None =>
38-
val arrayTpe = dimensions.reverse.tail.foldLeft[any.Type](
39-
polymorphismFactory.typeApplication(createArray(dimensions.last), Seq(tpe))
40-
) { case (arrayTpe, dimension) =>
41-
polymorphismFactory.typeApplication(createArray(dimension), Seq(arrayTpe))
61+
val (_, arrayExpr) = dimensions.reverse.tail.foldLeft[(any.Type, arraysOps.CreateArray)]({
62+
val arrayTpe = arraysOpsFactory.array(tpe)
63+
val arrayExpr = arraysOpsFactory.createArray(tpe, dimensions.last, Left(null)) // TODO: null needs to be a default expression of type tpe
64+
(arrayTpe, arrayExpr)
65+
}) { case ((arrayTpe, innerInitializer), dimension) =>
66+
val outerArrayTpe = arraysOpsFactory.array(arrayTpe)
67+
val outerArrayExpr = arraysOpsFactory.createArray(arrayTpe, dimension, Left(innerInitializer))
68+
(outerArrayTpe, outerArrayExpr)
4269
}
43-
polymorphismFactory.typeReferenceExpression(arrayTpe)
44-
}
70+
arrayExpr
71+
}
4572
}
4673

4774
def getArrayOp(): GetArrayOp
@@ -56,5 +83,7 @@ trait ArraysAST extends OperatorExpressionOpsAST with ParametricPolymorphismAST
5683
operatorExpressionsFactory.unaryExpression(lengthArrayOp(), ar)
5784
}
5885
}
86+
87+
val arraysOpsFinalTypes: arraysOps.FinalTypes
5988
val arraysOpsFactory: arraysOps.Factory
6089
}

0 commit comments

Comments
 (0)