示例#1
0
func NewRootScope() *Scope {
	root := NewScope(nil)
	root.Put("+", primitives.NewAdd())
	root.Put("-", primitives.NewSub())
	root.Put("*", primitives.NewMult())
	root.Put("/", primitives.NewDiv())
	root.Put("=", primitives.NewEq())
	root.Put(">", primitives.NewGt())
	root.Put(">=", primitives.NewGtE())
	root.Put("<", primitives.NewLt())
	root.Put("<=", primitives.NewLtE())
	root.Put("%", primitives.NewMod())
	root.Put("and", primitives.NewAnd())
	root.Put("or", primitives.NewOr())
	root.Put("eqv?", primitives.NewIsEqv())
	root.Put("type-of", primitives.NewTypeOf())
	root.Put("display", primitives.NewDisplay())
	root.Put("newline", primitives.NewNewline())
	root.Put("car", primitives.NewCar())
	root.Put("cdr", primitives.NewCdr())
	root.Put("cons", primitives.NewCons())
	root.Put("make-chan", primitives.NewMakeChan())
	root.Put("close-chan", primitives.NewCloseChan())
	root.Put("<-chan", primitives.NewChanRecv())
	root.Put("chan<-", primitives.NewChanSend())
	root.Put("sleep", primitives.NewSleep())
	root.Put("random", primitives.NewRandom())
	root.Put("#t", value.NewBoolValue(true))
	root.Put("#f", value.NewBoolValue(false))
	return root
}
示例#2
0
func (self *LtE) Apply(args []value.Value) value.Value {
	if len(args) != 2 {
		panic(fmt.Sprint("argument mismatch for `<=', expected 2, given: ", len(args)))
	}
	if v1, ok := args[0].(*value.IntValue); ok {
		if v2, ok := args[1].(*value.IntValue); ok {
			return value.NewBoolValue(v1.Value <= v2.Value)
		} else if v2, ok := args[1].(*value.FloatValue); ok {
			return value.NewBoolValue(float64(v1.Value) <= v2.Value)
		}
	} else if v1, ok := args[0].(*value.FloatValue); ok {
		if v2, ok := args[1].(*value.IntValue); ok {
			return value.NewBoolValue(v1.Value <= float64(v2.Value))
		} else if v2, ok := args[1].(*value.FloatValue); ok {
			return value.NewBoolValue(v1.Value <= v2.Value)
		}
	}
	panic(fmt.Sprint("incorrect argument type for `<=', expected number?"))
}
示例#3
0
func (self *IsEqv) Apply(args []value.Value) value.Value {
	if len(args) != 2 {
		panic(fmt.Sprint("argument mismatch for `eqv?', expected 2, given: ", len(args)))
	}
	typeof := NewTypeOf()
	symbol1 := typeof.Apply(args[0:1]).(*value.Symbol)
	symbol2 := typeof.Apply(args[1:2]).(*value.Symbol)

	if symbol1.Value != symbol2.Value {
		return value.NewBoolValue(false)
	}
	iseqv := false
	switch args[0].(type) {
	case *value.EmptyPairValue:
		iseqv = true
	case *value.BoolValue:
		val1 := args[0].(*value.BoolValue)
		val2 := args[1].(*value.BoolValue)
		iseqv = val1.Value == val2.Value
	case *value.IntValue:
		val1 := args[0].(*value.IntValue)
		val2 := args[1].(*value.IntValue)
		iseqv = val1.Value == val2.Value
	case *value.FloatValue:
		val1 := args[0].(*value.FloatValue)
		val2 := args[1].(*value.FloatValue)
		iseqv = val1.Value == val2.Value
	case *value.StringValue:
		val1 := args[0].(*value.StringValue)
		val2 := args[1].(*value.StringValue)
		iseqv = val1.Value == val2.Value
	case *value.Symbol:
		val1 := args[0].(*value.Symbol)
		val2 := args[1].(*value.Symbol)
		iseqv = val1.Value == val2.Value
	}
	return value.NewBoolValue(iseqv)
}