github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/transpiler/goto.go (about)

     1  // This file contains functions for transpiling goto/label statements.
     2  
     3  package transpiler
     4  
     5  import (
     6  	goast "go/ast"
     7  	"go/token"
     8  
     9  	"github.com/Konstantin8105/c4go/ast"
    10  	"github.com/Konstantin8105/c4go/program"
    11  	"github.com/Konstantin8105/c4go/util"
    12  )
    13  
    14  func transpileLabelStmt(n *ast.LabelStmt, p *program.Program) (*goast.LabeledStmt, []goast.Stmt, []goast.Stmt, error) {
    15  
    16  	var post []goast.Stmt
    17  	for _, node := range n.Children() {
    18  		var stmt goast.Stmt
    19  		stmt, preStmts, postStmts, err := transpileToStmt(node, p)
    20  		if err != nil {
    21  			return nil, nil, nil, err
    22  		}
    23  		post = combineStmts(preStmts, stmt, postStmts)
    24  	}
    25  
    26  	return &goast.LabeledStmt{
    27  		Label: util.NewIdent(n.Name),
    28  		Stmt:  &goast.EmptyStmt{},
    29  	}, []goast.Stmt{}, post, nil
    30  }
    31  
    32  func transpileGotoStmt(n *ast.GotoStmt, p *program.Program) (*goast.BranchStmt, error) {
    33  	return &goast.BranchStmt{
    34  		Label: util.NewIdent(n.Name),
    35  		Tok:   token.GOTO,
    36  	}, nil
    37  }