Adds receivers to filter and point structs
parent
5a85f9cc8f
commit
e2b5936197
@ -1,27 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"tildegit.org/sloum/filtress/parser"
|
||||
)
|
||||
|
||||
type point struct {
|
||||
x int
|
||||
y int
|
||||
maxX int
|
||||
maxY int
|
||||
x int
|
||||
y int
|
||||
maxX int
|
||||
maxY int
|
||||
}
|
||||
|
||||
type color int
|
||||
type mode int
|
||||
type filter struct {
|
||||
max int
|
||||
min int
|
||||
val int
|
||||
}
|
||||
|
||||
type filterState struct {
|
||||
red color
|
||||
green color
|
||||
blue color
|
||||
alpha color
|
||||
location point
|
||||
mod mode
|
||||
register int
|
||||
fade uint
|
||||
loop uint
|
||||
red filter
|
||||
green filter
|
||||
blue filter
|
||||
alpha filter
|
||||
location point
|
||||
mode filter
|
||||
}
|
||||
|
||||
var variables = map[string]int{}
|
||||
|
||||
func openFileFromPath(path string) *os.File {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Handle checking filter file argument
|
||||
flag.Parse()
|
||||
posArgs := flag.Args()
|
||||
if len(posArgs) > 1 || len(posArgs) < 1 {
|
||||
panic(fmt.Sprintf("Input error: Too many positional arguments. Expected one, received %d", len(flag.Args())))
|
||||
}
|
||||
fpath := posArgs[0]
|
||||
fext := strings.ToUpper(filepath.Ext(fpath))
|
||||
_, fname := filepath.Split(fpath)
|
||||
|
||||
if fext != ".FRS" {
|
||||
panic(fmt.Sprintf("Input error: Incorrect filetype. Expected .FRS, received \".%s\"", fext))
|
||||
}
|
||||
|
||||
// Open file and parse
|
||||
fmt.Println("Filtress v0.2.0")
|
||||
filterFile := openFileFromPath(fpath)
|
||||
parser := parser.NewParser(filterFile)
|
||||
fmt.Printf("Parsing file: %s ...\n", fname)
|
||||
tree := parser.Parse()
|
||||
if len(tree.Loops) > 0 {
|
||||
fmt.Printf("File has %d procedures\n", len(tree.Loops))
|
||||
fmt.Println("File is valid")
|
||||
} else {
|
||||
fmt.Println("Invalid input file")
|
||||
}
|
||||
|
||||
fmt.Print(tree)
|
||||
fmt.Println()
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,96 @@
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue