Simplify some `append` code
9be0a920e8deec3747399244c6d6276349f9e0d29288c8349b673aef57eb4a67
Makes use of the return value of `append`.
1 parent
c6a6897f
compiler/radiance.rad
+8 -13
| 548 | 548 | } |
|
| 549 | 549 | funcPath[desc.modPath.len - 1] = desc.fnName; |
|
| 550 | 550 | let funcArg = synthScopeAccess(arena, &funcPath[..desc.modPath.len]); |
|
| 551 | 551 | ||
| 552 | 552 | let a = ast::nodeAllocator(arena); |
|
| 553 | - | let mut args = ast::nodeSlice(arena, 3); |
|
| 554 | - | args.append(modArg, a); |
|
| 555 | - | args.append(nameArg, a); |
|
| 556 | - | args.append(funcArg, a); |
|
| 553 | + | let args = ast::nodeSlice(arena, 3) |
|
| 554 | + | .append(modArg, a) |
|
| 555 | + | .append(nameArg, a) |
|
| 556 | + | .append(funcArg, a); |
|
| 557 | 557 | ||
| 558 | 558 | return ast::synthNode(arena, ast::NodeValue::Call(ast::Call { callee, args })); |
|
| 559 | 559 | } |
|
| 560 | 560 | ||
| 561 | 561 | /// Inject a test runner into the entry package's root module. |
| 618 | 618 | target: arrayLit, mutable: false, |
|
| 619 | 619 | })); |
|
| 620 | 620 | ||
| 621 | 621 | // Build: `testing::runAllTests(&[...])`. |
|
| 622 | 622 | let runFn = synthScopeAccess(arena, &["testing", "runAllTests"]); |
|
| 623 | - | let mut callArgs = ast::nodeSlice(arena, 1); |
|
| 624 | - | callArgs.append(testsRef, a); |
|
| 623 | + | let callArgs = ast::nodeSlice(arena, 1).append(testsRef, a); |
|
| 625 | 624 | let callExpr = ast::synthNode(arena, ast::NodeValue::Call(ast::Call { |
|
| 626 | 625 | callee: runFn, args: callArgs, |
|
| 627 | 626 | })); |
|
| 628 | 627 | ||
| 629 | 628 | // Build: `return testing::runAllTests(&[...]);` |
|
| 630 | 629 | let retStmt = ast::synthNode(arena, ast::NodeValue::Return { value: callExpr }); |
|
| 631 | - | let mut bodyStmts = ast::nodeSlice(arena, 1); |
|
| 632 | - | bodyStmts.append(retStmt, a); |
|
| 630 | + | let bodyStmts = ast::nodeSlice(arena, 1).append(retStmt, a); |
|
| 633 | 631 | let fnBody = ast::synthNode(arena, ast::NodeValue::Block(ast::Block { statements: bodyStmts })); |
|
| 634 | 632 | ||
| 635 | 633 | // Build: `fn #testMain() -> i32` |
|
| 636 | 634 | let fnName = ast::synthNode(arena, ast::NodeValue::Ident(strings::intern(&mut STRING_POOL, "#testMain"))); |
|
| 637 | 635 | let returnType = ast::synthNode(arena, ast::NodeValue::TypeSig(ast::TypeSig::Integer { |
| 643 | 641 | throwList: ast::nodeSlice(arena, 0), |
|
| 644 | 642 | }; |
|
| 645 | 643 | ||
| 646 | 644 | // `@default` attribute. |
|
| 647 | 645 | let attrNode = ast::synthNode(arena, ast::NodeValue::Attribute(ast::Attribute::Default)); |
|
| 648 | - | let mut attrList = ast::nodeSlice(arena, 1); |
|
| 649 | - | attrList.append(attrNode, a); |
|
| 646 | + | let attrList = ast::nodeSlice(arena, 1).append(attrNode, a); |
|
| 650 | 647 | let fnAttrs = ast::Attributes { list: attrList }; |
|
| 651 | 648 | ||
| 652 | 649 | return ast::synthNode(arena, ast::NodeValue::FnDecl(ast::FnDecl { |
|
| 653 | 650 | name: fnName, sig: fnSig, body: fnBody, attrs: fnAttrs, |
|
| 654 | 651 | })); |
| 661 | 658 | decl: *ast::Node |
|
| 662 | 659 | ) { |
|
| 663 | 660 | let case ast::NodeValue::Block(block) = blockNode.value else { |
|
| 664 | 661 | panic "injectIntoBlock: expected Block node"; |
|
| 665 | 662 | }; |
|
| 666 | - | let mut stmts = block.statements; |
|
| 667 | - | stmts.append(decl, ast::nodeAllocator(arena)); |
|
| 668 | - | ||
| 663 | + | let stmts = block.statements.append(decl, ast::nodeAllocator(arena)); |
|
| 669 | 664 | blockNode.value = ast::NodeValue::Block(ast::Block { statements: stmts }); |
|
| 670 | 665 | } |
|
| 671 | 666 | ||
| 672 | 667 | /// Write code buffer to file as raw bytes. |
|
| 673 | 668 | fn writeCode(code: *[u32], path: *[u8]) -> bool { |
lib/std/lang/parser.rad
+2 -5
| 368 | 368 | return node(p, ast::NodeValue::ArrayRepeatLit( |
|
| 369 | 369 | ast::ArrayRepeatLit { item: firstExpr, count } |
|
| 370 | 370 | )); |
|
| 371 | 371 | } |
|
| 372 | 372 | // Regular array literal: `[a, b, ...]`. |
|
| 373 | - | let mut items = ast::nodeSlice(p.arena, 64); |
|
| 374 | - | items.append(firstExpr, p.allocator); |
|
| 373 | + | let mut items = ast::nodeSlice(p.arena, 64).append(firstExpr, p.allocator); |
|
| 375 | 374 | ||
| 376 | 375 | while consume(p, scanner::TokenKind::Comma) and not check(p, scanner::TokenKind::RBracket) { |
|
| 377 | 376 | let elem = try parseExpr(p); |
|
| 378 | 377 | items.append(elem, p.allocator); |
|
| 379 | 378 | } |
| 1094 | 1093 | return ast::Block { statements: ast::nodeSlice(p.arena, cap) }; |
|
| 1095 | 1094 | } |
|
| 1096 | 1095 | ||
| 1097 | 1096 | /// Create a block containing a single statement node. |
|
| 1098 | 1097 | fn mkBlockWith(p: *mut Parser, node: *ast::Node) -> ast::Block { |
|
| 1099 | - | let mut stmts = ast::nodeSlice(p.arena, 1); |
|
| 1100 | - | stmts.append(node, p.allocator); |
|
| 1101 | - | ||
| 1098 | + | let stmts = ast::nodeSlice(p.arena, 1).append(node, p.allocator); |
|
| 1102 | 1099 | return ast::Block { statements: stmts }; |
|
| 1103 | 1100 | } |
|
| 1104 | 1101 | ||
| 1105 | 1102 | /// Parse the branch that follows `else` in let-else style constructs. |
|
| 1106 | 1103 | /// |
lib/std/lang/resolver.rad
+1 -2
| 6161 | 6161 | /// Analyze a standalone expression by wrapping it in a synthetic function. |
|
| 6162 | 6162 | pub fn resolveExpr( |
|
| 6163 | 6163 | self: *mut Resolver, expr: *ast::Node, arena: *mut ast::NodeArena |
|
| 6164 | 6164 | ) -> Diagnostics throws (ResolveError) { |
|
| 6165 | 6165 | let a = ast::nodeAllocator(arena); |
|
| 6166 | - | let mut bodyStmts = ast::nodeSlice(arena, 1); |
|
| 6167 | 6166 | let exprStmt = ast::synthNode(arena, ast::NodeValue::ExprStmt(expr)); |
|
| 6168 | - | bodyStmts.append(exprStmt, a); |
|
| 6167 | + | let bodyStmts = ast::nodeSlice(arena, 1).append(exprStmt, a); |
|
| 6169 | 6168 | let module = ast::synthFnModule(arena, ANALYZE_EXPR_FN_NAME, bodyStmts); |
|
| 6170 | 6169 | ||
| 6171 | 6170 | let case ast::NodeValue::Block(block) = module.modBody.value |
|
| 6172 | 6171 | else panic "resolveExpr: expected block for module body"; |
|
| 6173 | 6172 | enterScope(self, module.modBody); |