diff --git a/condition_base.go b/condition_base.go deleted file mode 100644 index 5ce1d99..0000000 --- a/condition_base.go +++ /dev/null @@ -1,52 +0,0 @@ -package crontab - -func Add(v ...interface{}) interface{} { - return nil -} - -func Sub(v ...interface{}) interface{} { - return nil -} - -func Div(v ...interface{}) interface{} { - return nil -} - -func Mul(v ...interface{}) interface{} { - return nil -} - -func Or(v ...interface{}) interface{} { - return nil -} - -func And(v ...interface{}) interface{} { - return nil -} - -func parseConditionExpr(condExpr string) []string { - - var conds []string - OpenClose := 0 - cur := 0 - i := 0 - for ; i < len(condExpr); i++ { - switch condExpr[i] { - case ',': - if OpenClose == 0 { - conds = append(conds, condExpr[cur:i]) - cur = i + 1 - } - case '(': - OpenClose++ - case ')': - OpenClose-- - } - } - - if cur < i { - conds = append(conds, condExpr[cur:i]) - } - - return conds -} diff --git a/condition_test.go b/condition_test.go deleted file mode 100644 index be0397b..0000000 --- a/condition_test.go +++ /dev/null @@ -1,299 +0,0 @@ -package crontab - -// import ( -// "strconv" -// "testing" - -// "github.com/davecgh/go-spew/spew" -// ) - -// //xx={xx()},n>f|n > x |{w=5},5': - -// case '<': - -// case '=': - -// } -// } - -// return ops -// } - -// func parseCallParams(value string) []interface{} { - -// i := 0 -// s := 0 - -// var params []interface{} - -// for ; i < len(value); i++ { -// switch value[i] { -// case '{': -// s++ -// continue -// case ',', '=', ')': -// params = append(params, parseExpr(value[s:i])) -// s = i + 1 -// case '|': -// params = append(params, parseExpr(value[s:i])) -// s = i + 1 -// } -// } - -// return params -// // panic("can't find )") -// } - -// type Call struct { -// fname string -// params string -// } - -// func (call *Call) Execute() { -// parseFunc(call.fname)(parseCallParams(call.params)...) -// } - -// func parseCall(value string) *Call { - -// for i := 0; i < len(value); i++ { -// switch value[i] { -// case '(': -// return &Call{fname: value[0:i], params: value[i:]} -// // parseFunc(value[0:i]) -// // parseCallParams(value[i:]) -// case '=': // assignment -// return &Call{fname: "=", params: value} -// } -// } - -// return nil -// } - -// func parseCondCalls(expr string) []*Call { -// // var execList []string -// i := 0 -// s := 0 - -// var calls []*Call - -// for ; i < len(expr); i++ { -// switch expr[i] { -// case '|': -// calls = append(calls, &Call{fname: "|", params: expr[s:i]}) -// s = i + 1 -// case '&': -// calls = append(calls, &Call{fname: "&", params: expr[s:i]}) -// s = i + 1 -// } -// } - -// if s < i { -// calls = append(calls, &Call{fname: "", params: expr[s:i]}) //可能存在问题 -// } -// return calls - -// } - -// func parseCalls(expr string) []*Call { - -// // var execList []string -// i := 0 -// s := 0 - -// var calls []*Call -// FOR: -// for ; i < len(expr); i++ { -// switch expr[i] { -// case '}': -// break FOR -// case ';': -// calls = append(calls, parseCall(expr[s:i])) -// s = i + 1 -// } -// } - -// if s < i { -// calls = append(calls, parseCall(expr[s:i])) -// } -// return calls -// } - -// func parseDefine(v, method string) { - -// } - -// func parseConditionList(condExpr string) []string { - -// var conds []string -// OpenClose := 0 -// cur := 0 -// i := 0 -// for ; i < len(condExpr); i++ { -// switch condExpr[i] { -// case ',': -// if OpenClose == 0 { -// conds = append(conds, condExpr[cur:i]) -// cur = i + 1 -// } -// case '(': -// OpenClose++ -// case ')': -// OpenClose-- -// } -// } - -// if cur < i { -// conds = append(conds, condExpr[cur:i]) -// } - -// return conds -// } - -// func TestBaseCond(t *testing.T) { -// t.Error() -// condition := Condition{} -// condExpr := "5>n>12|n<15|add(1,5){w=5},f>5{w=5s;add(1,2)}" - -// ParseExpr: -// for _, expr := range parseConditionList(condExpr) { -// // condexpr - -// for i := 0; i < len(expr); i++ { - -// if expr[i] == '{' { -// s := i - -// for s > 0 { -// switch expr[s] { -// case ' ': -// s-- -// case '=': -// // 判断 = 定义表达 -// parseDefine(expr[0:s], expr[i+1:]) -// continue ParseExpr - -// default: -// // 判断 = 条件表达 - -// exec := Execute{conds: parseCondCalls(expr[0:s]), calls: parseCalls(expr[i+1:])} -// condition.execList = append(condition.execList, exec) - -// continue ParseExpr - -// } -// } - -// // 判断 = 执行表达 -// exec := Execute{calls: parseCalls(expr)} -// condition.execList = append(condition.execList, exec) - -// continue ParseExpr -// } - -// } - -// // 找不到{ 判断 = 执行表达 -// exec := Execute{calls: parseCalls(expr)} -// condition.execList = append(condition.execList, exec) -// } - -// spew.Dump(condition) -// } diff --git a/new_test.go b/new_test.go deleted file mode 100644 index 102d2f7..0000000 --- a/new_test.go +++ /dev/null @@ -1,108 +0,0 @@ -package crontab - -import ( - "strconv" - "testing" - - "github.com/davecgh/go-spew/spew" -) - -var VarDict = make(map[string]interface{}) -var FuncDict = make(map[string]func(vars ...interface{}) interface{}) - -func init() { - VarDict["w"] = float64(2.0) - VarDict["f"] = float64(0.0) - VarDict["s"] = float64(0.0) - - FuncDict["add"] = Add - FuncDict["sub"] = Sub - FuncDict["mul"] = Mul - FuncDict["div"] = Div -} - -type Object struct { - value string - ctype string - param *Object -} - -func (obj *Object) Value() interface{} { - switch obj.ctype { - case "const": - fvar, err := strconv.ParseFloat(obj.value, 64) - if err != nil { - panic(err) - } - return fvar - case "var": - return VarDict[obj.value] - case "call": - - var params []interface{} - cur := obj.param - for cur != nil { - params = append(params, cur.Value()) - } - return FuncDict[obj.value](params...) - } - - panic("err type") -} - -func (obj *Object) SetValue(v *Object) { - VarDict[obj.value] = v.Value() -} - -parse - -func TestParse(t *testing.T) { - t.Error() - condition := &Object{} - condExpr := "5>n>12|n<15|add(1,5){w=5},f>5{w=5s;add(1,2)}" - -ParseExpr: - for _, expr := range parseConditionExpr(condExpr) { - // condexpr - - for i := 0; i < len(expr); i++ { - - if expr[i] == '{' { - s := i - - for s > 0 { - switch expr[s] { - case ' ': - s-- - case '=': - // 判断 = 定义表达 - // parseDefine(expr[0:s], expr[i+1:]) - continue ParseExpr - - default: - // 判断 = 条件表达 - - exec := Execute{conds: parseCondCalls(expr[0:s]), calls: parseCalls(expr[i+1:])} - condition.execList = append(condition.execList, exec) - - continue ParseExpr - - } - } - - // 判断 = 执行表达 - exec := Execute{calls: parseCalls(expr)} - condition.execList = append(condition.execList, exec) - - continue ParseExpr - } - - } - - // 找不到{ 判断 = 执行表达 - exec := Execute{calls: parseCalls(expr)} - condition.execList = append(condition.execList, exec) - } - - spew.Dump(condition) -}