Пример #1
0
// StringTermNode is the visitor for deepest TermNodes nodes that holds a
// string.
func (exec Execute) StringTermNode(s *ast.TermNode) string {
	value := exec.resolveTermNode(s)
	switch t := value.(type) {
	default:
		log.Panicf(
			"%s: variable %s not a string. Got %T",
			s.Pos(), s.IdentifierReference(), t)
	case string:
		return value.(string)
	}
	return ""
}
Пример #2
0
// MathTermNode is the visitor for deepest TermNodes nodes that holds a number.
func (exec Execute) MathTermNode(s *ast.TermNode) float32 {
	value := exec.resolveTermNode(s)
	switch t := value.(type) {
	default:
		log.Panicf("%s: variable %s not a number. Got %T", s.Pos(),
			s.IdentifierReference(), t)
	case nil:
		return 0
	case int:
		return float32(value.(int))
	case float32:
		return value.(float32)
	case string:
		str := value.(string)
		v, err := strconv.ParseFloat(str, 32)
		if err != nil {
			log.Panicf("%s: variable %s not a number. Got: %s",
				s.Pos(), s.IdentifierReference(), str)
		}
		return float32(v)
	}
	return 0
}