filtress/receivers.go

97 lines
1.6 KiB
Go

package main
import (
"fmt"
"tildegit.org/sloum/filtress/parser"
)
func (f *filter) update(change parser.Expression, vars map[string]int) {
opand := change.Opperand
if change.Variable != "" {
if val, ok := vars[change.Variable]; ok {
opand = val
} else {
panic(fmt.Sprintf("Runtime error: Encountered unknown variable %q", val))
}
}
switch change.Opperator {
case '+':
f.val += opand
case '-':
f.val -= opand
case '*':
f.val *= opand
case '/':
if opand == 0 {
f.val = 1
} else {
f.val /= opand
}
case '=':
f.val = opand
}
if f.val > f.max {
f.val %= f.max
}
if f.val < f.min {
f.val = f.max - f.val%f.max
}
}
func (p *point) update(change []parser.Expression, vars map[string]int) {
var totalX, totalY, val, opand, max int
var target *int
for i, e := range change {
if i == 0 {
target = &totalX
val = p.x
max = p.maxX
} else if i == 1 {
target = &totalY
val = p.y
max = p.maxY
} else {
break
}
if e.Variable != "" {
if val, ok := vars[e.Variable]; ok {
opand = val
} else {
panic(fmt.Sprintf("Runtime error: Encountered unknown variable %q", val))
}
} else {
opand = e.Opperand
}
switch e.Opperator {
case '+':
*target = val + opand
case '-':
*target = val - opand
case '*':
*target = val * opand
case '/':
if opand == 0 {
*target = 1
} else {
*target = val / opand
}
case '=':
*target = opand
}
if *target > max {
*target %= max
}
if *target < 0 {
*target = max - *target%max
}
}
p.x = totalX
p.y = totalY
}