Пример #1
0
func compare(st p.State) (interface{}, error) {
	l, err := p.Choice(
		p.Try(IntValue).Bind(LessThanNumber),
		p.Try(NumberValue).Bind(LessThanFloat),
		p.Try(StringValue).Bind(LessThanString),
		p.P(TimeValue).Bind(LessThanTime),
	).Bind(func(l interface{}) p.P {
		return func(st p.State) (interface{}, error) {
			_, err := p.EOF(st)
			if err != nil {
				return nil, fmt.Errorf("less args sign error: expect eof")
			}
			return l, nil
		}
	})(st)
	if err == nil {
		return l, nil
	}
	return nil, fmt.Errorf("expect two lessable values compare but error %v", err)
}
Пример #2
0
func less(env Env) p.P {
	return func(st p.State) (interface{}, error) {
		l, err := p.Choice(
			p.Try(p.P(IntValue).Bind(LessThanNumber)),
			p.Try(p.P(NumberValue).Bind(LessThanFloat)),
			p.Try(p.P(StringValue).Bind(LessThanString)),
			p.Try(p.P(TimeValue).Bind(LessThanTime)),
			p.P(ListValue).Bind(LessThanList(env)),
		).Bind(func(l interface{}) p.P {
			return func(st p.State) (interface{}, error) {
				_, err := p.EOF(st)
				if err != nil {
					return nil, st.Trap("less args sign error: expect eof")
				}
				return l, nil
			}
		})(st)
		if err == nil {
			return l, nil
		}
		return nil, st.Trap("expect two lessable values compare but error %v", err)
	}
}