try: return an error that is not nil from the procedure it stands in Report 2026-10.20, section 7.8: try c calls c, whose last result is an ERROR, and if that is not nil the procedure returns it, with zeros for its other results, after the deferred statements; else it is the call's other results. A statement, an operand, or the right of a multiple assignment. try is a reserved word. The seed: sTry in scan, TryCall in parse, TryAlign and TryReturn in gen, which make no entry on the stack of values, so try can stand in the middle of an expression. conform/ has Try and twelve errors; test/err has the dropped values. The tutorial's Errors and the tour's Layers use it. The keyword lists of tools, self/, vim, emacs, the site and raz(7) know try.
| @@ -102,7 +102,8 @@ | ||
| 102 | 102 | defect, and the library grows only by what breaks no program written |
| 103 | 103 | for it. A wish for more, the owner's or yours, goes on the list in |
| 104 | 104 | `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, |
| 105 | -by the owner's word: report 2026-10.12; the compiler follows 2026-10.19, | |
| 105 | +by the owner's word: report 2026-10.12; the compiler follows 2026-10.20, whose `try` returns an error that isn't | |
| 106 | +nil from the procedure it stands in (10.20, the owner's word), and 2026-10.19, | |
| 106 | 107 | whose `Sys` writes without waiting, for rat's pastes (10.13), and |
| 107 | 108 | whose blank, `_ := f(x)`, drops a result that is otherwise warned of |
| 108 | 109 | (10.14), and whose `Math` has `pow`, the logarithms to 2 and 10, |
| @@ -17,6 +17,10 @@ | ||
| 17 | 17 | compiler lives in `self/`, one raz module to a seed unit, the same |
| 18 | 18 | names, and has its own targets; nothing in `src/` or `lib/` changes. |
| 19 | 19 | |
| 20 | +The language gained `try` (report 2026-10.20) for this: the parser's | |
| 21 | +errors will return an `ERROR` that `try` passes up, and `self/Jump.raz`, | |
| 22 | +which the scanner's `fail` uses now, goes when step 4 lands. | |
| 23 | + | |
| 20 | 24 | Each unit is ported in the seed's order (leaves first) and tested on |
| 21 | 25 | its own against the seed's, on the same inputs, since no program can |
| 22 | 26 | link both. The language and the library are frozen: a gap found goes |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module TryConversion; | |
| 2 | +var n: INT; | |
| 3 | +proc g: ERROR; | |
| 4 | +begin | |
| 5 | + n := try INT(n); | |
| 6 | + return nil; | |
| 7 | +end; | |
| 8 | +end TryConversion. |
| @@ -0,0 +1,12 @@ | ||
| 1 | +module TryCount; | |
| 2 | +proc f: (INT, ERROR); | |
| 3 | +begin | |
| 4 | + return 1, nil; | |
| 5 | +end; | |
| 6 | +proc g: ERROR; | |
| 7 | +var a, b: INT; | |
| 8 | +begin | |
| 9 | + a, b := try f(); | |
| 10 | + return nil; | |
| 11 | +end; | |
| 12 | +end TryCount. |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module TryDeferred; | |
| 2 | +proc f: ERROR; | |
| 3 | +begin | |
| 4 | + return nil; | |
| 5 | +end; | |
| 6 | +proc g: ERROR; | |
| 7 | +begin | |
| 8 | + defer try f(); | |
| 9 | + return nil; | |
| 10 | +end; | |
| 11 | +end TryDeferred. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module TryInBody; | |
| 2 | +proc f: ERROR; | |
| 3 | +begin | |
| 4 | + return nil; | |
| 5 | +end; | |
| 6 | +begin | |
| 7 | + try f(); | |
| 8 | +end TryInBody. |
| @@ -0,0 +1,10 @@ | ||
| 1 | +module TryInMain; | |
| 2 | +proc f: ERROR; | |
| 3 | +begin | |
| 4 | + return nil; | |
| 5 | +end; | |
| 6 | +proc main*; | |
| 7 | +begin | |
| 8 | + try f(); | |
| 9 | +end; | |
| 10 | +end TryInMain. |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module TryNeedsArguments; | |
| 2 | +proc f(n: INT): ERROR; | |
| 3 | +begin | |
| 4 | + return nil; | |
| 5 | +end; | |
| 6 | +proc g: ERROR; | |
| 7 | +begin | |
| 8 | + try f; | |
| 9 | + return nil; | |
| 10 | +end; | |
| 11 | +end TryNeedsArguments. |
| @@ -0,0 +1,12 @@ | ||
| 1 | +module TryNoValue; | |
| 2 | +proc f: ERROR; | |
| 3 | +begin | |
| 4 | + return nil; | |
| 5 | +end; | |
| 6 | +proc g: ERROR; | |
| 7 | +var n: INT; | |
| 8 | +begin | |
| 9 | + n := try f(); | |
| 10 | + return nil; | |
| 11 | +end; | |
| 12 | +end TryNoValue. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module TryNotACall; | |
| 2 | +var n: INT; | |
| 3 | +proc g: ERROR; | |
| 4 | +begin | |
| 5 | + try n; | |
| 6 | + return nil; | |
| 7 | +end; | |
| 8 | +end TryNotACall. |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module TryNotError; | |
| 2 | +proc f: INT; | |
| 3 | +begin | |
| 4 | + return 1; | |
| 5 | +end; | |
| 6 | +proc g: ERROR; | |
| 7 | +begin | |
| 8 | + try f; | |
| 9 | + return nil; | |
| 10 | +end; | |
| 11 | +end TryNotError. |
| @@ -0,0 +1,10 @@ | ||
| 1 | +module TryReturnsNothing; | |
| 2 | +proc f; | |
| 3 | +begin | |
| 4 | +end; | |
| 5 | +proc g: ERROR; | |
| 6 | +begin | |
| 7 | + try f(); | |
| 8 | + return nil; | |
| 9 | +end; | |
| 10 | +end TryReturnsNothing. |
| @@ -0,0 +1,12 @@ | ||
| 1 | +module TrySeveralValues; | |
| 2 | +proc f: (INT, INT, ERROR); | |
| 3 | +begin | |
| 4 | + return 1, 2, nil; | |
| 5 | +end; | |
| 6 | +proc g: ERROR; | |
| 7 | +var n: INT; | |
| 8 | +begin | |
| 9 | + n := try f(); | |
| 10 | + return nil; | |
| 11 | +end; | |
| 12 | +end TrySeveralValues. |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module TryWrongResults; | |
| 2 | +proc f: ERROR; | |
| 3 | +begin | |
| 4 | + return nil; | |
| 5 | +end; | |
| 6 | +proc g: (INT, BOOL); | |
| 7 | +begin | |
| 8 | + try f(); | |
| 9 | + return 1, true; | |
| 10 | +end; | |
| 11 | +end TryWrongResults. |
| @@ -0,0 +1,23 @@ | ||
| 1 | +one 4: 0 ok, log 92 | |
| 2 | +one -1: 0 bad, log 0 | |
| 3 | +one 0: 0 worse, log 0 | |
| 4 | +two 8: 13 ok, log 0 | |
| 5 | +two 0: 0 worse, log 0 | |
| 6 | +many 3: 9 ok, log 0 | |
| 7 | +15 | |
| 8 | +many -2: 0 bad, log 0 | |
| 9 | +0 | |
| 10 | +whole 4: 49 ok, log 0 | |
| 11 | +whole -4: 0 bad, log 0 | |
| 12 | +deferred 6: 103 ok, log 21 | |
| 13 | +deferred 0: 0 worse, log 21 | |
| 14 | +among 4 6: 123 ok, log 3 | |
| 15 | +among 4 0: 0 worse, log 3 | |
| 16 | +among 0 6: 0 worse, log 3 | |
| 17 | +shortcut 3: 0 bad, log 0 | |
| 18 | +shortcut 0: 0 ok, log 0 | |
| 19 | +through 3: 0 ok, log 0 | |
| 20 | +through -3: 0 bad, log 0 | |
| 21 | +loop 5 3: 12 ok, log 0 | |
| 22 | +loop 2 3: 0 ok, log 0 | |
| 23 | +loop 5 -2: 0 bad, log 0 |
| @@ -0,0 +1,178 @@ | ||
| 1 | +module Try; | |
| 2 | +(* try: an error that isn't nil is returned, with zeros for the other | |
| 3 | + results; else the call's other results are the value. Every shape of | |
| 4 | + results, in expressions, among arguments, past deferred statements. *) | |
| 5 | +import Out; | |
| 6 | + | |
| 7 | +const | |
| 8 | + Bad*: ERROR = "bad"; | |
| 9 | + Worse*: ERROR = "worse"; | |
| 10 | + | |
| 11 | +type Pair = record a, b: I64; end; | |
| 12 | + | |
| 13 | +var | |
| 14 | + log: INT; (* the deferred statements run, as digits *) | |
| 15 | + handler: proc(n: INT): ERROR; | |
| 16 | + | |
| 17 | +(* fails when n is negative *) | |
| 18 | +proc check(n: INT): ERROR; | |
| 19 | +begin | |
| 20 | + if n < 0 then return Bad; end; | |
| 21 | + return nil; | |
| 22 | +end; | |
| 23 | + | |
| 24 | +(* two words: a value, and Worse when n is zero *) | |
| 25 | +proc half(n: INT): (INT, ERROR); | |
| 26 | +begin | |
| 27 | + if n = 0 then return 7, Worse; end; | |
| 28 | + return n div 2, nil; | |
| 29 | +end; | |
| 30 | + | |
| 31 | +(* several results, in a temporary *) | |
| 32 | +proc three(n: INT): (I64, I64, ERROR); | |
| 33 | +begin | |
| 34 | + if n < 0 then return 5, 6, Bad; end; | |
| 35 | + return I64(n), I64(n * 2), nil; | |
| 36 | +end; | |
| 37 | + | |
| 38 | +proc real(n: INT): (F64, ERROR); | |
| 39 | +begin | |
| 40 | + if n < 0 then return 1.5, Bad; end; | |
| 41 | + return F64(n) / 2.0, nil; | |
| 42 | +end; | |
| 43 | + | |
| 44 | +proc pair(n: INT): (Pair, ERROR); | |
| 45 | +begin | |
| 46 | + if n < 0 then return {a: 1, b: 2}, Bad; end; | |
| 47 | + return {a: I64(n), b: I64(n + 1)}, nil; | |
| 48 | +end; | |
| 49 | + | |
| 50 | +proc add(a, b, c: INT): INT; | |
| 51 | +begin | |
| 52 | + return a * 100 + b * 10 + c; | |
| 53 | +end; | |
| 54 | + | |
| 55 | +proc yes(n: INT): (BOOL, ERROR); | |
| 56 | +begin | |
| 57 | + if n < 0 then return true, Bad; end; | |
| 58 | + return n > 5, nil; | |
| 59 | +end; | |
| 60 | + | |
| 61 | +proc setup: ERROR; | |
| 62 | +begin | |
| 63 | + log := log * 10 + 9; | |
| 64 | + return nil; | |
| 65 | +end; | |
| 66 | + | |
| 67 | +(* the enclosing procedure ends in a lone ERROR *) | |
| 68 | +proc one(n: INT): ERROR; | |
| 69 | +var h: INT; | |
| 70 | +begin | |
| 71 | + try check(n); | |
| 72 | + h := try half(n); | |
| 73 | + try setup; | |
| 74 | + log := log * 10 + h; | |
| 75 | + return nil; | |
| 76 | +end; | |
| 77 | + | |
| 78 | +(* in two words *) | |
| 79 | +proc two(n: INT): (INT, ERROR); | |
| 80 | +begin | |
| 81 | + return 1 + try half(n) * 3, nil; | |
| 82 | +end; | |
| 83 | + | |
| 84 | +(* behind a pointer *) | |
| 85 | +proc many(n: INT): (I64, F64, ERROR); | |
| 86 | +var a, b: I64; r: F64; | |
| 87 | +begin | |
| 88 | + a, b := try three(n); | |
| 89 | + r := try real(n); | |
| 90 | + return a + b, r, nil; | |
| 91 | +end; | |
| 92 | + | |
| 93 | +(* a record result, so the results of the caller are behind a pointer *) | |
| 94 | +proc whole(n: INT): (Pair, I64, ERROR); | |
| 95 | +var p: Pair; | |
| 96 | +begin | |
| 97 | + p := try pair(n); | |
| 98 | + return p, p.a + p.b, nil; | |
| 99 | +end; | |
| 100 | + | |
| 101 | +(* deferred statements run on the way out, the error unchanged *) | |
| 102 | +proc deferred(n: INT): (INT, ERROR); | |
| 103 | +begin | |
| 104 | + defer log := log * 10 + 1; | |
| 105 | + defer log := log * 10 + 2; | |
| 106 | + return try half(n) + 100, nil; | |
| 107 | +end; | |
| 108 | + | |
| 109 | +(* among the arguments of another call, with a value pushed already *) | |
| 110 | +proc among(n, m: INT): (INT, ERROR); | |
| 111 | +begin | |
| 112 | + defer log := log * 10 + 3; | |
| 113 | + return add(1, try half(n), try half(m)), nil; | |
| 114 | +end; | |
| 115 | + | |
| 116 | +(* only when its operand is evaluated *) | |
| 117 | +proc shortcut(n: INT): (BOOL, ERROR); | |
| 118 | +begin | |
| 119 | + return (n > 0) and try yes(n - 1 - 100), nil; | |
| 120 | +end; | |
| 121 | + | |
| 122 | +proc through(n: INT): ERROR; | |
| 123 | +begin | |
| 124 | + handler := check; | |
| 125 | + try handler(n); | |
| 126 | + return nil; | |
| 127 | +end; | |
| 128 | + | |
| 129 | +proc loop(n, off: INT): (INT, ERROR); | |
| 130 | +var i, sum: INT; | |
| 131 | +begin | |
| 132 | + for i := 1 to n do | |
| 133 | + if try yes(i + off) then sum := sum + try half(i * 2); end; | |
| 134 | + end; | |
| 135 | + return sum, nil; | |
| 136 | +end; | |
| 137 | + | |
| 138 | +proc flag(b: BOOL): INT; | |
| 139 | +begin | |
| 140 | + if b then return 1; end; | |
| 141 | + return 0; | |
| 142 | +end; | |
| 143 | + | |
| 144 | +proc show(what: []U8; v: INT; e: ERROR); | |
| 145 | +begin | |
| 146 | + Out.str(what); Out.str(": "); Out.int(v); | |
| 147 | + if e = nil then Out.str(" ok"); else Out.str(" "); Out.str(MSG(e)); end; | |
| 148 | + Out.str(", log "); Out.int(log); Out.ln; | |
| 149 | + log := 0; | |
| 150 | +end; | |
| 151 | + | |
| 152 | +proc main*; | |
| 153 | +var v: INT; e: ERROR; a: I64; f: F64; p: Pair; b: BOOL; | |
| 154 | +begin | |
| 155 | + e := one(4); show("one 4", 0, e); | |
| 156 | + e := one(-1); show("one -1", 0, e); | |
| 157 | + e := one(0); show("one 0", 0, e); | |
| 158 | + v, e := two(8); show("two 8", v, e); | |
| 159 | + v, e := two(0); show("two 0", v, e); | |
| 160 | + a, f, e := many(3); show("many 3", INT(a), e); Out.int(INT(f * 10.0)); Out.ln; | |
| 161 | + a, f, e := many(-2); show("many -2", INT(a), e); Out.int(INT(f * 10.0)); Out.ln; | |
| 162 | + p, a, e := whole(4); show("whole 4", INT(p.a * 10 + a), e); | |
| 163 | + p, a, e := whole(-4); show("whole -4", INT(p.a * 10 + a), e); | |
| 164 | + v, e := deferred(6); show("deferred 6", v, e); | |
| 165 | + v, e := deferred(0); show("deferred 0", v, e); | |
| 166 | + v, e := among(4, 6); show("among 4 6", v, e); | |
| 167 | + v, e := among(4, 0); show("among 4 0", v, e); | |
| 168 | + v, e := among(0, 6); show("among 0 6", v, e); | |
| 169 | + b, e := shortcut(3); show("shortcut 3", flag(b), e); | |
| 170 | + b, e := shortcut(0); show("shortcut 0", flag(b), e); | |
| 171 | + e := through(3); show("through 3", 0, e); | |
| 172 | + e := through(-3); show("through -3", 0, e); | |
| 173 | + v, e := loop(5, 3); show("loop 5 3", v, e); | |
| 174 | + v, e := loop(2, 3); show("loop 2 3", v, e); | |
| 175 | + v, e := loop(5, -2); show("loop 5 -2", v, e); | |
| 176 | +end; | |
| 177 | + | |
| 178 | +end Try. |
An empty file.
| @@ -1026,6 +1026,31 @@ | ||
| 1026 | 1026 | the end, `return` and `break`. A `return`'s result waits in the frame |
| 1027 | 1027 | meanwhile, so that gen's stack is empty when they run. |
| 1028 | 1028 | |
| 1029 | +`try` (report 7.8) is `TryCall`, for the statement, an operand | |
| 1030 | +(`Factor`) and the right of a multiple assignment (`MultiAssign`). It | |
| 1031 | +reads the call as `Designator` does, takes the error, the last of its | |
| 1032 | +results, out of the way into a slot of the frame, and leaves in `x` | |
| 1033 | +the results that are left: none, one value, or a tuple of the others, | |
| 1034 | +`WithoutLast`. Then, in the code before and after, this: | |
| 1035 | + | |
| 1036 | +``` | |
| 1037 | + load [slot]; jz ok ; JumpIf(ok, false) | |
| 1038 | + and rsp, -16 ; TryAlign, if a deferred statement is in force | |
| 1039 | + call deferred... ; RunDefers(0), as for a return | |
| 1040 | + mov rax/rdx or the results ; TryReturn(slot): zeros, and the error last | |
| 1041 | + jmp exit | |
| 1042 | +ok: ... | |
| 1043 | +``` | |
| 1044 | + | |
| 1045 | +Unlike `return`, which is a statement and finds gen's stack empty, | |
| 1046 | +`try` can stand in the middle of an expression, with values waiting | |
| 1047 | +on gen's stack and quadwords pushed for another call's arguments. The | |
| 1048 | +out-of-line code therefore makes no entry on the stack: `TryReturn` | |
| 1049 | +loads registers and jumps, and `TryAlign` makes the machine's stack | |
| 1050 | +even for the deferred statements, which expect it so. The code that | |
| 1051 | +follows is the code for an error that is nil, with the stack as it | |
| 1052 | +was, so no model needs resetting. | |
| 1053 | + | |
| 1029 | 1054 | Then declarations: `Declarations` reads `const`, `type`, `var`, `proc` |
| 1030 | 1055 | and `extern`, each one through `Declaration`, a recovery point, which |
| 1031 | 1056 | calls `ConstDecl`, `TypeDecl`, `VarDecl`, `ProcDecl` or `ExternDecl`. `ProcDecl` handles forward declarations, code procedures |
| @@ -513,6 +513,16 @@ | ||
| 513 | 513 | return INT(n), nil; |
| 514 | 514 | end; |
| 515 | 515 | |
| 516 | +(* the sum of the numbers in words: the first error ends it *) | |
| 517 | +proc total(words: [][]U8): (INT, ERROR); | |
| 518 | +var i, sum: INT; | |
| 519 | +begin | |
| 520 | + for i := 0 to LEN(words) - 1 do | |
| 521 | + sum := sum + try number(words[i]); | |
| 522 | + end; | |
| 523 | + return sum, nil; | |
| 524 | +end; | |
| 525 | + | |
| 516 | 526 | proc main*; |
| 517 | 527 | var words: [3][]U8; i, n: INT; err: ERROR; |
| 518 | 528 | begin |
| @@ -525,6 +535,10 @@ | ||
| 525 | 535 | end; |
| 526 | 536 | Out.ln; |
| 527 | 537 | end; |
| 538 | + n, err := total(["1", "2", "3"]); | |
| 539 | + Out.int(n); Out.ln; | |
| 540 | + n, err := total(words); | |
| 541 | + if err # nil then Out.str(MSG(err)); Out.ln; end; | |
| 528 | 542 | end; |
| 529 | 543 | |
| 530 | 544 | end Errors. |
| @@ -533,6 +547,8 @@ | ||
| 533 | 547 | 42 |
| 534 | 548 | empty |
| 535 | 549 | not a number up to 1000 |
| 550 | +6 | |
| 551 | +nothing to read | |
| 536 | 552 | .Ed |
| 537 | 553 | .Pp |
| 538 | 554 | A procedure that can fail returns an |
| @@ -550,6 +566,30 @@ | ||
| 550 | 566 | An exported one, as |
| 551 | 567 | .Cm Empty |
| 552 | 568 | is here, lets the callers tell it from the others. |
| 569 | +.Pp | |
| 570 | +A caller that can't deal with an error passes it on to its own caller | |
| 571 | +with | |
| 572 | +.Cm try , | |
| 573 | +as | |
| 574 | +.Cm total | |
| 575 | +does: if the call returned an error that isn't | |
| 576 | +.Cm nil , | |
| 577 | +the procedure | |
| 578 | +.Cm try | |
| 579 | +stands in returns that error, with zero for its other results, and | |
| 580 | +otherwise | |
| 581 | +.Cm try | |
| 582 | +is the call's other results, here a number. | |
| 583 | +.Cm try | |
| 584 | +is a | |
| 585 | +.Cm return , | |
| 586 | +written where the call is: it has no handler, keeps no state and | |
| 587 | +unwinds nothing, so every place a procedure can leave is in its text. | |
| 588 | +The procedure it stands in must itself end in an | |
| 589 | +.Cm ERROR , | |
| 590 | +so | |
| 591 | +.Cm main , | |
| 592 | +which returns none, tests each error and deals with it. | |
| 553 | 593 | raz has no exceptions. |
| 554 | 594 | .Sh 9. DEFER |
| 555 | 595 | .Bd -literal -offset indent |
| @@ -63,8 +63,8 @@ | ||
| 63 | 63 | .Bd -literal -offset indent |
| 64 | 64 | and begin break by code const defer div do else elsif end |
| 65 | 65 | extern for forward if import in link match mod module not of |
| 66 | -or packed proc record repeat return set shl shr then to type | |
| 67 | -until var variant VOLATILE while xor | |
| 66 | +or packed proc record repeat return set shl shr then to try | |
| 67 | +type until var variant VOLATILE while xor | |
| 68 | 68 | .Ed |
| 69 | 69 | .Pp |
| 70 | 70 | The basic types, |
| @@ -9,12 +9,13 @@ | ||
| 9 | 9 | This report defines the language. It is short on purpose. Whatever it |
| 10 | 10 | doesn't allow, raz doesn't have. |
| 11 | 11 | |
| 12 | -This is version 2026-10.19 of the report; section 12 says what each | |
| 12 | +This is version 2026-10.20 of the report; section 12 says what each | |
| 13 | 13 | version changed. 2026-10.11 and 2026-10.12 are a thaw the owner chose, |
| 14 | 14 | for a language anyone can build again. They break programs written |
| 15 | 15 | for 2026-10.10 and before: for 2026-10.9, say, the report of compiler |
| 16 | 16 | 0.5.0, the last release before them; and 2026-10.12 breaks code |
| 17 | -procedures written for 2026-10.11. | |
| 17 | +procedures written for 2026-10.11. 2026-10.20 is a third, for `try` | |
| 18 | +(section 7.8); it breaks a program that has a name `try`. | |
| 18 | 19 | |
| 19 | 20 | The language is frozen, softly, from this version on: it changes only |
| 20 | 21 | to mend a defect, and its library grows only by what breaks no program |
| @@ -59,8 +60,8 @@ | ||
| 59 | 60 | ``` |
| 60 | 61 | and begin break by code const defer div do else elsif end extern for |
| 61 | 62 | forward if import in link match mod module not of or packed proc record |
| 62 | -repeat return set shl shr then to type until var variant VOLATILE while | |
| 63 | -xor | |
| 63 | +repeat return set shl shr then to try type until var variant VOLATILE | |
| 64 | +while xor | |
| 64 | 65 | ``` |
| 65 | 66 | |
| 66 | 67 | `static`, in `link static;` (section 3), isn't one. |
| @@ -499,6 +500,10 @@ | ||
| 499 | 500 | const NotFound*: ERROR = "not found"; |
| 500 | 501 | ``` |
| 501 | 502 | |
| 503 | +An `ERROR` is how a procedure says it failed: by convention the last | |
| 504 | +of its results, and `nil` when nothing failed. A caller that can't | |
| 505 | +deal with a failure itself passes it on with `try` (section 7.8). | |
| 506 | + | |
| 502 | 507 | `ERROR` is a basic type as big as an address. A value of it is made only |
| 503 | 508 | by a constant declared as above, and each such declaration makes an |
| 504 | 509 | error different from every other, whatever its message: two modules that |
| @@ -588,15 +593,16 @@ | ||
| 588 | 593 | | "not" Factor | "~" Factor |
| 589 | 594 | | "[" [ Expr { "," Expr } ] "]" |
| 590 | 595 | | "{" [ Element { "," Element } ] "}" |
| 591 | - | "^" [ "VOLATILE" ] Type "(" Expr ")" . | |
| 596 | + | "^" [ "VOLATILE" ] Type "(" Expr ")" | |
| 597 | + | "try" Designator . | |
| 592 | 598 | Element = Expr | ident ":" Expr . |
| 593 | 599 | Designator = QualIdent { Selector } . |
| 594 | 600 | Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" |
| 595 | 601 | | "(" [ Expr { "," Expr } ] ")" . |
| 596 | 602 | ``` |
| 597 | 603 | |
| 598 | -The operators bind in the order of the grammar: `not` and `~` most | |
| 599 | -tightly, then the multiplying operators, then the adding ones, then | |
| 604 | +The operators bind in the order of the grammar: `not`, `~` and `try` | |
| 605 | +(section 7.8) most tightly, then the multiplying operators, then the adding ones, then | |
| 600 | 606 | the relations, then `and`, then `or`. So `a + b * c = d` is |
| 601 | 607 | `(a + (b * c)) = d`, `x & 0FH = 0` is `(x & 0FH) = 0`, and |
| 602 | 608 | `i < n and a[i] # 0 or done` is `((i < n) and (a[i] # 0)) or done`. A |
| @@ -607,7 +613,7 @@ | ||
| 607 | 613 | |
| 608 | 614 | A pointer to a record isn't the record: its fields are `p^.f`, never |
| 609 | 615 | `p.f`, and the elements of a pointer to an array are `p^[i]`. A |
| 610 | -statement starts with a name, so `^T(x)^ := v` isn't one. | |
| 616 | +statement starts with a name or `try`, so `^T(x)^ := v` isn't one. | |
| 611 | 617 | |
| 612 | 618 | ### 6.1 Integers |
| 613 | 619 | |
| @@ -819,7 +825,8 @@ | ||
| 819 | 825 | | "match" Expr "of" { "|" Arm } "end" |
| 820 | 826 | | "return" [ Expr { "," Expr } ] |
| 821 | 827 | | "break" |
| 822 | - | "defer" Statement . | |
| 828 | + | "defer" Statement | |
| 829 | + | "try" Designator . | |
| 823 | 830 | Arm = ( ident [ "(" ident { "," ident } ")" ] |
| 824 | 831 | | Label { "," Label } | "else" ) ":" Statements . |
| 825 | 832 | Label = Expr [ ".." Expr ] . |
| @@ -983,10 +990,105 @@ | ||
| 983 | 990 | ``` |
| 984 | 991 | |
| 985 | 992 | `return e;` evaluates `e` before the deferred statements run. A |
| 986 | -deferred statement can't contain `return`, `break` or `defer` that | |
| 987 | -would leave it. A failed check, `Rt.exit` and `Sys.exit` end the | |
| 993 | +deferred statement can't contain `return`, `break`, `defer` or `try` | |
| 994 | +that would leave it. A failed check, `Rt.exit` and `Sys.exit` end the | |
| 988 | 995 | program without running the deferred statements still pending. |
| 996 | + | |
| 997 | +### 7.8 Try | |
| 998 | + | |
| 999 | +A procedure that can fail returns an `ERROR` as its last result | |
| 1000 | +(section 5.8), `nil` when it didn't. A caller that can't deal with the | |
| 1001 | +failure passes it on to its own caller, which `try` does in a word: | |
| 989 | 1002 | |
| 1003 | +``` | |
| 1004 | +proc total(words: [][]U8): (INT, ERROR); | |
| 1005 | +var i, sum: INT; | |
| 1006 | +begin | |
| 1007 | + for i := 0 to LEN(words) - 1 do | |
| 1008 | + sum := sum + try number(words[i]); | |
| 1009 | + end; | |
| 1010 | + return sum, nil; | |
| 1011 | +end; | |
| 1012 | +``` | |
| 1013 | + | |
| 1014 | +`try c`, where `c` is a call (below) of a procedure whose last result | |
| 1015 | +is an `ERROR`, makes the call and looks at that error. If it is `nil`, | |
| 1016 | +the program goes on, and `try c` is what `c` returned without the | |
| 1017 | +error. If it isn't, the procedure `try` stands in returns at once, as | |
| 1018 | +`return` does (section 7.3): its last result is that error, and every | |
| 1019 | +other result is zero, as a variable of its type starts (section 4). | |
| 1020 | +Nothing more of the statement `try` stands in is done; in the example, | |
| 1021 | +`sum` isn't assigned. So | |
| 1022 | + | |
| 1023 | +``` | |
| 1024 | +x := try f(a); | |
| 1025 | +``` | |
| 1026 | + | |
| 1027 | +is, with `e` a variable the program can't name, and `0` standing for | |
| 1028 | +a zero of each of the procedure's other results: | |
| 1029 | + | |
| 1030 | +``` | |
| 1031 | +x, e := f(a); | |
| 1032 | +if e # nil then return 0, e; end; | |
| 1033 | +``` | |
| 1034 | + | |
| 1035 | +The error is the one `c` returned, and compares with `=` as it did | |
| 1036 | +there. `try` has no handler and keeps no state: it is a `return`, | |
| 1037 | +written where the call is, so that every place a procedure can leave is | |
| 1038 | +in its text. A program that means to deal with an error tests it, as | |
| 1039 | +section 5.8 shows. | |
| 1040 | + | |
| 1041 | +**What `try` is applied to.** `c` is a designator that ends in a call: | |
| 1042 | +`f(x)`, `Mod.f(x)`, `p^.next(x)`, or a call through a variable of | |
| 1043 | +procedure type. Where the procedure has no parameters its parentheses | |
| 1044 | +may be left out, as in a statement (section 7.1), and `try` calls it: | |
| 1045 | +`try setup;`. A conversion, a built-in procedure (section 6.8) and the | |
| 1046 | +case of a variant are written like calls and aren't procedures'; `try` | |
| 1047 | +can't be applied to them. | |
| 1048 | + | |
| 1049 | +**What `try c` is.** `c`'s results but the last. | |
| 1050 | + | |
| 1051 | +- With none, `try c` isn't a value and stands only as a statement: | |
| 1052 | + `try flush;`. | |
| 1053 | +- With one, it is a value of that type, an operand wherever `c` | |
| 1054 | + would be: `n := try number(s);`, `sum + try number(s)`, | |
| 1055 | + `if try ready(fd) then`. Like `not`, it applies to the factor after | |
| 1056 | + it: `try f(x) + 1` is `(try f(x)) + 1`. | |
| 1057 | +- With several, it stands only on the right of a multiple assignment, | |
| 1058 | + as `c` itself would, with a target for each: `a, b := try f(x);`. | |
| 1059 | + | |
| 1060 | +As a statement, `try c;` drops the values of `c` that it has, and a | |
| 1061 | +dropped value is warned of as a call that drops a result is (section | |
| 1062 | +10); `_ := try c;` and `a, _ := try c;` say it is meant (section 7.1). | |
| 1063 | +The error is never dropped: `try` is the way of passing it on. | |
| 1064 | + | |
| 1065 | +**Where `try` may stand.** Every use of `try` is an error unless: | |
| 1066 | + | |
| 1067 | +- the procedure it stands in has an `ERROR` as its last result. A | |
| 1068 | + module's body, and a procedure with no results, such as `proc | |
| 1069 | + main*;`, have none: there an error is dealt with where it is found, | |
| 1070 | + by a test; | |
| 1071 | +- the last result of the procedure `c` calls is an `ERROR`; | |
| 1072 | +- it is not inside a deferred statement, which `try` would leave | |
| 1073 | + (section 7.7). | |
| 1074 | + | |
| 1075 | +A generic module is checked in each instance (section 3), so a | |
| 1076 | +procedure of an instance whose last result, by its type arguments, is | |
| 1077 | +no `ERROR` and uses `try` is an error in that instance. | |
| 1078 | + | |
| 1079 | +**Order.** The arguments of `c` are evaluated from left to right, then | |
| 1080 | +`c` is called, and then its error is looked at (section 7.5). The | |
| 1081 | +designators on the left of an assignment are evaluated before its | |
| 1082 | +right side as always, so in `a[i()] := try g()`, `i` is called, then | |
| 1083 | +`g`; if `g` fails, nothing is assigned. A `try` in the right operand | |
| 1084 | +of `and` or `or` is reached only when that operand is evaluated | |
| 1085 | +(section 6.2). | |
| 1086 | + | |
| 1087 | +**Deferred statements.** When `try` returns, the statements deferred | |
| 1088 | +in the procedure and not yet run are run, the last first, as for a | |
| 1089 | +`return` (section 7.7), after the error and the zero results have been | |
| 1090 | +chosen; no deferred statement can change them. | |
| 1091 | + | |
| 990 | 1092 | ## 8. Procedures |
| 991 | 1093 | |
| 992 | 1094 | ``` |
| @@ -1016,7 +1118,9 @@ | ||
| 1016 | 1118 | that returns several values can only stand on the right of a multiple |
| 1017 | 1119 | assignment, alone as a statement, or alone after `return` in a |
| 1018 | 1120 | procedure whose results are of the same types, in the same order, |
| 1019 | -which then returns them as they are: `return divmod(n, 2);`. | |
| 1121 | +which then returns them as they are: `return divmod(n, 2);`. A | |
| 1122 | +procedure that can fail has an `ERROR` as its last result (section | |
| 1123 | +5.8), which `try` passes on (section 7.8). | |
| 1020 | 1124 | |
| 1021 | 1125 | ``` |
| 1022 | 1126 | module Divide; |
| @@ -1933,6 +2037,10 @@ | ||
| 1933 | 2037 | - **2026-10.18.** A code procedure takes and returns slices (section |
| 1934 | 2038 | 8). |
| 1935 | 2039 | - **2026-10.19.** The library gains `Bytes` (section 11). |
| 2040 | +- **2026-10.20.** `try`: a call of a procedure whose last result is an | |
| 2041 | + `ERROR`, which makes the procedure it stands in return that error | |
| 2042 | + when it isn't `nil` (sections 2, 5.8, 6, 7.8 and appendix B). `try` | |
| 2043 | + becomes a reserved word, which breaks a program with a name `try`. | |
| 1936 | 2044 | |
| 1937 | 2045 | ## Appendix A. What an implementation chooses |
| 1938 | 2046 | |
| @@ -2057,7 +2165,8 @@ | ||
| 2057 | 2165 | | "not" Factor | "~" Factor |
| 2058 | 2166 | | "[" [ Expr { "," Expr } ] "]" |
| 2059 | 2167 | | "{" [ Element { "," Element } ] "}" |
| 2060 | - | "^" [ "VOLATILE" ] Type "(" Expr ")" . | |
| 2168 | + | "^" [ "VOLATILE" ] Type "(" Expr ")" | |
| 2169 | + | "try" Designator . | |
| 2061 | 2170 | Element = Expr | ident ":" Expr . |
| 2062 | 2171 | Designator = QualIdent { Selector } . |
| 2063 | 2172 | Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" |
| @@ -2075,7 +2184,8 @@ | ||
| 2075 | 2184 | | "match" Expr "of" { "|" Arm } "end" |
| 2076 | 2185 | | "return" [ Expr { "," Expr } ] |
| 2077 | 2186 | | "break" |
| 2078 | - | "defer" Statement . | |
| 2187 | + | "defer" Statement | |
| 2188 | + | "try" Designator . | |
| 2079 | 2189 | Arm = ( ident [ "(" ident { "," ident } ")" ] |
| 2080 | 2190 | | Label { "," Label } | "else" ) ":" Statements . |
| 2081 | 2191 | Label = Expr [ ".." Expr ] . |
| @@ -24,8 +24,8 @@ | ||
| 24 | 24 | '("and" "begin" "break" "by" "code" "const" "defer" "div" "do" "else" |
| 25 | 25 | "elsif" "end" "extern" "for" "forward" "if" "import" "in" "link" |
| 26 | 26 | "match" "mod" "module" "not" "of" "or" "packed" "proc" "record" |
| 27 | - "repeat" "return" "set" "shl" "shr" "then" "to" "type" "until" "var" | |
| 28 | - "variant" "VOLATILE" "while" "xor")) | |
| 27 | + "repeat" "return" "set" "shl" "shr" "then" "to" "try" "type" "until" | |
| 28 | + "var" "variant" "VOLATILE" "while" "xor")) | |
| 29 | 29 | |
| 30 | 30 | (defconst raz-types |
| 31 | 31 | '("I8" "I16" "I32" "I64" "INT" "UINT" "U8" "U16" "U32" "U64" "BOOL" "F32" "F64" "ERROR")) |
| @@ -10,8 +10,8 @@ | ||
| 10 | 10 | syntax keyword razKeyword and begin break by code const defer div do else |
| 11 | 11 | syntax keyword razKeyword elsif end extern for forward if import in link |
| 12 | 12 | syntax keyword razKeyword match mod module not of or packed proc record |
| 13 | -syntax keyword razKeyword repeat return set shl shr then to type until var | |
| 14 | -syntax keyword razKeyword variant VOLATILE while xor | |
| 13 | +syntax keyword razKeyword repeat return set shl shr then to try type until | |
| 14 | +syntax keyword razKeyword var variant VOLATILE while xor | |
| 15 | 15 | syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR |
| 16 | 16 | syntax keyword razBuiltin SIZE ADR LEN NEW MSG SYSCALL FENCE CAS XCHG XADD |
| 17 | 17 | syntax keyword razBuiltin ARGC ARGV INC DEC |
| @@ -26,7 +26,7 @@ | ||
| 26 | 26 | sAnd, sBegin, sBreak, sBy, sCode, sConst, sDefer, sDiv, sDo, sElse, |
| 27 | 27 | sElsif, sEnd, sExtern, sFor, sForward, sIf, sImport, sIn, sLink, |
| 28 | 28 | sMatch, sMod, sModule, sNot, sOf, sOr, sPacked, sProc, sRecord, |
| 29 | - sRepeat, sReturn, sSet, sShl, sShr, sThen, sTo, sType, sUntil, sVar, | |
| 29 | + sRepeat, sReturn, sSet, sShl, sShr, sThen, sTo, sTry, sType, sUntil, sVar, | |
| 30 | 30 | sVariant, sVolatile, sWhile, sXor, |
| 31 | 31 | sEof); |
| 32 | 32 | |
| @@ -47,14 +47,15 @@ | ||
| 47 | 47 | const |
| 48 | 48 | MaxIdent* = 63; |
| 49 | 49 | MaxStr* = 65536; |
| 50 | - NumWords = 42; | |
| 50 | + NumWords = 43; | |
| 51 | 51 | NumOps = 28; |
| 52 | 52 | words: [NumWords][]U8 = [ |
| 53 | 53 | "and", "begin", "break", "by", "code", "const", "defer", "div", "do", |
| 54 | 54 | "else", "elsif", "end", "extern", "for", "forward", "if", "import", |
| 55 | 55 | "in", "link", "match", "mod", "module", "not", "of", "or", "packed", |
| 56 | 56 | "proc", "record", "repeat", "return", "set", "shl", "shr", "then", |
| 57 | - "to", "type", "until", "var", "variant", "VOLATILE", "while", "xor"]; | |
| 57 | + "to", "try", "type", "until", "var", "variant", "VOLATILE", "while", | |
| 58 | + "xor"]; | |
| 58 | 59 | (* sPlus to sBlank *) |
| 59 | 60 | ops: [NumOps][]U8 = [ |
| 60 | 61 | "+", "-", "*", "/", "=", "#", "<", "<=", ">", ">=", |
| @@ -22,7 +22,7 @@ | ||
| 22 | 22 | "tab": "\t\tx := @;\n", "multi": "x := \"\u00e9\u00e9\u00e9\" + @;\n", "ident64": "x" * 64 + ";\n", "ident63": "x" * 63 + ";\n", |
| 23 | 23 | "bigstr": "x := \"" + "a" * 70000 + "\";\n", "empty": "", "nonl": "x := y", |
| 24 | 24 | "ops": "+ - * / = # < <= > >= := : ; , . .. ... ( ) [ ] { } ^ & ~ | _ __ _a\n", |
| 25 | - "words": "and begin break by code const defer div do else elsif end extern for forward if import in link match mod module not of or packed proc record repeat return set shl shr then to type until var variant VOLATILE while xor Volatile\n", | |
| 25 | + "words": "and begin break by code const defer div do else elsif end extern for forward if import in link match mod module not of or packed proc record repeat return set shl shr then to try type until var variant VOLATILE while xor Volatile\n", | |
| 26 | 26 | } |
| 27 | 27 | for k, v in cases.items(): |
| 28 | 28 | open(f"{d}/{k}.raz", "wb").write(v.encode("utf-8", "surrogateescape") if isinstance(v, str) else v) |
| @@ -48,7 +48,7 @@ | ||
| 48 | 48 | |
| 49 | 49 | KEYWORDS = set("""and begin break by code const defer div do else elsif end |
| 50 | 50 | extern for forward if import in link match mod module not of or packed proc |
| 51 | -record repeat return set shl shr then to type until var variant VOLATILE | |
| 51 | +record repeat return set shl shr then to try type until var variant VOLATILE | |
| 52 | 52 | while xor""".split()) |
| 53 | 53 | PREDECLARED = set("""INT UINT I8 I16 I32 I64 U8 U16 U32 U64 F32 F64 BOOL ERROR |
| 54 | 54 | true false nil LEN ADR NEW SIZE INC DEC MSG XADD CAS XCHG""".split()) |
| @@ -53,7 +53,14 @@ | ||
| 53 | 53 | |
| 54 | 54 | ## Passing errors up |
| 55 | 55 | |
| 56 | -When a procedure can't do anything useful with an error, it returns it, so the first caller that can decide gets it. There is no hidden unwinding: the `return` is right there. | |
| 56 | +When a procedure can't do anything useful with an error, it returns it, so the first caller that can decide gets it. Written out, that is a test and a `return` after each call: | |
| 57 | + | |
| 58 | +```raz | |
| 59 | +x, err := value(a); | |
| 60 | +if err # nil then return 0, err; end; | |
| 61 | +``` | |
| 62 | + | |
| 63 | +`try` says the same in a word. `x := try value(a)` calls `value` and, if the error it returned isn't `nil`, returns that error from the procedure it stands in, with zero for that procedure's other results; otherwise it is `value`'s number. There is no hidden unwinding and no handler: `try` is a `return`, written where the call is, and a procedure that uses it must end in an `ERROR` itself, so its signature says it can fail. `main` returns none, so it tests its errors and deals with them. | |
| 57 | 64 | |
| 58 | 65 | ```raz Layers run |
| 59 | 66 | module Layers; |
| @@ -71,15 +78,10 @@ | ||
| 71 | 78 | return INT(n), nil; |
| 72 | 79 | end; |
| 73 | 80 | |
| 74 | -(* the sum of the values of two lines *) | |
| 81 | +(* the sum of the values of two lines: the first error ends it *) | |
| 75 | 82 | proc sum(a, b: []U8): (INT, ERROR); |
| 76 | -var x, y: INT; err: ERROR; | |
| 77 | 83 | begin |
| 78 | - x, err := value(a); | |
| 79 | - if err # nil then return 0, err; end; (* passed up *) | |
| 80 | - y, err := value(b); | |
| 81 | - if err # nil then return 0, err; end; | |
| 82 | - return x + y, nil; | |
| 84 | + return try value(a) + try value(b), nil; | |
| 83 | 85 | end; |
| 84 | 86 | |
| 85 | 87 | proc main*; |
| @@ -65,7 +65,7 @@ | ||
| 65 | 65 | <span class="cmd">$ ldd Bare</span> |
| 66 | 66 | not a dynamic executable |
| 67 | 67 | <span class="cmd">$ ls -l Bare | awk '{print $5, "bytes"}'</span> |
| 68 | -26176 bytes</code></pre> | |
| 68 | +26184 bytes</code></pre> | |
| 69 | 69 | <p>It is statically linked: no interpreter, no shared libraries, no <code>libc</code>. <code>ldd</code> says so.</p> |
| 70 | 70 | <h2>What it does when it runs</h2> |
| 71 | 71 | <p><code>strace</code> lists every system call a program makes. This is the whole of it:</p> |
| @@ -134,14 +134,14 @@ | ||
| 134 | 134 | <pre class="output term"><code><span class="cmd">$ raz Lean.raz</span> |
| 135 | 135 | <span class="cmd">$ raz Heavy.raz</span> |
| 136 | 136 | <span class="cmd">$ ls -l Lean Heavy | awk '{print $9, $5}'</span> |
| 137 | -Heavy 28384 | |
| 138 | -Lean 28376</code></pre> | |
| 137 | +Heavy 28392 | |
| 138 | +Lean 28384</code></pre> | |
| 139 | 139 | <p>The sizes differ by a few bytes, not by the hundreds of kilobytes that the table and the procedure would take: they are not in the executable at all. You can link in a library module without worrying what else it offers.</p> |
| 140 | 140 | <h2>Stripping</h2> |
| 141 | 141 | <p><code>-s</code> leaves out what is there for finding faults: the symbols and line numbers, the places a failure is told by, and the profiler. It is for the final build of a size-conscious program; the default keeps them so that gdb and failure messages work.</p> |
| 142 | 142 | <pre class="output term"><code><span class="cmd">$ raz -s -o Lean-stripped Lean.raz</span> |
| 143 | 143 | <span class="cmd">$ ls -l Lean Lean-stripped | awk '{print $9, $5}'</span> |
| 144 | -Lean 28376 | |
| 144 | +Lean 28384 | |
| 145 | 145 | Lean-stripped 6720 |
| 146 | 146 | <span class="cmd">$ ./Lean-stripped</span> |
| 147 | 147 | same program</code></pre> |
| @@ -36,7 +36,7 @@ | ||
| 36 | 36 | </ul> |
| 37 | 37 | <p>raz is a small systems language, first made for x86-64 Linux. A raz program can lay a record over a device’s registers, read and write a device’s memory exactly as written, take part in lock-free algorithms, make system calls and call C libraries. It has no garbage collector and no hidden run time: what the program does is what its text says.</p> |
| 38 | 38 | <p>This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.</p> |
| 39 | -<p>This is version 2026-10.19 of the report; section 12 says what each version changed. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11.</p> | |
| 39 | +<p>This is version 2026-10.20 of the report; section 12 says what each version changed. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>.</p> | |
| 40 | 40 | <p>The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program written for it. What anyone wishes it had waits on a list, for a later thaw.</p> |
| 41 | 41 | <h1 id="a-first-look">1. A first look</h1> |
| 42 | 42 | <pre><code>module Hello; |
| @@ -55,8 +55,8 @@ | ||
| 55 | 55 | <p><strong>Reserved words</strong> are lowercase, except <code>VOLATILE</code>:</p> |
| 56 | 56 | <pre><code>and begin break by code const defer div do else elsif end extern for |
| 57 | 57 | forward if import in link match mod module not of or packed proc record |
| 58 | -repeat return set shl shr then to type until var variant VOLATILE while | |
| 59 | -xor</code></pre> | |
| 58 | +repeat return set shl shr then to try type until var variant VOLATILE | |
| 59 | +while xor</code></pre> | |
| 60 | 60 | <p><code>static</code>, in <code>link static;</code> (section 3), isn’t one.</p> |
| 61 | 61 | <p><strong>Predeclared names</strong> are the basic types (section 5.1), <code>ERROR</code>, <code>true</code>, <code>false</code>, <code>nil</code> and the built-in procedures (section 6.8). They belong to a scope around every module, so a module or a procedure may declare the same name for something else, which hides them there.</p> |
| 62 | 62 | <p><strong>Numbers.</strong> <code>123</code> is decimal. <code>0FFH</code> is hexadecimal: it starts with a digit, its digits are <code>0</code> to <code>9</code> and <code>A</code> to <code>F</code>, and it ends in <code>H</code>, all in capitals. <code>1.5</code> and <code>2.0E-3</code> are reals; a real always has a digit on both sides of its point, and its exponent, if it has one, is <code>E</code>, a sign if wanted, and digits. A decimal number above the largest <code>I64</code>, or a hexadecimal one above the largest <code>U64</code>, is an error.</p> |
| @@ -229,6 +229,7 @@ | ||
| 229 | 229 | <p><code>set of E</code>, for an enumeration <code>E</code> of at most 64 names, holds any choice of <code>E</code>’s names: bit <code>i</code> stands for the name at position <code>i</code>. It takes the smallest of 1, 2, 4 and 8 bytes that has a bit for each name, so a set can stand for a C flags word.</p> |
| 230 | 230 | <h2 id="errors">5.8 Errors</h2> |
| 231 | 231 | <pre><code>const NotFound*: ERROR = "not found";</code></pre> |
| 232 | +<p>An <code>ERROR</code> is how a procedure says it failed: by convention the last of its results, and <code>nil</code> when nothing failed. A caller that can’t deal with a failure itself passes it on with <code>try</code> (section 7.8).</p> | |
| 232 | 233 | <p><code>ERROR</code> is a basic type as big as an address. A value of it is made only by a constant declared as above, and each such declaration makes an error different from every other, whatever its message: two modules that both declare <code>"not found"</code> have two different errors. A generic module makes its errors once per instance. <code>nil</code> is no error.</p> |
| 233 | 234 | <p>Errors compare with <code>=</code> and <code>#</code> only, by which declaration made them, never by their message. <code>MSG(e)</code> is the message, a <code>[]U8</code>; <code>MSG(nil)</code> is <code>""</code>.</p> |
| 234 | 235 | <pre><code>proc find*(key: I64): (^Node, ERROR); |
| @@ -266,14 +267,15 @@ | ||
| 266 | 267 | | "not" Factor | "~" Factor |
| 267 | 268 | | "[" [ Expr { "," Expr } ] "]" |
| 268 | 269 | | "{" [ Element { "," Element } ] "}" |
| 269 | - | "^" [ "VOLATILE" ] Type "(" Expr ")" . | |
| 270 | + | "^" [ "VOLATILE" ] Type "(" Expr ")" | |
| 271 | + | "try" Designator . | |
| 270 | 272 | Element = Expr | ident ":" Expr . |
| 271 | 273 | Designator = QualIdent { Selector } . |
| 272 | 274 | Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" |
| 273 | 275 | | "(" [ Expr { "," Expr } ] ")" .</code></pre> |
| 274 | -<p>The operators bind in the order of the grammar: <code>not</code> and <code>~</code> most tightly, then the multiplying operators, then the adding ones, then the relations, then <code>and</code>, then <code>or</code>. So <code>a + b * c = d</code> is <code>(a + (b * c)) = d</code>, <code>x & 0FH = 0</code> is <code>(x & 0FH) = 0</code>, and <code>i < n and a[i] # 0 or done</code> is <code>((i < n) and (a[i] # 0)) or done</code>. A unary minus applies to the first term: <code>-a * b</code> is <code>-(a * b)</code>.</p> | |
| 276 | +<p>The operators bind in the order of the grammar: <code>not</code>, <code>~</code> and <code>try</code> (section 7.8) most tightly, then the multiplying operators, then the adding ones, then the relations, then <code>and</code>, then <code>or</code>. So <code>a + b * c = d</code> is <code>(a + (b * c)) = d</code>, <code>x & 0FH = 0</code> is <code>(x & 0FH) = 0</code>, and <code>i < n and a[i] # 0 or done</code> is <code>((i < n) and (a[i] # 0)) or done</code>. A unary minus applies to the first term: <code>-a * b</code> is <code>-(a * b)</code>.</p> | |
| 275 | 277 | <p><code>and</code>, <code>or</code> and <code>not</code> are for <code>BOOL</code>s only; <code>&</code>, <code>|</code>, <code>xor</code> and <code>~</code> are for integers only.</p> |
| 276 | -<p>A pointer to a record isn’t the record: its fields are <code>p^.f</code>, never <code>p.f</code>, and the elements of a pointer to an array are <code>p^[i]</code>. A statement starts with a name, so <code>^T(x)^ := v</code> isn’t one.</p> | |
| 278 | +<p>A pointer to a record isn’t the record: its fields are <code>p^.f</code>, never <code>p.f</code>, and the elements of a pointer to an array are <code>p^[i]</code>. A statement starts with a name or <code>try</code>, so <code>^T(x)^ := v</code> isn’t one.</p> | |
| 277 | 279 | <h2 id="integers">6.1 Integers</h2> |
| 278 | 280 | <p>Both operands of <code>+ - * div mod & | xor</code>, and of a relation, have the same integer type, or one is an untyped constant that takes the other’s type, which it must fit. So do <code>v</code> and <code>k</code> in <code>INC(v, k)</code> and <code>DEC(v, k)</code>. The result has that type and wraps around at its width: in <code>U8</code>, <code>200 + 100</code> is <code>44</code>. A unary minus wraps too: for <code>x</code> of an unsigned type, <code>-x</code> is <code>0 - x</code>. Two integer types are never mixed, whatever their sizes: with <code>i: I64</code>, <code>i < LEN(s)</code> is an error, and <code>i</code> should be an <code>INT</code>.</p> |
| 279 | 281 | <p>Where the report asks for an integer and names no type, as an index, a slice’s bound, a shift’s count, a pointer’s offset <code>i</code> in <code>p + i</code>, or a conversion’s operand, any integer type will do, and its value is taken. On a target whose <code>INT</code> is narrower, a pointer’s offset is cut to an <code>INT</code>.</p> |
| @@ -467,7 +469,8 @@ | ||
| 467 | 469 | | "match" Expr "of" { "|" Arm } "end" |
| 468 | 470 | | "return" [ Expr { "," Expr } ] |
| 469 | 471 | | "break" |
| 470 | - | "defer" Statement . | |
| 472 | + | "defer" Statement | |
| 473 | + | "try" Designator . | |
| 471 | 474 | Arm = ( ident [ "(" ident { "," ident } ")" ] |
| 472 | 475 | | Label { "," Label } | "else" ) ":" Statements . |
| 473 | 476 | Label = Expr [ ".." Expr ] . |
| @@ -534,7 +537,40 @@ | ||
| 534 | 537 | end; |
| 535 | 538 | |
| 536 | 539 | end Head.</code></pre> |
| 537 | -<p><code>return e;</code> evaluates <code>e</code> before the deferred statements run. A deferred statement can’t contain <code>return</code>, <code>break</code> or <code>defer</code> that would leave it. A failed check, <code>Rt.exit</code> and <code>Sys.exit</code> end the program without running the deferred statements still pending.</p> | |
| 540 | +<p><code>return e;</code> evaluates <code>e</code> before the deferred statements run. A deferred statement can’t contain <code>return</code>, <code>break</code>, <code>defer</code> or <code>try</code> that would leave it. A failed check, <code>Rt.exit</code> and <code>Sys.exit</code> end the program without running the deferred statements still pending.</p> | |
| 541 | +<h2 id="try">7.8 Try</h2> | |
| 542 | +<p>A procedure that can fail returns an <code>ERROR</code> as its last result (section 5.8), <code>nil</code> when it didn’t. A caller that can’t deal with the failure passes it on to its own caller, which <code>try</code> does in a word:</p> | |
| 543 | +<pre><code>proc total(words: [][]U8): (INT, ERROR); | |
| 544 | +var i, sum: INT; | |
| 545 | +begin | |
| 546 | + for i := 0 to LEN(words) - 1 do | |
| 547 | + sum := sum + try number(words[i]); | |
| 548 | + end; | |
| 549 | + return sum, nil; | |
| 550 | +end;</code></pre> | |
| 551 | +<p><code>try c</code>, where <code>c</code> is a call (below) of a procedure whose last result is an <code>ERROR</code>, makes the call and looks at that error. If it is <code>nil</code>, the program goes on, and <code>try c</code> is what <code>c</code> returned without the error. If it isn’t, the procedure <code>try</code> stands in returns at once, as <code>return</code> does (section 7.3): its last result is that error, and every other result is zero, as a variable of its type starts (section 4). Nothing more of the statement <code>try</code> stands in is done; in the example, <code>sum</code> isn’t assigned. So</p> | |
| 552 | +<pre><code>x := try f(a);</code></pre> | |
| 553 | +<p>is, with <code>e</code> a variable the program can’t name, and <code>0</code> standing for a zero of each of the procedure’s other results:</p> | |
| 554 | +<pre><code>x, e := f(a); | |
| 555 | +if e # nil then return 0, e; end;</code></pre> | |
| 556 | +<p>The error is the one <code>c</code> returned, and compares with <code>=</code> as it did there. <code>try</code> has no handler and keeps no state: it is a <code>return</code>, written where the call is, so that every place a procedure can leave is in its text. A program that means to deal with an error tests it, as section 5.8 shows.</p> | |
| 557 | +<p><strong>What <code>try</code> is applied to.</strong> <code>c</code> is a designator that ends in a call: <code>f(x)</code>, <code>Mod.f(x)</code>, <code>p^.next(x)</code>, or a call through a variable of procedure type. Where the procedure has no parameters its parentheses may be left out, as in a statement (section 7.1), and <code>try</code> calls it: <code>try setup;</code>. A conversion, a built-in procedure (section 6.8) and the case of a variant are written like calls and aren’t procedures’; <code>try</code> can’t be applied to them.</p> | |
| 558 | +<p><strong>What <code>try c</code> is.</strong> <code>c</code>’s results but the last.</p> | |
| 559 | +<ul> | |
| 560 | +<li>With none, <code>try c</code> isn’t a value and stands only as a statement: <code>try flush;</code>.</li> | |
| 561 | +<li>With one, it is a value of that type, an operand wherever <code>c</code> would be: <code>n := try number(s);</code>, <code>sum + try number(s)</code>, <code>if try ready(fd) then</code>. Like <code>not</code>, it applies to the factor after it: <code>try f(x) + 1</code> is <code>(try f(x)) + 1</code>.</li> | |
| 562 | +<li>With several, it stands only on the right of a multiple assignment, as <code>c</code> itself would, with a target for each: <code>a, b := try f(x);</code>.</li> | |
| 563 | +</ul> | |
| 564 | +<p>As a statement, <code>try c;</code> drops the values of <code>c</code> that it has, and a dropped value is warned of as a call that drops a result is (section 10); <code>_ := try c;</code> and <code>a, _ := try c;</code> say it is meant (section 7.1). The error is never dropped: <code>try</code> is the way of passing it on.</p> | |
| 565 | +<p><strong>Where <code>try</code> may stand.</strong> Every use of <code>try</code> is an error unless:</p> | |
| 566 | +<ul> | |
| 567 | +<li>the procedure it stands in has an <code>ERROR</code> as its last result. A module’s body, and a procedure with no results, such as <code>proc main*;</code>, have none: there an error is dealt with where it is found, by a test;</li> | |
| 568 | +<li>the last result of the procedure <code>c</code> calls is an <code>ERROR</code>;</li> | |
| 569 | +<li>it is not inside a deferred statement, which <code>try</code> would leave (section 7.7).</li> | |
| 570 | +</ul> | |
| 571 | +<p>A generic module is checked in each instance (section 3), so a procedure of an instance whose last result, by its type arguments, is no <code>ERROR</code> and uses <code>try</code> is an error in that instance.</p> | |
| 572 | +<p><strong>Order.</strong> The arguments of <code>c</code> are evaluated from left to right, then <code>c</code> is called, and then its error is looked at (section 7.5). The designators on the left of an assignment are evaluated before its right side as always, so in <code>a[i()] := try g()</code>, <code>i</code> is called, then <code>g</code>; if <code>g</code> fails, nothing is assigned. A <code>try</code> in the right operand of <code>and</code> or <code>or</code> is reached only when that operand is evaluated (section 6.2).</p> | |
| 573 | +<p><strong>Deferred statements.</strong> When <code>try</code> returns, the statements deferred in the procedure and not yet run are run, the last first, as for a <code>return</code> (section 7.7), after the error and the zero results have been chosen; no deferred statement can change them.</p> | |
| 538 | 574 | <h1 id="procedures">8. Procedures</h1> |
| 539 | 575 | <pre><code>ProcDeclaration = "proc" IdentDef Signature ";" |
| 540 | 576 | ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] |
| @@ -548,7 +584,7 @@ | ||
| 548 | 584 | Results = Type | "(" Type { "," Type } ")" .</code></pre> |
| 549 | 585 | <p>A procedure without parameters may leave out the parentheses, both where it is declared (<code>proc main*;</code>) and where it is called (<code>Out.ln</code>). Used without parentheses in an expression, a procedure is a procedure value, not a call.</p> |
| 550 | 586 | <p><strong>Parameters.</strong> A value parameter is a copy: the procedure may change it, and the caller’s variable stays as it was. A <code>var</code> parameter is the caller’s variable itself; its argument must be a variable of exactly the parameter’s type.</p> |
| 551 | -<p><strong>Results.</strong> A procedure returns nothing, one value, or several. A call that returns several values can only stand on the right of a multiple assignment, alone as a statement, or alone after <code>return</code> in a procedure whose results are of the same types, in the same order, which then returns them as they are: <code>return divmod(n, 2);</code>.</p> | |
| 587 | +<p><strong>Results.</strong> A procedure returns nothing, one value, or several. A call that returns several values can only stand on the right of a multiple assignment, alone as a statement, or alone after <code>return</code> in a procedure whose results are of the same types, in the same order, which then returns them as they are: <code>return divmod(n, 2);</code>. A procedure that can fail has an <code>ERROR</code> as its last result (section 5.8), which <code>try</code> passes on (section 7.8).</p> | |
| 552 | 588 | <pre><code>module Divide; |
| 553 | 589 | import Out; |
| 554 | 590 | |
| @@ -1059,6 +1095,7 @@ | ||
| 1059 | 1095 | <li><strong>2026-10.17.</strong> <code>COPY</code> copies the elements of one array or slice to another as a block (section 6.8).</li> |
| 1060 | 1096 | <li><strong>2026-10.18.</strong> A code procedure takes and returns slices (section 8).</li> |
| 1061 | 1097 | <li><strong>2026-10.19.</strong> The library gains <code>Bytes</code> (section 11).</li> |
| 1098 | +<li><strong>2026-10.20.</strong> <code>try</code>: a call of a procedure whose last result is an <code>ERROR</code>, which makes the procedure it stands in return that error when it isn’t <code>nil</code> (sections 2, 5.8, 6, 7.8 and appendix B). <code>try</code> becomes a reserved word, which breaks a program with a name <code>try</code>.</li> | |
| 1062 | 1099 | </ul> |
| 1063 | 1100 | <h1 id="appendix-a.-what-an-implementation-chooses">Appendix A. What an implementation chooses</h1> |
| 1064 | 1101 | <p>Everything in this report binds every implementation of raz, except what follows, which each chooses for itself and documents. This compiler’s choices are in <code>doc/compiler.md</code>, under “This compiler’s choices”. The programs in <code>conform/</code> are how an implementation shows that it follows this report: it passes them all, its reals aside where its target has none; <code>conform/check.sh</code> runs them.</p> |
| @@ -1133,7 +1170,8 @@ | ||
| 1133 | 1170 | | "not" Factor | "~" Factor |
| 1134 | 1171 | | "[" [ Expr { "," Expr } ] "]" |
| 1135 | 1172 | | "{" [ Element { "," Element } ] "}" |
| 1136 | - | "^" [ "VOLATILE" ] Type "(" Expr ")" . | |
| 1173 | + | "^" [ "VOLATILE" ] Type "(" Expr ")" | |
| 1174 | + | "try" Designator . | |
| 1137 | 1175 | Element = Expr | ident ":" Expr . |
| 1138 | 1176 | Designator = QualIdent { Selector } . |
| 1139 | 1177 | Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" |
| @@ -1151,7 +1189,8 @@ | ||
| 1151 | 1189 | | "match" Expr "of" { "|" Arm } "end" |
| 1152 | 1190 | | "return" [ Expr { "," Expr } ] |
| 1153 | 1191 | | "break" |
| 1154 | - | "defer" Statement . | |
| 1192 | + | "defer" Statement | |
| 1193 | + | "try" Designator . | |
| 1155 | 1194 | Arm = ( ident [ "(" ident { "," ident } ")" ] |
| 1156 | 1195 | | Label { "," Label } | "else" ) ":" Statements . |
| 1157 | 1196 | Label = Expr [ ".." Expr ] . |
| @@ -56,6 +56,16 @@ | ||
| 56 | 56 | <span class="kw">return</span> <span class="ty">INT</span>(n), <span class="ty">nil</span>; |
| 57 | 57 | <span class="kw">end</span>; |
| 58 | 58 | |
| 59 | +<span class="comment">(* the sum of the numbers in words: the first error ends it *)</span> | |
| 60 | +<span class="kw">proc</span> total(words: [][]<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 61 | +<span class="kw">var</span> i, sum: <span class="ty">INT</span>; | |
| 62 | +<span class="kw">begin</span> | |
| 63 | + <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(words) - <span class="number">1</span> <span class="kw">do</span> | |
| 64 | + sum := sum + <span class="kw">try</span> number(words[i]); | |
| 65 | + <span class="kw">end</span>; | |
| 66 | + <span class="kw">return</span> sum, <span class="ty">nil</span>; | |
| 67 | +<span class="kw">end</span>; | |
| 68 | + | |
| 59 | 69 | <span class="kw">proc</span> main*; |
| 60 | 70 | <span class="kw">var</span> words: [<span class="number">3</span>][]<span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>; |
| 61 | 71 | <span class="kw">begin</span> |
| @@ -68,12 +78,18 @@ | ||
| 68 | 78 | <span class="kw">end</span>; |
| 69 | 79 | Out.ln; |
| 70 | 80 | <span class="kw">end</span>; |
| 81 | + n, err := total([<span class="string">"1"</span>, <span class="string">"2"</span>, <span class="string">"3"</span>]); | |
| 82 | + Out.int(n); Out.ln; | |
| 83 | + n, err := total(words); | |
| 84 | + <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">end</span>; | |
| 71 | 85 | <span class="kw">end</span>; |
| 72 | 86 | |
| 73 | 87 | <span class="kw">end</span> Errors.</code></pre> |
| 74 | 88 | <pre class="output"><code>42 |
| 75 | 89 | empty |
| 76 | 90 | not a number up to 1000 |
| 91 | +6 | |
| 92 | +nothing to read | |
| 77 | 93 | </code></pre> |
| 78 | 94 | <p>A procedure that can fail returns an <code>ERROR</code> beside its result, and <code>nil</code> when it didn't. Each <code>ERROR</code> constant is an error of its own, compared with <code>=</code> by which declaration made it, never by its message; <code>MSG(err)</code> is the message. An exported one, as <code>Empty</code> is here, lets the callers tell it from the others. raz has no exceptions, so every failure is visible in the signature and handled where the call is written.</p> |
| 79 | 95 | <h2>Handling them by kind</h2> |
| @@ -121,7 +137,9 @@ | ||
| 121 | 137 | error: out of range |
| 122 | 138 | </code></pre> |
| 123 | 139 | <h2>Passing errors up</h2> |
| 124 | -<p>When a procedure can't do anything useful with an error, it returns it, so the first caller that can decide gets it. There is no hidden unwinding: the <code>return</code> is right there.</p> | |
| 140 | +<p>When a procedure can't do anything useful with an error, it returns it, so the first caller that can decide gets it. Written out, that is a test and a <code>return</code> after each call:</p> | |
| 141 | +<p>``<code>raz x, err := value(a); if err # nil then return 0, err; end; </code>``</p> | |
| 142 | +<p><code>try</code> says the same in a word. <code>x := try value(a)</code> calls <code>value</code> and, if the error it returned isn't <code>nil</code>, returns that error from the procedure it stands in, with zero for that procedure's other results; otherwise it is <code>value</code>'s number. There is no hidden unwinding and no handler: <code>try</code> is a <code>return</code>, written where the call is, and a procedure that uses it must end in an <code>ERROR</code> itself, so its signature says it can fail. <code>main</code> returns none, so it tests its errors and deals with them.</p> | |
| 125 | 143 | <div class="file"><span>Layers.raz</span></div><pre class="code"><code><span class="kw">module</span> Layers; |
| 126 | 144 | <span class="kw">import</span> Out, Str; |
| 127 | 145 | |
| @@ -137,15 +155,10 @@ | ||
| 137 | 155 | <span class="kw">return</span> <span class="ty">INT</span>(n), <span class="ty">nil</span>; |
| 138 | 156 | <span class="kw">end</span>; |
| 139 | 157 | |
| 140 | -<span class="comment">(* the sum of the values of two lines *)</span> | |
| 158 | +<span class="comment">(* the sum of the values of two lines: the first error ends it *)</span> | |
| 141 | 159 | <span class="kw">proc</span> sum(a, b: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); |
| 142 | -<span class="kw">var</span> x, y: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>; | |
| 143 | 160 | <span class="kw">begin</span> |
| 144 | - x, err := value(a); | |
| 145 | - <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, err; <span class="kw">end</span>; <span class="comment">(* passed up *)</span> | |
| 146 | - y, err := value(b); | |
| 147 | - <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, err; <span class="kw">end</span>; | |
| 148 | - <span class="kw">return</span> x + y, <span class="ty">nil</span>; | |
| 161 | + <span class="kw">return</span> <span class="kw">try</span> value(a) + <span class="kw">try</span> value(b), <span class="ty">nil</span>; | |
| 149 | 162 | <span class="kw">end</span>; |
| 150 | 163 | |
| 151 | 164 | <span class="kw">proc</span> main*; |
| @@ -70,7 +70,7 @@ | ||
| 70 | 70 | <span class="cmd">$ raz run Quiet.raz</span> |
| 71 | 71 | hello, again |
| 72 | 72 | <span class="cmd">$ ls -l Quiet | awk '{print $5, "bytes"}'</span> |
| 73 | -28384 bytes</code></pre> | |
| 73 | +28392 bytes</code></pre> | |
| 74 | 74 | <p>The program is a static executable with no run time besides the code it uses, so it is a few kilobytes, and runs without any library installed.</p> |
| 75 | 75 | <h2>What <code>Out</code> can write</h2> |
| 76 | 76 | <p><code>Out</code> is the library's buffered output. It writes strings, single characters, numbers in decimal or hexadecimal, and a line feed. A string is written as it is, with <code>\n</code> for a line feed, <code>\t</code> for a tab, <code>\"</code> and <code>\\</code>.</p> |
| @@ -206,6 +206,8 @@ | ||
| 206 | 206 | procedure ResultTo(off: int64; size: longint); |
| 207 | 207 | procedure CopyResult(off, size: int64); |
| 208 | 208 | procedure ReturnJump; |
| 209 | +procedure TryAlign; | |
| 210 | +procedure TryReturn(slot: int64); | |
| 209 | 211 | procedure EndProc(c: longint; open: boolean); |
| 210 | 212 | function DeferBegin(var L, saved: longint): longint; |
| 211 | 213 | procedure DeferEnd(L, saved: longint); |
| @@ -2062,6 +2064,41 @@ | ||
| 2062 | 2064 | if resType^.form = fReal then begin XValueTo(depth - 1, 0); Drop end |
| 2063 | 2065 | else PopTo(RAX); |
| 2064 | 2066 | end; |
| 2067 | + Jmp(exitL); | |
| 2068 | +end; | |
| 2069 | + | |
| 2070 | +{ The first of what try does where the error isn't nil (the report, 7.8): | |
| 2071 | + the machine's stack made even, as the deferred statements about to be | |
| 2072 | + run expect to find it. try can stand among the arguments of another | |
| 2073 | + call, where quadwords have been pushed. Nothing is lost: the frame is | |
| 2074 | + above rsp, and the code returns through the frame pointer. } | |
| 2075 | +procedure TryAlign; | |
| 2076 | +begin | |
| 2077 | + AluI(aluAnd, RSP, -16); | |
| 2078 | +end; | |
| 2079 | + | |
| 2080 | +{ The rest of it: the procedure returns the error in the frame slot, and | |
| 2081 | + a zero for each of its other results. Only code is made: no entry of | |
| 2082 | + the stack of values is touched, since the code that follows is the | |
| 2083 | + code for an error that is nil, with the stack as it was. } | |
| 2084 | +procedure TryReturn(slot: int64); | |
| 2085 | +var f: PObj; | |
| 2086 | +begin | |
| 2087 | + if BehindPointer(resType) then begin | |
| 2088 | + Load(RDI, Mem(RBP, retSlot), 8, false); | |
| 2089 | + MovRI(RCX, resType^.size); | |
| 2090 | + MovRI(RAX, 0); | |
| 2091 | + RepStosb; | |
| 2092 | + f := resType^.fields; | |
| 2093 | + while f^.next <> nil do f := f^.next; | |
| 2094 | + Load(RCX, Mem(RBP, slot), 8, false); | |
| 2095 | + Load(RDX, Mem(RBP, retSlot), 8, false); | |
| 2096 | + Store(Mem(RDX, f^.val), RCX, 8); | |
| 2097 | + Load(RAX, Mem(RBP, retSlot), 8, false); | |
| 2098 | + end else if InTwoRegs(resType) then begin | |
| 2099 | + Load(RDX, Mem(RBP, slot), 8, false); | |
| 2100 | + MovRI(RAX, 0); | |
| 2101 | + end else Load(RAX, Mem(RBP, slot), 8, false); | |
| 2065 | 2102 | Jmp(exitL); |
| 2066 | 2103 | end; |
| 2067 | 2104 |
| @@ -1074,6 +1074,91 @@ | ||
| 1074 | 1074 | end; |
| 1075 | 1075 | end; |
| 1076 | 1076 | |
| 1077 | +procedure RunDefers(base: longint); forward; | |
| 1078 | + | |
| 1079 | +{ The type of the last result of t, the results of a procedure: t if | |
| 1080 | + it has one, else the last of its several. } | |
| 1081 | +function LastResult(t: PType): PType; | |
| 1082 | +var f: PObj; | |
| 1083 | +begin | |
| 1084 | + if not t^.tuple then exit(t); | |
| 1085 | + f := t^.fields; | |
| 1086 | + while f^.next <> nil do f := f^.next; | |
| 1087 | + LastResult := f^.typ; | |
| 1088 | +end; | |
| 1089 | + | |
| 1090 | +{ The results of the tuple t but the last: the same fields, one fewer } | |
| 1091 | +function WithoutLast(t: PType): PType; | |
| 1092 | +var r: PType; | |
| 1093 | +begin | |
| 1094 | + r := NewType(fRecord, t^.size, t^.align); | |
| 1095 | + r^.tuple := true; | |
| 1096 | + r^.fields := t^.fields; | |
| 1097 | + r^.len := t^.len - 1; | |
| 1098 | + WithoutLast := r; | |
| 1099 | +end; | |
| 1100 | + | |
| 1101 | +{ try c (the report, 7.8): the call c, then the error it returned, its | |
| 1102 | + last result: if that isn't nil, the procedure returns it, and a zero | |
| 1103 | + for each of its other results, after running the deferred statements | |
| 1104 | + in force. x is left c's other results: none (mNone), one (a value) or | |
| 1105 | + several (a tuple, for a multiple assignment). The error is kept in a | |
| 1106 | + slot of the frame, and tested there; the code for it, out of line, | |
| 1107 | + is made with the stack of values as it stands, and leaves it so, | |
| 1108 | + since what follows is the code for an error that is nil. } | |
| 1109 | +procedure TryCall(var x: TItem); | |
| 1110 | +var | |
| 1111 | + called: boolean; c: TCall; start, at: TPos; res: PType; last: PObj; slot: int64; ok: longint; | |
| 1112 | +begin | |
| 1113 | + start := TokPos; | |
| 1114 | + Next; | |
| 1115 | + if deferring then MarkAt(SpanFrom(start), 'try can''t leave a deferred statement'); | |
| 1116 | + if curResult = nil then | |
| 1117 | + MarkAt(SpanFrom(start), 'try needs a procedure that ends in an ERROR result, and this returns nothing') | |
| 1118 | + else if LastResult(curResult) <> tError then | |
| 1119 | + MarkAt(SpanFrom(start), 'try needs a procedure that ends in an ERROR result, and this returns ' + TypeName(curResult)); | |
| 1120 | + Designator(x, called); | |
| 1121 | + if not called and ((x.mode = mProc) or ((x.mode in [mVar, mStack]) and (x.typ^.form = fProc))) then begin | |
| 1122 | + { a procedure without parameters, written without parentheses } | |
| 1123 | + at := x.pos; | |
| 1124 | + BeginCall(c, x); | |
| 1125 | + EndCall(c, x); | |
| 1126 | + x.pos := at; | |
| 1127 | + called := true; | |
| 1128 | + end; | |
| 1129 | + if not called then MarkAt(x.pos, 'try needs a call, found ' + What(x)); | |
| 1130 | + if x.mode = mNone then | |
| 1131 | + MarkAt(x.pos, 'try needs a call that returns an ERROR last, and this returns nothing'); | |
| 1132 | + if LastResult(x.typ) <> tError then | |
| 1133 | + MarkAt(x.pos, 'try needs a call that returns an ERROR last, and this returns ' + TypeName(x.typ)); | |
| 1134 | + res := x.typ; | |
| 1135 | + if x.mode = mStack then begin | |
| 1136 | + { an ERROR alone } | |
| 1137 | + slot := AllocTemp(8, 8); | |
| 1138 | + PopVar(FrameAdr(slot), 8); | |
| 1139 | + x.mode := mNone; x.typ := tNone; | |
| 1140 | + end else if x.mode = mWords then begin | |
| 1141 | + { two words, the error on top } | |
| 1142 | + slot := AllocTemp(8, 8); | |
| 1143 | + PopVar(FrameAdr(slot), 8); | |
| 1144 | + x.mode := mStack; x.typ := res^.fields^.typ; | |
| 1145 | + end else begin | |
| 1146 | + { several, in a temporary: the error is where it is } | |
| 1147 | + last := res^.fields; | |
| 1148 | + while last^.next <> nil do last := last^.next; | |
| 1149 | + slot := x.off + last^.val; | |
| 1150 | + if res^.len = 2 then Field(x, res^.fields) else x.typ := WithoutLast(res); | |
| 1151 | + end; | |
| 1152 | + ok := NoLabel; | |
| 1153 | + PushVar(FrameAdr(slot), 8, false, kWord); | |
| 1154 | + JumpIf(ok, false); | |
| 1155 | + if numDefers > 0 then TryAlign; | |
| 1156 | + RunDefers(0); | |
| 1157 | + TryReturn(slot); | |
| 1158 | + Fix(ok); | |
| 1159 | + x.pos := SpanFrom(start); | |
| 1160 | +end; | |
| 1161 | + | |
| 1077 | 1162 | procedure Factor(var x: TItem; want: PType); |
| 1078 | 1163 | var called, vol, several: boolean; t: PType; start: TPos; s: shortstring; |
| 1079 | 1164 | begin |
| @@ -1101,6 +1186,12 @@ | ||
| 1101 | 1186 | end; |
| 1102 | 1187 | end; |
| 1103 | 1188 | sLparen: begin Next; Expression(x, want); Expect(sRparen) end; |
| 1189 | + sTry: begin | |
| 1190 | + TryCall(x); | |
| 1191 | + if x.mode = mNone then MarkAt(x.pos, 'this try has no value'); | |
| 1192 | + if x.typ^.tuple then | |
| 1193 | + MarkHelp(x.pos, 'this try has several values', 'they go to variables: a, b := try f(...)'); | |
| 1194 | + end; | |
| 1104 | 1195 | sNot: begin Next; Factor(x, nil); NotOp(x) end; |
| 1105 | 1196 | sTilde: begin Next; Factor(x, nil); Complement(x) end; |
| 1106 | 1197 | sLbrak: ArrayLiteral(x, want); |
| @@ -1610,7 +1701,10 @@ | ||
| 1610 | 1701 | end; |
| 1611 | 1702 | until false; |
| 1612 | 1703 | Expect(sBecomes); |
| 1613 | - Designator(y, called); | |
| 1704 | + if tok = sTry then begin | |
| 1705 | + TryCall(y); | |
| 1706 | + called := true; | |
| 1707 | + end else Designator(y, called); | |
| 1614 | 1708 | if not called or not y.typ^.tuple or (y.typ^.len <> n) then begin |
| 1615 | 1709 | MarkAt(y.pos, 'expected a call with ' + IntStr(n) + ' results'); |
| 1616 | 1710 | if y.mode = mWords then CopyToTemp(y); |
| @@ -1792,6 +1886,15 @@ | ||
| 1792 | 1886 | Jump(curLoop^.brk); |
| 1793 | 1887 | end; |
| 1794 | 1888 | sDefer: Defer; |
| 1889 | + sTry: begin | |
| 1890 | + TryCall(x); | |
| 1891 | + if (x.mode <> mNone) and (numDrops <= High(drops)) then begin | |
| 1892 | + drops[numDrops].at := x.pos; | |
| 1893 | + if x.typ^.tuple then drops[numDrops].n := x.typ^.len else drops[numDrops].n := 1; | |
| 1894 | + inc(numDrops); | |
| 1895 | + end; | |
| 1896 | + Discard(x); | |
| 1897 | + end; | |
| 1795 | 1898 | sArrow: MarkHelp(TokPos, 'a statement starts with a name', 'convert into a pointer variable, then assign through that'); |
| 1796 | 1899 | else if AtLineStart then EndMissing(MissingPos, 'expected "end", found ' + Found) |
| 1797 | 1900 | else Expected('a statement'); |
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| 35 | 35 | 0.3.0-5-g38d29a8 five commits past it } |
| 36 | 36 | Version = {$I version.inc}; |
| 37 | - Report = '2026-10.19'; { the version of doc/raz.md it follows } | |
| 37 | + Report = '2026-10.20'; { the version of doc/raz.md it follows } | |
| 38 | 38 | MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } |
| 39 | 39 | MfdCloexec = 1; { an ELF still executes: the kernel opens it first } |
| 40 | 40 |
| @@ -29,7 +29,7 @@ | ||
| 29 | 29 | sAnd, sBegin, sBreak, sBy, sCode, sConst, sDefer, sDiv, sDo, sElse, |
| 30 | 30 | sElsif, sEnd, sExtern, sFor, sForward, sIf, sImport, sIn, sLink, |
| 31 | 31 | sMatch, sMod, sModule, sNot, sOf, sOr, sPacked, sProc, sRecord, |
| 32 | - sRepeat, sReturn, sSet, sShl, sShr, sThen, sTo, sType, sUntil, sVar, | |
| 32 | + sRepeat, sReturn, sSet, sShl, sShr, sThen, sTo, sTry, sType, sUntil, sVar, | |
| 33 | 33 | sVariant, sVolatile, sWhile, sXor, |
| 34 | 34 | sEof); |
| 35 | 35 | |
| @@ -106,7 +106,7 @@ | ||
| 106 | 106 | 'else', 'elsif', 'end', 'extern', 'for', 'forward', 'if', 'import', |
| 107 | 107 | 'in', 'link', 'match', 'mod', 'module', 'not', 'of', 'or', 'packed', |
| 108 | 108 | 'proc', 'record', 'repeat', 'return', 'set', 'shl', 'shr', 'then', |
| 109 | - 'to', 'type', 'until', 'var', 'variant', 'VOLATILE', 'while', 'xor'); | |
| 109 | + 'to', 'try', 'type', 'until', 'var', 'variant', 'VOLATILE', 'while', 'xor'); | |
| 110 | 110 | Ops: array[sPlus..sBlank] of string[3] = ( |
| 111 | 111 | '+', '-', '*', '/', '=', '#', '<', '<=', '>', '>=', |
| 112 | 112 | ':=', ':', ';', ',', '.', '..', '...', |
| @@ -0,0 +1 @@ | ||
| 1 | +-Werror |
| @@ -0,0 +1,9 @@ | ||
| 1 | +test/err/TryDrops.raz:13:5: error: the result of try half is dropped | |
| 2 | + 13 | try half(4); | |
| 3 | + | ^^^^^^^^^^^ | |
| 4 | +help: if that is meant, write _ := try half(...) | |
| 5 | +test/err/TryDrops.raz:16:5: error: the results of try three are dropped | |
| 6 | + 16 | try three(4); | |
| 7 | + | ^^^^^^^^^^^^ | |
| 8 | +help: if that is meant, write _, _ := try three(...) | |
| 9 | +raz: 2 errors |
| @@ -0,0 +1,20 @@ | ||
| 1 | +module TryDrops; | |
| 2 | +proc half(n: INT): (INT, ERROR); | |
| 3 | +begin | |
| 4 | + return n div 2, nil; | |
| 5 | +end; | |
| 6 | +proc three(n: INT): (INT, INT, ERROR); | |
| 7 | +begin | |
| 8 | + return n, n, nil; | |
| 9 | +end; | |
| 10 | +proc g*: ERROR; | |
| 11 | +var n: INT; | |
| 12 | +begin | |
| 13 | + try half(4); | |
| 14 | + _ := try half(4); | |
| 15 | + n, _ := try three(4); | |
| 16 | + try three(4); | |
| 17 | + _ := n; | |
| 18 | + return nil; | |
| 19 | +end; | |
| 20 | +end TryDrops. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryConversion.raz:5:14: error: try needs a call, found INT | |
| 2 | + 5 | n := try INT(n); | |
| 3 | + | ^^^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryCount.raz:9:13: error: expected a call with 2 results | |
| 2 | + 9 | a, b := try f(); | |
| 3 | + | ^^^^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryDeferred.raz:8:11: error: try can't leave a deferred statement | |
| 2 | + 8 | defer try f(); | |
| 3 | + | ^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryInBody.raz:7:5: error: try needs a procedure that ends in an ERROR result, and this returns nothing | |
| 2 | + 7 | try f(); | |
| 3 | + | ^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryInMain.raz:8:5: error: try needs a procedure that ends in an ERROR result, and this returns nothing | |
| 2 | + 8 | try f(); | |
| 3 | + | ^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +conform/err/TryNeedsArguments.raz:8:10: error: too few arguments: f takes 1 | |
| 2 | + 8 | try f; | |
| 3 | + | ^ | |
| 4 | +conform/err/TryNeedsArguments.raz:2:6: note: the signature is here | |
| 5 | + 2 | proc f(n: INT): ERROR; | |
| 6 | + | ^ | |
| 7 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryNoValue.raz:9:10: error: this try has no value | |
| 2 | + 9 | n := try f(); | |
| 3 | + | ^^^^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryNotACall.raz:5:9: error: try needs a call, found INT | |
| 2 | + 5 | try n; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryNotError.raz:8:9: error: try needs a call that returns an ERROR last, and this returns INT | |
| 2 | + 8 | try f; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryReturnsNothing.raz:7:9: error: try needs a call that returns an ERROR last, and this returns nothing | |
| 2 | + 7 | try f(); | |
| 3 | + | ^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/TrySeveralValues.raz:9:10: error: this try has several values | |
| 2 | + 9 | n := try f(); | |
| 3 | + | ^^^^^^^ | |
| 4 | +help: they go to variables: a, b := try f(...) | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/TryWrongResults.raz:8:5: error: try needs a procedure that ends in an ERROR result, and this returns (INT, BOOL) | |
| 2 | + 8 | try f(); | |
| 3 | + | ^^^ | |
| 4 | +raz: 1 error |
| @@ -20,7 +20,7 @@ | ||
| 20 | 20 | end; |
| 21 | 21 | |
| 22 | 22 | const |
| 23 | - words = "and begin break by code const defer div do else elsif end extern for forward if import in link match mod module not of or packed proc record repeat return set shl shr then to type until var variant VOLATILE while xor"; | |
| 23 | + words = "and begin break by code const defer div do else elsif end extern for forward if import in link match mod module not of or packed proc record repeat return set shl shr then to try type until var variant VOLATILE while xor"; | |
| 24 | 24 | |
| 25 | 25 | proc isSpace*(c: U8): BOOL; |
| 26 | 26 | begin |