This commit is contained in:
eson 2018-12-26 05:11:46 +08:00
parent d9a422dfec
commit 5d7d9fc547
2 changed files with 230 additions and 14 deletions

17
condition_base.go Normal file
View File

@ -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
}

View File

@ -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<f<8{w=5s;},
// Condition 条件表达式的结构实例
type Condition struct {
execList []Execute
}
type Cond struct {
@ -16,14 +20,25 @@ type Cond struct {
op string
}
type Execute struct {
conds []Cond
calls []*Call
}
type CondExec struct {
params []interface{}
execType int
}
func (cond *Cond) Execute() bool {
i := 0
var ce []CondExec
for ; i < len(cond.expr); i++ {
switch cond.expr[i] {
case '(':
case '(': // 函数方法
ce = append(ce)
case '<':
case '>':
@ -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)
}