Пример #1
0
// 环境做了妥协
var Axiom = Toolkit{
	Meta: map[string]interface{}{
		"name":     "axiom",
		"category": "package",
	},
	Content: map[string]interface{}{
		"quote": LispExpr(func(env Env, args ...interface{}) (Lisp, error) {
			if len(args) != 1 {
				return nil, fmt.Errorf("Quote Args Error: except only one arg but %v", args)
			}
			return Q(Q(args[0])), nil
		}),
		"var": LispExpr(func(env Env, args ...interface{}) (Lisp, error) {
			st := px.NewStateInMemory(args)
			_, err := px.Binds_(TypeAs(ATOM), px.Either(px.Try(px.Eof),
				px.Bind_(px.AnyOne, px.Eof)))(st)
			if err != nil {
				return nil, err
			}
			first := args[0].(Atom)
			slot := VarSlot(first.Type)
			if len(args) == 1 {
				err := env.Defvar(first.Name, slot)
				return Q(nil), err
			}
			val, err := Eval(env, args[1])
			slot.Set(val)
			err = env.Defvar(first.Name, slot)
			return Q(val), err
		}),
		"set": LispExpr(func(env Env, args ...interface{}) (Lisp, error) {
Пример #2
0
				}
			}},
		"chan<-": SimpleBox{
			ParsexSignChecker(px.Binds_(
				TypeAs(reflect.TypeOf((*reflect.Type)(nil)).Elem()),
				TypeAs(INT),
				px.Eof)),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					return MakeSendChan(args[0].(reflect.Type), args[1].(Int)), nil
				}
			}},
		"send": SimpleBox{
			ParsexSignChecker(px.Binds_(
				TypeAs(reflect.TypeOf((*Chan)(nil))),
				px.Either(px.Try(TypeAs(ANYOPTION)), TypeAs(ANYMUST)),
				px.Eof)),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					ch := args[0].(*Chan)
					ch.Send(args[1])
					return nil, nil
				}
			}},
		"send?": SimpleBox{
			ParsexSignChecker(px.Binds_(
				TypeAs(reflect.TypeOf((*Chan)(nil))),
				px.Either(px.Try(TypeAs(ANYOPTION)), TypeAs(ANYMUST)),
				px.Eof)),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
Пример #3
0
			case Parsexer:
				return ParsexBox(px.Try(parser.Parser)), nil
			default:
				return nil, ParsexSignErrorf(
					"Try Arg Error:except 1 parser arg but %v.",
					reflect.TypeOf(param))
			}

		},
		"either": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((px.Parser)(nil))
			params, err := GetArgs(env, px.UnionAll(TypeAs(ptype), TypeAs(ptype), px.Eof), args)
			if err != nil {
				return nil, err
			}
			return ParsexBox(px.Either(params[0].(Parsexer).Parser, params[1].(Parsexer).Parser)), nil
		},
		"choice": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((px.Parser)(nil))
			params, err := GetArgs(env, px.ManyTil(TypeAs(ptype), px.Eof), args)
			if err != nil {
				return nil, err
			}
			parsers := make([]px.Parser, len(params))
			for idx, prs := range params {
				if parser, ok := prs.(Parsexer); ok {
					parsers[idx] = parser.Parser
				}
				return nil, ParsexSignErrorf("Choice Args Error:except parsec parsers but %v is %v",
					prs, reflect.TypeOf(prs))
			}