From bc22ce64b82ed79b4c7dc5b5c47c4e58e2737026 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:41:16 +0100 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index dd97738..3057968 100644 --- a/PLAN.md +++ b/PLAN.md @@ -438,7 +438,7 @@ name. Tests: equal, of different lengths, empty, a string literal against an array, and in `test/err` arrays of `F64`, of records, and of two element types. -- [ ] 45. Operators: `&` and `~` in the scanner, `|` an operator where +- [x] 45. Operators: `&` and `~` in the scanner, `|` an operator where an operand came before it; the parser's levels, `or` over `and` over the relations; `and`, `or`, `not` refused on integers and `&`, `|`, `xor`, `~` on `BOOL`s, each with a message that names diff --git a/doc/compiler.md b/doc/compiler.md index 2317ce7..9b5c1f7 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -9,11 +9,11 @@ | File | Lines | Code | What | |---|---|---|---| | `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts | -| `src/scan.pas` | 705 | 567 | Symbols, literals, comments, positions | +| `src/scan.pas` | 706 | 568 | Symbols, literals, comments, positions | | `src/sym.pas` | 593 | 493 | Types, objects, scopes, layout | | `src/x64.pas` | 693 | 556 | Code buffer, chunks, fixups, places, instruction encoder | -| `src/gen.pas` | 1,570 | 1,358 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,290 | 2,069 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/gen.pas` | 1,596 | 1,378 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 2,323 | 2,098 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 227 | 178 | The command line, the entry points | @@ -310,7 +310,7 @@ the report, calling `gen.pas` as it goes. Start with expressions, bottom up: `Factor`, `Term`, `SimpleExpr`, -`Expression`. Then `Designator`, which reads a name and what follows +`Relation`, `Conjunct` (the `and`s), `Expression` (the `or`s). Then `Designator`, which reads a name and what follows it: `.field`, `^`, `[i]`, `[lo..hi]`, a call, a conversion. Each step calls the matching routine in `gen.pas` on the item it has. The `want` parameter carries the type an expression must have, where it is diff --git a/editor/emacs/raz-mode.el b/editor/emacs/raz-mode.el index fdda521..3652c46 100644 --- a/editor/emacs/raz-mode.el +++ b/editor/emacs/raz-mode.el @@ -44,7 +44,7 @@ (modify-syntax-entry ?\' "\"" st) (modify-syntax-entry ?\\ "\\" st) (modify-syntax-entry ?_ "_" st) - (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?|)) + (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?| ?& ?~)) (modify-syntax-entry c "." st)) st) "Syntax table for `raz-mode'.") @@ -115,7 +115,7 @@ "Whether the expression on line N goes on to the next line." (let ((code (raz--code n))) (or (> (raz--count "[([{]" code) (raz--count "[])}]" code)) - (string-match-p "\\(?:,\\|:=\\|[-+*/=#<>]\\|\\_]\\|\\_ len(substitute(code, '[^)\]}]', '', 'g')) return 1 endif - return code =~# '\%(,\|:=\|[-+*/=#<>]\|\]\|\ Largest) or (align < 1) or (align > Mem.Page) - or (align and (align - 1) # 0) then + or (align & (align - 1) # 0) then return nil; end; n := size + Header; diff --git a/lib/Map.raz b/lib/Map.raz index 746d7af..84bf36c 100644 --- a/lib/Map.raz +++ b/lib/Map.raz @@ -26,9 +26,9 @@ proc slot(m: ^Map; k: K): I64; var i: I64; begin - i := I64(hash(k) and U64(m^.cap - 1)); + i := I64(hash(k) & U64(m^.cap - 1)); while m^.slots[i].used and not eq(m^.slots[i].key, k) do - i := (i + 1) and (m^.cap - 1); + i := (i + 1) & (m^.cap - 1); end; return i; end; diff --git a/lib/Out.raz b/lib/Out.raz index 5597fec..ce3937a 100644 --- a/lib/Out.raz +++ b/lib/Out.raz @@ -86,7 +86,7 @@ i := 16; repeat i := i - 1; - d := U8(n and 15); + d := U8(n & 15); if d < 10 then buf[i] := d + '0'; else buf[i] := d - 10 + 'A'; end; n := n shr 4; digits := digits - 1; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index c1334d0..5e9063d 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -143,7 +143,7 @@ r := SYSCALL(4, ADR(a), ADR(st)); (* stat *) if r # 0 then return i, r; end; i.size := st[6]; (* st_size *) - i.dir := st[3] and 0F000H = 4000H; (* st_mode is S_IFDIR *) + i.dir := st[3] & 0F000H = 4000H; (* st_mode is S_IFDIR *) return i, 0; end; @@ -196,10 +196,10 @@ begin if SYSCALL(16, fd, 5401H, ADR(saved)) # 0 then return false; end; (* TCGETS *) t := saved; - t.iflag := t.iflag and not U32(5EBH); (* IGNBRK BRKINT PARMRK ISTRIP INLCR IGNCR ICRNL IXON *) - t.oflag := t.oflag and not U32(1); (* OPOST *) - t.cflag := t.cflag and not U32(130H) or 30H; (* CS8, no PARENB *) - t.lflag := t.lflag and not U32(804BH); (* ISIG ICANON ECHO ECHONL IEXTEN *) + t.iflag := t.iflag & ~U32(5EBH); (* IGNBRK BRKINT PARMRK ISTRIP INLCR IGNCR ICRNL IXON *) + t.oflag := t.oflag & ~U32(1); (* OPOST *) + t.cflag := t.cflag & ~U32(130H) | 30H; (* CS8, no PARENB *) + t.lflag := t.lflag & ~U32(804BH); (* ISIG ICANON ECHO ECHONL IEXTEN *) t.cc[5] := 0; (* VTIME: no time limit *) t.cc[6] := 1; (* VMIN: a byte at least *) return SYSCALL(16, fd, 5402H, ADR(t)) = 0; (* TCSETS *) diff --git a/src/gen.pas b/src/gen.pas index aa69428..3a05500 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -88,6 +88,7 @@ procedure Neg(var x: TItem); procedure NotOp(var x: TItem); +procedure Complement(var x: TItem); procedure Op1(op: TSym; var x: TItem; var L: longint); procedure Op2(op: TSym; var x, y: TItem; L: longint); procedure Convert(var x: TItem; t: PType); @@ -714,13 +715,18 @@ procedure NotOp(var x: TItem); begin - if x.typ^.form = fBool then begin - if x.mode = mConst then x.val := 1 - x.val - else begin Value(x); AluI(aluXor, RAX, 1) end; - end else if IsInt(x.typ) then begin - if x.mode = mConst then x.val := Cut(not x.val, x.typ) - else begin Value(x); Unary(2, RAX); Norm(x.typ) end; - end else MarkAt(x.pos, '"not" needs a BOOL or an integer, found ' + What(x)); + if IsInt(x.typ) then MarkHelp(x.pos, '"not" needs a BOOL, found ' + What(x), 'the bits of an integer flip with ~'); + if x.typ^.form <> fBool then MarkAt(x.pos, '"not" needs a BOOL, found ' + What(x)); + if x.mode = mConst then x.val := 1 - x.val + else begin Value(x); AluI(aluXor, RAX, 1) end; +end; + +procedure Complement(var x: TItem); +begin + if x.typ^.form = fBool then MarkHelp(x.pos, '"~" needs an integer, found ' + What(x), 'a BOOL is negated with not'); + if not IsInt(x.typ) then MarkAt(x.pos, '"~" needs an integer, found ' + What(x)); + if x.mode = mConst then x.val := Cut(not x.val, x.typ) + else begin Value(x); Unary(2, RAX); Norm(x.typ) end; end; { Arrays, slices and strings, which = and # compare element by element } @@ -814,8 +820,8 @@ else if op = sDiv then FoldInt := a div b else FoldInt := a mod b; end; - sAnd: FoldInt := a and b; - sOr: FoldInt := a or b; + sAmp: FoldInt := a and b; + sBar: FoldInt := a or b; sXor: FoldInt := a xor b; sShl: FoldInt := a shl (b and 63); sShr: if u then FoldInt := int64(qword(a) shr (b and 63)) @@ -853,6 +859,22 @@ if r.typ^.form = fReal then RealTo(r, t); end; +{ and, or and not are for BOOLs; &, |, xor and ~ for the bits of + integers: x is the wrong kind for op, and the message names the + operator it may have meant } +procedure WrongLogic(op: TSym; var x: TItem); +const Bits = 'the bits of integers take '; +begin + case op of + sAnd: if IsInt(x.typ) then MarkHelp(x.pos, '"and" needs BOOLs, found ' + What(x), Bits + '&'); + sOr: if IsInt(x.typ) then MarkHelp(x.pos, '"or" needs BOOLs, found ' + What(x), Bits + '|'); + sAmp: MarkHelp(x.pos, '"&" needs integers, found ' + What(x), 'BOOLs take and'); + sBar: MarkHelp(x.pos, '"|" needs integers, found ' + What(x), 'BOOLs take or'); + sXor: MarkHelp(x.pos, '"xor" needs integers, found ' + What(x), 'BOOLs that differ are #'); + end; + MarkAt(x.pos, SymText(op) + ' needs BOOLs, found ' + What(x)); +end; + procedure Op2(op: TSym; var x, y: TItem; L: longint); const Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq]; @@ -864,8 +886,9 @@ xt := x.typ; yt := y.typ; if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y); - if (op in [sAnd, sOr]) and (xt^.form = fBool) then begin - if yt^.form <> fBool then MarkAt(y.pos, SymText(op) + ' needs BOOLs, found ' + What(y)); + if op in [sAnd, sOr] then begin + if xt^.form <> fBool then WrongLogic(op, x); + if yt^.form <> fBool then WrongLogic(op, y); if (x.mode = mConst) and (y.mode = mConst) then begin FixLink(L); if op = sAnd then x.val := x.val and y.val else x.val := x.val or y.val; @@ -876,6 +899,9 @@ x.mode := mReg; x.typ := tBool; exit; end; + + if (op in [sAmp, sBar, sXor]) and (xt^.form = fBool) then WrongLogic(op, x); + if (op in [sAmp, sBar, sXor]) and (yt^.form = fBool) then WrongLogic(op, y); { the lengths, then the bytes, LEN * SIZE(T) of them: the elements are of types whose bytes say all there is to their values } @@ -932,7 +958,7 @@ if (x.mode = mConst) and (x.typ <> t) then RealTo(x, t); if (y.mode = mConst) and (y.typ <> t) then RealTo(y, t); end else if (xt^.form = fBool) and (yt^.form = fBool) then begin - if not (op in [sXor, sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); + if not (op in [sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); t := tBool; end else if (xt^.form = fPtr) and IsInt(yt) and (op in [sPlus, sMinus]) then begin t := xt; scale := xt^.base^.size; isPtr := true; @@ -1059,8 +1085,8 @@ else begin Cqo; Unary(7, RCX) end; if op = sMod then MovRR(RAX, RDX); end; - sAnd: Alu(aluAnd, RAX, RCX); - sOr: Alu(aluOr, RAX, RCX); + sAmp: Alu(aluAnd, RAX, RCX); + sBar: Alu(aluOr, RAX, RCX); sXor: Alu(aluXor, RAX, RCX); sShl: Shift(shShl, RAX); sShr: if unsign then Shift(shShr, RAX) else Shift(shSar, RAX); diff --git a/src/parse.pas b/src/parse.pas index 0d5b90f..5779a32 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -207,22 +207,25 @@ { Reads to the end of a statement or declaration, from its first symbol: up to the ";" that ends it, not read, or to the "end", - "else", "elsif", "until" or "|" of the statement around it. False if - it stopped at a symbol that starts a line, or the end of the file. } + "else", "elsif", "until" or "|" of the statement around it (an arm's + "|" comes after a ";", the operator's after an operand). False if it + stopped at a symbol that starts a line, or the end of the file. } function SkipConstruct: boolean; -var d, p: longint; +var d, p: longint; prev: TSym; begin - d := 0; p := 0; + d := 0; p := 0; prev := sSemicolon; repeat case tok of sIf, sWhile, sFor, sMatch, sRepeat, sRecord, sVariant: inc(d); sEnd, sUntil: if d = 0 then exit(true) else dec(d); - sElse, sElsif, sBar: if (d = 0) and (p = 0) then exit(true); + sElse, sElsif: if (d = 0) and (p = 0) then exit(true); + sBar: if (d = 0) and (p = 0) and (prev = sSemicolon) then exit(true); sLparen, sLbrak, sLbrace: inc(p); sRparen, sRbrak, sRbrace: if p > 0 then dec(p); sSemicolon: if (d = 0) and (p = 0) then exit(true); sEof: exit(false); end; + prev := tok; Next; until AtLineStart; SkipConstruct := false; @@ -1040,6 +1043,7 @@ end; sLparen: begin Next; Expression(x, want); Expect(sRparen) end; sNot: begin Next; Factor(x, nil); NotOp(x) end; + sTilde: begin Next; Factor(x, nil); Complement(x) end; sLbrak: ArrayLiteral(x, want); sLbrace: if (want <> nil) and (want^.form = fRecord) then RecordLiteral(x, want) else SetLiteral(x, want); @@ -1063,7 +1067,7 @@ begin start := TokPos; Factor(x, want); - while tok in [sTimes, sSlash, sDiv, sMod, sAnd, sShl, sShr] do begin + while tok in [sTimes, sSlash, sDiv, sMod, sAmp, sShl, sShr] do begin op := tok; Next; Op1(op, x, L); @@ -1083,7 +1087,7 @@ x.pos := SpanFrom(start); Neg(x); end else Term(x, want); - while tok in [sPlus, sMinus, sOr, sXor] do begin + while tok in [sPlus, sMinus, sBar, sXor] do begin op := tok; Next; Op1(op, x, L); @@ -1093,7 +1097,7 @@ end; end; -procedure Expression(var x: TItem; want: PType); +procedure Relation(var x: TItem; want: PType); var op: TSym; y: TItem; L: longint; begin SimpleExpr(x, want); @@ -1104,6 +1108,35 @@ if op = sIn then SimpleExpr(y, nil) else SimpleExpr(y, x.typ); Op2(op, x, y, L); x.pos := Join(x.pos, y.pos); + end; +end; + +{ and binds tighter than or, and both below the relations } +procedure Conjunct(var x: TItem; want: PType); +var y: TItem; L: longint; start: TPos; +begin + start := TokPos; + Relation(x, want); + while tok = sAnd do begin + Next; + Op1(sAnd, x, L); + Relation(y, nil); + Op2(sAnd, x, y, L); + x.pos := SpanFrom(start); + end; +end; + +procedure Expression(var x: TItem; want: PType); +var y: TItem; L: longint; start: TPos; +begin + start := TokPos; + Conjunct(x, want); + while tok = sOr do begin + Next; + Op1(sOr, x, L); + Conjunct(y, nil); + Op2(sOr, x, y, L); + x.pos := SpanFrom(start); end; end; diff --git a/src/scan.pas b/src/scan.pas index 2cf8192..e3ada9d 100644 --- a/src/scan.pas +++ b/src/scan.pas @@ -23,7 +23,7 @@ TSym = (sNull, sIdent, sInt, sReal, sChar, sString, sPlus, sMinus, sTimes, sSlash, sEql, sNeq, sLss, sLeq, sGtr, sGeq, sBecomes, sColon, sSemicolon, sComma, sPeriod, sUpto, sEllipsis, - sLparen, sRparen, sLbrak, sRbrak, sLbrace, sRbrace, sArrow, sBar, + sLparen, sRparen, sLbrak, sRbrak, sLbrace, sRbrace, sArrow, sAmp, sTilde, sBar, { the reserved words, in the order of Words below } sAnd, sBegin, sBreak, sBy, sCode, sConst, sDefer, sDiv, sDo, sElse, sElsif, sEnd, sExtern, sFor, sForward, sIf, sImport, sIn, sLink, @@ -107,7 +107,7 @@ Ops: array[sPlus..sBar] of string[3] = ( '+', '-', '*', '/', '=', '#', '<', '<=', '>', '>=', ':=', ':', ';', ',', '.', '..', '...', - '(', ')', '[', ']', '{', '}', '^', '|'); + '(', ')', '[', ']', '{', '}', '^', '&', '~', '|'); var cur: TScanState; { buf .. path; tok, ival, rval and id live above } @@ -638,7 +638,7 @@ else begin inc(cur.pos); tok := sEql end; '&': if cur.buf[cur.pos + 1] = '&' then Slip(2, sAnd, '"and" is written and, not &&') - else MarkBytes(cur.pos, cur.pos + 1, 'unexpected character "&"'); + else begin inc(cur.pos); tok := sAmp end; '#': begin inc(cur.pos); tok := sNeq end; '<': begin inc(cur.pos); @@ -675,6 +675,7 @@ '|': if cur.buf[cur.pos + 1] = '|' then Slip(2, sOr, '"or" is written or, not ||') else begin inc(cur.pos); tok := sBar end; + '~': begin inc(cur.pos); tok := sTilde end; '!': if cur.buf[cur.pos + 1] = '=' then Slip(2, sNeq, '"not equal" is written #, not !=') else Slip(1, sNot, '"not" is written not, not !'); diff --git a/test/err/AmpBools.err b/test/err/AmpBools.err new file mode 100644 index 0000000..f2763fb --- /dev/null +++ b/test/err/AmpBools.err @@ -0,0 +1,5 @@ +test/err/AmpBools.raz:5:10: error: "&" needs integers, found BOOL + 5 | b := b & true; + | ^ +help: BOOLs take and +raz: 1 error diff --git a/test/err/AmpBools.raz b/test/err/AmpBools.raz new file mode 100644 index 0000000..3ed1ef7 --- /dev/null +++ b/test/err/AmpBools.raz @@ -0,0 +1,7 @@ +module AmpBools; +proc main*; +var b: BOOL; x: I64; +begin + b := b & true; +end; +end AmpBools. diff --git a/test/err/AtAnd.err b/test/err/AtAnd.err index 2f1851b..78519ad 100644 --- a/test/err/AtAnd.err +++ b/test/err/AtAnd.err @@ -1,4 +1,5 @@ test/err/AtAnd.raz:5:16: error: "and" needs BOOLs, found I64 5 | b := b and (x + 1); | ^^^^^^^ +help: the bits of integers take & raz: 1 error diff --git a/test/err/AtNot.err b/test/err/AtNot.err index 5e06b42..5bce7e2 100644 --- a/test/err/AtNot.err +++ b/test/err/AtNot.err @@ -1,4 +1,4 @@ -test/err/AtNot.raz:5:14: error: "not" needs a BOOL or an integer, found []U8 +test/err/AtNot.raz:5:14: error: "not" needs a BOOL, found []U8 5 | b := not s; | ^ raz: 1 error diff --git a/test/err/BarBools.err b/test/err/BarBools.err new file mode 100644 index 0000000..5b4525f --- /dev/null +++ b/test/err/BarBools.err @@ -0,0 +1,5 @@ +test/err/BarBools.raz:5:10: error: "|" needs integers, found BOOL + 5 | b := b | true; + | ^ +help: BOOLs take or +raz: 1 error diff --git a/test/err/BarBools.raz b/test/err/BarBools.raz new file mode 100644 index 0000000..d73b4c3 --- /dev/null +++ b/test/err/BarBools.raz @@ -0,0 +1,7 @@ +module BarBools; +proc main*; +var b: BOOL; x: I64; +begin + b := b | true; +end; +end BarBools. diff --git a/test/err/NotInt.err b/test/err/NotInt.err new file mode 100644 index 0000000..a618cec --- /dev/null +++ b/test/err/NotInt.err @@ -0,0 +1,5 @@ +test/err/NotInt.raz:5:14: error: "not" needs a BOOL, found I64 + 5 | x := not x; + | ^ +help: the bits of an integer flip with ~ +raz: 1 error diff --git a/test/err/NotInt.raz b/test/err/NotInt.raz new file mode 100644 index 0000000..0f1fd2a --- /dev/null +++ b/test/err/NotInt.raz @@ -0,0 +1,7 @@ +module NotInt; +proc main*; +var b: BOOL; x: I64; +begin + x := not x; +end; +end NotInt. diff --git a/test/err/OrInts.err b/test/err/OrInts.err new file mode 100644 index 0000000..d563187 --- /dev/null +++ b/test/err/OrInts.err @@ -0,0 +1,5 @@ +test/err/OrInts.raz:5:10: error: "or" needs BOOLs, found I64 + 5 | x := x or 1; + | ^ +help: the bits of integers take | +raz: 1 error diff --git a/test/err/OrInts.raz b/test/err/OrInts.raz new file mode 100644 index 0000000..7f23abb --- /dev/null +++ b/test/err/OrInts.raz @@ -0,0 +1,7 @@ +module OrInts; +proc main*; +var b: BOOL; x: I64; +begin + x := x or 1; +end; +end OrInts. diff --git a/test/err/TildeBool.err b/test/err/TildeBool.err new file mode 100644 index 0000000..f405aca --- /dev/null +++ b/test/err/TildeBool.err @@ -0,0 +1,5 @@ +test/err/TildeBool.raz:5:11: error: "~" needs an integer, found BOOL + 5 | b := ~b; + | ^ +help: a BOOL is negated with not +raz: 1 error diff --git a/test/err/TildeBool.raz b/test/err/TildeBool.raz new file mode 100644 index 0000000..46526e6 --- /dev/null +++ b/test/err/TildeBool.raz @@ -0,0 +1,7 @@ +module TildeBool; +proc main*; +var b: BOOL; x: I64; +begin + b := ~b; +end; +end TildeBool. diff --git a/test/err/XorBools.err b/test/err/XorBools.err new file mode 100644 index 0000000..504744d --- /dev/null +++ b/test/err/XorBools.err @@ -0,0 +1,5 @@ +test/err/XorBools.raz:5:10: error: "xor" needs integers, found BOOL + 5 | b := b xor true; + | ^ +help: BOOLs that differ are # +raz: 1 error diff --git a/test/err/XorBools.raz b/test/err/XorBools.raz new file mode 100644 index 0000000..6aafbaa --- /dev/null +++ b/test/err/XorBools.raz @@ -0,0 +1,7 @@ +module XorBools; +proc main*; +var b: BOOL; x: I64; +begin + b := b xor true; +end; +end XorBools. diff --git a/test/genbench.py b/test/genbench.py index 2da5cf9..39f52da 100644 --- a/test/genbench.py +++ b/test/genbench.py @@ -46,8 +46,8 @@ | rect(w, h): t := t + w * h; | dot: t := t - 1; end; - g[a and 15] := r.x + r.y; - return p{i - 1}(t and 1023, b, s[0..LEN(s) div 2]) + r.x; + g[a & 15] := r.x + r.y; + return p{i - 1}(t & 1023, b, s[0..LEN(s) div 2]) + r.x; end; """) print(f"""proc main*; diff --git a/test/pretty/Messy.expect b/test/pretty/Messy.expect index 0390c35..46a04dc 100644 --- a/test/pretty/Messy.expect +++ b/test/pretty/Messy.expect @@ -46,6 +46,8 @@ until s < 0; if (s # -1) or (max2(3, 4) # 4) or (area({dot}) # 0) then Rt.stop("Messy"); end; + s := s & ~3 | s xor 1 & max + | 4; p := {a: -s, b:2}; Out.int(p.a); (* the first *) Out.ln; diff --git a/test/pretty/Messy.raz b/test/pretty/Messy.raz index 7b38220..d5a95bd 100644 --- a/test/pretty/Messy.raz +++ b/test/pretty/Messy.raz @@ -46,6 +46,8 @@ until s<0; if (s # -1) or (max2(3,4) # 4) or (area({dot}) # 0) then Rt.stop("Messy"); end; + s:=s&~3|s xor 1 & max + | 4; p := {a: -s, b:2}; Out.int(p.a); (* the first *) Out.ln; diff --git a/test/run/Arith.raz b/test/run/Arith.raz index 8fe90cb..a0080dc 100644 --- a/test/run/Arith.raz +++ b/test/run/Arith.raz @@ -47,10 +47,10 @@ show("shl", 1 shl 10); show("shr", -16 shr 2); show("ushr", I64(U64(-16) shr 60)); - show("and", 12 and 10); - show("or", 12 or 3); + show("and", 12 & 10); + show("or", 12 | 3); show("xor", 12 xor 10); - show("not", not 0); + show("not", ~0); r := F64(a) / 2.0; show("real", I64(r * 10.0)); r := -2.75; diff --git a/tools/Pretty.raz b/tools/Pretty.raz index 83c78ab..4adbed0 100644 --- a/tools/Pretty.raz +++ b/tools/Pretty.raz @@ -17,7 +17,7 @@ first line moved; so does the text of a comment. Within a line, a binary operator has one space on each side, a unary - minus none after it, and , and ; none before them and one after. A + minus or ~ none after it, and , and ; none before them and one after. A comment after code keeps its column. Other spaces stay as written, so that lined-up match arms stay lined up. No line ends in spaces. *) module Pretty; @@ -28,7 +28,7 @@ openers = "if while for match record variant begin repeat"; declarations = "module import link extern proc const type var begin code"; binaryWords = "div mod and or xor shl shr in"; - binarySymbols = "+ - * / = # < <= > >= :="; + binarySymbols = "+ - * / & | = # < <= > >= :="; var sc: Scan.Scanner; (* the source, at the symbol being written *) @@ -118,7 +118,10 @@ if (sc.kind # Scan.symbol) or not Scan.among(Scan.text(ADR(sc)), binarySymbols) then return false; end; - if Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+") then return endsOperand(ADR(prev)); end; + (* a | after an operand is or; one after anything else starts an arm *) + if Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+") or Scan.is(ADR(sc), "|") then + return endsOperand(ADR(prev)); + end; if Scan.is(ADR(sc), "*") and (prev.kind = Scan.word) then n := following(); if Scan.among(Scan.text(ADR(n)), "; , : = )") then return false; end; @@ -135,6 +138,7 @@ if (brackets > 0) or prevBinary or Scan.is(ADR(prev), ",") then return true; end; t := Scan.text(ADR(sc)); if sc.kind = Scan.word then return Scan.among(t, binaryWords) or Scan.among(t, "then do of"); end; + if Scan.is(ADR(sc), "|") then return endsOperand(ADR(prev)); end; return (sc.kind = Scan.symbol) and Scan.among(t, binarySymbols); end; @@ -258,7 +262,8 @@ elsif Scan.among(t, ") ] }") and (brackets > 0) then DEC(brackets); end; end; - prevUnary := (Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+")) and not binaryNow; + prevUnary := (Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+")) and not binaryNow + or Scan.is(ADR(sc), "~"); prevBinary := binaryNow; prev2 := prev; prev := sc;