-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_lang_utils.go
266 lines (218 loc) · 8.01 KB
/
gf_lang_utils.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
GloFlow application and media management/publishing platform
Copyright (C) 2022 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_lang
import (
"errors"
"fmt"
"math"
"math/rand"
"github.com/gloflow/gloflow/go/gf_core"
)
//-------------------------------------------------
// RULES
//-------------------------------------------------
func getRulesAllNames(pRulesDefsMap GFruleDefs) []string {
rulesNamesLst := []string{}
for k, _ := range pRulesDefsMap {
rulesNamesLst = append(rulesNamesLst, k)
}
return rulesNamesLst
}
func ruleGetItersNum(pState *GFstate) int {
return pState.RulesItersNumStackLst[len(pState.RulesItersNumStackLst)-1]
}
func ruleGetName(pState *GFstate) (string, error) {
if len(pState.RulesNamesStackLst) == 0 {
return "", errors.New("no rules in rules stack")
}
ruleNameStr := pState.RulesNamesStackLst[len(pState.RulesNamesStackLst)-1]
return ruleNameStr, nil
}
//-------------------------------------------------
func incrementItersNum(pState *GFstate) int {
newRuleItersNumInt := ruleGetItersNum(pState) + 1
pState.RulesItersNumStackLst[len(pState.RulesItersNumStackLst)-1] = newRuleItersNumInt
pState.VarsMap["$i"].Val = newRuleItersNumInt
return newRuleItersNumInt
}
func addNewItersNumState(pState *GFstate) {
pState.RulesItersNumStackLst = append(pState.RulesItersNumStackLst, 0)
pState.VarsMap["$i"].Val = 0
}
//-------------------------------------------------
// called when one rule exits (finishes executing) and returns execution
// to its parent rule (not when the same rule recurses into itself).
func restorePreviousRulesItersNum(pState *GFstate) {
_, newRulesItersNumStackLst := gf_core.ListPop(pState.RulesItersNumStackLst)
pState.RulesItersNumStackLst = newRulesItersNumStackLst
// reinitialize $i to the parents number of iterations
pState.VarsMap["$i"].Val = pState.RulesItersNumStackLst[len(pState.RulesItersNumStackLst)-1]
}
//-------------------------------------------------
func pickRuleRandomDef(pRuleNameStr string,
pRulesDefsMap GFruleDefs) (*GFruleDef, []interface{}) {
ruleDefsLst := pRulesDefsMap[pRuleNameStr]
ruleDefsNumInt := len(ruleDefsLst)
randomRuleDefIndexInt := int(math.Floor(rand.Float64() * float64(ruleDefsNumInt)))
ruleDef := ruleDefsLst[randomRuleDefIndexInt]
ruleExpressionsLst := ruleDef.ExpressionsLst
return ruleDef, ruleExpressionsLst
}
//-------------------------------------------------
// CLONING
//-------------------------------------------------
func cloneExpr(pExprLst GFexpr) GFexpr {
copyLst := make([]interface{}, len(pExprLst))
copy(copyLst, pExprLst)
return copyLst
}
//-------------------------------------------------
func cloneExprNtimes(pExprLst GFexpr, pNint int) GFexpr {
clonesLst := []interface{}{}
for i := 0; i < pNint; i++ {
clonedExprLst := cloneExpr(pExprLst)
clonesLst = append(clonesLst, clonedExprLst)
}
return GFexpr(clonesLst)
}
//-------------------------------------------------
func cloneItersNumStack(pRulesItersNumStackLst []int) []int {
copyLst := make([]int, len(pRulesItersNumStackLst))
copy(copyLst, pRulesItersNumStackLst)
return copyLst
}
func cloneRulesNamesStack(pRulesNamesStackLst []string) []string {
copyLst := make([]string, len(pRulesNamesStackLst))
copy(copyLst, pRulesNamesStackLst)
return copyLst
}
// clone program vars. this still assumes simple variables with primitive values.
func cloneAnimations(pAnimationsMap map[string]interface{}) map[string]interface{} {
cloneMap := map[string]interface{}{}
for k, v := range pAnimationsMap {
cloneMap[k] = v
}
return cloneMap
}
//-------------------------------------------------
// SYMBOLS
//-------------------------------------------------
func getSymbolsAndConstants() *GFsymbols {
ruleLevelMaxInt := 250
systemRulesLst := []string{
"cube",
"sphere",
"line",
}
predefinedPropertiesLst := []string{
"x", // x-coordinate
"y", // y-coordinate
"z", // z-coordinate
"rx", // x-rotation
"ry", // y-rotation
"rz", // z-rotation
"sx", // x-scale
"sy", // y-scale
"sz", // z-scale
"cr", // red-channel-color
"cg", // green-channel-color
"cb", // blue-channel-color
}
logicOperatorsMap := map[string]func(float64, float64) bool {
"==": func(p1 float64, p2 float64) bool {return p1 == p2},
"!=": func(p1 float64, p2 float64) bool {return p1 != p2},
"<": func(p1 float64, p2 float64) bool {return p1 < p2},
">": func(p1 float64, p2 float64) bool {return p1 > p2},
"<=": func(p1 float64, p2 float64) bool {return p1 <= p2},
">=": func(p1 float64, p2 float64) bool {return p1 >= p2},
}
arithmeticOpsMap := map[string]func(float64, float64) float64{
"+": func(p1 float64, p2 float64) float64 {return p1 + p2},
"-": func(p1 float64, p2 float64) float64 {return p1 - p2},
"*": func(p1 float64, p2 float64) float64 {return p1 * p2},
"/": func(p1 float64, p2 float64) float64 {return p1 / p2},
"%": func(p1 float64, p2 float64) float64 {return float64(int(p1) % int(p2))},
}
systemVarsLst := []string{
"$i", // current rule iteration
}
systemFunctionsLst := getSysFunctionNames()
symbols := &GFsymbols{
RuleLevelMaxInt: ruleLevelMaxInt,
SystemRulesLst: systemRulesLst,
PredefinedPropertiesLst: predefinedPropertiesLst,
LogicOperatorsMap: logicOperatorsMap,
ArithmeticOpsMap: arithmeticOpsMap,
SystemVarsLst: systemVarsLst,
SystemFunctionsLst: systemFunctionsLst,
}
return symbols
}
//-------------------------------------------------
// CHECKS
//-------------------------------------------------
func checkIsArithmeticOp(pOpToCheckStr string) bool {
for opStr, _ := range getSymbolsAndConstants().ArithmeticOpsMap {
if pOpToCheckStr == opStr {
return true
}
}
return false
}
//-------------------------------------------------
func checkIsReturnExpr(pExprLst GFexpr) bool {
if pExprLst[0].(string) == "return" {
return true
}
return false
}
//-------------------------------------------------
// CASTS
//-------------------------------------------------
func CastToExpr(pExprLst []interface{}) GFexpr {
exprLst := []interface{}{}
for _, el := range pExprLst {
if eLst, ok := el.([]interface{}); ok {
exprCastedLst := CastToExpr(eLst)
exprLst = append(exprLst, exprCastedLst)
} else {
// not an expression, so no need to cast
exprLst = append(exprLst, el)
}
}
return GFexpr(exprLst)
}
//-------------------------------------------------
func castToFloat(pN interface{}) float64 {
if nInt, ok := pN.(int); ok {
return float64(nInt)
}
if nF, ok := pN.(float64); ok {
return nF
}
panic(fmt.Sprintf("number %s is not a int or float64", pN))
return 0.0
}
func castToInt(pN interface{}) int {
if nInt, ok := pN.(int); ok {
return nInt
}
if nF, ok := pN.(float64); ok {
return int(nF)
}
panic(fmt.Sprintf("number %s is not a int or float64", pN))
return 0
}