Exemple #1
0
func scm_multiply(args *goscm.Pair, env *goscm.Environ) (goscm.SCMT, error) {
	argss, err := args.ToSlice()
	if err != nil {
		return goscm.SCM_Nil, err
	}

	ret := 1
	for i := 0; i < len(argss); i++ {
		if reflect.TypeOf(argss[i]) != reflect.TypeOf(&goscm.PlainInt{}) {
			return goscm.SCM_Nil, errors.New("Expected integer type")
		}
		ret *= argss[i].(*goscm.PlainInt).Value
	}
	return goscm.NewSCMT(ret), nil
}
Exemple #2
0
func scm_begin(args *goscm.Pair, env *goscm.Environ) (goscm.SCMT, error) {
	var result goscm.SCMT

	argss, err := args.ToSlice()
	if err != nil {
		return goscm.SCM_Nil, err
	}

	for i := 0; i < len(argss); i++ {
		result, err = argss[i].Eval(env)
		if err != nil {
			return goscm.SCM_Nil, err
		}
	}

	return result, nil
}
Exemple #3
0
func scm_map(args *goscm.Pair, env *goscm.Environ) (goscm.SCMT, error) {
	argss, err := args.ToSlice()
	if err != nil {
		return goscm.SCM_Nil, err
	}

	if len(argss) < 2 {
		return goscm.SCM_Nil, errors.New("Too few arguments")
	}
	if len(argss) > 2 {
		return goscm.SCM_Nil, errors.New("Too many arguments")
	}

	proc, ok := argss[0].(goscm.Func)
	if !ok {
		return goscm.SCM_Nil, errors.New("Non-function in first position")
	}

	if reflect.TypeOf(argss[1]) != reflect.TypeOf(&goscm.Pair{}) {
		return goscm.SCM_Nil, errors.New("Expected pair")
	}

	tail, err := argss[1].(*goscm.Pair).ToSlice()
	if err != nil {
		return goscm.SCM_Nil, err
	}

	ret := goscm.SCM_Nil
	for i := len(tail) - 1; i >= 0; i-- {
		result, err := proc.Apply(goscm.NewList(tail[i]), env)
		if err != nil {
			return goscm.SCM_Nil, err
		}
		ret = goscm.Cons(result, ret)
	}

	return ret, nil
}
Exemple #4
0
func scm_divide(args *goscm.Pair, env *goscm.Environ) (goscm.SCMT, error) {
	argss, err := args.ToSlice()
	if err != nil {
		return goscm.SCM_Nil, err
	}

	if len(argss) == 0 {
		return goscm.SCM_Nil, errors.New("Expected at least one argument")
	}

	if reflect.TypeOf(argss[0]) != reflect.TypeOf(&goscm.PlainInt{}) {
		return goscm.SCM_Nil, errors.New("Expected integer type")
	}
	ret := argss[0].(*goscm.PlainInt).Value

	for i := 1; i < len(argss); i++ {
		if reflect.TypeOf(argss[i]) != reflect.TypeOf(&goscm.PlainInt{}) {
			return goscm.SCM_Nil, errors.New("Expected integer type")
		}
		ret /= argss[i].(*goscm.PlainInt).Value
	}
	return goscm.NewSCMT(ret), nil
}
Exemple #5
0
func scm_apply(args *goscm.Pair, env *goscm.Environ) (goscm.SCMT, error) {
	argss, err := args.ToSlice()
	if err != nil {
		return goscm.SCM_Nil, err
	}
	if len(argss) < 2 {
		return goscm.SCM_Nil, errors.New("Too few arguments")
	}
	if len(argss) > 3 {
		return goscm.SCM_Nil, errors.New("Too many arguments")
	}

	f, ok := argss[0].(goscm.Func)
	if !ok {
		return goscm.SCM_Nil, errors.New("Attempt to apply non-function type")
	}

	if reflect.TypeOf(argss[1]) != reflect.TypeOf(&goscm.Pair{}) {
		return goscm.SCM_Nil, errors.New("Non-list to apply to")
	}
	target := argss[1].(*goscm.Pair)
	return f.Apply(target, env)
}
Exemple #6
0
func scm_numeq(args *goscm.Pair, env *goscm.Environ) (goscm.SCMT, error) {
	if args == goscm.SCM_Nil {
		return goscm.NewSCMT(true), nil
	}

	argss, err := args.ToSlice()
	if err != nil {
		return goscm.SCM_Nil, err
	}

	base := argss[0].(*goscm.PlainInt).Value

	for i := 1; i < len(argss); i++ {
		if reflect.TypeOf(argss[i]) != reflect.TypeOf(&goscm.PlainInt{}) {
			return goscm.SCM_Nil, errors.New("Expected integer argument")
		}

		if argss[i].(*goscm.PlainInt).Value != base {
			return goscm.NewSCMT(false), nil
		}
	}

	return goscm.NewSCMT(true), nil
}