Skip to content

Commit

Permalink
refactor: remove always ni types.Type result
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Dec 2, 2024
1 parent a8b1991 commit c0e2546
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions internal/check/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ func (s *scope) checkNode(tree *parse.Tree, dot types.Type, node parse.Node) (ty
case *parse.DotNode:
return dot, nil
case *parse.ListNode:
return s.checkListNode(tree, dot, n)
return nil, s.checkListNode(tree, dot, n)
case *parse.ActionNode:
return s.checkActionNode(tree, dot, n)
return nil, s.checkActionNode(tree, dot, n)
case *parse.CommandNode:
return s.checkCommandNode(tree, dot, n)
case *parse.FieldNode:
return s.checkFieldNode(tree, dot, n)
case *parse.PipeNode:
return s.checkPipeNode(tree, dot, n)
case *parse.IfNode:
return s.checkIfNode(tree, dot, n)
return nil, s.checkIfNode(tree, dot, n)
case *parse.RangeNode:
return s.checkRangeNode(tree, dot, n)
return nil, s.checkRangeNode(tree, dot, n)
case *parse.TemplateNode:
return s.checkTemplateNode(tree, dot, n)
return nil, s.checkTemplateNode(tree, dot, n)
case *parse.BoolNode:
return types.Typ[types.UntypedBool], nil
case *parse.StringNode:
Expand All @@ -89,22 +89,22 @@ func (s *scope) checkVariableNode(tree *parse.Tree, n *parse.VariableNode) (type
return s.checkIdentifiers(tree, tp, n, n.Ident[1:])
}

func (s *scope) checkListNode(tree *parse.Tree, dot types.Type, n *parse.ListNode) (types.Type, error) {
func (s *scope) checkListNode(tree *parse.Tree, dot types.Type, n *parse.ListNode) error {
for _, child := range n.Nodes {
if _, err := s.checkNode(tree, dot, child); err != nil {
return nil, err
return err
}
}
return nil, nil
return nil
}

func (s *scope) checkActionNode(tree *parse.Tree, dot types.Type, n *parse.ActionNode) (types.Type, error) {
func (s *scope) checkActionNode(tree *parse.Tree, dot types.Type, n *parse.ActionNode) error {
for _, cmd := range n.Pipe.Cmds {
if _, err := s.checkNode(tree, dot, cmd); err != nil {
return nil, err
return err
}
}
return nil, nil
return nil
}

func (s *scope) checkPipeNode(tree *parse.Tree, dot types.Type, n *parse.PipeNode) (types.Type, error) {
Expand Down Expand Up @@ -150,15 +150,15 @@ func (s *scope) checkPipeNode(tree *parse.Tree, dot types.Type, n *parse.PipeNod
return x, nil
}

func (s *scope) checkIfNode(tree *parse.Tree, dot types.Type, n *parse.IfNode) (types.Type, error) {
func (s *scope) checkIfNode(tree *parse.Tree, dot types.Type, n *parse.IfNode) error {
_, err := s.checkNode(tree, dot, n.Pipe)
if err != nil {
return nil, err
return err
}
if _, err := s.checkNode(tree, dot, n.List); err != nil {
return nil, err
return err
}
return nil, nil
return nil
}

func newNumberNodeType(n *parse.NumberNode) (types.Type, error) {
Expand All @@ -177,28 +177,29 @@ func newNumberNodeType(n *parse.NumberNode) (types.Type, error) {
return nil, fmt.Errorf("failed to evaluate template *parse.NumberNode type")
}

func (s *scope) checkTemplateNode(tree *parse.Tree, dot types.Type, n *parse.TemplateNode) (types.Type, error) {
func (s *scope) checkTemplateNode(tree *parse.Tree, dot types.Type, n *parse.TemplateNode) error {
x := dot
if n.Pipe != nil {
tp, err := s.checkNode(tree, x, n.Pipe)
if err != nil {
return nil, err
return err
}
x = tp
} else {
x = types.Typ[types.UntypedNil]
}
childTree, ok := s.FindTree(n.Name)
if !ok {
return nil, fmt.Errorf("template %q not found", n.Name)
return fmt.Errorf("template %q not found", n.Name)
}
childScope := scope{
global: s.global,
variables: map[string]types.Type{
"$": x,
},
}
return childScope.checkNode(childTree, x, childTree.Root)
_, err := childScope.checkNode(childTree, x, childTree.Root)
return err
}

func (s *scope) checkFieldNode(tree *parse.Tree, dot types.Type, n *parse.FieldNode) (types.Type, error) {
Expand Down Expand Up @@ -271,14 +272,14 @@ func (s *scope) checkIdentifiers(tree *parse.Tree, dot types.Type, n parse.Node,
return x, nil
}

func (s *scope) checkRangeNode(tree *parse.Tree, dot types.Type, n *parse.RangeNode) (types.Type, error) {
func (s *scope) checkRangeNode(tree *parse.Tree, dot types.Type, n *parse.RangeNode) error {
loopScope := &scope{
global: s.global,
variables: maps.Clone(s.variables),
}
pipeType, err := loopScope.checkNode(tree, dot, n.Pipe)
if err != nil {
return nil, err
return err
}
var x types.Type
switch pt := pipeType.(type) {
Expand All @@ -289,17 +290,17 @@ func (s *scope) checkRangeNode(tree *parse.Tree, dot types.Type, n *parse.RangeN
case *types.Map:
x = pt.Elem()
default:
return nil, fmt.Errorf("failed to range over %s", pipeType)
return fmt.Errorf("failed to range over %s", pipeType)
}
if _, err := loopScope.checkNode(tree, x, n.List); err != nil {
return nil, err
return err
}
if n.ElseList != nil {
if _, err := loopScope.checkNode(tree, x, n.ElseList); err != nil {
return nil, err
return err
}
}
return nil, nil
return nil
}

func (s *scope) checkIdentifierNode(n *parse.IdentifierNode) (types.Type, error) {
Expand Down

0 comments on commit c0e2546

Please sign in to comment.