-
Notifications
You must be signed in to change notification settings - Fork 4
/
sttypes.go
172 lines (152 loc) · 3.84 KB
/
sttypes.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package stconverter
import (
"github.com/PRETgroup/goFB/iec61499/postfix"
)
//STExpression is an interface defining an assignments and comparison function tree
//E.g. A := (2 + y) can be defined using STExpressions
type STExpression interface {
HasValue() string //IsValue reflects that this STExpression is an STExpressionValue, and is just a single variable or value
HasOperator() postfix.Operator
GetArguments() []STExpression
IsInstruction() bool
}
//STExpressionValue is a type of STExpression that is just a single variable or value (i.e. an operand)
type STExpressionValue struct {
Value string
}
//HasValue returns the internal value
func (s STExpressionValue) HasValue() string {
return s.Value
}
//HasOperator returns nothing, as an STExpressionValue is not an operator
func (s STExpressionValue) HasOperator() postfix.Operator {
return nil
}
//GetArguments returns the internal value as a single-element slice
func (s STExpressionValue) GetArguments() []STExpression {
return []STExpression{s}
}
//STExpressionOperator is a type of STExpression that is an operator or a function with a list of arguments
type STExpressionOperator struct {
Operator postfix.Operator
Arguments []STExpression
}
//HasValue returns nothing, as STExpressionOperator is not a value
func (s STExpressionOperator) HasValue() string {
return ""
}
//HasOperator returns the internal operator
func (s STExpressionOperator) HasOperator() postfix.Operator {
return s.Operator
}
//GetArguments returns the list of arguments
func (s STExpressionOperator) GetArguments() []STExpression {
return s.Arguments
}
//STIfThen is used as part of an STIfElsIfElse
// it holds the if [boolean expression] then <statements...>; part
type STIfThen struct {
IfExpression STExpression
ThenSequence []STInstruction
}
//STIfElsIfElse is used to make up the full if... elsif... elsif.... else... sequence
// the ifThens are evaluated in order
//example:
/*
IF [boolean expression] THEN
<statement>;
ELSIF [boolean expression] THEN
<statement>;
ELSE
<statement>;
END_IF ;
A := 0;
IF A = 0 THEN
B := 0;
END_IF ;
*/
type STIfElsIfElse struct {
IfThens []STIfThen
ElseSequence []STInstruction
}
//STCase is used inside STSwitchCase to store the different case options
type STCase struct {
CaseValues []string
Sequence []STInstruction
}
//STSwitchCase is used for the switch... case... case... else sequence
//examples:
/*
CASE [numeric expression] OF
result1, result2: <statement>;
resultN[, resultN]: <statemtent>;
ELSE
<statement>;
END_CASE;
CASE StateMachine OF
1:
StateMachine := 2;
2:
StateMachine := 1;
ELSE
StateMachine := 1;
END_CASE;
*/
type STSwitchCase struct {
SwitchOn STExpression
Cases []STCase
ElseSequence []STInstruction
}
//STForLoop is used for for loops
//Example:
/*
FOR count := initial_value TO final_value BY increment DO
<statement>;
END_FOR;
*/
type STForLoop struct {
ForAssignment STExpression
ToValue STExpression
ByIncrement STExpression
Sequence []STInstruction
}
//FindCounterName will return the variable name assigned to in the ForAssignment, if one can easily be found
func (f STForLoop) FindCounterName() string {
if f.ForAssignment == nil {
return ""
}
faop := f.ForAssignment.HasOperator()
if faop == nil {
return ""
}
if faop.GetToken() != stAssignment {
return ""
}
if len(f.ForAssignment.GetArguments()) < 2 {
return ""
}
return f.ForAssignment.GetArguments()[1].HasValue()
}
//STWhileLoop is used for while loops
//Example:
/*
WHILE [boolean expression] DO
<statement>;
END_WHILE;
*/
type STWhileLoop struct {
WhileExpression STExpression
Sequence []STInstruction
}
//STRepeatLoop is used for Repeat....Until loops
//Example:
/*
REPEAT
<statement>;
UNTIL [boolean expression]
END_REPEAT;
*/
type STRepeatLoop struct {
UntilExpression STExpression
Sequence []STInstruction
}