Beispiel #1
0
func (self *Name) Eval(env *scope.Scope) Value {
	if val := env.Lookup(self.Identifier); val != nil {
		return val.(Value)
	} else {
		panic(fmt.Sprintf("%s: undefined identifier", self.Identifier))
	}
}
Beispiel #2
0
func Assign(s *scope.Scope, pattern string, value interface{}) {
	if env := s.FindScope(pattern); env != nil {
		env.Put(pattern, value)
	} else {
		panic(fmt.Sprintf("%s was not defined", pattern))
	}
}
Beispiel #3
0
func BindArguments(env *scope.Scope, params Node, args Value) {
	if name, ok := params.(*Name); ok && args == NilPairValue {
		// ((lambda x <body>) '())
		env.Put(name.Identifier, args)
		return
	}
	for {
		if params == NilPair && args == NilPairValue {
			return
		} else if params == NilPair && args != NilPairValue {
			panic(fmt.Sprint("too many arguments"))
		} else if params != NilPair && args == NilPairValue {
			panic(fmt.Sprint("missing arguments"))
		}
		switch params.(type) {
		case *Pair:
			// R5RS declare first element must be a *Name*
			name, _ := params.(*Pair).First.(*Name)
			pair, ok := args.(*PairValue)
			if !ok {
				panic(fmt.Sprint("arguments does not match given number"))
			}
			env.Put(name.Identifier, pair.First)
			params = params.(*Pair).Second
			args = pair.Second
		case *Name:
			env.Put(params.(*Name).Identifier, args)
			return
		}
	}
}
Beispiel #4
0
func Define(env *scope.Scope, pattern string, value interface{}) {
	env.Put(pattern, value)
}