raz / commit a48b818d

commit
a48b818d9c85807700ffa0e37034338b72477c3e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e9738450
Warnings: an = or # of two BOOLs beside and or or, and \0 before a digit in a string

patch · log

9 files changed, +183 -41

doc/compiler.md (history) modified +18 -6

@@ -17,13 +17,13 @@
1717 | File | Lines | Code | What |
1818 |---|---|---|---|
1919 | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts |
20-| `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions |
20+| `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions |
2121 | `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,592 | 1,843 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,223 | 1,785 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,323 | 3,735 | Declarations, statements, expressions, built-ins, recovery, warnings |
26+| `src/Parse.raz` | 4,396 | 3,796 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 418 | 333 | The command line |
@@ -921,6 +921,7 @@
921921 report's section 2), and leaving carriage returns out. `readReal` turns a real's
922922 digits into the nearest `F64` exactly, as a fraction of two big integers
923923 (`Big`), since a library's reader can be one bit off.
924+`zeroDigit` reads the text of the current string again, for a `\0` before a digit, which the parser warns of.
924925
925926 Identifiers are *interned*: `intern` puts each distinct name once in a
926927 pool and hands back a small integer, so every later comparison of names
@@ -1606,8 +1607,8 @@
16061607 reports what isn't exported, doesn't start with `_`, and was never
16071608 used, or, for a variable, only assigned, and a procedure with results
16081609 that may reach its end (`openEnd`, from `ends`, which each statement
1609-sets). The calls that drop results are a third list, `drops`, in
1610-order: `compileFile` notes where the module's own begin (`numDrops`),
1610+sets). The calls that drop results, and the other doubts, are a third list, `doubts`, in
1611+order: `compileFile` notes where the module's own begin (`numDoubts`),
16111612 since the instances it makes along the way add theirs and take them
16121613 back. A
16131614 generic module's instances
@@ -1837,8 +1838,19 @@
18371838 way through its statements must end in a `return`, an `if` with an
18381839 `else`, a `match`, a `while true` or a `repeat ... until false`
18391840 with no `break`. So is a call standing on its own that drops a
1840- result (`drops`, kept by `noteDrop` and told by `dropped`);
1841- `_ := f(x)` drops one on purpose. Warnings don't stop the compile;
1841+ result (`doubts`, kept by `noteDrop` and told by `dropped`);
1842+ `_ := f(x)` drops one on purpose. Two more are noted the same way,
1843+ by `noteDoubt`, which keeps the words to say. A relation binds
1844+ tighter than `and` and `or`, so `a # b and c` is `(a # b) and c`, as
1845+ a Pascal reader doesn't expect: `relation` sets `bareBoolRel` when it
1846+ read an `=` or `#` of two `BOOL`s (a parenthesised one is a factor,
1847+ and the relation around it sets it false), and `conjunct` and
1848+ `expression` ask `noteBoolRel` of each operand of an `and` or an
1849+ `or`. And a string with `\0` before a digit, `"\01"`, is a zero byte
1850+ and a `1`, not C's octal escape: `Scan.zeroDigit` looks for it in the
1851+ text of the string, and `factor` notes it (`noteZeroDigit`); `\x00`
1852+ keeps the two apart. A text the compiler reads twice, a `while`'s
1853+ condition, is noted once. Warnings don't stop the compile;
18421854 `raz -Werror`, an option of this compiler's, makes each an error.
18431855 - **Registers for variables.** Up to five of a procedure's single
18441856 values, not reals, whose address is never taken, live in `rbx` and

src/Parse.raz (history) modified +98 -35

@@ -62,7 +62,7 @@
6262 MaxNames = 64; (* names in one declaration, and the like *)
6363 MaxFwd = 256; (* pointers to types not yet declared *)
6464 MaxDefers = 64; (* defer statements in force *)
65- MaxDrops = 4096; (* calls that drop results, in one module *)
65+ MaxDoubts = 4096; (* places to warn of, noted while a module is read *)
6666 MaxDeferred = 64; (* generic imports not yet made *)
6767 MaxLabels = 1024; (* labels of one match *)
6868 MaxElements = 1024; (* elements of an array literal *)
@@ -108,12 +108,15 @@
108108 stray: BOOL; (* an "end" in this procedure stood left of its statement's line *)
109109 strayAt: Diag.Pos; (* ... the first such statement *)
110110 elseIf: BOOL; (* an "else if" in this procedure *)
111- drops: [MaxDrops]record
112- at: Diag.Pos; (* a call standing on its own that drops results *)
113- n: INT; (* ... how many *)
111+ doubts: [MaxDoubts]record
112+ at: Diag.Pos; (* a place the module's end warns of *)
113+ n: INT; (* a call standing on its own that drops this many results, or *)
114+ msg, hint: []U8; (* any other doubt, in words (msg is empty for a drop) *)
114115 end;
115- numDrops: INT; (* the module being compiled has those from its
116+ numDoubts: INT; (* the module being compiled has those from its
116117 first one, saved by compileFile, to here *)
118+ bareBoolRel: BOOL; (* the relation just read is an = or # of two BOOLs,
119+ not in parentheses *)
117120 ends: BOOL; (* the statements just read never go on past
118121 their end: each way through them returns *)
119122 useAt: Diag.Pos; (* ... which is here *)
@@ -1548,6 +1551,33 @@
15481551 return nil;
15491552 end;
15501553
1554+(* A doubt other than a drop, to be warned of at p with msg and hint.
1555+ Text read twice, as a while's condition is, is noted once. *)
1556+proc noteDoubt(p: Diag.Pos; msg, hint: []U8);
1557+var i: INT;
1558+begin
1559+ for i := 0 to numDoubts - 1 do
1560+ if (doubts[i].at.f = p.f) and (doubts[i].at.a = p.a) then return; end;
1561+ end;
1562+ if numDoubts < MaxDoubts then
1563+ doubts[numDoubts].at := p;
1564+ doubts[numDoubts].msg := msg;
1565+ doubts[numDoubts].hint := hint;
1566+ INC(numDoubts);
1567+ end;
1568+end;
1569+
1570+(* Notes the string at the scanner if it has a \0 before a digit *)
1571+proc noteZeroDigit;
1572+var at: Diag.Pos; yes: BOOL;
1573+begin
1574+ at, yes := Scan.zeroDigit();
1575+ if yes then
1576+ noteDoubt(at, "\\0 is a zero byte and the digit after it is itself",
1577+ "write \\x00 to keep them apart");
1578+ end;
1579+end;
1580+
15511581 proc factor(var x: TItem; want: PType): ERROR;
15521582 var called, vol, several: BOOL; t: PType; start: INT; s: []U8;
15531583 begin
@@ -1565,6 +1595,7 @@
15651595 Scan.next;
15661596 | sString:
15671597 Item.makeStr(x);
1598+ noteZeroDigit();
15681599 Scan.next;
15691600 | sIdent:
15701601 try designator(x, called);
@@ -1684,33 +1715,54 @@
16841715 return nil;
16851716 end;
16861717
1718+(* A relation. It sets bareBoolRel, for the and or or that may follow:
1719+ a relation binds tighter than and and or, so the # in "a # b and c"
1720+ is read first, as (a # b) and c, which a Pascal reader doesn't expect. *)
16871721 proc relation(var x: TItem; want: PType): ERROR;
1688-var op: Scan.Sym; y: TItem; L: INT;
1722+var op: Scan.Sym; y: TItem; L: INT; bools: BOOL;
16891723 begin
16901724 try simpleExpr(x, want);
1725+ bools := false;
16911726 if isRelOp() then
16921727 op := Scan.tok;
16931728 Scan.next;
16941729 try Item.op1(op, x, L);
16951730 if op = Scan.sIn then try simpleExpr(y, nil); else try simpleExpr(y, x.typ); end;
1731+ bools := ((op = Scan.sEql) or (op = Scan.sNeq)) and (x.typ = Sym.tBool) and (y.typ = Sym.tBool);
16961732 try Item.op2(op, x, y, L);
16971733 x.pos := Diag.join(x.pos, y.pos);
16981734 end;
1735+ bareBoolRel := bools; (* a relation in parentheses comes here as a factor, and sets it false *)
16991736 return nil;
17001737 end;
17011738
1739+(* Notes x, an operand of the and or or (word) just read, if it is an
1740+ unparenthesised = or # of two BOOLs *)
1741+proc noteBoolRel(const x: TItem; word: []U8);
1742+begin
1743+ if bareBoolRel then
1744+ noteDoubt(x.pos, say2("{} is read first: = and # bind tighter than {}", Diag.source(x.pos), word),
1745+ "put the = or # of two BOOLs in parentheses");
1746+ end;
1747+end;
1748+
17021749 (* and binds tighter than or, and both below the relations *)
17031750 proc conjunct(var x: TItem; want: PType): ERROR;
17041751 var y: TItem; L: INT; start: INT;
17051752 begin
17061753 start := Scan.tokStart();
17071754 try relation(x, want);
1708- while Scan.tok = Scan.sAnd do
1709- Scan.next;
1710- try Item.op1(Scan.sAnd, x, L);
1711- try relation(y, nil);
1712- try Item.op2(Scan.sAnd, x, y, L);
1713- x.pos := Scan.spanFromAt(start);
1755+ if Scan.tok = Scan.sAnd then
1756+ noteBoolRel(x, "and");
1757+ repeat
1758+ Scan.next;
1759+ try Item.op1(Scan.sAnd, x, L);
1760+ try relation(y, nil);
1761+ noteBoolRel(y, "and");
1762+ try Item.op2(Scan.sAnd, x, y, L);
1763+ x.pos := Scan.spanFromAt(start);
1764+ until Scan.tok # Scan.sAnd;
1765+ bareBoolRel := false; (* x is a conjunction now *)
17141766 end;
17151767 return nil;
17161768 end;
@@ -1720,12 +1772,17 @@
17201772 begin
17211773 start := Scan.tokStart();
17221774 try conjunct(x, want);
1723- while Scan.tok = Scan.sOr do
1724- Scan.next;
1725- try Item.op1(Scan.sOr, x, L);
1726- try conjunct(y, nil);
1727- try Item.op2(Scan.sOr, x, y, L);
1728- x.pos := Scan.spanFromAt(start);
1775+ if Scan.tok = Scan.sOr then
1776+ noteBoolRel(x, "or");
1777+ repeat
1778+ Scan.next;
1779+ try Item.op1(Scan.sOr, x, L);
1780+ try conjunct(y, nil);
1781+ noteBoolRel(y, "or");
1782+ try Item.op2(Scan.sOr, x, y, L);
1783+ x.pos := Scan.spanFromAt(start);
1784+ until Scan.tok # Scan.sOr;
1785+ bareBoolRel := false; (* x is a disjunction now *)
17291786 end;
17301787 return nil;
17311788 end;
@@ -2464,10 +2521,11 @@
24642521 to be warned of *)
24652522 proc noteDrop(const x: TItem);
24662523 begin
2467- if (x.mode # Item.mNone) and (numDrops < MaxDrops) then
2468- drops[numDrops].at := x.pos;
2469- if x.typ^.tuple then drops[numDrops].n := INT(x.typ^.len); else drops[numDrops].n := 1; end;
2470- INC(numDrops);
2524+ if (x.mode # Item.mNone) and (numDoubts < MaxDoubts) then
2525+ doubts[numDoubts].at := x.pos;
2526+ doubts[numDoubts].msg := "";
2527+ if x.typ^.tuple then doubts[numDoubts].n := INT(x.typ^.len); else doubts[numDoubts].n := 1; end;
2528+ INC(numDoubts);
24712529 end;
24722530 end;
24732531
@@ -2955,7 +3013,7 @@
29553013 proc body(p: PObj; sig: PType; line: INT): ERROR;
29563014 var
29573015 q, params, last, o, before: PObj; old: PType; st: Scan.State;
2958- mark: Gen.CodeMark; drops0, errs: INT;
3016+ mark: Gen.CodeMark; doubts0, errs: INT;
29593017 begin
29603018 try Sym.openScope(Sym.newScope());
29613019 params := nil; last := nil;
@@ -2986,7 +3044,7 @@
29863044 placeLocals(params, before);
29873045 (* the body, made again if a variable in a register must be in memory *)
29883046 Scan.saveScan(st);
2989- Gen.markCode(mark); drops0 := numDrops; errs := Diag.errors;
3047+ Gen.markCode(mark); doubts0 := numDoubts; errs := Diag.errors;
29903048 old := curResult;
29913049 repeat
29923050 Item.setDemoted(false);
@@ -3008,7 +3066,7 @@
30083066 o := o^.next;
30093067 end;
30103068 end;
3011- numDrops := drops0;
3069+ numDoubts := doubts0;
30123070 Gen.redo(mark);
30133071 placeLocals(params, before);
30143072 Scan.restoreScan(st);
@@ -3798,20 +3856,25 @@
37983856
37993857 (* The warnings of module m, by where each declaration is: its own
38003858 declarations and its procedures' are two lists, each in order, and
3801- firstDrop on are the calls that drop results. The
3859+ firstDoubt on are the calls that drop results and the other doubts. The
38023860 instances of a generic module share its text, and so its warnings:
38033861 only the first is warned of. *)
3804-proc warnings(m: PModule; firstDrop: INT);
3862+proc warnings(m: PModule; firstDoubt: INT);
38053863 var a, b: PObj; n: PModule; i: INT;
38063864 begin
38073865 n := firstModule;
38083866 while (n # nil) and (n^.name # m^.name) do n := n^.next; end;
38093867 if n # nil then return; end;
38103868 a := m^.first; b := m^.locals;
3811- i := firstDrop;
3812- while (a # nil) or (b # nil) or (i < numDrops) do
3813- if (i < numDrops) and ((a = nil) or (drops[i].at.a < a^.pos.a)) and ((b = nil) or (drops[i].at.a < b^.pos.a)) then
3814- dropped(drops[i].at, drops[i].n);
3869+ i := firstDoubt;
3870+ while (a # nil) or (b # nil) or (i < numDoubts) do
3871+ if (i < numDoubts) and ((a = nil) or (doubts[i].at.a < a^.pos.a)) and ((b = nil) or (doubts[i].at.a < b^.pos.a)) then
3872+ if LEN(doubts[i].msg) = 0 then
3873+ dropped(doubts[i].at, doubts[i].n);
3874+ else
3875+ Diag.warning(doubts[i].at, doubts[i].msg);
3876+ Diag.help(doubts[i].hint);
3877+ end;
38153878 INC(i);
38163879 elsif (b = nil) or ((a # nil) and (a^.pos.a < b^.pos.a)) then
38173880 warn(a);
@@ -3869,9 +3932,9 @@
38693932 st: Scan.State; saved: [Sym.MaxDepth]INT; d: INT;
38703933 oldMod: PModule; oldCheck, oldChecks: BOOL; m: PModule;
38713934 lib: ^Sym.Lib; start: Diag.Pos; alias, mname, line, oldPath: INT; o: PObj; bodyChunk: INT; iargs: Sym.Args;
3872- generic: BOOL; argsAt: Scan.State; why: []U8; firstDrop: INT;
3935+ generic: BOOL; argsAt: Scan.State; why: []U8; firstDoubt: INT;
38733936 begin
3874- firstDrop := numDrops;
3937+ firstDoubt := numDoubts;
38753938 Scan.saveScan(st);
38763939 d := Sym.saveScopes(saved[0..Sym.MaxDepth]);
38773940 oldMod := Sym.curModule; oldCheck := checkMode; oldPath := Chunk.curFilePath(); oldChecks := Item.checks;
@@ -4020,8 +4083,8 @@
40204083 end;
40214084 o := o^.chain;
40224085 end;
4023- if (Diag.errors = 0) and not checkOnly then warnings(m, firstDrop); end;
4024- numDrops := firstDrop;
4086+ if (Diag.errors = 0) and not checkOnly then warnings(m, firstDoubt); end;
4087+ numDoubts := firstDoubt;
40254088 if Diag.errors > 0 then Diag.finish(); end; (* a module with errors stops the compiler after it *)
40264089 Sym.closeScope();
40274090 m^.done := true;

src/Scan.raz (history) modified +21 -0

@@ -282,6 +282,27 @@
282282 return {f: cur.f, a: cur.commentEnd - 2, b: cur.commentEnd}, true;
283283 end;
284284
285+(* Whether the string at the scanner has a \0 before a
286+ digit, '0' to '7': where, and true. A zero byte and the digit are two
287+ characters, but a C reader sees one octal escape. A raw string has no
288+ escapes. *)
289+proc zeroDigit*: (Diag.Pos, BOOL);
290+var i: INT;
291+begin
292+ i := cur.tokPos;
293+ if (cur.buf[i] # '"') and (cur.buf[i] # '\'') then return {}, false; end;
294+ while i < cur.pos do
295+ if cur.buf[i] # '\\' then
296+ INC(i);
297+ elsif (cur.buf[i + 1] = '0') and (cur.buf[i + 2] >= '0') and (cur.buf[i + 2] <= '7') then
298+ return {f: cur.f, a: i, b: i + 3}, true;
299+ else
300+ INC(i, 2); (* an escape: the character after the backslash is not one *)
301+ end;
302+ end;
303+ return {}, false;
304+end;
305+
285306 (* From the start of p to the end of the symbol before the current one:
286307 the whole of a construct that has just been read *)
287308 proc spanFrom*(p: Diag.Pos): Diag.Pos;

test/err/BoolRel.args (history) added +1 -0

@@ -0,0 +1 @@
1+-Werror

test/err/BoolRel.err (history) added +9 -0

@@ -0,0 +1,9 @@
1+test/err/BoolRel.raz:13:10: error: a # b is read first: = and # bind tighter than and
2+ 13 | a := a # b and c;
3+ | ^^^^^
4+help: put the = or # of two BOOLs in parentheses
5+test/err/BoolRel.raz:14:15: error: b = c is read first: = and # bind tighter than or
6+ 14 | a := a or b = c;
7+ | ^^^^^
8+help: put the = or # of two BOOLs in parentheses
9+raz: 2 errors

test/err/BoolRel.raz (history) added +16 -0

@@ -0,0 +1,16 @@
1+module BoolRel;
2+(* A = or # of two BOOLs beside and or or binds first, as written; the
3+ others, in parentheses or of integers, are quiet. *)
4+var a, b, c: BOOL; i, n: INT;
5+proc main*;
6+begin
7+ a := (b = c) and a;
8+ a := a and (b # c);
9+ a := (b # c) or (a = b) and c;
10+ a := (i < n) and (i # 0);
11+ a := i < n and i # 0;
12+ a := b = c;
13+ a := a # b and c;
14+ a := a or b = c;
15+end;
16+end BoolRel.

test/err/ZeroDigit.args (history) added +1 -0

@@ -0,0 +1 @@
1+-Werror

test/err/ZeroDigit.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+test/err/ZeroDigit.raz:12:13: error: \0 is a zero byte and the digit after it is itself
2+ 12 | s := "\0\01234567890-=";
3+ | ^^^
4+help: write \x00 to keep them apart
5+raz: 1 error

test/err/ZeroDigit.raz (history) added +14 -0

@@ -0,0 +1,14 @@
1+module ZeroDigit;
2+(* \0 before a digit is a warning; \x00, \0 at the end, \0 before a
3+ letter, an escaped backslash before 0 and a raw string are quiet. *)
4+var s*: []U8; c*: U8;
5+proc main*;
6+begin
7+ s := "a\x0012";
8+ s := "ab\0";
9+ s := "\0x\\01";
10+ s := `\01`;
11+ c := '\0';
12+ s := "\0\01234567890-=";
13+end;
14+end ZeroDigit.