Skip to content

Commit

Permalink
transformer: Fix range slicing in if expr (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 authored Jan 28, 2025
1 parent 0cc9457 commit bb6caa0
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/bait/ast/ast.bt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct EnumField {
}

pub struct ExprStmt {
pub expr Expr
global expr Expr
global typ Type
}

Expand Down
2 changes: 0 additions & 2 deletions lib/bait/builder/builder.bt
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ pub fun compile(prefs preference.Prefs) i32 {
return 1
}

// TODO sema_ctx.free()

// Transformer
timers.start('TRANSFORM')
mut trans := transformer.Transformer{
Expand Down
2 changes: 0 additions & 2 deletions lib/bait/gen/c/cgen.bt
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ fun (mut g Gen) save_stmt_offset() {
g.stmt_offsets.push(g.out.length)
}

// TODO evaluate where c_esc can be replaced by this (see also jsgen)
// TODO consider splitting this up even further for different places
fun c_name(n string) string {
return n.replace('.', '__').replace('[]', 'Array_')
}
Expand Down
2 changes: 0 additions & 2 deletions lib/bait/gen/js/jsgen.bt
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ fun (g Gen) concrete_sym(typ ast.Type) ast.TypeSymbol {
return g.table.get_sym(g.cur_concrete_types[sym.name])
}

// TODO evaluate where js_esc can be replaced by this (see also cgen)
// TODO consider splitting this up even further for different places
fun js_name(n string) string {
return n.replace('.', '__').replace('[]', 'Array_').replace('[', '_').replace(']', '_')
}
Expand Down
14 changes: 6 additions & 8 deletions lib/bait/transformer/transformer.bt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fun (mut t Transformer) transform_files(mut files []ast.File) {
pub fun (mut t Transformer) stmts(mut stmts []ast.Stmt) {
// TODO js: fix crash with InvalidExpr
// for mut stmt in stmts {
// stmt = t.stmt(stmt)
// stmt = t.stmt(mut stmt)
// }

for i := 0; i < stmts.length; i += 1 {
Expand All @@ -41,7 +41,7 @@ fun (mut t Transformer) stmt(mut stmt ast.Stmt) ast.Stmt {
ast.AssignStmt { return t.assign_stmt(mut stmt) }
ast.Block { t.stmts(mut stmt.stmts) }
ast.ConstDecl { return t.const_decl(stmt) }
ast.ExprStmt { _ = t.expr(mut stmt.expr) }
ast.ExprStmt { stmt.expr = t.expr(mut stmt.expr) }
ast.EnumDecl {}
ast.ForLoop { t.for_loop(mut stmt) }
ast.ForClassicLoop { t.for_classic_loop(mut stmt) }
Expand Down Expand Up @@ -74,7 +74,7 @@ fun (mut t Transformer) expr(mut expr ast.Expr) ast.Expr {
ast.FloatLiteral {}
ast.HashExpr {}
ast.Ident {}
ast.IfMatch {}
ast.IfMatch { t.if_match(mut expr) }
ast.IndexExpr { return t.index_expr(expr) }
ast.InfixExpr { t.infix_expr(mut expr) }
ast.IntegerLiteral {}
Expand Down Expand Up @@ -182,8 +182,7 @@ fun (mut t Transformer) for_in_loop(mut node ast.ForInLoop) ast.ForInLoop {
index = node.idxvar
}
} as ast.Stmt
// TODO array.insert(el, idx)
node.stmts = [val_decl].concat(node.stmts)
node.stmts.insert(0, val_decl)

t.stmts(node.stmts)

Expand All @@ -201,6 +200,7 @@ fun (mut t Transformer) fun_decl(mut node ast.FunDecl) ast.Stmt {

fun (mut t Transformer) if_match(mut node ast.IfMatch) {
for mut branch in node.branches {
_ = t.expr(branch.cond)
t.stmts(branch.stmts)
}
}
Expand All @@ -226,9 +226,7 @@ fun (mut t Transformer) call_expr(mut node ast.CallExpr) {
field_name = "msg"
}
} as ast.Stmt

// TODO array.insert(el, idx)
node.or_block.stmts = [err_decl].concat(node.or_block.stmts)
node.or_block.stmts.insert(0, err_decl)

// Last line: Set Result data
// `_t12.data = <expr>`
Expand Down
6 changes: 6 additions & 0 deletions tests/array/slicing_test.bt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ fun test_array_slice_syntax() {

assert a[2..4] == [3, 4]
}

fun test_slice_in_if_expr() {
a := [1, 2, 3]
b := if true { a[0 + 1..] } else { a }
assert b.length == 2
}

0 comments on commit bb6caa0

Please sign in to comment.