summaryrefslogtreecommitdiff
path: root/chapter31/docs
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-13 16:17:13 -0800
committerCliff Click <cliffc@acm.org>2026-11-13 16:17:13 -0800
commitafcf96444366fb4b7c83f938763013cbc360aade (patch)
tree67eb69ac6cc58322549020550d48aceb8f2f5ff8 /chapter31/docs
parentd2c59dbe9d6cc8dc4ff4d1e7949bfeddd8bef5d0 (diff)
downloadSimple-afcf96444366fb4b7c83f938763013cbc360aade.tar.gz
Simple-afcf96444366fb4b7c83f938763013cbc360aade.zip
Array init syntaxmain
New array init syntax; used in a bunch of places. Allows e.g. read-only arrays with not-null entries. Fix bug x86 encoding of mem/cmp/fp compares
Diffstat (limited to 'chapter31/docs')
-rw-r--r--chapter31/docs/31-grammar.md11
-rw-r--r--chapter31/docs/examples/Dijkstra.smp9
-rw-r--r--chapter31/docs/examples/TicTacToe.smp3
3 files changed, 13 insertions, 10 deletions
diff --git a/chapter31/docs/31-grammar.md b/chapter31/docs/31-grammar.md
index 3083fcc2..f4ca76f1 100644
--- a/chapter31/docs/31-grammar.md
+++ b/chapter31/docs/31-grammar.md
@@ -185,7 +185,14 @@ argumentList : assignment (',' assignment)* ','? ;
// Allocation invokes the declared constructor; inline initializer blocks from
// earlier chapters are replaced by constructor arguments.
-newExpression : 'new' type ('[' assignment ']' | callSuffix)? ;
+newExpression
+ : 'new' type ('[' '~'? assignment ']' arrayInitializer? | arrayInitializer | callSuffix)?
+ ;
+// With no explicit length, type must end in [] or [~] and the initializer is a list.
+arrayInitializer
+ : '{' (assignment (',' assignment)* ','?)? '}'
+ | '{' ('int' | 'i32' | 'i64' | 'u32') IDENTIFIER '->' statement* '}'
+ ;
INTEGER_LITERAL : [1-9] DIGIT* | '0' [0-7]* | '0' [xX] HEX_DIGIT+ ;
// The f suffix also accepts an integer-shaped token, e.g. 12f.
@@ -231,7 +238,7 @@ there is no inheritance chain, AST, or additional compiler pass.
| `assignment` through `primaryExpression` | [`Expressions`](../src/main/java/com/seaofnodes/simple/parser/Expressions.java): `parseAssignment`, `parseExpression`, `parseLogical`, `parseBitwise`, `parseEquality`, `parseComparison`, `parseShift`, `parseAddition`, `parseMultiplication`, `parseUnary`, `parsePrimary` |
| `postfixExpression`, member selection and updates | `Expressions.parsePostfix` / `parsePostfixMethod` dispatch to [`References.parseField`](../src/main/java/com/seaofnodes/simple/parser/References.java); `References.parseName` handles bare names, implicit receivers, and forward references |
| `functionExpression`, `parameterList`, calls, `argumentList` | [`Functions`](../src/main/java/com/seaofnodes/simple/parser/Functions.java): `parseFunction`, `parseConstructor`, shared `parameters` and `arguments`, then `parseBody` |
-| `newExpression` | [`Allocations.parseNew`](../src/main/java/com/seaofnodes/simple/parser/Allocations.java); the allocation-helper graph builder follows the expression routines |
+| `newExpression`, `arrayInitializer` | [`Allocations.parseNew`](../src/main/java/com/seaofnodes/simple/parser/Allocations.java), [`ArrayInitializers`](../src/main/java/com/seaofnodes/simple/parser/ArrayInitializers.java); private initialization, list checks, and generator loops |
| `stringExpression`, `interpolation` | [`Strings.parse`](../src/main/java/com/seaofnodes/simple/parser/Strings.java), using `Lexer.stringPart` / `stringFormat`; normal expression parsing resumes inside each interpolation |
| Literals, identifiers, comments, token matching | [`Lexer`](../src/main/java/com/seaofnodes/simple/parser/Lexer.java); it also supplies source-location snapshots used by diagnostics and serialized IR |
diff --git a/chapter31/docs/examples/Dijkstra.smp b/chapter31/docs/examples/Dijkstra.smp
index 768bdd08..a8fcf3da 100644
--- a/chapter31/docs/examples/Dijkstra.smp
+++ b/chapter31/docs/examples/Dijkstra.smp
@@ -3,13 +3,10 @@
// https://sampleprograms.io/projects/dijkstra/
val shortestPath = { !sys.aryi64 matrix, int nodes, int src, int dst ->
- i64 infinity = (1 << 63)-1;
- var dist = new i64[nodes];
+ i64 infinity = sys.integer.MAX_I64;
var visited = new sys.adt.bitset();
- // All nodes are infinitely far from each other
- for( int i=0; i<nodes; i++ ) dist[i] = infinity;
- // Starting point is zero far from starting point
- dist[src] = 0;
+ // All nodes are infinitely far from each other, except the starting point
+ var dist = new i64[nodes]{ int i -> i==src ? 0 : infinity; };
i64 result = -1;
int count = 0;
diff --git a/chapter31/docs/examples/TicTacToe.smp b/chapter31/docs/examples/TicTacToe.smp
index a710e440..1e0f3ca0 100644
--- a/chapter31/docs/examples/TicTacToe.smp
+++ b/chapter31/docs/examples/TicTacToe.smp
@@ -3,8 +3,7 @@ val main = { u8[~][] args ->
sys.io.p("Tic-tac-toe!\nYou are X; the CPU is O. You go first.\n");
sys.io.p("Press a square's number (no Enter needed), or q/Esc to quit.\n");
sys.io.p("Squares:\n 1 | 2 | 3 \n---+---+---\n 4 | 5 | 6 \n---+---+---\n 7 | 8 | 9 \n\n");
- val board = new u8[9];
- for( int i=0; i<board#; i++ ) board[i]=' ';
+ val board = new u8[9]{ int i -> ' '; }; // Initialize the board
// Draw empty board
_draw(board);