Operators: and, or, not for BOOLs below the relations; &, |, ~ for bits Report 2026-10.3, sections 2 and 6. The scanner reads & and ~; | is or after an operand. Relation, Conjunct (and) and Expression (or) are the new levels. and, or and not on integers, and &, |, xor and ~ on BOOLs, are errors whose help names the other operator. SkipConstruct stops at a | only after a ;, where an arm starts. Sources moved: a build of the old compiler listed each and, or and not it applied to integers, and they became &, | and ~ in place, with their old binding; it found no and or or between relations that would change meaning, and no xor on BOOLs. Pretty spaces & and | as it does +, and leaves an arm's | alone; the vim and emacs indenters continue a line that ends in & or |. test/err: AmpBools, BarBools, NotInt, OrInts, TildeBool, XorBools.
| @@ -438,7 +438,7 @@ | ||
| 438 | 438 | name. Tests: equal, of different lengths, empty, a string |
| 439 | 439 | literal against an array, and in `test/err` arrays of `F64`, of |
| 440 | 440 | records, and of two element types. |
| 441 | -- [ ] 45. Operators: `&` and `~` in the scanner, `|` an operator where | |
| 441 | +- [x] 45. Operators: `&` and `~` in the scanner, `|` an operator where | |
| 442 | 442 | an operand came before it; the parser's levels, `or` over `and` |
| 443 | 443 | over the relations; `and`, `or`, `not` refused on integers and |
| 444 | 444 | `&`, `|`, `xor`, `~` on `BOOL`s, each with a message that names |
| @@ -9,11 +9,11 @@ | ||
| 9 | 9 | | File | Lines | Code | What | |
| 10 | 10 | |---|---|---|---| |
| 11 | 11 | | `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts | |
| 12 | -| `src/scan.pas` | 705 | 567 | Symbols, literals, comments, positions | | |
| 12 | +| `src/scan.pas` | 706 | 568 | Symbols, literals, comments, positions | | |
| 13 | 13 | | `src/sym.pas` | 593 | 493 | Types, objects, scopes, layout | |
| 14 | 14 | | `src/x64.pas` | 693 | 556 | Code buffer, chunks, fixups, places, instruction encoder | |
| 15 | -| `src/gen.pas` | 1,570 | 1,358 | Items: loading, storing, copying, frames, calls | | |
| 16 | -| `src/parse.pas` | 2,290 | 2,069 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 15 | +| `src/gen.pas` | 1,596 | 1,378 | Items: loading, storing, copying, frames, calls | | |
| 16 | +| `src/parse.pas` | 2,323 | 2,098 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 17 | 17 | | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places | |
| 18 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| 19 | 19 | | `src/raz.pas` | 227 | 178 | The command line, the entry points | |
| @@ -310,7 +310,7 @@ | ||
| 310 | 310 | the report, calling `gen.pas` as it goes. |
| 311 | 311 | |
| 312 | 312 | Start with expressions, bottom up: `Factor`, `Term`, `SimpleExpr`, |
| 313 | -`Expression`. Then `Designator`, which reads a name and what follows | |
| 313 | +`Relation`, `Conjunct` (the `and`s), `Expression` (the `or`s). Then `Designator`, which reads a name and what follows | |
| 314 | 314 | it: `.field`, `^`, `[i]`, `[lo..hi]`, a call, a conversion. Each step |
| 315 | 315 | calls the matching routine in `gen.pas` on the item it has. The |
| 316 | 316 | `want` parameter carries the type an expression must have, where it is |
| @@ -44,7 +44,7 @@ | ||
| 44 | 44 | (modify-syntax-entry ?\' "\"" st) |
| 45 | 45 | (modify-syntax-entry ?\\ "\\" st) |
| 46 | 46 | (modify-syntax-entry ?_ "_" st) |
| 47 | - (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?|)) | |
| 47 | + (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?| ?& ?~)) | |
| 48 | 48 | (modify-syntax-entry c "." st)) |
| 49 | 49 | st) |
| 50 | 50 | "Syntax table for `raz-mode'.") |
| @@ -115,7 +115,7 @@ | ||
| 115 | 115 | "Whether the expression on line N goes on to the next line." |
| 116 | 116 | (let ((code (raz--code n))) |
| 117 | 117 | (or (> (raz--count "[([{]" code) (raz--count "[])}]" code)) |
| 118 | - (string-match-p "\\(?:,\\|:=\\|[-+*/=#<>]\\|\\_<and\\|\\_<or\\)\\'" code)))) | |
| 118 | + (string-match-p "\\(?:,\\|:=\\|[-+*/&|=#<>]\\|\\_<and\\|\\_<or\\)\\'" code)))) | |
| 119 | 119 | |
| 120 | 120 | (defun raz--start (n) |
| 121 | 121 | "The first line of the statement that ends on line N." |
| @@ -46,7 +46,7 @@ | ||
| 46 | 46 | if len(substitute(code, '[^([{]', '', 'g')) > len(substitute(code, '[^)\]}]', '', 'g')) |
| 47 | 47 | return 1 |
| 48 | 48 | endif |
| 49 | - return code =~# '\%(,\|:=\|[-+*/=#<>]\|\<and\|\<or\)$' | |
| 49 | + return code =~# '\%(,\|:=\|[-+*/&|=#<>]\|\<and\|\<or\)$' | |
| 50 | 50 | endfunction |
| 51 | 51 | |
| 52 | 52 | let s:leading = '^\s*\%(or\|and\)\>' |
| @@ -44,7 +44,7 @@ | ||
| 44 | 44 | var c, n: I64; b: ^Block; p: ^U8; |
| 45 | 45 | begin |
| 46 | 46 | if (size < 0) or (size > Largest) or (align < 1) or (align > Mem.Page) |
| 47 | - or (align and (align - 1) # 0) then | |
| 47 | + or (align & (align - 1) # 0) then | |
| 48 | 48 | return nil; |
| 49 | 49 | end; |
| 50 | 50 | n := size + Header; |
| @@ -26,9 +26,9 @@ | ||
| 26 | 26 | proc slot(m: ^Map; k: K): I64; |
| 27 | 27 | var i: I64; |
| 28 | 28 | begin |
| 29 | - i := I64(hash(k) and U64(m^.cap - 1)); | |
| 29 | + i := I64(hash(k) & U64(m^.cap - 1)); | |
| 30 | 30 | while m^.slots[i].used and not eq(m^.slots[i].key, k) do |
| 31 | - i := (i + 1) and (m^.cap - 1); | |
| 31 | + i := (i + 1) & (m^.cap - 1); | |
| 32 | 32 | end; |
| 33 | 33 | return i; |
| 34 | 34 | end; |
| @@ -86,7 +86,7 @@ | ||
| 86 | 86 | i := 16; |
| 87 | 87 | repeat |
| 88 | 88 | i := i - 1; |
| 89 | - d := U8(n and 15); | |
| 89 | + d := U8(n & 15); | |
| 90 | 90 | if d < 10 then buf[i] := d + '0'; else buf[i] := d - 10 + 'A'; end; |
| 91 | 91 | n := n shr 4; |
| 92 | 92 | digits := digits - 1; |
| @@ -143,7 +143,7 @@ | ||
| 143 | 143 | r := SYSCALL(4, ADR(a), ADR(st)); (* stat *) |
| 144 | 144 | if r # 0 then return i, r; end; |
| 145 | 145 | i.size := st[6]; (* st_size *) |
| 146 | - i.dir := st[3] and 0F000H = 4000H; (* st_mode is S_IFDIR *) | |
| 146 | + i.dir := st[3] & 0F000H = 4000H; (* st_mode is S_IFDIR *) | |
| 147 | 147 | return i, 0; |
| 148 | 148 | end; |
| 149 | 149 | |
| @@ -196,10 +196,10 @@ | ||
| 196 | 196 | begin |
| 197 | 197 | if SYSCALL(16, fd, 5401H, ADR(saved)) # 0 then return false; end; (* TCGETS *) |
| 198 | 198 | t := saved; |
| 199 | - t.iflag := t.iflag and not U32(5EBH); (* IGNBRK BRKINT PARMRK ISTRIP INLCR IGNCR ICRNL IXON *) | |
| 200 | - t.oflag := t.oflag and not U32(1); (* OPOST *) | |
| 201 | - t.cflag := t.cflag and not U32(130H) or 30H; (* CS8, no PARENB *) | |
| 202 | - t.lflag := t.lflag and not U32(804BH); (* ISIG ICANON ECHO ECHONL IEXTEN *) | |
| 199 | + t.iflag := t.iflag & ~U32(5EBH); (* IGNBRK BRKINT PARMRK ISTRIP INLCR IGNCR ICRNL IXON *) | |
| 200 | + t.oflag := t.oflag & ~U32(1); (* OPOST *) | |
| 201 | + t.cflag := t.cflag & ~U32(130H) | 30H; (* CS8, no PARENB *) | |
| 202 | + t.lflag := t.lflag & ~U32(804BH); (* ISIG ICANON ECHO ECHONL IEXTEN *) | |
| 203 | 203 | t.cc[5] := 0; (* VTIME: no time limit *) |
| 204 | 204 | t.cc[6] := 1; (* VMIN: a byte at least *) |
| 205 | 205 | return SYSCALL(16, fd, 5402H, ADR(t)) = 0; (* TCSETS *) |
| @@ -88,6 +88,7 @@ | ||
| 88 | 88 | |
| 89 | 89 | procedure Neg(var x: TItem); |
| 90 | 90 | procedure NotOp(var x: TItem); |
| 91 | +procedure Complement(var x: TItem); | |
| 91 | 92 | procedure Op1(op: TSym; var x: TItem; var L: longint); |
| 92 | 93 | procedure Op2(op: TSym; var x, y: TItem; L: longint); |
| 93 | 94 | procedure Convert(var x: TItem; t: PType); |
| @@ -714,13 +715,18 @@ | ||
| 714 | 715 | |
| 715 | 716 | procedure NotOp(var x: TItem); |
| 716 | 717 | begin |
| 717 | - if x.typ^.form = fBool then begin | |
| 718 | - if x.mode = mConst then x.val := 1 - x.val | |
| 719 | - else begin Value(x); AluI(aluXor, RAX, 1) end; | |
| 720 | - end else if IsInt(x.typ) then begin | |
| 721 | - if x.mode = mConst then x.val := Cut(not x.val, x.typ) | |
| 722 | - else begin Value(x); Unary(2, RAX); Norm(x.typ) end; | |
| 723 | - end else MarkAt(x.pos, '"not" needs a BOOL or an integer, found ' + What(x)); | |
| 718 | + if IsInt(x.typ) then MarkHelp(x.pos, '"not" needs a BOOL, found ' + What(x), 'the bits of an integer flip with ~'); | |
| 719 | + if x.typ^.form <> fBool then MarkAt(x.pos, '"not" needs a BOOL, found ' + What(x)); | |
| 720 | + if x.mode = mConst then x.val := 1 - x.val | |
| 721 | + else begin Value(x); AluI(aluXor, RAX, 1) end; | |
| 722 | +end; | |
| 723 | + | |
| 724 | +procedure Complement(var x: TItem); | |
| 725 | +begin | |
| 726 | + if x.typ^.form = fBool then MarkHelp(x.pos, '"~" needs an integer, found ' + What(x), 'a BOOL is negated with not'); | |
| 727 | + if not IsInt(x.typ) then MarkAt(x.pos, '"~" needs an integer, found ' + What(x)); | |
| 728 | + if x.mode = mConst then x.val := Cut(not x.val, x.typ) | |
| 729 | + else begin Value(x); Unary(2, RAX); Norm(x.typ) end; | |
| 724 | 730 | end; |
| 725 | 731 | |
| 726 | 732 | { Arrays, slices and strings, which = and # compare element by element } |
| @@ -814,8 +820,8 @@ | ||
| 814 | 820 | else if op = sDiv then FoldInt := a div b |
| 815 | 821 | else FoldInt := a mod b; |
| 816 | 822 | end; |
| 817 | - sAnd: FoldInt := a and b; | |
| 818 | - sOr: FoldInt := a or b; | |
| 823 | + sAmp: FoldInt := a and b; | |
| 824 | + sBar: FoldInt := a or b; | |
| 819 | 825 | sXor: FoldInt := a xor b; |
| 820 | 826 | sShl: FoldInt := a shl (b and 63); |
| 821 | 827 | sShr: if u then FoldInt := int64(qword(a) shr (b and 63)) |
| @@ -853,6 +859,22 @@ | ||
| 853 | 859 | if r.typ^.form = fReal then RealTo(r, t); |
| 854 | 860 | end; |
| 855 | 861 | |
| 862 | +{ and, or and not are for BOOLs; &, |, xor and ~ for the bits of | |
| 863 | + integers: x is the wrong kind for op, and the message names the | |
| 864 | + operator it may have meant } | |
| 865 | +procedure WrongLogic(op: TSym; var x: TItem); | |
| 866 | +const Bits = 'the bits of integers take '; | |
| 867 | +begin | |
| 868 | + case op of | |
| 869 | + sAnd: if IsInt(x.typ) then MarkHelp(x.pos, '"and" needs BOOLs, found ' + What(x), Bits + '&'); | |
| 870 | + sOr: if IsInt(x.typ) then MarkHelp(x.pos, '"or" needs BOOLs, found ' + What(x), Bits + '|'); | |
| 871 | + sAmp: MarkHelp(x.pos, '"&" needs integers, found ' + What(x), 'BOOLs take and'); | |
| 872 | + sBar: MarkHelp(x.pos, '"|" needs integers, found ' + What(x), 'BOOLs take or'); | |
| 873 | + sXor: MarkHelp(x.pos, '"xor" needs integers, found ' + What(x), 'BOOLs that differ are #'); | |
| 874 | + end; | |
| 875 | + MarkAt(x.pos, SymText(op) + ' needs BOOLs, found ' + What(x)); | |
| 876 | +end; | |
| 877 | + | |
| 856 | 878 | procedure Op2(op: TSym; var x, y: TItem; L: longint); |
| 857 | 879 | const |
| 858 | 880 | Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq]; |
| @@ -864,8 +886,9 @@ | ||
| 864 | 886 | xt := x.typ; yt := y.typ; |
| 865 | 887 | if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y); |
| 866 | 888 | |
| 867 | - if (op in [sAnd, sOr]) and (xt^.form = fBool) then begin | |
| 868 | - if yt^.form <> fBool then MarkAt(y.pos, SymText(op) + ' needs BOOLs, found ' + What(y)); | |
| 889 | + if op in [sAnd, sOr] then begin | |
| 890 | + if xt^.form <> fBool then WrongLogic(op, x); | |
| 891 | + if yt^.form <> fBool then WrongLogic(op, y); | |
| 869 | 892 | if (x.mode = mConst) and (y.mode = mConst) then begin |
| 870 | 893 | FixLink(L); |
| 871 | 894 | if op = sAnd then x.val := x.val and y.val else x.val := x.val or y.val; |
| @@ -876,6 +899,9 @@ | ||
| 876 | 899 | x.mode := mReg; x.typ := tBool; |
| 877 | 900 | exit; |
| 878 | 901 | end; |
| 902 | + | |
| 903 | + if (op in [sAmp, sBar, sXor]) and (xt^.form = fBool) then WrongLogic(op, x); | |
| 904 | + if (op in [sAmp, sBar, sXor]) and (yt^.form = fBool) then WrongLogic(op, y); | |
| 879 | 905 | |
| 880 | 906 | { the lengths, then the bytes, LEN * SIZE(T) of them: the elements |
| 881 | 907 | are of types whose bytes say all there is to their values } |
| @@ -932,7 +958,7 @@ | ||
| 932 | 958 | if (x.mode = mConst) and (x.typ <> t) then RealTo(x, t); |
| 933 | 959 | if (y.mode = mConst) and (y.typ <> t) then RealTo(y, t); |
| 934 | 960 | end else if (xt^.form = fBool) and (yt^.form = fBool) then begin |
| 935 | - if not (op in [sXor, sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 961 | + if not (op in [sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 936 | 962 | t := tBool; |
| 937 | 963 | end else if (xt^.form = fPtr) and IsInt(yt) and (op in [sPlus, sMinus]) then begin |
| 938 | 964 | t := xt; scale := xt^.base^.size; isPtr := true; |
| @@ -1059,8 +1085,8 @@ | ||
| 1059 | 1085 | else begin Cqo; Unary(7, RCX) end; |
| 1060 | 1086 | if op = sMod then MovRR(RAX, RDX); |
| 1061 | 1087 | end; |
| 1062 | - sAnd: Alu(aluAnd, RAX, RCX); | |
| 1063 | - sOr: Alu(aluOr, RAX, RCX); | |
| 1088 | + sAmp: Alu(aluAnd, RAX, RCX); | |
| 1089 | + sBar: Alu(aluOr, RAX, RCX); | |
| 1064 | 1090 | sXor: Alu(aluXor, RAX, RCX); |
| 1065 | 1091 | sShl: Shift(shShl, RAX); |
| 1066 | 1092 | sShr: if unsign then Shift(shShr, RAX) else Shift(shSar, RAX); |
| @@ -207,22 +207,25 @@ | ||
| 207 | 207 | |
| 208 | 208 | { Reads to the end of a statement or declaration, from its first |
| 209 | 209 | symbol: up to the ";" that ends it, not read, or to the "end", |
| 210 | - "else", "elsif", "until" or "|" of the statement around it. False if | |
| 211 | - it stopped at a symbol that starts a line, or the end of the file. } | |
| 210 | + "else", "elsif", "until" or "|" of the statement around it (an arm's | |
| 211 | + "|" comes after a ";", the operator's after an operand). False if it | |
| 212 | + stopped at a symbol that starts a line, or the end of the file. } | |
| 212 | 213 | function SkipConstruct: boolean; |
| 213 | -var d, p: longint; | |
| 214 | +var d, p: longint; prev: TSym; | |
| 214 | 215 | begin |
| 215 | - d := 0; p := 0; | |
| 216 | + d := 0; p := 0; prev := sSemicolon; | |
| 216 | 217 | repeat |
| 217 | 218 | case tok of |
| 218 | 219 | sIf, sWhile, sFor, sMatch, sRepeat, sRecord, sVariant: inc(d); |
| 219 | 220 | sEnd, sUntil: if d = 0 then exit(true) else dec(d); |
| 220 | - sElse, sElsif, sBar: if (d = 0) and (p = 0) then exit(true); | |
| 221 | + sElse, sElsif: if (d = 0) and (p = 0) then exit(true); | |
| 222 | + sBar: if (d = 0) and (p = 0) and (prev = sSemicolon) then exit(true); | |
| 221 | 223 | sLparen, sLbrak, sLbrace: inc(p); |
| 222 | 224 | sRparen, sRbrak, sRbrace: if p > 0 then dec(p); |
| 223 | 225 | sSemicolon: if (d = 0) and (p = 0) then exit(true); |
| 224 | 226 | sEof: exit(false); |
| 225 | 227 | end; |
| 228 | + prev := tok; | |
| 226 | 229 | Next; |
| 227 | 230 | until AtLineStart; |
| 228 | 231 | SkipConstruct := false; |
| @@ -1040,6 +1043,7 @@ | ||
| 1040 | 1043 | end; |
| 1041 | 1044 | sLparen: begin Next; Expression(x, want); Expect(sRparen) end; |
| 1042 | 1045 | sNot: begin Next; Factor(x, nil); NotOp(x) end; |
| 1046 | + sTilde: begin Next; Factor(x, nil); Complement(x) end; | |
| 1043 | 1047 | sLbrak: ArrayLiteral(x, want); |
| 1044 | 1048 | sLbrace: if (want <> nil) and (want^.form = fRecord) then RecordLiteral(x, want) |
| 1045 | 1049 | else SetLiteral(x, want); |
| @@ -1063,7 +1067,7 @@ | ||
| 1063 | 1067 | begin |
| 1064 | 1068 | start := TokPos; |
| 1065 | 1069 | Factor(x, want); |
| 1066 | - while tok in [sTimes, sSlash, sDiv, sMod, sAnd, sShl, sShr] do begin | |
| 1070 | + while tok in [sTimes, sSlash, sDiv, sMod, sAmp, sShl, sShr] do begin | |
| 1067 | 1071 | op := tok; |
| 1068 | 1072 | Next; |
| 1069 | 1073 | Op1(op, x, L); |
| @@ -1083,7 +1087,7 @@ | ||
| 1083 | 1087 | x.pos := SpanFrom(start); |
| 1084 | 1088 | Neg(x); |
| 1085 | 1089 | end else Term(x, want); |
| 1086 | - while tok in [sPlus, sMinus, sOr, sXor] do begin | |
| 1090 | + while tok in [sPlus, sMinus, sBar, sXor] do begin | |
| 1087 | 1091 | op := tok; |
| 1088 | 1092 | Next; |
| 1089 | 1093 | Op1(op, x, L); |
| @@ -1093,7 +1097,7 @@ | ||
| 1093 | 1097 | end; |
| 1094 | 1098 | end; |
| 1095 | 1099 | |
| 1096 | -procedure Expression(var x: TItem; want: PType); | |
| 1100 | +procedure Relation(var x: TItem; want: PType); | |
| 1097 | 1101 | var op: TSym; y: TItem; L: longint; |
| 1098 | 1102 | begin |
| 1099 | 1103 | SimpleExpr(x, want); |
| @@ -1104,6 +1108,35 @@ | ||
| 1104 | 1108 | if op = sIn then SimpleExpr(y, nil) else SimpleExpr(y, x.typ); |
| 1105 | 1109 | Op2(op, x, y, L); |
| 1106 | 1110 | x.pos := Join(x.pos, y.pos); |
| 1111 | + end; | |
| 1112 | +end; | |
| 1113 | + | |
| 1114 | +{ and binds tighter than or, and both below the relations } | |
| 1115 | +procedure Conjunct(var x: TItem; want: PType); | |
| 1116 | +var y: TItem; L: longint; start: TPos; | |
| 1117 | +begin | |
| 1118 | + start := TokPos; | |
| 1119 | + Relation(x, want); | |
| 1120 | + while tok = sAnd do begin | |
| 1121 | + Next; | |
| 1122 | + Op1(sAnd, x, L); | |
| 1123 | + Relation(y, nil); | |
| 1124 | + Op2(sAnd, x, y, L); | |
| 1125 | + x.pos := SpanFrom(start); | |
| 1126 | + end; | |
| 1127 | +end; | |
| 1128 | + | |
| 1129 | +procedure Expression(var x: TItem; want: PType); | |
| 1130 | +var y: TItem; L: longint; start: TPos; | |
| 1131 | +begin | |
| 1132 | + start := TokPos; | |
| 1133 | + Conjunct(x, want); | |
| 1134 | + while tok = sOr do begin | |
| 1135 | + Next; | |
| 1136 | + Op1(sOr, x, L); | |
| 1137 | + Conjunct(y, nil); | |
| 1138 | + Op2(sOr, x, y, L); | |
| 1139 | + x.pos := SpanFrom(start); | |
| 1107 | 1140 | end; |
| 1108 | 1141 | end; |
| 1109 | 1142 |
| @@ -23,7 +23,7 @@ | ||
| 23 | 23 | TSym = (sNull, sIdent, sInt, sReal, sChar, sString, |
| 24 | 24 | sPlus, sMinus, sTimes, sSlash, sEql, sNeq, sLss, sLeq, sGtr, sGeq, |
| 25 | 25 | sBecomes, sColon, sSemicolon, sComma, sPeriod, sUpto, sEllipsis, |
| 26 | - sLparen, sRparen, sLbrak, sRbrak, sLbrace, sRbrace, sArrow, sBar, | |
| 26 | + sLparen, sRparen, sLbrak, sRbrak, sLbrace, sRbrace, sArrow, sAmp, sTilde, sBar, | |
| 27 | 27 | { the reserved words, in the order of Words below } |
| 28 | 28 | sAnd, sBegin, sBreak, sBy, sCode, sConst, sDefer, sDiv, sDo, sElse, |
| 29 | 29 | sElsif, sEnd, sExtern, sFor, sForward, sIf, sImport, sIn, sLink, |
| @@ -107,7 +107,7 @@ | ||
| 107 | 107 | Ops: array[sPlus..sBar] of string[3] = ( |
| 108 | 108 | '+', '-', '*', '/', '=', '#', '<', '<=', '>', '>=', |
| 109 | 109 | ':=', ':', ';', ',', '.', '..', '...', |
| 110 | - '(', ')', '[', ']', '{', '}', '^', '|'); | |
| 110 | + '(', ')', '[', ']', '{', '}', '^', '&', '~', '|'); | |
| 111 | 111 | |
| 112 | 112 | var |
| 113 | 113 | cur: TScanState; { buf .. path; tok, ival, rval and id live above } |
| @@ -638,7 +638,7 @@ | ||
| 638 | 638 | else begin inc(cur.pos); tok := sEql end; |
| 639 | 639 | '&': |
| 640 | 640 | if cur.buf[cur.pos + 1] = '&' then Slip(2, sAnd, '"and" is written and, not &&') |
| 641 | - else MarkBytes(cur.pos, cur.pos + 1, 'unexpected character "&"'); | |
| 641 | + else begin inc(cur.pos); tok := sAmp end; | |
| 642 | 642 | '#': begin inc(cur.pos); tok := sNeq end; |
| 643 | 643 | '<': begin |
| 644 | 644 | inc(cur.pos); |
| @@ -675,6 +675,7 @@ | ||
| 675 | 675 | '|': |
| 676 | 676 | if cur.buf[cur.pos + 1] = '|' then Slip(2, sOr, '"or" is written or, not ||') |
| 677 | 677 | else begin inc(cur.pos); tok := sBar end; |
| 678 | + '~': begin inc(cur.pos); tok := sTilde end; | |
| 678 | 679 | '!': |
| 679 | 680 | if cur.buf[cur.pos + 1] = '=' then Slip(2, sNeq, '"not equal" is written #, not !=') |
| 680 | 681 | else Slip(1, sNot, '"not" is written not, not !'); |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/AmpBools.raz:5:10: error: "&" needs integers, found BOOL | |
| 2 | + 5 | b := b & true; | |
| 3 | + | ^ | |
| 4 | +help: BOOLs take and | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module AmpBools; | |
| 2 | +proc main*; | |
| 3 | +var b: BOOL; x: I64; | |
| 4 | +begin | |
| 5 | + b := b & true; | |
| 6 | +end; | |
| 7 | +end AmpBools. |
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | test/err/AtAnd.raz:5:16: error: "and" needs BOOLs, found I64 |
| 2 | 2 | 5 | b := b and (x + 1); |
| 3 | 3 | | ^^^^^^^ |
| 4 | +help: the bits of integers take & | |
| 4 | 5 | raz: 1 error |
| @@ -1,4 +1,4 @@ | ||
| 1 | -test/err/AtNot.raz:5:14: error: "not" needs a BOOL or an integer, found []U8 | |
| 1 | +test/err/AtNot.raz:5:14: error: "not" needs a BOOL, found []U8 | |
| 2 | 2 | 5 | b := not s; |
| 3 | 3 | | ^ |
| 4 | 4 | raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/BarBools.raz:5:10: error: "|" needs integers, found BOOL | |
| 2 | + 5 | b := b | true; | |
| 3 | + | ^ | |
| 4 | +help: BOOLs take or | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module BarBools; | |
| 2 | +proc main*; | |
| 3 | +var b: BOOL; x: I64; | |
| 4 | +begin | |
| 5 | + b := b | true; | |
| 6 | +end; | |
| 7 | +end BarBools. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/NotInt.raz:5:14: error: "not" needs a BOOL, found I64 | |
| 2 | + 5 | x := not x; | |
| 3 | + | ^ | |
| 4 | +help: the bits of an integer flip with ~ | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module NotInt; | |
| 2 | +proc main*; | |
| 3 | +var b: BOOL; x: I64; | |
| 4 | +begin | |
| 5 | + x := not x; | |
| 6 | +end; | |
| 7 | +end NotInt. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/OrInts.raz:5:10: error: "or" needs BOOLs, found I64 | |
| 2 | + 5 | x := x or 1; | |
| 3 | + | ^ | |
| 4 | +help: the bits of integers take | | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module OrInts; | |
| 2 | +proc main*; | |
| 3 | +var b: BOOL; x: I64; | |
| 4 | +begin | |
| 5 | + x := x or 1; | |
| 6 | +end; | |
| 7 | +end OrInts. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/TildeBool.raz:5:11: error: "~" needs an integer, found BOOL | |
| 2 | + 5 | b := ~b; | |
| 3 | + | ^ | |
| 4 | +help: a BOOL is negated with not | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module TildeBool; | |
| 2 | +proc main*; | |
| 3 | +var b: BOOL; x: I64; | |
| 4 | +begin | |
| 5 | + b := ~b; | |
| 6 | +end; | |
| 7 | +end TildeBool. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/XorBools.raz:5:10: error: "xor" needs integers, found BOOL | |
| 2 | + 5 | b := b xor true; | |
| 3 | + | ^ | |
| 4 | +help: BOOLs that differ are # | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module XorBools; | |
| 2 | +proc main*; | |
| 3 | +var b: BOOL; x: I64; | |
| 4 | +begin | |
| 5 | + b := b xor true; | |
| 6 | +end; | |
| 7 | +end XorBools. |
| @@ -46,8 +46,8 @@ | ||
| 46 | 46 | | rect(w, h): t := t + w * h; |
| 47 | 47 | | dot: t := t - 1; |
| 48 | 48 | end; |
| 49 | - g[a and 15] := r.x + r.y; | |
| 50 | - return p{i - 1}(t and 1023, b, s[0..LEN(s) div 2]) + r.x; | |
| 49 | + g[a & 15] := r.x + r.y; | |
| 50 | + return p{i - 1}(t & 1023, b, s[0..LEN(s) div 2]) + r.x; | |
| 51 | 51 | end; |
| 52 | 52 | """) |
| 53 | 53 | print(f"""proc main*; |
| @@ -46,6 +46,8 @@ | ||
| 46 | 46 | until s < 0; |
| 47 | 47 | if (s # -1) or (max2(3, 4) # 4) |
| 48 | 48 | or (area({dot}) # 0) then Rt.stop("Messy"); end; |
| 49 | + s := s & ~3 | s xor 1 & max | |
| 50 | + | 4; | |
| 49 | 51 | p := {a: -s, b:2}; |
| 50 | 52 | Out.int(p.a); (* the first *) |
| 51 | 53 | Out.ln; |
| @@ -46,6 +46,8 @@ | ||
| 46 | 46 | until s<0; |
| 47 | 47 | if (s # -1) or (max2(3,4) # 4) |
| 48 | 48 | or (area({dot}) # 0) then Rt.stop("Messy"); end; |
| 49 | + s:=s&~3|s xor 1 & max | |
| 50 | + | 4; | |
| 49 | 51 | p := {a: -s, b:2}; |
| 50 | 52 | Out.int(p.a); (* the first *) |
| 51 | 53 | Out.ln; |
| @@ -47,10 +47,10 @@ | ||
| 47 | 47 | show("shl", 1 shl 10); |
| 48 | 48 | show("shr", -16 shr 2); |
| 49 | 49 | show("ushr", I64(U64(-16) shr 60)); |
| 50 | - show("and", 12 and 10); | |
| 51 | - show("or", 12 or 3); | |
| 50 | + show("and", 12 & 10); | |
| 51 | + show("or", 12 | 3); | |
| 52 | 52 | show("xor", 12 xor 10); |
| 53 | - show("not", not 0); | |
| 53 | + show("not", ~0); | |
| 54 | 54 | r := F64(a) / 2.0; |
| 55 | 55 | show("real", I64(r * 10.0)); |
| 56 | 56 | r := -2.75; |
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | first line moved; so does the text of a comment. |
| 18 | 18 | |
| 19 | 19 | Within a line, a binary operator has one space on each side, a unary |
| 20 | - minus none after it, and , and ; none before them and one after. A | |
| 20 | + minus or ~ none after it, and , and ; none before them and one after. A | |
| 21 | 21 | comment after code keeps its column. Other spaces stay as written, |
| 22 | 22 | so that lined-up match arms stay lined up. No line ends in spaces. *) |
| 23 | 23 | module Pretty; |
| @@ -28,7 +28,7 @@ | ||
| 28 | 28 | openers = "if while for match record variant begin repeat"; |
| 29 | 29 | declarations = "module import link extern proc const type var begin code"; |
| 30 | 30 | binaryWords = "div mod and or xor shl shr in"; |
| 31 | - binarySymbols = "+ - * / = # < <= > >= :="; | |
| 31 | + binarySymbols = "+ - * / & | = # < <= > >= :="; | |
| 32 | 32 | |
| 33 | 33 | var |
| 34 | 34 | sc: Scan.Scanner; (* the source, at the symbol being written *) |
| @@ -118,7 +118,10 @@ | ||
| 118 | 118 | if (sc.kind # Scan.symbol) or not Scan.among(Scan.text(ADR(sc)), binarySymbols) then |
| 119 | 119 | return false; |
| 120 | 120 | end; |
| 121 | - if Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+") then return endsOperand(ADR(prev)); end; | |
| 121 | + (* a | after an operand is or; one after anything else starts an arm *) | |
| 122 | + if Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+") or Scan.is(ADR(sc), "|") then | |
| 123 | + return endsOperand(ADR(prev)); | |
| 124 | + end; | |
| 122 | 125 | if Scan.is(ADR(sc), "*") and (prev.kind = Scan.word) then |
| 123 | 126 | n := following(); |
| 124 | 127 | if Scan.among(Scan.text(ADR(n)), "; , : = )") then return false; end; |
| @@ -135,6 +138,7 @@ | ||
| 135 | 138 | if (brackets > 0) or prevBinary or Scan.is(ADR(prev), ",") then return true; end; |
| 136 | 139 | t := Scan.text(ADR(sc)); |
| 137 | 140 | if sc.kind = Scan.word then return Scan.among(t, binaryWords) or Scan.among(t, "then do of"); end; |
| 141 | + if Scan.is(ADR(sc), "|") then return endsOperand(ADR(prev)); end; | |
| 138 | 142 | return (sc.kind = Scan.symbol) and Scan.among(t, binarySymbols); |
| 139 | 143 | end; |
| 140 | 144 | |
| @@ -258,7 +262,8 @@ | ||
| 258 | 262 | elsif Scan.among(t, ") ] }") and (brackets > 0) then DEC(brackets); |
| 259 | 263 | end; |
| 260 | 264 | end; |
| 261 | - prevUnary := (Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+")) and not binaryNow; | |
| 265 | + prevUnary := (Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+")) and not binaryNow | |
| 266 | + or Scan.is(ADR(sc), "~"); | |
| 262 | 267 | prevBinary := binaryNow; |
| 263 | 268 | prev2 := prev; |
| 264 | 269 | prev := sc; |