-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast_def.go
58 lines (49 loc) · 921 Bytes
/
ast_def.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
package spiker
import (
"fmt"
"strings"
)
// NodeFuncDef function define
type NodeFuncDef struct {
Ast
Name NodeVariable
Params []NodeParam
Body []AstNode
SingleStmt bool
}
// Format .
func (fn NodeFuncDef) Format() string {
var ps []string
for _, v := range fn.Params {
ps = append(ps, v.Format())
}
var p = strings.Join(ps, ", ")
if len(ps) > 1 {
p = "(" + p + ")"
}
var b string
var l = len(fn.Body)
if l == 0 {
b = "{}"
} else if l == 1 && fn.SingleStmt {
b = fn.Body[0].Format()
} else {
var str = formatBody(fn.Body)
b = "{\n" + str + "}"
}
return fmt.Sprintf("%s = %s -> %s;", fn.Name.Format(), p, b)
}
// NodeParam function param
type NodeParam struct {
Ast
Default interface{}
Name NodeVariable
}
// Format .
func (p NodeParam) Format() string {
f := p.Name.Format()
if p.Default != nil {
f += " = " + Interface2String(p.Default)
}
return f
}