raz / commit bc22ce64

commit
bc22ce64b82ed79b4c7dc5b5c47c4e58e2737026
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
2d0814f3
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.

patch · log

30 files changed, +194 -52

PLAN.md (history) modified +1 -1

@@ -438,7 +438,7 @@
438438 name. Tests: equal, of different lengths, empty, a string
439439 literal against an array, and in `test/err` arrays of `F64`, of
440440 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
442442 an operand came before it; the parser's levels, `or` over `and`
443443 over the relations; `and`, `or`, `not` refused on integers and
444444 `&`, `|`, `xor`, `~` on `BOOL`s, each with a message that names

doc/compiler.md (history) modified +4 -4

@@ -9,11 +9,11 @@
99 | File | Lines | Code | What |
1010 |---|---|---|---|
1111 | `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 |
1313 | `src/sym.pas` | 593 | 493 | Types, objects, scopes, layout |
1414 | `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 |
1717 | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places |
1818 | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers |
1919 | `src/raz.pas` | 227 | 178 | The command line, the entry points |
@@ -310,7 +310,7 @@
310310 the report, calling `gen.pas` as it goes.
311311
312312 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
314314 it: `.field`, `^`, `[i]`, `[lo..hi]`, a call, a conversion. Each step
315315 calls the matching routine in `gen.pas` on the item it has. The
316316 `want` parameter carries the type an expression must have, where it is

editor/emacs/raz-mode.el (history) modified +2 -2

@@ -44,7 +44,7 @@
4444 (modify-syntax-entry ?\' "\"" st)
4545 (modify-syntax-entry ?\\ "\\" st)
4646 (modify-syntax-entry ?_ "_" st)
47- (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?|))
47+ (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?| ?& ?~))
4848 (modify-syntax-entry c "." st))
4949 st)
5050 "Syntax table for `raz-mode'.")
@@ -115,7 +115,7 @@
115115 "Whether the expression on line N goes on to the next line."
116116 (let ((code (raz--code n)))
117117 (or (> (raz--count "[([{]" code) (raz--count "[])}]" code))
118- (string-match-p "\\(?:,\\|:=\\|[-+*/=#<>]\\|\\_<and\\|\\_<or\\)\\'" code))))
118+ (string-match-p "\\(?:,\\|:=\\|[-+*/&|=#<>]\\|\\_<and\\|\\_<or\\)\\'" code))))
119119
120120 (defun raz--start (n)
121121 "The first line of the statement that ends on line N."

editor/vim/indent/raz.vim (history) modified +1 -1

@@ -46,7 +46,7 @@
4646 if len(substitute(code, '[^([{]', '', 'g')) > len(substitute(code, '[^)\]}]', '', 'g'))
4747 return 1
4848 endif
49- return code =~# '\%(,\|:=\|[-+*/=#<>]\|\<and\|\<or\)$'
49+ return code =~# '\%(,\|:=\|[-+*/&|=#<>]\|\<and\|\<or\)$'
5050 endfunction
5151
5252 let s:leading = '^\s*\%(or\|and\)\>'

lib/Heap.raz (history) modified +1 -1

@@ -44,7 +44,7 @@
4444 var c, n: I64; b: ^Block; p: ^U8;
4545 begin
4646 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
4848 return nil;
4949 end;
5050 n := size + Header;

lib/Map.raz (history) modified +2 -2

@@ -26,9 +26,9 @@
2626 proc slot(m: ^Map; k: K): I64;
2727 var i: I64;
2828 begin
29- i := I64(hash(k) and U64(m^.cap - 1));
29+ i := I64(hash(k) & U64(m^.cap - 1));
3030 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);
3232 end;
3333 return i;
3434 end;

lib/Out.raz (history) modified +1 -1

@@ -86,7 +86,7 @@
8686 i := 16;
8787 repeat
8888 i := i - 1;
89- d := U8(n and 15);
89+ d := U8(n & 15);
9090 if d < 10 then buf[i] := d + '0'; else buf[i] := d - 10 + 'A'; end;
9191 n := n shr 4;
9292 digits := digits - 1;

lib/Sys.linux-x86_64.raz (history) modified +5 -5

@@ -143,7 +143,7 @@
143143 r := SYSCALL(4, ADR(a), ADR(st)); (* stat *)
144144 if r # 0 then return i, r; end;
145145 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 *)
147147 return i, 0;
148148 end;
149149
@@ -196,10 +196,10 @@
196196 begin
197197 if SYSCALL(16, fd, 5401H, ADR(saved)) # 0 then return false; end; (* TCGETS *)
198198 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 *)
203203 t.cc[5] := 0; (* VTIME: no time limit *)
204204 t.cc[6] := 1; (* VMIN: a byte at least *)
205205 return SYSCALL(16, fd, 5402H, ADR(t)) = 0; (* TCSETS *)

src/gen.pas modified +40 -14

@@ -88,6 +88,7 @@
8888
8989 procedure Neg(var x: TItem);
9090 procedure NotOp(var x: TItem);
91+procedure Complement(var x: TItem);
9192 procedure Op1(op: TSym; var x: TItem; var L: longint);
9293 procedure Op2(op: TSym; var x, y: TItem; L: longint);
9394 procedure Convert(var x: TItem; t: PType);
@@ -714,13 +715,18 @@
714715
715716 procedure NotOp(var x: TItem);
716717 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;
724730 end;
725731
726732 { Arrays, slices and strings, which = and # compare element by element }
@@ -814,8 +820,8 @@
814820 else if op = sDiv then FoldInt := a div b
815821 else FoldInt := a mod b;
816822 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;
819825 sXor: FoldInt := a xor b;
820826 sShl: FoldInt := a shl (b and 63);
821827 sShr: if u then FoldInt := int64(qword(a) shr (b and 63))
@@ -853,6 +859,22 @@
853859 if r.typ^.form = fReal then RealTo(r, t);
854860 end;
855861
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+
856878 procedure Op2(op: TSym; var x, y: TItem; L: longint);
857879 const
858880 Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq];
@@ -864,8 +886,9 @@
864886 xt := x.typ; yt := y.typ;
865887 if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y);
866888
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);
869892 if (x.mode = mConst) and (y.mode = mConst) then begin
870893 FixLink(L);
871894 if op = sAnd then x.val := x.val and y.val else x.val := x.val or y.val;
@@ -876,6 +899,9 @@
876899 x.mode := mReg; x.typ := tBool;
877900 exit;
878901 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);
879905
880906 { the lengths, then the bytes, LEN * SIZE(T) of them: the elements
881907 are of types whose bytes say all there is to their values }
@@ -932,7 +958,7 @@
932958 if (x.mode = mConst) and (x.typ <> t) then RealTo(x, t);
933959 if (y.mode = mConst) and (y.typ <> t) then RealTo(y, t);
934960 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));
936962 t := tBool;
937963 end else if (xt^.form = fPtr) and IsInt(yt) and (op in [sPlus, sMinus]) then begin
938964 t := xt; scale := xt^.base^.size; isPtr := true;
@@ -1059,8 +1085,8 @@
10591085 else begin Cqo; Unary(7, RCX) end;
10601086 if op = sMod then MovRR(RAX, RDX);
10611087 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);
10641090 sXor: Alu(aluXor, RAX, RCX);
10651091 sShl: Shift(shShl, RAX);
10661092 sShr: if unsign then Shift(shShr, RAX) else Shift(shSar, RAX);

src/parse.pas modified +41 -8

@@ -207,22 +207,25 @@
207207
208208 { Reads to the end of a statement or declaration, from its first
209209 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. }
212213 function SkipConstruct: boolean;
213-var d, p: longint;
214+var d, p: longint; prev: TSym;
214215 begin
215- d := 0; p := 0;
216+ d := 0; p := 0; prev := sSemicolon;
216217 repeat
217218 case tok of
218219 sIf, sWhile, sFor, sMatch, sRepeat, sRecord, sVariant: inc(d);
219220 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);
221223 sLparen, sLbrak, sLbrace: inc(p);
222224 sRparen, sRbrak, sRbrace: if p > 0 then dec(p);
223225 sSemicolon: if (d = 0) and (p = 0) then exit(true);
224226 sEof: exit(false);
225227 end;
228+ prev := tok;
226229 Next;
227230 until AtLineStart;
228231 SkipConstruct := false;
@@ -1040,6 +1043,7 @@
10401043 end;
10411044 sLparen: begin Next; Expression(x, want); Expect(sRparen) end;
10421045 sNot: begin Next; Factor(x, nil); NotOp(x) end;
1046+ sTilde: begin Next; Factor(x, nil); Complement(x) end;
10431047 sLbrak: ArrayLiteral(x, want);
10441048 sLbrace: if (want <> nil) and (want^.form = fRecord) then RecordLiteral(x, want)
10451049 else SetLiteral(x, want);
@@ -1063,7 +1067,7 @@
10631067 begin
10641068 start := TokPos;
10651069 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
10671071 op := tok;
10681072 Next;
10691073 Op1(op, x, L);
@@ -1083,7 +1087,7 @@
10831087 x.pos := SpanFrom(start);
10841088 Neg(x);
10851089 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
10871091 op := tok;
10881092 Next;
10891093 Op1(op, x, L);
@@ -1093,7 +1097,7 @@
10931097 end;
10941098 end;
10951099
1096-procedure Expression(var x: TItem; want: PType);
1100+procedure Relation(var x: TItem; want: PType);
10971101 var op: TSym; y: TItem; L: longint;
10981102 begin
10991103 SimpleExpr(x, want);
@@ -1104,6 +1108,35 @@
11041108 if op = sIn then SimpleExpr(y, nil) else SimpleExpr(y, x.typ);
11051109 Op2(op, x, y, L);
11061110 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);
11071140 end;
11081141 end;
11091142

src/scan.pas modified +4 -3

@@ -23,7 +23,7 @@
2323 TSym = (sNull, sIdent, sInt, sReal, sChar, sString,
2424 sPlus, sMinus, sTimes, sSlash, sEql, sNeq, sLss, sLeq, sGtr, sGeq,
2525 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,
2727 { the reserved words, in the order of Words below }
2828 sAnd, sBegin, sBreak, sBy, sCode, sConst, sDefer, sDiv, sDo, sElse,
2929 sElsif, sEnd, sExtern, sFor, sForward, sIf, sImport, sIn, sLink,
@@ -107,7 +107,7 @@
107107 Ops: array[sPlus..sBar] of string[3] = (
108108 '+', '-', '*', '/', '=', '#', '<', '<=', '>', '>=',
109109 ':=', ':', ';', ',', '.', '..', '...',
110- '(', ')', '[', ']', '{', '}', '^', '|');
110+ '(', ')', '[', ']', '{', '}', '^', '&', '~', '|');
111111
112112 var
113113 cur: TScanState; { buf .. path; tok, ival, rval and id live above }
@@ -638,7 +638,7 @@
638638 else begin inc(cur.pos); tok := sEql end;
639639 '&':
640640 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;
642642 '#': begin inc(cur.pos); tok := sNeq end;
643643 '<': begin
644644 inc(cur.pos);
@@ -675,6 +675,7 @@
675675 '|':
676676 if cur.buf[cur.pos + 1] = '|' then Slip(2, sOr, '"or" is written or, not ||')
677677 else begin inc(cur.pos); tok := sBar end;
678+ '~': begin inc(cur.pos); tok := sTilde end;
678679 '!':
679680 if cur.buf[cur.pos + 1] = '=' then Slip(2, sNeq, '"not equal" is written #, not !=')
680681 else Slip(1, sNot, '"not" is written not, not !');

test/err/AmpBools.err added +5 -0

@@ -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

test/err/AmpBools.raz added +7 -0

@@ -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.

test/err/AtAnd.err modified +1 -0

@@ -1,4 +1,5 @@
11 test/err/AtAnd.raz:5:16: error: "and" needs BOOLs, found I64
22 5 | b := b and (x + 1);
33 | ^^^^^^^
4+help: the bits of integers take &
45 raz: 1 error

test/err/AtNot.err modified +1 -1

@@ -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
22 5 | b := not s;
33 | ^
44 raz: 1 error

test/err/BarBools.err added +5 -0

@@ -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

test/err/BarBools.raz added +7 -0

@@ -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.

test/err/NotInt.err added +5 -0

@@ -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

test/err/NotInt.raz added +7 -0

@@ -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.

test/err/OrInts.err added +5 -0

@@ -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

test/err/OrInts.raz added +7 -0

@@ -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.

test/err/TildeBool.err added +5 -0

@@ -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

test/err/TildeBool.raz added +7 -0

@@ -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.

test/err/XorBools.err added +5 -0

@@ -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

test/err/XorBools.raz added +7 -0

@@ -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.

test/genbench.py modified +2 -2

@@ -46,8 +46,8 @@
4646 | rect(w, h): t := t + w * h;
4747 | dot: t := t - 1;
4848 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;
5151 end;
5252 """)
5353 print(f"""proc main*;

test/pretty/Messy.expect (history) modified +2 -0

@@ -46,6 +46,8 @@
4646 until s < 0;
4747 if (s # -1) or (max2(3, 4) # 4)
4848 or (area({dot}) # 0) then Rt.stop("Messy"); end;
49+ s := s & ~3 | s xor 1 & max
50+ | 4;
4951 p := {a: -s, b:2};
5052 Out.int(p.a); (* the first *)
5153 Out.ln;

test/pretty/Messy.raz (history) modified +2 -0

@@ -46,6 +46,8 @@
4646 until s<0;
4747 if (s # -1) or (max2(3,4) # 4)
4848 or (area({dot}) # 0) then Rt.stop("Messy"); end;
49+ s:=s&~3|s xor 1 & max
50+ | 4;
4951 p := {a: -s, b:2};
5052 Out.int(p.a); (* the first *)
5153 Out.ln;

test/run/Arith.raz modified +3 -3

@@ -47,10 +47,10 @@
4747 show("shl", 1 shl 10);
4848 show("shr", -16 shr 2);
4949 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);
5252 show("xor", 12 xor 10);
53- show("not", not 0);
53+ show("not", ~0);
5454 r := F64(a) / 2.0;
5555 show("real", I64(r * 10.0));
5656 r := -2.75;

tools/Pretty.raz (history) modified +9 -4

@@ -17,7 +17,7 @@
1717 first line moved; so does the text of a comment.
1818
1919 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
2121 comment after code keeps its column. Other spaces stay as written,
2222 so that lined-up match arms stay lined up. No line ends in spaces. *)
2323 module Pretty;
@@ -28,7 +28,7 @@
2828 openers = "if while for match record variant begin repeat";
2929 declarations = "module import link extern proc const type var begin code";
3030 binaryWords = "div mod and or xor shl shr in";
31- binarySymbols = "+ - * / = # < <= > >= :=";
31+ binarySymbols = "+ - * / & | = # < <= > >= :=";
3232
3333 var
3434 sc: Scan.Scanner; (* the source, at the symbol being written *)
@@ -118,7 +118,10 @@
118118 if (sc.kind # Scan.symbol) or not Scan.among(Scan.text(ADR(sc)), binarySymbols) then
119119 return false;
120120 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;
122125 if Scan.is(ADR(sc), "*") and (prev.kind = Scan.word) then
123126 n := following();
124127 if Scan.among(Scan.text(ADR(n)), "; , : = )") then return false; end;
@@ -135,6 +138,7 @@
135138 if (brackets > 0) or prevBinary or Scan.is(ADR(prev), ",") then return true; end;
136139 t := Scan.text(ADR(sc));
137140 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;
138142 return (sc.kind = Scan.symbol) and Scan.among(t, binarySymbols);
139143 end;
140144
@@ -258,7 +262,8 @@
258262 elsif Scan.among(t, ") ] }") and (brackets > 0) then DEC(brackets);
259263 end;
260264 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), "~");
262267 prevBinary := binaryNow;
263268 prev2 := prev;
264269 prev := sc;