t51_strcat.pas 1.3 KB

1234567891011121314151617181920212223242526272829
  1. { t51_strcat: string concatenation, `a + b', in the places its result can go.
  2. A concatenation needs storage for its result before anything can consume it,
  3. so each `+' in the source gets its own 256-byte scratch STRING and the result
  4. IS that variable. These are the shapes that consume one: as a `writeln'
  5. argument, assigned to a target, and then used again as an operand.
  6. The operands cover var+var, literal+var, var+literal, var+char and char+var -
  7. every pairing a string operand allows except two CHARs, which t53 rejects.
  8. The CHAR cases are the reason a one-character literal and a CHAR variable
  9. take the same path: a CHAR operand becomes a one-character string on the
  10. stack (a length byte and the byte), not a string copied from memory. }
  11. program t51_strcat (input, output);
  12. var
  13. a, b, c : string[12];
  14. ch : char;
  15. begin
  16. a := 'hello';
  17. b := 'world';
  18. ch := 'X';
  19. writeln (a + b); { var + var -> helloworld }
  20. writeln ('say ' + b); { literal + var -> say world }
  21. writeln (a + '!'); { var + literal -> hello! }
  22. writeln (a + ch); { var + char -> helloX }
  23. writeln (ch + a); { char + var -> Xhello }
  24. c := a + b; { the result assigned to a variable... }
  25. writeln (c);
  26. writeln (b + c) { ...and then used as an operand }
  27. end.