Skip to content

Commit

Permalink
fix(c): Prevent c error if blank identifier is used (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 authored Jan 14, 2025
1 parent a8bde3c commit 0fe4a12
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All notable changes are documented in this file.
### Other Changes
- fix: `static` visibility now controlled by `pub` keyword
- lexer: Disallow floats with trailing deciaml point
- fix(c): Prevent c error if blank identifier is used


## 0.0.8 - 2024-12-25
Expand Down
2 changes: 1 addition & 1 deletion lib/bait/gen/c/expr.bt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun (mut g Gen) expr(expr ast.Expr) {
ast.AnonFun { g.anon_fun(expr) }
ast.ArrayInit { g.array_init(expr) }
ast.AsCast { g.as_cast(expr) }
ast.BlankIdent { g.write(g.new_temp_var()) }
ast.BlankIdent { g.write("_") }
ast.BoolLiteral { g.bool_literal(expr) }
ast.CallExpr { g.call_expr(expr) }
ast.CharLiteral { g.char_literal(expr) }
Expand Down
13 changes: 11 additions & 2 deletions lib/bait/transformer/transformer.bt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fun (t Transformer) stmts(mut stmts []ast.Stmt) {
fun (t Transformer) stmt(mut stmt ast.Stmt) ast.Stmt {
match stmt {
ast.AssertStmt { t.assert_stmt(mut stmt) }
ast.AssignStmt { t.assign_stmt(mut 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) }
Expand Down Expand Up @@ -89,7 +89,7 @@ fun (t Transformer) assert_stmt(mut node ast.AssertStmt) {
node.expr = t.expr(mut node.expr)
}

fun (t Transformer) assign_stmt(mut node ast.AssignStmt) {
fun (t Transformer) assign_stmt(mut node ast.AssignStmt) ast.Stmt {
// JS: allow for correct integer rounding
if t.prefs.backend == .js and node.op == .div_assign {
node.op = .assign
Expand All @@ -103,6 +103,15 @@ fun (t Transformer) assign_stmt(mut node ast.AssignStmt) {

node.left = t.expr(node.left)
node.right = t.expr(node.right)

if t.prefs.backend == .c and node.left is ast.BlankIdent {
node.op = .decl_assign
return ast.Block{
stmts = [node as ast.Stmt]
}
}

return node
}

fun (t Transformer) const_decl(node ast.ConstDecl) ast.Stmt {
Expand Down

0 comments on commit 0fe4a12

Please sign in to comment.