Mercurial > projects > ddmd
annotate dmd/expression/Slice.d @ 135:af1bebfd96a4 dmd2037
dmd 2.038
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 13 Sep 2010 22:19:42 +0100 |
parents | e28b18c23469 |
children |
rev | line source |
---|---|
0 | 1 module dmd.expression.Slice; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Expression; |
5 import dmd.Type; | |
6 import dmd.Loc; | |
7 import dmd.TOK; | |
8 import dmd.StringExp; | |
9 import dmd.GlobalExpressions; | |
10 import dmd.ArrayLiteralExp; | |
11 import dmd.ArrayTypes; | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
12 import dmd.Util : printf; |
0 | 13 |
4 | 14 import core.memory; |
2 | 15 |
0 | 16 import core.stdc.stdlib; |
17 import core.stdc.string; | |
18 | |
34 | 19 import std.exception; |
0 | 20 |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
21 |
0 | 22 /* Also return EXP_CANT_INTERPRET if this fails |
23 */ | |
24 Expression Slice(Type type, Expression e1, Expression lwr, Expression upr) | |
25 { | |
26 Expression e = EXP_CANT_INTERPRET; | |
27 Loc loc = e1.loc; | |
28 | |
29 version (LOG) { | |
30 printf("Slice()\n"); | |
31 if (lwr) | |
32 { | |
33 printf("\te1 = %s\n", e1.toChars()); | |
34 printf("\tlwr = %s\n", lwr.toChars()); | |
35 printf("\tupr = %s\n", upr.toChars()); | |
36 } | |
37 } | |
38 if (e1.op == TOKstring && lwr.op == TOKint64 && upr.op == TOKint64) | |
39 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
34
diff
changeset
|
40 auto es1 = cast(StringExp)e1; |
0 | 41 ulong ilwr = lwr.toInteger(); |
42 ulong iupr = upr.toInteger(); | |
43 | |
44 if (iupr > es1.len || ilwr > iupr) | |
45 e1.error("string slice [%ju .. %ju] is out of bounds", ilwr, iupr); | |
46 else | |
47 { | |
48 size_t len = cast(size_t)(iupr - ilwr); | |
49 int sz = es1.sz; | |
50 | |
2 | 51 char* s = cast(char*)GC.malloc((len + 1) * sz); |
0 | 52 memcpy(s, cast(ubyte*)es1.string_ + ilwr * sz, len * sz); |
53 memset(s + len * sz, 0, sz); | |
54 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
34
diff
changeset
|
55 auto es = new StringExp(loc, assumeUnique(s[0..len]), es1.postfix); |
0 | 56 es.sz = cast(ubyte)sz; |
57 es.committed = 1; | |
58 es.type = type; | |
59 e = es; | |
60 } | |
61 } | |
62 else if (e1.op == TOKarrayliteral && | |
63 lwr.op == TOKint64 && upr.op == TOKint64 && | |
64 !e1.checkSideEffect(2)) | |
65 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
34
diff
changeset
|
66 auto es1 = cast(ArrayLiteralExp)e1; |
0 | 67 ulong ilwr = lwr.toInteger(); |
68 ulong iupr = upr.toInteger(); | |
69 | |
70 if (iupr > es1.elements.dim || ilwr > iupr) | |
71 e1.error("array slice [%ju .. %ju] is out of bounds", ilwr, iupr); | |
72 else | |
73 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
34
diff
changeset
|
74 auto elements = new Expressions(); |
0 | 75 elements.setDim(cast(uint)(iupr - ilwr)); |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
76 memcpy(elements.ptr, |
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
77 es1.elements.ptr + ilwr, |
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
78 cast(uint)(iupr - ilwr) * (*es1.elements.ptr).sizeof); |
0 | 79 e = new ArrayLiteralExp(e1.loc, elements); |
80 e.type = type; | |
81 } | |
82 } | |
83 return e; | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
84 } |