From 69532854a7f3eca07fa7e90c22894cab4d7af1bc Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:26:27 +0100 Subject: [PATCH] Report 2026-10.11: array constants, joined strings, match on integers The three wishes come into the thaw, by the owner's word: a constant may have an array type, used as a variable that can't be changed; + joins string constants; match takes an integer, labels being constants and inclusive ranges, else required. PLAN.md: steps 6a-c; the budget is now a soft 8,000 lines, the compiler documented throughout (also CLAUDE.md). --- diff --git a/CLAUDE.md b/CLAUDE.md index 8b6b181..56b1a2b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -55,8 +55,10 @@ `Luis <42945842+ldiaz94@users.noreply.github.com>`, unsigned (`--no-gpg-sign`), no attribution trailer. Push to `main`. No pull requests unless asked. -- Budget: about 6,000 lines of code (blank and comment lines not - counted), within 10%; at least 400,000 lines of raz a second. +- Budget: a soft limit of 8,000 lines of code (blank and comment lines + not counted), with the compiler documented and commented throughout, + most of all where it is obscure or needs a compiler writer's + knowledge; at least 400,000 lines of raz a second. - Style: clarity over cleverness; each unit opens with what it is, what it depends on and what it leaves out; comments say why. Match the report's plain, short prose in documentation. diff --git a/PLAN.md b/PLAN.md index e9242eb..5e61c4f 100644 --- a/PLAN.md +++ b/PLAN.md @@ -66,8 +66,8 @@ ## Proposed, v0.6.0: raz to be rebuilt by anyone -Under way: steps 1 to 4 are done, step 3 set aside since; the change -to steps 7 and 8, and dropping 9 and 9f, is for review. The freeze +Under way: steps 1 to 4 are done, step 3 set aside since. The owner +has agreed the rest, to step 10, to be done without stopping. The freeze doesn't hold for this work, by the owner's word. **For:** a language a person alone can implement again, from the @@ -78,9 +78,12 @@ **Done when:** the report alone is enough to rebuild raz, with a conformance suite to say when a rebuild is done; the report has a system integer and optional reals, and leaves separate compilation to -the implementation; the parser reaches the machine only through +the implementation; it has array constants, joined string constants +and `match` on integers; the parser reaches the machine only through `gen`, the boundary a port writes again, and the back end keeps its -values in registers as Wirth's compilers do; `make bench` still passes; a new code budget is written down; `v0.6.0` is tagged. +values in registers as Wirth's compilers do; `make bench` still +passes; the compiler is within the new budget below; `v0.6.0` is +tagged. **Kept out:** a second back end (aarch64 is the milestone after, to measure a port); an optimiser; separate compilation: object files, an @@ -118,6 +121,14 @@ passes, apart from this compiler's own messages. - [ ] 5. The system integer. - [ ] 6. Optional reals: a target says whether it has them. +- [ ] 6a. `+` joins string constants (report, section 4). +- [ ] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only + data, used as a variable that can't be changed (section 4). + Done so that a string constant is the same mechanism. +- [ ] 6c. `match` on integers: constants and inclusive ranges as + labels, no value in two, `else` required (7.4); compares and + branches, no jump table. `examples/Sh.raz`'s chains of `elsif` + become `match`. - [ ] 7. The parser reaches the machine only through `gen`: calls of a stack machine, with the kind of each value (a word, a 64-bit integer, an `F32`, an `F64`) and the place in the source, and no @@ -148,20 +159,21 @@ - [ ] 9e. A profiler in `Rt`: a program run with `RAZ_PROFILE` set samples where it is, and at its end says which procedures and lines took the time. -- [ ] 10. `doc/compiler.md`, README, the budget, the report's version - and the compiler's; tag `v0.6.0`. +- [ ] 10. `doc/compiler.md`, README, the report's version and the + compiler's; tag `v0.6.0`. +The budget, by the owner's word: a soft limit of 8,000 lines of code, +with the compiler documented and commented throughout, most of all +where it is obscure, not obvious, or needs a compiler writer's +knowledge. + ## Wishes, for a later thaw What the language or the library might have, met while writing raz; none is built while the freeze holds. -- `case` on integers and characters: `match` takes variants only, so - a choice among numbers is a chain of `elsif` (`examples/Sh.raz`). -- Constant arrays: a table of numbers is assigned in a procedure, as - `Math`'s bits of 2/pi are, or kept in a variable. -- Joining string constants, `"a" + B`: a test's expected text is - written out whole each time (`Opt`'s test). +Taken into v0.6.0: `match` on integers, array constants, joining +string constants. ## After v0.5.0 diff --git a/doc/raz.md b/doc/raz.md index aa24358..f40fd37 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -277,7 +277,7 @@ ## 4. Declarations ``` -Declaration = "const" { IdentDef [ ":" "ERROR" ] "=" Expr ";" } +Declaration = "const" { IdentDef [ ":" Type ] "=" Expr ";" } | "type" { IdentDef "=" Type ";" } | "var" { IdentList ":" Type ";" } | ProcDeclaration ";" @@ -294,17 +294,36 @@ **Constants** are evaluated by the compiler. Their expressions use only literals, other constants, operators, conversions, `SIZE` and the `LEN` of arrays and strings. A constant is an integer, a real, a `BOOL`, a -name of an enumeration, a set, a string or an `ERROR`. A constant of an integer type wraps as the +name of an enumeration, a set, a string, an array or an `ERROR`. A constant of an integer type wraps as the program would: `U8(250) + 10` is 4. Dividing by zero in a constant is an error. A string constant can be indexed and sliced as an array of -`U8` is, and its `LEN` is its length in bytes: +`U8` is, and its `LEN` is its length in bytes. `+` joins two string +constants into one: ``` const Text = "the cat"; + Line = Text + "\n"; ... w := Text[4..7]; (* "cat" *) ``` +A constant with a type is an `ERROR` (section 5.8) or an array. An +array constant has a type `[n]T`, where `T` is an integer type, a +real, `BOOL`, an enumeration or a set, and its value is an array +literal (section 6.5) of constants: + +``` +const Primes: [5]I64 = [2, 3, 5, 7, 11]; +... +p := Primes[i]; +``` + +It is used as a variable of that type is, but can't be changed: it can +be indexed and sliced, its `LEN` is `n`, it can be assigned and +compared, and it can be passed as a `[n]T` or a `[]T`. Its elements +are constant as a string's bytes are (section 5.10). An element of it +isn't a constant, even at a constant index. + **Variables** of a module live as long as the program; variables of a procedure live as long as its call. Both start at zero: every byte of them is zero. So a number starts as `0` (`0.0` for `F32` and `F64`), @@ -781,7 +800,9 @@ | "return" [ Expr { "," Expr } ] | "break" | "defer" Statement . -Arm = ( ident [ "(" ident { "," ident } ")" ] | "else" ) ":" Statements . +Arm = ( ident [ "(" ident { "," ident } ")" ] + | Label { "," Label } | "else" ) ":" Statements . +Label = Expr [ ".." Expr ] . ``` Every statement ends in `;`, including the ones that end in `end` or @@ -848,6 +869,23 @@ `match` on an enumeration works the same way: each arm names one of the enumeration's names. +`match` on an integer runs the arm whose labels hold its value: + +``` +match ch of +| ' ', '\t': skip := true; +| '0' .. '9': digit := true; +| else: +end; +``` + +A label is a constant, or `lo .. hi`, the constants from `lo` to `hi`, +both included (unlike a slice, which leaves `hi` out). Each must fit +the integer's type, and no value may be in two labels. An integer +`match` must have an `| else:` arm, which may be empty. Whether an arm +names a case or gives labels is set by the type matched: `| Max:` is a +case of a variant or an enumeration, and a constant for an integer. + ### 7.5 Order of evaluation Operands, arguments and the designators of an assignment are evaluated @@ -927,7 +965,7 @@ ProcDeclaration = "proc" IdentDef Signature ";" ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] | "code" Expr { "," Expr } ) . -LocalDeclaration = "const" { ident [ ":" "ERROR" ] "=" Expr ";" } +LocalDeclaration = "const" { ident [ ":" Type ] "=" Expr ";" } | "type" { ident "=" Type ";" } | "var" { ident { "," ident } ":" Type ";" } . Signature = "(" [ Params { ";" Params } [ ";" "..." ] ] ")" [ ":" Results ] @@ -1684,7 +1722,9 @@ `main`'s and `Rt.exit`'s among them, and `I32` for a character; `I64` stays for what may pass an `INT` on a small target: a file's size and position, times, and numbers read from text (section - 11). + 11). An array constant has a type and is used as a variable that + can't be changed; `+` joins string constants (section 4). `match` + takes an integer, with constants and ranges as labels (7.4). ## Appendix A. What an implementation chooses @@ -1769,7 +1809,7 @@ Link = "link" ( string | "static" ) ";" . Check = "check" "off" ";" . -Declaration = "const" { IdentDef [ ":" "ERROR" ] "=" Expr ";" } +Declaration = "const" { IdentDef [ ":" Type ] "=" Expr ";" } | "type" { IdentDef "=" Type ";" } | "var" { IdentList ":" Type ";" } | ProcDeclaration ";" @@ -1820,12 +1860,14 @@ | "return" [ Expr { "," Expr } ] | "break" | "defer" Statement . -Arm = ( ident [ "(" ident { "," ident } ")" ] | "else" ) ":" Statements . +Arm = ( ident [ "(" ident { "," ident } ")" ] + | Label { "," Label } | "else" ) ":" Statements . +Label = Expr [ ".." Expr ] . ProcDeclaration = "proc" IdentDef Signature ";" ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] | "code" Expr { "," Expr } ) . -LocalDeclaration = "const" { ident [ ":" "ERROR" ] "=" Expr ";" } +LocalDeclaration = "const" { ident [ ":" Type ] "=" Expr ";" } | "type" { ident "=" Type ";" } | "var" { ident { "," ident } ":" Type ";" } . Signature = "(" [ Params { ";" Params } [ ";" "..." ] ] ")" [ ":" Results ]