2020-10-08 23:59:03 +00:00
|
|
|
package treeops
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2020-09-25 06:23:33 +00:00
|
|
|
import "fmt"
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2020-10-08 23:59:03 +00:00
|
|
|
var myPathTokeniser = NewPathTokeniser()
|
|
|
|
var myPathPostfixer = NewPathPostFixer()
|
|
|
|
|
2020-09-25 06:23:33 +00:00
|
|
|
type PathTreeNode struct {
|
2020-10-20 04:33:20 +00:00
|
|
|
Operation *Operation
|
2020-09-25 06:23:33 +00:00
|
|
|
Lhs *PathTreeNode
|
|
|
|
Rhs *PathTreeNode
|
|
|
|
}
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2020-09-25 06:23:33 +00:00
|
|
|
type PathTreeCreator interface {
|
2020-10-08 23:59:03 +00:00
|
|
|
ParsePath(path string) (*PathTreeNode, error)
|
2020-10-20 04:33:20 +00:00
|
|
|
CreatePathTree(postFixPath []*Operation) (*PathTreeNode, error)
|
2020-09-25 06:23:33 +00:00
|
|
|
}
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2020-09-25 06:23:33 +00:00
|
|
|
type pathTreeCreator struct {
|
|
|
|
}
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2020-09-25 06:23:33 +00:00
|
|
|
func NewPathTreeCreator() PathTreeCreator {
|
|
|
|
return &pathTreeCreator{}
|
2020-09-18 07:08:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 23:59:03 +00:00
|
|
|
func (p *pathTreeCreator) ParsePath(path string) (*PathTreeNode, error) {
|
|
|
|
tokens, err := myPathTokeniser.Tokenise(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-20 04:33:20 +00:00
|
|
|
var Operations []*Operation
|
|
|
|
Operations, err = myPathPostfixer.ConvertToPostfix(tokens)
|
2020-10-08 23:59:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-20 04:33:20 +00:00
|
|
|
return p.CreatePathTree(Operations)
|
2020-10-08 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 04:33:20 +00:00
|
|
|
func (p *pathTreeCreator) CreatePathTree(postFixPath []*Operation) (*PathTreeNode, error) {
|
2020-09-25 06:23:33 +00:00
|
|
|
var stack = make([]*PathTreeNode, 0)
|
|
|
|
|
2020-10-13 01:51:37 +00:00
|
|
|
if len(postFixPath) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 04:33:20 +00:00
|
|
|
for _, Operation := range postFixPath {
|
|
|
|
var newNode = PathTreeNode{Operation: Operation}
|
|
|
|
log.Debugf("pathTree %v ", Operation.toString())
|
|
|
|
if Operation.OperationType.NumArgs > 0 {
|
|
|
|
numArgs := Operation.OperationType.NumArgs
|
2020-10-16 01:29:26 +00:00
|
|
|
if numArgs == 1 {
|
2020-10-12 01:24:59 +00:00
|
|
|
remaining, rhs := stack[:len(stack)-1], stack[len(stack)-1]
|
|
|
|
newNode.Rhs = rhs
|
|
|
|
stack = remaining
|
2020-10-17 11:10:47 +00:00
|
|
|
} else if numArgs == 2 {
|
2020-10-12 01:24:59 +00:00
|
|
|
remaining, lhs, rhs := stack[:len(stack)-2], stack[len(stack)-2], stack[len(stack)-1]
|
|
|
|
newNode.Lhs = lhs
|
|
|
|
newNode.Rhs = rhs
|
|
|
|
stack = remaining
|
|
|
|
}
|
2020-09-25 06:23:33 +00:00
|
|
|
}
|
|
|
|
stack = append(stack, &newNode)
|
|
|
|
}
|
|
|
|
if len(stack) != 1 {
|
|
|
|
return nil, fmt.Errorf("expected stack to have 1 thing but its %v", stack)
|
2020-09-18 07:08:33 +00:00
|
|
|
}
|
2020-09-25 06:23:33 +00:00
|
|
|
return stack[0], nil
|
2020-09-18 07:08:33 +00:00
|
|
|
}
|