Esempio n. 1
0
// MatchArgsSign 校验参数是否匹配
func (lambda Lambda) MatchArgsSign(env Env, args ...interface{}) (interface{}, error) {
	params := make([]interface{}, len(args))
	for idx, arg := range args {
		param, err := Eval(env, arg)
		if err != nil {
			return nil, err
		}
		params[idx] = param
	}
	pxs := lambda.Meta["parameter parsexs"].([]p.P)
	st := p.NewBasicState(params)
	return p.UnionAll(pxs...)(&st)
}
Esempio n. 2
0
			if err != nil {
				return nil, err
			}
			switch parser := param.(type) {
			case Parsecer:
				return ParsecBox(p.Try(parser.Parser)), nil
			default:
				return nil, fmt.Errorf(
					"Try Arg Error:expect 1 parser arg but %v.",
					reflect.TypeOf(param))
			}

		},
		"either": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((p.P)(nil))
			params, err := GetArgs(env, p.UnionAll(TypeAs(ptype), TypeAs(ptype), p.EOF), args)
			if err != nil {
				return nil, err
			}
			return ParsecBox(p.Choice(params[0].(Parsecer).Parser, params[1].(Parsecer).Parser)), nil
		},
		"choice": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((p.P)(nil))
			params, err := GetArgs(env, p.Many(TypeAs(ptype)).Over(p.EOF), args)
			if err != nil {
				return nil, err
			}
			parsers := make([]p.P, len(params))
			for idx, prs := range params {
				if parser, ok := prs.(Parsecer); ok {
					parsers[idx] = parser.Parser