aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scalam/m/ast/expressions.scala
blob: 81e048ed82960175c08e657d4625a71cafd430f3 (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
package scalam.m.ast

/**
 * Represents a matlab expression.
 * @define construct expression
 */
trait Expression extends Mable with Root {
  def m: String = this match {
    case IntLiteral(x) => x.toString
    case DoubleLiteral(x) => x.toString
    case StringLiteral(x) => "'" + x + "'"
    case SliceLiteral => ":"
    case ArrayLiteral(elements @ _*) => elements.map(_.m).mkString("[", ",", "]")
    case MatrixLiteral(rows @ _*) => rows.map(_.m).mkString("[", ";", "]")

    case Variable(id) => id.m
    case IndexMatrix(id, indices @ _*) => id.m + indices.map(_.m).mkString("(", ",", ")")
    case IndexStructure(id, indices @ _*) => id.m + indices.map(_.m).mkString("{", ",", "}")
    case Function(id, params @ _*) => id.m + params.map(_.m).mkString("(", ",", ")")

    case _ => throw new IllegalArgumentException("unkown expression: " + this)
  }
}

/**
 * A matlab integer literal.
 * @define construct integer literal
 */
case class IntLiteral(x: Int) extends Expression

/**
 * A matlab double literal.
 * @define construct double literal
 */
case class DoubleLiteral(x: Double) extends Expression

/**
 * A matlab string literal.
 * @define construct string literal
 */
case class StringLiteral(x: String) extends Expression

/**
 * The matlab slice literal (:).
 * @define construct slice literal
 */
case object SliceLiteral extends Expression

/**
 * A matlab array or single-line matrix literal.
 * @param elements elements of the array
 *
 * @define construct array literal
 */
case class ArrayLiteral(elements: Expression*) extends Expression

/**
 * A matlab matrix literal.
 * @param rows rows of the matrix
 *
 * @define construct matrix literal
 */
case class MatrixLiteral(rows: Expression*) extends Expression

/**
 * A matlab variable access.
 * @param id identifier ("variable name") of the variable
 *
 * @define construct variable access
 */
case class Variable(id: Identifier) extends Expression

/**
 * A matlab matrix access by index.
 * @param id identifier of the variable to index
 * @param indices indices
 *
 * @define construct matrix access by index
 */
case class IndexMatrix(id: Identifier, indices: Expression*) extends Expression

/**
 * A matlab structure access by index.
 * @param id identifier of the variable to index
 * @param indices indices
 *
 * @define construct structure access by index
 */
case class IndexStructure(id: Identifier, indices: Expression*) extends Expression

/**
 * A matlab function call.
 * @param function identifier of the function
 * @param params parameters to pass to function call
 *
 * @define construct function call
 */
case class Function(function: Identifier, params: Expression*) extends Expression