Пример #1
0
func div(s *scheme.Scope, args []scheme.Sexpr) (scheme.Sexpr, error) {
	if len(args) != 2 {
		return nil, scheme.ProcError("/ requires exactly 2 arguments")
	}
	if !scheme.IsNumber(args[0]) || !scheme.IsNumber(args[1]) {
		return nil, scheme.ProcError("/ requires all number arguments")
	}
	left := args[0].(*scheme.Number).Val
	right := args[1].(*scheme.Number).Val
	return scheme.NumberFromInt(left / right), nil
}
Пример #2
0
func plus(s *scheme.Scope, args []scheme.Sexpr) (scheme.Sexpr, error) {
	if len(args) < 2 {
		return nil, scheme.ProcError("+ requires at least 2 arguments")
	}
	var sum int64 = 0
	for _, a := range args {
		if scheme.IsNumber(a) {
			sum += a.(*scheme.Number).Val
		} else {
			return nil, scheme.ProcError("+ requires all number arguments")
		}
	}
	return scheme.NumberFromInt(sum), nil
}
Пример #3
0
func mul(s *scheme.Scope, args []scheme.Sexpr) (scheme.Sexpr, error) {
	if len(args) < 2 {
		return nil, scheme.ProcError("* requires at least 2 arguments")
	}
	var product int64 = 1
	for _, a := range args {
		if scheme.IsNumber(a) {
			product *= a.(*scheme.Number).Val
		} else {
			return nil, scheme.ProcError("* requires all number arguments")
		}
	}
	return scheme.NumberFromInt(product), nil
}
Пример #4
0
func parseAtom(tok lexer.Token) scheme.Sexpr {
	if isString(tok) {
		return scheme.StringFromString(string(tok.Val[1 : len(tok.Val)-1]))
	} else if isBoolean(tok) {
		return scheme.BooleanFromString(tok.Val)
	} else if isNumber(tok) {
		n, _ := strconv.ParseInt(tok.Val, 10, 64)
		return scheme.NumberFromInt(n)
	} else if isChar(tok) {
		n, _ := strconv.ParseInt(tok.Val[3:], 16, 64)
		return scheme.CharFromRune(rune(n))
	}
	return scheme.SymbolFromString(tok.Val)
}