Exemplo n.º 1
0
Arquivo: math.go Projeto: zy02636/gisp
func xEof(x interface{}) px.Parser {
	return px.Bind_(px.Eof, px.Return(x))
}
Exemplo n.º 2
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) {
Exemplo n.º 3
0
Arquivo: time.go Projeto: gale320/gisp
// 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))
				}
			}},
	},
}
Exemplo n.º 4
0
						return func(st px.ParsexState) (interface{}, error) {
							return nil, ParsexSignErrorf("excpet got a parser but %v", pr)
						}
					}
				})), nil
			default:
				return nil, ParsexSignErrorf("excpet got a parser but %v", prs)
			}
		},
		"bind_": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((*Parsecer)(nil)).Elem()
			params, err := GetArgs(env, px.UnionAll(TypeAs(ptype), TypeAs(ptype), px.Eof), args)
			if err != nil {
				return nil, err
			}
			return ParsexBox(px.Bind_(params[0].(Parsexer).Parser, params[1].(Parsexer).Parser)), nil
		},
		"sepby1": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((*Parsexer)(nil)).Elem()
			params, err := GetArgs(env, px.UnionAll(TypeAs(ptype), TypeAs(ptype), px.Eof), args)
			if err != nil {
				return nil, err
			}
			return ParsexBox(px.SepBy1(params[0].(Parsexer).Parser, params[1].(Parsexer).Parser)), nil
		},
		"sepby": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((*Parsexer)(nil)).Elem()
			params, err := GetArgs(env, px.UnionAll(TypeAs(ptype), TypeAs(ptype), px.Eof), args)
			if err != nil {
				return nil, err
			}