Beispiel #1
0
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
}
Beispiel #2
0
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
	}
}
Beispiel #3
0
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()
}
Beispiel #4
0
func ensureArity(expectedLength int, actualLength int) {
	sanity.Ensure(actualLength == expectedLength, "Function expects [%d] arguments, but got [%d]", expectedLength, actualLength)
}
Beispiel #5
0
Datei: vm.go Projekt: jerluc/rift
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())
}
Beispiel #6
0
func (n *Node) List() *List {
	sanity.Ensure(n.Type == LIST, "Node must be [%s], but was [%s]", LIST, n.Type)
	return &List{n}
}
Beispiel #7
0
func (n *Node) Block() *Block {
	sanity.Ensure(n.Type == BLOCK, "Node must be [%s], but was [%s]", BLOCK, n.Type)
	return &Block{n}
}
Beispiel #8
0
func (n *Node) Operation() *Operation {
	sanity.Ensure(n.Type == OP, "Node must be [%s], but was [%s]", OP, n.Type)
	return &Operation{n}
}
Beispiel #9
0
func (n *Node) If() *If {
	sanity.Ensure(n.Type == IF, "Node must be [%s], but was [%s]", IF, n.Type)
	return &If{n}
}
Beispiel #10
0
func (n *Node) Func() *Func {
	sanity.Ensure(n.Type == FUNC, "Node must be [%s], but was [%s]", FUNC, n.Type)
	return &Func{n}
}
Beispiel #11
0
func (n *Node) FuncApply() *FuncApply {
	sanity.Ensure(n.Type == FUNCAPPLY, "Node must be [%s], but was [%s]", FUNCAPPLY, n.Type)
	return &FuncApply{n}
}
Beispiel #12
0
func (n *Node) Assignment() *Assignment {
	sanity.Ensure(n.Type == ASSIGNMENT, "Node must be [%s], but was [%s]", ASSIGNMENT, n.Type)
	return &Assignment{n}
}
Beispiel #13
0
func (n *Node) Ref() *Ref {
	sanity.Ensure(n.Type == REF, "Node must be [%s], but was [%s]", REF, n.Type)
	return &Ref{n}
}
Beispiel #14
0
func (n *Node) Rift() *Rift {
	sanity.Ensure(n.Type == RIFT, "Node must be [%s], but was [%s]", RIFT, n.Type)
	return &Rift{n}
}