<commit_before>// Copyright (c) 2017, <NAME> <<EMAIL>>
// See LICENSE for licensing information

package expand

import (
	"fmt"
	"strconv"

	"mvdan.cc/sh/v3/syntax"
)

func Arithm(cfg *Config, expr syntax.ArithmExpr) (int, error) {
	switch x := expr.(type) {
	case *syntax.Word:
		str, err := Literal(cfg, x)
		if err != nil {
			return 0, err
		}
		// recursively fetch vars
		i := 0
		for str != "" && syntax.ValidName(str) {
			val := cfg.envGet(str)
			if val == "" {
				break
			}
			if i++; i >= maxNameRefDepth {
				break
			}
			str = val
		}
		// default to 0
		return atoi(str), nil
	case *syntax.ParenArithm:
		return Arithm(cfg, x.X)
	case *syntax.UnaryArithm:
		switch x.Op {
		case syntax.Inc, syntax.Dec:
			name := x.X.(*syntax.Word).Lit()
			old := atoi(cfg.envGet(name))
			val := old
			if x.Op == syntax.Inc {
				val++<commit_msg>expand: simplify ValidName arithmetic loop<commit_after>// Copyright (c) 2017, <NAME> <<EMAIL>>
// See LICENSE for licensing information

package expand

import (
	"fmt"
	"strconv"

	"mvdan.cc/sh/v3/syntax"
)

func Arithm(cfg *Config, expr syntax.ArithmExpr) (int, error) {
	switch x := expr.(type) {
	case *syntax.Word:
		str, err := Literal(cfg, x)
		if err != nil {
			return 0, err
		}
		// recursively fetch vars
		i := 0
		for syntax.ValidName(str) {
			val := cfg.envGet(str)
			if val == "" {
				break
			}
			if i++; i >= maxNameRefDepth {
				break
			}
			str = val
		}
		// default to 0
		return atoi(str), nil
	case *syntax.ParenArithm:
		return Arithm(cfg, x.X)
	case *syntax.UnaryArithm:
		switch x.Op {
		case syntax.Inc, syntax.Dec:
			name := x.X.(*syntax.Word).Lit()
			old := atoi(cfg.envGet(name))
			val := old
			if x.Op == syntax.Inc {
				val++
{'fraction_non_alphanumeric': 0.1583526682134571, 'fraction_numerical': 0.009280742459396751, 'mean_word_length': 2.758169934640523, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3166913', 'n_tokens_mistral': 794, 'n_tokens_neox': 660, 'n_words': 176}