From c6f0123ef903197d1b876fb76c540faffd9520e2 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 09:15:25 +0100 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index 8f545a1..7a7f5f3 100644 --- a/PLAN.md +++ b/PLAN.md @@ -121,7 +121,7 @@ passes, apart from this compiler's own messages. - [x] 5. The system integer. - [x] 6. Optional reals: a target says whether it has them. -- [ ] 6a. `+` joins string constants (report, section 4). +- [x] 6a. `+` joins string constants (report, section 4). - [ ] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only data, used as a variable that can't be changed (section 4). Done so that a string constant is the same mechanism. diff --git a/conform/err/JoinInt.raz b/conform/err/JoinInt.raz new file mode 100644 index 0000000..7ce4b0f --- /dev/null +++ b/conform/err/JoinInt.raz @@ -0,0 +1,5 @@ +module JoinInt; + +const Bad = "count: " + 1; + +end JoinInt. diff --git a/conform/run/JoinStr.expect b/conform/run/JoinStr.expect new file mode 100644 index 0000000..13b3c9e --- /dev/null +++ b/conform/run/JoinStr.expect @@ -0,0 +1,5 @@ +the cat + +9 +cat sat on +10 diff --git a/conform/run/JoinStr.raz b/conform/run/JoinStr.raz new file mode 100644 index 0000000..ad3daf4 --- /dev/null +++ b/conform/run/JoinStr.raz @@ -0,0 +1,21 @@ +module JoinStr; +import Out; + +const + Text = "the cat"; + Line = Text + "\n"; + Both = "<" + Text + ">" + ""; + Word = ("sat" + " ") + "on"; + +proc main*; +var w: []U8; +begin + Out.str(Line); + Out.str(Both); Out.ln; + Out.int(LEN(Both)); Out.ln; + w := Text[4..7]; + Out.str(w); Out.str(" "); Out.str(Word + "\n"); + Out.int(I64(Line[7])); Out.ln; +end; + +end JoinStr. diff --git a/doc/compiler.md b/doc/compiler.md index dd12b4a..2f92134 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -275,7 +275,9 @@ -1 fault as an `I64`'s does. `=` and `#` on arrays, slices and strings make a slice of each side (`SeqSlice`), compare the lengths, and then the bytes with `repe -cmpsb`, right where the comparison stands: no call. +cmpsb`, right where the comparison stands: no call. `+` on two string +constants is `JoinStr`, which makes one string of them, in a chunk of +its own. Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a variable into an item for part of it, mostly by adding to `off`. An diff --git a/src/gen.pas b/src/gen.pas index b047817..a739c2d 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -865,6 +865,8 @@ if op = sAnd then Jcc(ccE, L) else Jcc(ccNE, L); end else if (op in [sEql, sNeq]) and IsSeq(x.typ) then SeqSlice(x) + else if (op = sPlus) and (x.mode = mStr) then + { two string constants to be joined: nothing to compute } else if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then NotValue(x) else if x.mode <> mConst then begin @@ -1010,6 +1012,22 @@ MarkAt(x.pos, SymText(op) + ' needs BOOLs, found ' + What(x)); end; +{ x + y, of two string constants: one string, in a chunk of its own + (report section 4) } +procedure JoinStr(var x, y: TItem); +var p: PBytes; c: longint; +begin + if y.mode <> mStr then MarkAt(y.pos, '"+" joins two strings, found ' + What(y)); + p := GetMem(x.val + y.val + 1); + Move(rodata^[chunks^[x.chunk].start], p^[0], x.val); + Move(rodata^[chunks^[y.chunk].start], p^[x.val], y.val + 1); + c := NewChunk(secRodata, -1, -1); + SetData(c, p, x.val + y.val + 1, 1); + FreeMem(p); + x.chunk := c; + x.val := x.val + y.val; +end; + procedure Op2(op: TSym; var x, y: TItem; L: longint); const Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq]; @@ -1020,6 +1038,7 @@ begin xt := x.typ; yt := y.typ; if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y); + if (op = sPlus) and (x.mode = mStr) then begin JoinStr(x, y); exit end; if op in [sAnd, sOr] then begin if xt^.form <> fBool then WrongLogic(op, x); diff --git a/test/msg/GenStringOperand.err b/test/msg/GenStringOperand.err index 7695fb4..a4085b9 100644 --- a/test/msg/GenStringOperand.err +++ b/test/msg/GenStringOperand.err @@ -1,4 +1,4 @@ -conform/err/GenStringOperand.raz:6:10: error: a string can't be an operand +conform/err/GenStringOperand.raz:6:16: error: "+" joins two strings, found 1 6 | x := "a" + 1; - | ^^^ + | ^ raz: 1 error diff --git a/test/msg/JoinInt.err b/test/msg/JoinInt.err new file mode 100644 index 0000000..e12706b --- /dev/null +++ b/test/msg/JoinInt.err @@ -0,0 +1,4 @@ +conform/err/JoinInt.raz:3:25: error: "+" joins two strings, found 1 + 3 | const Bad = "count: " + 1; + | ^ +raz: 1 error