@@ -33,20 +33,18 @@ type IExpression interface {
3333type Expression struct {
3434}
3535
36+ // Interpret : 解釋器, 參數多帶一個IExpression 讓繼承的可以使用他的Execute
3637func (e * Expression ) Interpret (context * PlayContext , ie IExpression ) {
37- // fmt.Println("context.Text() ", context.Text())
3838 if len (context .Text ()) == 1 {
3939 return
4040 }
4141 playKey := context .Text ()[0 :1 ]
4242 context .SetText (context .Text ()[2 :])
4343 spaceIndex := strings .Index (context .Text (), " " )
44- // fmt.Println("spaceIndex ", spaceIndex)
4544 if spaceIndex < 0 {
4645 spaceIndex = 0
4746 }
4847 playVal , _ := strconv .Atoi (string (context .Text ()[0 :spaceIndex ]))
49- // fmt.Println("playVal", string(context.Text()[0]))
5048 context .SetText (context .Text ()[strings .Index (context .Text (), " " )+ 1 :])
5149
5250 ie .Execute (playKey , playVal )
@@ -81,7 +79,7 @@ func (n *Note) Execute(key string, val int) {
8179 case "B" :
8280 note = "7"
8381 }
84- fmt .Printf ("%s" , note )
82+ fmt .Printf ("%s " , note )
8583}
8684
8785type Scale struct {
@@ -102,5 +100,25 @@ func (s *Scale) Execute(key string, val int) {
102100 case 3 :
103101 scale = "高音"
104102 }
105- fmt .Printf ("%s" , scale )
103+ fmt .Printf ("%s " , scale )
104+ }
105+
106+ type Speed struct {
107+ Expression
108+ }
109+
110+ func NewSpeed () * Speed {
111+ return & Speed {Expression {}}
112+ }
113+
114+ func (s * Speed ) Execute (key string , val int ) {
115+ var speed string
116+ if val < 500 {
117+ speed = "快速"
118+ } else if val >= 1000 {
119+ speed = "慢速"
120+ } else {
121+ speed = "中速"
122+ }
123+ fmt .Printf ("%s " , speed )
106124}
0 commit comments