Exemple #1
0
// ValueParser 实现简单的值解释器
func ValueParser() p.P {
	return func(state p.State) (interface{}, error) {
		value, err := p.Choice(p.Try(StringParser),
			p.Try(FloatParser),
			p.Try(IntParser),
			p.Try(RuneParser),
			p.Try(StringParser),
			p.Try(BoolParser),
			p.Try(NilParser),
			p.Try(p.P(AtomParser).Bind(SuffixParser)),
			p.Try(p.P(ListParser()).Bind(SuffixParser)),
			p.Try(DotExprParser),
			QuoteParser,
		)(state)
		return value, err
	}
}
Exemple #2
0
func TestIntParser3(t *testing.T) {
	data := "3.14"
	st := p.BasicStateFromText(data)
	o, err := p.P(IntParser).Then(p.EOF)(&st)
	if err == nil {
		t.Fatalf("expect a Float parse error but got %v", o)
	}
}
Exemple #3
0
// StringValue 判断 state 中下一个值是否为 String
func StringValue(st p.State) (interface{}, error) {
	return p.Do(func(state p.State) interface{} {
		val := p.P(p.One).Exec(state)
		if _, ok := val.(string); ok {
			return val
		}
		panic(st.Trap("expect a string but %v", val))
	})(st)
}
Exemple #4
0
func TestDotParser(t *testing.T) {
	data := "now.Year"
	st := p.BasicStateFromText(data)
	re, err := p.P(AtomParser).Bind(DotSuffixParser)(&st)
	if err != nil {
		t.Fatalf("expect a Dot but error %v", err)
	}
	t.Log(re)
}
Exemple #5
0
func TestBracketParser(t *testing.T) {
	data := "dict[\"meta\"]"
	st := p.BasicStateFromText(data)
	re, err := p.P(AtomParser).Bind(BracketSuffixParser)(&st)
	if err != nil {
		t.Fatalf("expect a Dot but error %v", err)
	}
	t.Log(re)
}
Exemple #6
0
// QuoteParser 实现 Quote 语法的解析
func QuoteParser(st p.State) (interface{}, error) {
	lisp, err := p.Chr('\'').Then(
		p.Choice(
			p.Try(p.P(AtomParser).Bind(SuffixParser)),
			ListParser().Bind(SuffixParser),
		))(st)
	if err == nil {
		return Quote{lisp}, nil
	}
	return nil, err
}
Exemple #7
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)
	}
}
Exemple #8
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)
}
Exemple #9
0
func equalsOption(st p.State) (interface{}, error) {
	return p.P(p.One).Bind(eqsOption)(st)
}
Exemple #10
0
			if reflect.DeepEqual(err, io.EOF) {
				return false, nil
			}
			return nil, err
		}
		if y == nil {
			return false, nil
		}
		if !reflect.DeepEqual(x, y) {
			return true, nil
		}
	}
}

// String2Values 将两个 StringValue 串为 List
var String2Values = p.P(StringValue).Bind(func(x interface{}) p.P {
	return func(st p.State) (interface{}, error) {
		y, err := StringValue(st)
		if err != nil {
			return nil, err
		}
		return []interface{}{x, y}, nil
	}
})

//TimeValue 将两个 Time 值串为 List
var Time2Values = p.P(TimeValue).Bind(func(x interface{}) p.P {
	return func(st p.State) (interface{}, error) {
		y, err := TimeValue(st)
		if err != nil {
			return nil, err
Exemple #11
0
func xEOF(x interface{}) p.P {
	return p.P(p.EOF).Then(p.Return(x))
}
Exemple #12
0
		return Float(val), nil
	case Int:
		return Float(val), nil
	case float32:
		return Float(val), nil
	case float64:
		return Float(val), nil
	case Float:
		return val, nil
	default:
		return nil, NotNumberError{v}
	}
}

// Int2Values 获取多个 int
var Int2Values = p.P(IntValue).Bind(func(x interface{}) p.P {
	return func(st p.State) (interface{}, error) {
		y, err := IntValue(st)
		if err != nil {
			return nil, err
		}
		return []interface{}{x, y}, nil
	}
})

// Num2Values 获取多个 int
var Num2Values = p.P(NumberValue).Bind(func(x interface{}) p.P {
	return func(st p.State) (interface{}, error) {
		y, err := NumberValue(st)
		if err != nil {
			return nil, err
Exemple #13
0
func (ch *Chan) TryRecv() (x interface{}, ok bool) {
	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{
			SignChecker(p.P(TypeAs(reflect.TypeOf((*reflect.Type)(nil)).Elem())).
				Then(TypeAs(INT)).Then(p.EOF)),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					return MakeBothChan(args[0].(reflect.Type), args[1].(Int)), nil
				}
			}},
		"chan->": SimpleBox{
			SignChecker(TypeAs(reflect.TypeOf((*reflect.Type)(nil)).Elem()).
				Then(TypeAs(INT)).Then(p.EOF)),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					return MakeRecvChan(args[0].(reflect.Type), args[1].(Int)), nil
				}
			}},
		"chan<-": SimpleBox{
Exemple #14
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: expect only one arg but %v", args)
			}
			return Q(Q(args[0])), nil
		}),
		"var": LispExpr(func(env Env, args ...interface{}) (Lisp, error) {
			st := p.NewBasicState(args)
			_, err := TypeAs(ATOM).Then(p.Choice(p.Try(p.EOF), p.P(p.One).Then(p.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
		}),
Exemple #15
0
func notEquals(st p.State) (interface{}, error) {
	return p.P(p.One).Bind(neqs)(st)
}
Exemple #16
0
func stopWord(x interface{}) p.P {
	return p.P(stop).Then(p.Return(x))
}
Exemple #17
0
// Time 包引入了go的time包功能
var Time = Toolkit{
	Meta: map[string]interface{}{
		"category": "toolkit",
		"name":     "time",
	},
	Content: map[string]interface{}{
		"now": SimpleBox{
			SignChecker(p.EOF),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					return tm.Now(), nil
				}
			}},
		"parseDuration": SimpleBox{
			SignChecker(p.P(StringValue).Then(p.EOF)),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					return tm.ParseDuration(args[0].(string))
				}
			}},
		"parseTime": SimpleBox{
			SignChecker(p.P(StringValue).Then(StringValue).Then(p.EOF)),
			func(args ...interface{}) Tasker {
				return func(env Env) (interface{}, error) {
					return tm.Parse(args[0].(string), args[1].(string))
				}
			}},
	},
}