• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tbklang / tlang / #973

03 Jan 2025 09:39AM UTC coverage: 79.97% (-0.4%) from 80.342%
#973

push

coveralls-ruby

deavmi
Dep-gen

- Added missing import

4839 of 6051 relevant lines covered (79.97%)

368.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.78
/source/tlang/compiler/symbols/expressions.d
1
module tlang.compiler.symbols.expressions;
2

3
import tlang.compiler.symbols.data;
4
import std.conv : to;
5

6
// AST manipulation interfaces
7
import tlang.compiler.symbols.mcro : MStatementSearchable, MStatementReplaceable, MCloneable;
8
import std.string : format;
9

10

11

12
public class OperatorExpression : Expression
13
{
14
    /* Operator */
15
    private SymbolType operator;
16

17
    this(SymbolType operator)
126✔
18
    {
19
        this.operator = operator;
126✔
20
    }
21

22
    public SymbolType getOperator()
23
    {
24
        return operator;
179✔
25
    }
26
}
27

28
public class UnaryOperatorExpression : OperatorExpression
29
{
30
    private Expression exp;
31

32
    this(SymbolType operator, Expression exp)
27✔
33
    {
34
        super(operator);
27✔
35
        this.exp = exp;
27✔
36
    }
37

38
    public override string toString()
39
    {
40
        return "[unaryOperator: Op: "~to!(string)(operator)~", Expr: "~to!(string)(exp);
483✔
41
    }
42

43
    public Expression getExpression()
44
    {
45
        return exp;
21✔
46
    }
47
}
48

49
public class BinaryOperatorExpression : OperatorExpression, MStatementSearchable, MStatementReplaceable, MCloneable
50
{
51
    private Expression lhs, rhs;
52

53
    /* TODO: Take in operator */
54
    this(SymbolType operator, Expression lhs, Expression rhs)
99✔
55
    {
56
        super(operator);
99✔
57
        this.lhs = lhs;
99✔
58
        this.rhs = rhs;
99✔
59
    }
60

61
    public Expression getLeftExpression()
62
    {
63
        return lhs;
274✔
64
    }
65

66
    public Expression getRightExpression()
67
    {
68
        return rhs;
274✔
69
    }
70

71
    public override string toString()
72
    {
73
        /* TODO: FIll in */
74
        return "[BinOpExp: Op: "~to!(string)(operator)~", Lhs: "~lhs.toString()~", Rhs: "~rhs.toString()~"]";
1,049✔
75
    }
76

77
    public override Statement[] search(TypeInfo_Class clazzType)
78
    {
79
        /* List of returned matches */
80
        Statement[] matches;
440✔
81

82
        /* Are we (ourselves) of this type? */
83
        if(clazzType.isBaseOf(this.classinfo))
440✔
84
        {
85
            matches ~= [this];
×
86
        }
87

88
        /* Recurse on our left-hand side `Expression` (if possible) */
89
        MStatementSearchable lhsCasted = cast(MStatementSearchable)lhs;
440✔
90
        if(lhsCasted)
440✔
91
        {
92
            matches ~= lhsCasted.search(clazzType); 
266✔
93
        }
94

95
        /* Recurse on our right-hand side `Expression` (if possible) */
96
        MStatementSearchable rhsCasted = cast(MStatementSearchable)rhs;
440✔
97
        if(rhsCasted)
440✔
98
        {
99
            matches ~= rhsCasted.search(clazzType); 
260✔
100
        }
101

102
        return matches;
440✔
103
    }
104

105
    public override bool replace(Statement thiz, Statement that)
106
    {
107
        /* We cannot directly replace ourselves */
108
        if(this == thiz)
×
109
        {
110
            return false;
×
111
        }
112
        /* Is the left-hand side `Expression` to be replaced? */
113
        else if(thiz == lhs)
×
114
        {
115
            lhs = cast(Expression)that;
×
116
            return true;
×
117
        }
118
        /* Is the right-hand side `Expression` to be replaced? */
119
        else if(thiz == rhs)
×
120
        {
121
            rhs = cast(Expression)that;
×
122
            return true;
×
123
        }
124
        /* If not direct match, then recurse and replace on left-hand side `Expression` (if possible) */
125
        else if(cast(MStatementReplaceable)lhs)
×
126
        {
127
            MStatementReplaceable lhsCasted = cast(MStatementReplaceable)lhs;
×
128
            return lhsCasted.replace(thiz, that);
×
129
        }
130
        /* If not direct match, then recurse and replace on right-hand side `Expression` (if possible) */
131
        else if(cast(MStatementReplaceable)rhs)
×
132
        {
133
            MStatementReplaceable rhsCasted = cast(MStatementReplaceable)rhs;
×
134
            return rhsCasted.replace(thiz, that);
×
135
        }
136
        /* If not direct match and not replaceable */
137
        else
138
        {
139
            return false;
×
140
        }
141
    }
142

143
    /** 
144
     * Clones this binery operator expression recursively
145
     * returning a fresh new copy of itself and its
146
     * left and right operands
147
     *
148
     * Param:
149
     *   newParent = the `Container` to re-parent the
150
     *   cloned `Statement`'s self to
151
     *
152
     * Returns: the cloned `Statement`
153
     */
154
    public override Statement clone(Container newParent = null)
155
    {
156
        BinaryOperatorExpression clonedBinaryOp;
×
157

158
        // Clone the left-hand operand expression (if supported, TODO: throw an error if not)
159
        Expression clonedLeftOperandExpression = null;
×
160
        if(cast(MCloneable)this.lhs)
×
161
        {
162
            MCloneable cloneableExpression = cast(MCloneable)this.lhs;
×
163
            clonedLeftOperandExpression = cast(Expression)cloneableExpression.clone(); // NOTE: We must parent it if needs be
×
164
        }
165

166
        // Clone the left-hand operand expression (if supported, TODO: throw an error if not)
167
        Expression clonedRightOperandExpression = null;
×
168
        if(cast(MCloneable)this.rhs)
×
169
        {
170
            MCloneable cloneableExpression = cast(MCloneable)this.rhs;
×
171
            clonedRightOperandExpression = cast(Expression)cloneableExpression.clone(); // NOTE: We must parent it if needs be
×
172
        }
173

174
        // Clone ourselves
175
        clonedBinaryOp = new BinaryOperatorExpression(this.operator, clonedLeftOperandExpression, clonedRightOperandExpression);
×
176

177
        // Parent outselves to the given parent
178
        clonedBinaryOp.parentTo(newParent);
×
179

180
        return clonedBinaryOp;
×
181
    }
182
}
183

184
public enum IntegerLiteralEncoding
185
{
186
    SIGNED_INTEGER,
187
    UNSIGNED_INTEGER,
188
    SIGNED_LONG,
189
    UNSIGNED_LONG
190
}
191

192
public class IntegerLiteral : NumberLiteral, MCloneable
193
{
194
    private IntegerLiteralEncoding encoding;
195

196
    this(string integerLiteral, IntegerLiteralEncoding encoding)
259✔
197
    {
198
        super(integerLiteral);
259✔
199
        this.encoding = encoding;
259✔
200
    }
201

202
    public IntegerLiteralEncoding getEncoding()
203
    {
204
        return encoding;
213✔
205
    }
206

207
    public override string toString()
208
    {
209
        return "[integerLiteral: "~numberLiteral~" ("~to!(string)(encoding)~")]";
3,680✔
210
    }
211

212
    /** 
213
     * Clones this integer literal
214
     *
215
     * Param:
216
     *   newParent = the `Container` to re-parent the
217
     *   cloned `Statement`'s self to
218
     *
219
     * Returns: the cloned `Statement`
220
     */
221
    public override Statement clone(Container newParent = null)
222
    {
223
        IntegerLiteral clonedIntegerLiteral;
×
224

225
        clonedIntegerLiteral = new IntegerLiteral(this.numberLiteral, this.encoding);
×
226

227
        // Parent outselves to the given parent
228
        clonedIntegerLiteral.parentTo(newParent);
×
229

230
        return clonedIntegerLiteral;
×
231
    }
232
}
233

234
//TODO: Work on floating point literal encodings
235
public final class FloatingLiteral : NumberLiteral
236
{
237
    // TODO: Put the equivalent of FloatingLiteralEncoding here
238

239
    this(string floatingLiteral)
×
240
    {
241
        super(floatingLiteral);
×
242
    }
243

244
    public override string toString()
245
    {
246
        return "[floatingLiteral: "~numberLiteral~"]"; // ("~to!(string)(encoding)~")]";
×
247
    }
248
}
249

250
public abstract class NumberLiteral : Expression
251
{
252
    private string numberLiteral;
253

254
    this(string numberLiteral)
259✔
255
    {
256
        this.numberLiteral = numberLiteral;
259✔
257
    }
258

259
    public final string getNumber()
260
    {
261
        return numberLiteral;
230✔
262
    }
263

264
    public final void setNumber(string numberLiteral)
265
    {
266
        this.numberLiteral = numberLiteral;
×
267
    }
268
}
269

270
public abstract class Expression : Statement
271
{
272
    
273
}
274

275
public final class NewExpression : Expression
276
{
277
    private FunctionCall funcCall;
278

279
    this(FunctionCall funcCall)
×
280
    {
281
        this.funcCall = funcCall;
×
282
    }
283

284
    public FunctionCall getFuncCall()
285
    {
286
        return funcCall;
×
287
    }
288
}
289

290
public final class CastedExpression : Expression, MCloneable
291
{
292
    private Expression uncastedExpression;
293
    private string toType;
294

295
    this(string toType, Expression uncastedExpression)
10✔
296
    {
297
        this.toType = toType;
10✔
298
        this.uncastedExpression = uncastedExpression;
10✔
299
    }
300

301
    public string getToType()
302
    {
303
        return toType;
10✔
304
    }
305

306
    public Expression getEmbeddedExpression()
307
    {
308
        return uncastedExpression;
10✔
309
    }
310

311
    /** 
312
     * Clones this casted expression recursively
313
     * and returns a fresh copy of it
314
     *
315
     * Param:
316
     *   newParent = the `Container` to re-parent the
317
     *   cloned `Statement`'s self to
318
     *
319
     * Returns: the cloned `Statement`
320
     */
321
    public override Statement clone(Container newParent = null)
322
    {
323
        CastedExpression clonedCastedExpression;
×
324

325
        // Clone the uncasted expression (if supported, TODO: throw an error if not)
326
        Expression clonedUncastedExpression = null;
×
327
        if(cast(MCloneable)this.uncastedExpression)
×
328
        {
329
            MCloneable cloneableExpression = cast(MCloneable)this.uncastedExpression;
×
330
            clonedUncastedExpression = cast(Expression)cloneableExpression.clone(); // NOTE: We must parent it if needs be
×
331
        }
332

333
        clonedCastedExpression = new CastedExpression(this.toType, clonedUncastedExpression);
×
334

335
        // Parent outselves to the given parent
336
        clonedCastedExpression.parentTo(newParent);
×
337

338
        return clonedCastedExpression;
×
339
    }
340
}
341

342
public final class ArrayIndex : Expression
343
{
344
    /* The expression to index of */
345
    private Expression indexInto;
346

347
    /* The expression used as the index */
348
    private Expression index;
349

350
    this(Expression indexInto, Expression index)
44✔
351
    {
352
        this.indexInto = indexInto;
44✔
353
        this.index = index;
44✔
354
    }
355

356
    public Expression getIndexed()
357
    {
358
        return indexInto;
44✔
359
    }
360

361
    public Expression getIndex()
362
    {
363
        return index;
44✔
364
    }
365

366
    public override string toString()
367
    {
368
        return "ArrayIndex [to: "~indexInto.toString()~", idx: "~index.toString()~"]";
1,120✔
369
    }
370
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc