Пример #1
0
// Time 包引入了go的time包功能
var Time = Toolkit{
	Meta: map[string]interface{}{
		"category": "toolkit",
		"name":     "time",
	},
	Content: map[string]interface{}{
		"now": SimpleBox{
			ParsexSignChecker(px.Eof),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					return tm.Now(), nil
				}
			}},
		"parseDuration": SimpleBox{
			ParsexSignChecker(px.Bind_(StringValue, px.Eof)),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					return tm.ParseDuration(args[0].(string))
				}
			}},
		"parseTime": SimpleBox{
			ParsexSignChecker(px.Binds_(StringValue, StringValue, px.Eof)),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					return tm.Parse(args[0].(string), args[1].(string))
				}
			}},
	},
}
Пример #2
0
	val, ok := ch.value.TryRecv()
	if val.IsValid() {
		return val.Interface(), ok
	}
	return nil, ok
}

var channel = Toolkit{
	Meta: map[string]interface{}{
		"category": "toolkit",
		"name":     "channel",
	},
	Content: map[string]interface{}{
		"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 MakeBothChan(args[0].(reflect.Type), args[1].(Int)), nil
				}
			}},
		"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 MakeRecvChan(args[0].(reflect.Type), args[1].(Int)), nil
				}
Пример #3
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) {