statements.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package generator
  2. import (
  3. "go/ast"
  4. "go/token"
  5. )
  6. func wrapExprsWithStmt(exps []ast.Expr) []ast.Stmt {
  7. out := make([]ast.Stmt, len(exps))
  8. for i, v := range exps {
  9. out[i] = makeExprStmt(v)
  10. }
  11. return out
  12. }
  13. func makeBlockStmt(statements []ast.Stmt) *ast.BlockStmt {
  14. return &ast.BlockStmt{List: statements}
  15. }
  16. func makeExprStmt(exp ast.Expr) ast.Stmt {
  17. return &ast.ExprStmt{X: exp}
  18. }
  19. func makeIfStmt(cond ast.Expr, body *ast.BlockStmt, otherwise ast.Stmt) *ast.IfStmt {
  20. return &ast.IfStmt{
  21. Cond: cond,
  22. Body: body,
  23. Else: otherwise,
  24. }
  25. }
  26. func makeAssignStmt(names, vals []ast.Expr, assignType token.Token) *ast.AssignStmt {
  27. return &ast.AssignStmt{
  28. Lhs: names,
  29. Rhs: vals,
  30. Tok: assignType,
  31. }
  32. }
  33. func makeBranchStmt(tok token.Token, labelName *ast.Ident) *ast.BranchStmt {
  34. return &ast.BranchStmt{
  35. Tok: tok,
  36. Label: labelName,
  37. }
  38. }
  39. func makeLabeledStmt(labelName *ast.Ident, stmt ast.Stmt) *ast.LabeledStmt {
  40. return &ast.LabeledStmt{
  41. Label: labelName,
  42. Stmt: stmt,
  43. }
  44. }
  45. func makeForStmt(init, post ast.Stmt, cond ast.Expr, body *ast.BlockStmt) *ast.ForStmt {
  46. return &ast.ForStmt{
  47. Init: init,
  48. Post: post,
  49. Cond: cond,
  50. Body: body,
  51. }
  52. }