raz / commit c6f0123e

commit
c6f0123ef903197d1b876fb76c540faffd9520e2
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
74792a82
Step 6a: + joins string constants

Op1 leaves a string constant for +; Op2's JoinStr makes one rodata
chunk of the two (the old ones die in the link if unused). Anything
but a string on the right: '"+" joins two strings, found ...'.
conform/run/JoinStr, conform/err/JoinInt; GenStringOperand's message
changes to the new one.

patch · log

8 files changed, +60 -4

PLAN.md (history) modified +1 -1

@@ -121,7 +121,7 @@
121121 passes, apart from this compiler's own messages.
122122 - [x] 5. The system integer.
123123 - [x] 6. Optional reals: a target says whether it has them.
124-- [ ] 6a. `+` joins string constants (report, section 4).
124+- [x] 6a. `+` joins string constants (report, section 4).
125125 - [ ] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only
126126 data, used as a variable that can't be changed (section 4).
127127 Done so that a string constant is the same mechanism.

conform/err/JoinInt.raz (history) added +5 -0

@@ -0,0 +1,5 @@
1+module JoinInt;
2+
3+const Bad = "count: " + 1;
4+
5+end JoinInt.

conform/run/JoinStr.expect (history) added +5 -0

@@ -0,0 +1,5 @@
1+the cat
2+<the cat>
3+9
4+cat sat on
5+10

conform/run/JoinStr.raz (history) added +21 -0

@@ -0,0 +1,21 @@
1+module JoinStr;
2+import Out;
3+
4+const
5+ Text = "the cat";
6+ Line = Text + "\n";
7+ Both = "<" + Text + ">" + "";
8+ Word = ("sat" + " ") + "on";
9+
10+proc main*;
11+var w: []U8;
12+begin
13+ Out.str(Line);
14+ Out.str(Both); Out.ln;
15+ Out.int(LEN(Both)); Out.ln;
16+ w := Text[4..7];
17+ Out.str(w); Out.str(" "); Out.str(Word + "\n");
18+ Out.int(I64(Line[7])); Out.ln;
19+end;
20+
21+end JoinStr.

doc/compiler.md (history) modified +3 -1

@@ -275,7 +275,9 @@
275275 -1 fault as an `I64`'s does.
276276 `=` and `#` on arrays, slices and strings make a slice of each side
277277 (`SeqSlice`), compare the lengths, and then the bytes with `repe
278-cmpsb`, right where the comparison stands: no call.
278+cmpsb`, right where the comparison stands: no call. `+` on two string
279+constants is `JoinStr`, which makes one string of them, in a chunk of
280+its own.
279281
280282 Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a
281283 variable into an item for part of it, mostly by adding to `off`. An

src/gen.pas modified +19 -0

@@ -865,6 +865,8 @@
865865 if op = sAnd then Jcc(ccE, L) else Jcc(ccNE, L);
866866 end else if (op in [sEql, sNeq]) and IsSeq(x.typ) then
867867 SeqSlice(x)
868+ else if (op = sPlus) and (x.mode = mStr) then
869+ { two string constants to be joined: nothing to compute }
868870 else if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then
869871 NotValue(x)
870872 else if x.mode <> mConst then begin
@@ -1010,6 +1012,22 @@
10101012 MarkAt(x.pos, SymText(op) + ' needs BOOLs, found ' + What(x));
10111013 end;
10121014
1015+{ x + y, of two string constants: one string, in a chunk of its own
1016+ (report section 4) }
1017+procedure JoinStr(var x, y: TItem);
1018+var p: PBytes; c: longint;
1019+begin
1020+ if y.mode <> mStr then MarkAt(y.pos, '"+" joins two strings, found ' + What(y));
1021+ p := GetMem(x.val + y.val + 1);
1022+ Move(rodata^[chunks^[x.chunk].start], p^[0], x.val);
1023+ Move(rodata^[chunks^[y.chunk].start], p^[x.val], y.val + 1);
1024+ c := NewChunk(secRodata, -1, -1);
1025+ SetData(c, p, x.val + y.val + 1, 1);
1026+ FreeMem(p);
1027+ x.chunk := c;
1028+ x.val := x.val + y.val;
1029+end;
1030+
10131031 procedure Op2(op: TSym; var x, y: TItem; L: longint);
10141032 const
10151033 Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq];
@@ -1020,6 +1038,7 @@
10201038 begin
10211039 xt := x.typ; yt := y.typ;
10221040 if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y);
1041+ if (op = sPlus) and (x.mode = mStr) then begin JoinStr(x, y); exit end;
10231042
10241043 if op in [sAnd, sOr] then begin
10251044 if xt^.form <> fBool then WrongLogic(op, x);

test/msg/GenStringOperand.err (history) modified +2 -2

@@ -1,4 +1,4 @@
1-conform/err/GenStringOperand.raz:6:10: error: a string can't be an operand
1+conform/err/GenStringOperand.raz:6:16: error: "+" joins two strings, found 1
22 6 | x := "a" + 1;
3- | ^^^
3+ | ^
44 raz: 1 error

test/msg/JoinInt.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/JoinInt.raz:3:25: error: "+" joins two strings, found 1
2+ 3 | const Bad = "count: " + 1;
3+ | ^
4+raz: 1 error