-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_func.go
59 lines (49 loc) · 1.2 KB
/
block_func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package codegen
import "strings"
type funcBlock struct {
name string
params []*ParamDecl
retTypes []*TypeDecl
stmts []Stmt
newLine bool
}
// Func creates a new function code block
func (f *File) Func(name string) *funcBlock {
fnc := newFunc(name)
f.append(fnc)
return fnc
}
// Params appends function parameters
func (f *funcBlock) Params(params ...*ParamDecl) *funcBlock {
f.params = params
return f
}
// ReturnTypes appends function return parameters
func (f *funcBlock) ReturnTypes(returnTypes ...*TypeDecl) *funcBlock {
f.retTypes = returnTypes
return f
}
// Block appends the function block
func (f *funcBlock) Block(stmts ...Stmt) *funcBlock {
f.stmts = stmts
return f
}
// AddStatement adds a new statement to the function block
func (f *funcBlock) AddStatement(stmt Stmt) {
f.stmts = append(f.stmts, stmt)
}
func newFunc(name string) *funcBlock {
return &funcBlock{
name: name,
params: make([]*ParamDecl, 0),
retTypes: make([]*TypeDecl, 0),
stmts: make([]Stmt, 0),
newLine: true,
}
}
func (f *funcBlock) write(sb *strings.Builder) {
writeF(sb, "func %s", f.name)
writeParams(sb, f.params)
writeReturnTypes(sb, f.retTypes)
writeStmtsBlock(sb, f.stmts, f.newLine)
}