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.
| @@ -121,7 +121,7 @@ | ||
| 121 | 121 | passes, apart from this compiler's own messages. |
| 122 | 122 | - [x] 5. The system integer. |
| 123 | 123 | - [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). | |
| 125 | 125 | - [ ] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only |
| 126 | 126 | data, used as a variable that can't be changed (section 4). |
| 127 | 127 | Done so that a string constant is the same mechanism. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +module JoinInt; | |
| 2 | + | |
| 3 | +const Bad = "count: " + 1; | |
| 4 | + | |
| 5 | +end JoinInt. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +the cat | |
| 2 | +<the cat> | |
| 3 | +9 | |
| 4 | +cat sat on | |
| 5 | +10 |
| @@ -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. |
| @@ -275,7 +275,9 @@ | ||
| 275 | 275 | -1 fault as an `I64`'s does. |
| 276 | 276 | `=` and `#` on arrays, slices and strings make a slice of each side |
| 277 | 277 | (`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. | |
| 279 | 281 | |
| 280 | 282 | Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a |
| 281 | 283 | variable into an item for part of it, mostly by adding to `off`. An |
| @@ -865,6 +865,8 @@ | ||
| 865 | 865 | if op = sAnd then Jcc(ccE, L) else Jcc(ccNE, L); |
| 866 | 866 | end else if (op in [sEql, sNeq]) and IsSeq(x.typ) then |
| 867 | 867 | SeqSlice(x) |
| 868 | + else if (op = sPlus) and (x.mode = mStr) then | |
| 869 | + { two string constants to be joined: nothing to compute } | |
| 868 | 870 | else if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then |
| 869 | 871 | NotValue(x) |
| 870 | 872 | else if x.mode <> mConst then begin |
| @@ -1010,6 +1012,22 @@ | ||
| 1010 | 1012 | MarkAt(x.pos, SymText(op) + ' needs BOOLs, found ' + What(x)); |
| 1011 | 1013 | end; |
| 1012 | 1014 | |
| 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 | + | |
| 1013 | 1031 | procedure Op2(op: TSym; var x, y: TItem; L: longint); |
| 1014 | 1032 | const |
| 1015 | 1033 | Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq]; |
| @@ -1020,6 +1038,7 @@ | ||
| 1020 | 1038 | begin |
| 1021 | 1039 | xt := x.typ; yt := y.typ; |
| 1022 | 1040 | 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; | |
| 1023 | 1042 | |
| 1024 | 1043 | if op in [sAnd, sOr] then begin |
| 1025 | 1044 | if xt^.form <> fBool then WrongLogic(op, x); |
| @@ -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 | |
| 2 | 2 | 6 | x := "a" + 1; |
| 3 | - | ^^^ | |
| 3 | + | ^ | |
| 4 | 4 | raz: 1 error |
| @@ -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 |