/
switch.go
105 lines (85 loc) · 1.91 KB
/
switch.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2020 Salif Mehmed
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package ast
import (
"strings"
"github.com/i5/i5/src/constants"
)
type Switch struct {
line uint32
token string
condition Node
cases []Case
}
func (this Switch) GetType() string {
return SWITCH
}
func (this Switch) Debug() string {
var result strings.Builder
result.WriteString(this.token)
result.WriteString(" ")
result.WriteString(this.condition.Debug())
result.WriteString("{")
var n []string
for _, i := range this.cases {
n = append(n, i.Debug())
}
result.WriteString(strings.Join(n, "; "))
result.WriteString("}")
return result.String()
}
func (this Switch) GetLine() uint32 {
return this.line
}
func (this Switch) Init(line uint32, token string) Switch {
this.line = line
this.token = token
return this
}
func (this Switch) GetCondition() Node {
return this.condition
}
func (this Switch) GetCases() []Case {
return this.cases
}
func (this *Switch) SetCondition(condition Node) {
this.condition = condition
}
func (this *Switch) SetCases(cases []Case) {
this.cases = cases
}
type Case struct {
line uint32
_case Node
body Block
}
func (this Case) Debug() string {
var result strings.Builder
result.WriteString(constants.TOKEN_CASE)
result.WriteString(" ")
result.WriteString(this._case.Debug())
result.WriteString(" => ")
result.WriteString(this.body.Debug())
return result.String()
}
func (this Case) GetLine() uint32 {
return this.line
}
func (this Case) Init(line uint32) Case {
this.line = line
return this
}
func (this Case) GetCase() Node {
return this._case
}
func (this Case) GetBody() Node {
return this.body
}
func (this *Case) SetCase(_case Node) {
this._case = _case
}
func (this *Case) SetBody(body Block) {
this.body = body
}