func (numericNode *Node) Int() int { sanity.Ensure(numericNode.Type == NUM, "Invalid cast from type [%s] to [%s]", numericNode.Type, NUM) intAsString := numericNode.Values[0].(string) intValue, parseErr := strconv.Atoi(intAsString) sanity.Ensure(parseErr == nil, "Invalid integer value [%s]", intAsString) return intValue }
func (boolNode *Node) Bool() bool { sanity.Ensure(boolNode.Type == BOOL, "Invalid cast from type [%s] to [%s]", boolNode.Type, BOOL) boolAsString := boolNode.Values[0].(string) switch boolAsString { default: sanity.Fail("Invalid boolean value [%s]", boolAsString) return false case "true": return true case "false": return false } }
func (stringNode *Node) Str() string { sanity.Ensure(stringNode.Type == STRING, "Invalid cast from type [%s] to [%s]", stringNode.Type, STRING) origStr := stringNode.Values[0].(string) var buffer bytes.Buffer var escaped bool for _, c := range origStr { if escaped { switch c { case '\\': buffer.WriteRune('\\') case '\'': buffer.WriteRune('\'') case '?': buffer.WriteRune('?') case '"': buffer.WriteRune('"') case 'a': buffer.WriteRune('\a') case 'b': buffer.WriteRune('\b') case 'f': buffer.WriteRune('\f') case 'n': buffer.WriteRune('\n') case 'r': buffer.WriteRune('\r') case 't': buffer.WriteRune('\t') case 'v': buffer.WriteRune('\v') } escaped = false continue } if c == '\\' { escaped = true } else { buffer.WriteRune(c) } } return buffer.String() }
func ensureArity(expectedLength int, actualLength int) { sanity.Ensure(actualLength == expectedLength, "Function expects [%d] arguments, but got [%d]", expectedLength, actualLength) }
func dereference(rift *lang.Rift, env collections.PersistentMap, ref *lang.Ref) interface{} { // TODO: Support gravity sanity.Ensure(env.Contains(ref.String()), "Undefined reference to [%s]", ref.String()) return env.GetOrNil(ref.String()) }
func (n *Node) List() *List { sanity.Ensure(n.Type == LIST, "Node must be [%s], but was [%s]", LIST, n.Type) return &List{n} }
func (n *Node) Block() *Block { sanity.Ensure(n.Type == BLOCK, "Node must be [%s], but was [%s]", BLOCK, n.Type) return &Block{n} }
func (n *Node) Operation() *Operation { sanity.Ensure(n.Type == OP, "Node must be [%s], but was [%s]", OP, n.Type) return &Operation{n} }
func (n *Node) If() *If { sanity.Ensure(n.Type == IF, "Node must be [%s], but was [%s]", IF, n.Type) return &If{n} }
func (n *Node) Func() *Func { sanity.Ensure(n.Type == FUNC, "Node must be [%s], but was [%s]", FUNC, n.Type) return &Func{n} }
func (n *Node) FuncApply() *FuncApply { sanity.Ensure(n.Type == FUNCAPPLY, "Node must be [%s], but was [%s]", FUNCAPPLY, n.Type) return &FuncApply{n} }
func (n *Node) Assignment() *Assignment { sanity.Ensure(n.Type == ASSIGNMENT, "Node must be [%s], but was [%s]", ASSIGNMENT, n.Type) return &Assignment{n} }
func (n *Node) Ref() *Ref { sanity.Ensure(n.Type == REF, "Node must be [%s], but was [%s]", REF, n.Type) return &Ref{n} }
func (n *Node) Rift() *Rift { sanity.Ensure(n.Type == RIFT, "Node must be [%s], but was [%s]", RIFT, n.Type) return &Rift{n} }