Skip to content

Commit 877f072

Browse files
committed
refactor(compiler): flatten bracket literal dispatch
Separate solved Array and Table lowering from the shared AST entry point, using guard clauses for array strategy selection.
1 parent 722e392 commit 877f072

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

compiler/array.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,13 @@ func (c *Compiler) ArrayGetBorrowed(arr *Symbol, elem Type, idx llvm.Value) llvm
277277

278278
// compileArrayExpression materializes a bracket literal according to its solved
279279
// Array or Table type.
280-
func (c *Compiler) compileArrayExpression(e *ast.ArrayLiteral, _ []*ast.Identifier) (res []*Symbol) {
280+
func (c *Compiler) compileArrayExpression(e *ast.ArrayLiteral, _ []*ast.Identifier) []*Symbol {
281281
lit, info := c.resolveArrayLiteralRewrite(e)
282282
switch typ := info.OutTypes[0].(type) {
283283
case Array:
284-
if typ.Rank > 1 {
285-
if c.arrayLiteralHasArrayCells(lit) {
286-
if len(info.CollectRanges) > 0 {
287-
return []*Symbol{c.compileStackedArrayCollector(lit, info, typ)}
288-
}
289-
return []*Symbol{c.compileStackedArrayLiteral(lit, typ)}
290-
}
291-
return []*Symbol{c.compileRectangularArrayLiteral(lit, typ)}
292-
}
293-
return []*Symbol{c.compileArrayLiteralInDomain(lit, info, nil, nil)}
284+
return []*Symbol{c.compileArray(lit, info, typ)}
294285
case Table:
295-
return []*Symbol{c.compileTableLiteral(lit, typ)}
286+
return []*Symbol{c.compileTable(lit, typ)}
296287
default:
297288
c.Errors = append(c.Errors, &token.CompileError{
298289
Token: lit.Tok(),
@@ -302,6 +293,19 @@ func (c *Compiler) compileArrayExpression(e *ast.ArrayLiteral, _ []*ast.Identifi
302293
}
303294
}
304295

296+
func (c *Compiler) compileArray(lit *ast.ArrayLiteral, info *ExprInfo, arrayType Array) *Symbol {
297+
if arrayType.Rank == 1 {
298+
return c.compileArrayLiteralInDomain(lit, info, nil, nil)
299+
}
300+
if !c.arrayLiteralHasArrayCells(lit) {
301+
return c.compileRectangularArrayLiteral(lit, arrayType)
302+
}
303+
if len(info.CollectRanges) == 0 {
304+
return c.compileStackedArrayLiteral(lit, arrayType)
305+
}
306+
return c.compileStackedArrayCollector(lit, info, arrayType)
307+
}
308+
305309
func (c *Compiler) arrayLiteralHasArrayCells(lit *ast.ArrayLiteral) bool {
306310
for _, row := range lit.Rows {
307311
for _, cell := range row {

compiler/table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"tinygo.org/x/go-llvm"
66
)
77

8-
func (c *Compiler) compileTableLiteral(lit *ast.ArrayLiteral, tableType Table) *Symbol {
8+
func (c *Compiler) compileTable(lit *ast.ArrayLiteral, tableType Table) *Symbol {
99
if len(lit.Rows) == 0 {
1010
return c.makeZeroValue(tableType)
1111
}

0 commit comments

Comments
 (0)