Example #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 ""
}
Example #2
0
func (exec *Execute) resolveNumeric(n *ast.TermNode) float32 {
	node := exec.resolveTermNode(n)
	var nodeVal float32
	switch t := node.(type) {
	default:
		pos := n.Pos()
		log.Panicf(
			"%s:runtime error: Type impossible to execute comparison. got: %T",
			pos, t)
	case int:
		nodeVal = float32(node.(int))
	case float32:
		nodeVal = node.(float32)
	}
	return nodeVal
}
Example #3
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
}