From 5d7d9fc5471ba75851bcd8140994d6fe0ecfce45 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Wed, 26 Dec 2018 05:11:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=89=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- condition_base.go | 17 ++++ condition_test.go | 227 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 230 insertions(+), 14 deletions(-) create mode 100644 condition_base.go diff --git a/condition_base.go b/condition_base.go new file mode 100644 index 0000000..cfc350a --- /dev/null +++ b/condition_base.go @@ -0,0 +1,17 @@ +package crontab + +func Add(v1, v2 interface{}) interface{} { + return nil +} + +func Sub(v1, v2 interface{}) interface{} { + return nil +} + +func Div(v1, v2 interface{}) interface{} { + return nil +} + +func Mul(v1, v2 interface{}) interface{} { + return nil +} diff --git a/condition_test.go b/condition_test.go index 3054afa..86d9f25 100644 --- a/condition_test.go +++ b/condition_test.go @@ -1,14 +1,18 @@ package crontab import ( - "strings" + "log" + "strconv" "testing" + + "github.com/davecgh/go-spew/spew" ) //xx={xx()},n>f|n > x |{w=5},5': @@ -42,7 +57,7 @@ func (cond *Cond) Execute() bool { // return 0, 1 // } -func parseCond(cond string) { +func parseCond(cond string) []Cond { var CondList []Cond s := 0 @@ -64,44 +79,228 @@ func parseCond(cond string) { } + if s < i { + CondList = append(CondList, Cond{expr: cond[s:i], op: ""}) + } + + return CondList } -func parseMethod(expr string) { +type Method struct { + expr string + exec []string +} +var VarDict = make(map[string]interface{}) +var FuncDict = make(map[string]func(vars ...interface{}) interface{}) + +func parseFunc(fname string) func(...interface{}) interface{} { + return FuncDict[fname] +} + +func parseVar(v string) float64 { + fvar, err := strconv.ParseFloat(v, 64) + if err != nil { + return *VarDict[v].(*float64) + } + return fvar +} + +func parseExpr(e string) []interface{} { + i := 1 + s := 1 + + isCall := false + // isVar := false + + for ; i < len(e); i++ { + switch e[i] { + case '(': + isCall = true + case '+': + if isCall { + parseCall(e[s:i]) + } else { + parseVar(e[s:i]) + } + + s = i + 1 + isCall = false + case '-': + if isCall { + parseCall(e[s:i]) + } else { + parseVar(e[s:i]) + } + + s = i + 1 + isCall = false + case '*': + if isCall { + parseCall(e[s:i]) + } else { + parseVar(e[s:i]) + } + + s = i + 1 + isCall = false + case '/': + if isCall { + parseCall(e[s:i]) + } else { + parseVar(e[s:i]) + } + + s = i + 1 + isCall = false + } + } + + return nil +} + +func parseCallParams(value string) []interface{} { + + i := 1 + s := 1 + + var params []interface{} + + for ; i < len(value); i++ { + switch value[i] { + case ',': + params = append(params, parseExpr(value[s:i])) + s = i + 1 + case '=': + params = append(params, parseExpr(value[s:i])) + s = i + 1 + case ')': + params = append(params, parseExpr(value[s:i])) + 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: "1", params: value} + } + } + + return nil +} + +func parseCalls(expr string) []*Call { + + // var execList []string + i := 0 + s := i + + var calls []*Call +FOR: + for ; i < len(expr); i++ { + switch expr[i] { + case '}': + break FOR + case ';': + 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) { - condExpr := "n>f|n > x |{w=5},{w=5s;}" - for _, expr := range strings.Split(condExpr, ",") { + t.Error() + condition := Condition{} + condExpr := "5>n>12|n<15|add(1,5){w=5},f>5{w=5s;add(1,2)}" + + for _, expr := range parseConditionList(condExpr) { // condexpr - for i := 0; i <= len(expr); i++ { + log.Println(expr) + ParseExpr: + for i := 0; i < len(expr); i++ { if expr[i] == '{' { - s := i - 1 + s := i + for s > 0 { switch expr[s] { case ' ': s-- case '=': // 判断 = 定义表达 - parseDefine(expr[0:s], expr[s+1:]) + parseDefine(expr[0:s], expr[i+1:]) + break ParseExpr default: // 判断 = 条件表达 - parseCond(expr[0:i]) - parseMethod(expr[i:]) + exec := Execute{conds: parseCond(expr[0:s]), calls: parseCalls(expr[i+1:])} + condition.execList = append(condition.execList, exec) + + break ParseExpr } } + // 判断 = 执行表达 - parseMethod(expr) + exec := Execute{calls: parseCalls(expr)} + condition.execList = append(condition.execList, exec) } } // 找不到{ 判断 = 执行表达 - parseMethod(expr) + exec := Execute{calls: parseCalls(expr)} + condition.execList = append(condition.execList, exec) } + + spew.Dump(condition) }