save
This commit is contained in:
parent
5d7d9fc547
commit
cc9071bc70
|
@ -1,17 +1,52 @@
|
|||
package crontab
|
||||
|
||||
func Add(v1, v2 interface{}) interface{} {
|
||||
func Add(v ...interface{}) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Sub(v1, v2 interface{}) interface{} {
|
||||
func Sub(v ...interface{}) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Div(v1, v2 interface{}) interface{} {
|
||||
func Div(v ...interface{}) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Mul(v1, v2 interface{}) interface{} {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -1,306 +1,299 @@
|
|||
package crontab
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"testing"
|
||||
// import (
|
||||
// "strconv"
|
||||
// "testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
// "github.com/davecgh/go-spew/spew"
|
||||
// )
|
||||
|
||||
//xx={xx()},n>f|n > x |{w=5},5<f<8{w=5s;},
|
||||
// //xx={xx()},n>f|n > x |{w=5},5<f<8{w=5s;},
|
||||
|
||||
// Condition 条件表达式的结构实例
|
||||
type Condition struct {
|
||||
execList []Execute
|
||||
}
|
||||
|
||||
type Cond struct {
|
||||
expr string
|
||||
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 '(': // 函数方法
|
||||
ce = append(ce)
|
||||
case '<':
|
||||
|
||||
case '>':
|
||||
|
||||
case '=':
|
||||
|
||||
case ' ':
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// func indexOperator(s string, start int) (start int, end int) {
|
||||
// return 0, 1
|
||||
// // Condition 条件表达式的结构实例
|
||||
// type Condition struct {
|
||||
// execList []Execute
|
||||
// }
|
||||
|
||||
func parseCond(cond string) []Cond {
|
||||
var CondList []Cond
|
||||
// type Cond struct {
|
||||
// expr string
|
||||
// op string
|
||||
// }
|
||||
|
||||
s := 0
|
||||
i := 0
|
||||
for ; i < len(cond); i++ {
|
||||
// type Execute struct {
|
||||
// conds []*Call
|
||||
// calls []*Call
|
||||
// }
|
||||
|
||||
switch cond[i] {
|
||||
// case '{':
|
||||
// // take execute proccess
|
||||
case '|':
|
||||
CondList = append(CondList, Cond{expr: cond[s:i], op: "|"})
|
||||
s = i + 1
|
||||
case '&':
|
||||
CondList = append(CondList, Cond{expr: cond[s:i], op: "&"})
|
||||
s = i + 1
|
||||
case ' ':
|
||||
i++
|
||||
}
|
||||
// // func indexOperator(s string, start int) (start int, end int) {
|
||||
// // return 0, 1
|
||||
// // }
|
||||
|
||||
}
|
||||
// var VarDict = make(map[string]interface{})
|
||||
// var FuncDict = make(map[string]func(vars ...interface{}) interface{})
|
||||
|
||||
if s < i {
|
||||
CondList = append(CondList, Cond{expr: cond[s:i], op: ""})
|
||||
}
|
||||
// func init() {
|
||||
// VarDict["w"] = float64(2.0)
|
||||
// VarDict["f"] = float64(0.0)
|
||||
// VarDict["s"] = float64(0.0)
|
||||
// }
|
||||
|
||||
return CondList
|
||||
}
|
||||
// func parseFunc(fname string) func(...interface{}) interface{} {
|
||||
// return FuncDict[fname]
|
||||
// }
|
||||
|
||||
type Method struct {
|
||||
expr string
|
||||
exec []string
|
||||
}
|
||||
// func parseVar(v string) float64 {
|
||||
// fvar, err := strconv.ParseFloat(v, 64)
|
||||
// if err != nil {
|
||||
// return VarDict[v].(float64)
|
||||
// }
|
||||
// return fvar
|
||||
// }
|
||||
|
||||
var VarDict = make(map[string]interface{})
|
||||
var FuncDict = make(map[string]func(vars ...interface{}) interface{})
|
||||
// type Operator struct {
|
||||
// Value interface{}
|
||||
// Operate func(v1, v2 interface{}) interface{}
|
||||
// }
|
||||
|
||||
func parseFunc(fname string) func(...interface{}) interface{} {
|
||||
return FuncDict[fname]
|
||||
}
|
||||
// func parseExpr(e string) []Operator {
|
||||
// i := 1
|
||||
// s := 1
|
||||
|
||||
func parseVar(v string) float64 {
|
||||
fvar, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
return *VarDict[v].(*float64)
|
||||
}
|
||||
return fvar
|
||||
}
|
||||
// isCall := false
|
||||
// // isVar := false
|
||||
|
||||
func parseExpr(e string) []interface{} {
|
||||
i := 1
|
||||
s := 1
|
||||
// var ops []Operator
|
||||
|
||||
isCall := false
|
||||
// isVar := false
|
||||
// for ; i < len(e); i++ {
|
||||
// switch e[i] {
|
||||
// case '(':
|
||||
// isCall = true
|
||||
// case '+':
|
||||
// if isCall {
|
||||
// ops = append(ops, Operator{Value: parseCall(e[s:i]), Operate: Add})
|
||||
// } else {
|
||||
// ops = append(ops, Operator{Value: parseVar(e[s:i]), Operate: Add})
|
||||
// }
|
||||
|
||||
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
|
||||
|
||||
s = i + 1
|
||||
isCall = false
|
||||
case '-':
|
||||
if isCall {
|
||||
parseCall(e[s:i])
|
||||
} else {
|
||||
parseVar(e[s:i])
|
||||
}
|
||||
// case '-':
|
||||
// if isCall {
|
||||
// ops = append(ops, Operator{Value: parseCall(e[s:i]), Operate: Sub})
|
||||
// } else {
|
||||
// ops = append(ops, Operator{Value: parseVar(e[s:i]), Operate: Sub})
|
||||
// }
|
||||
|
||||
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 {
|
||||
// ops = append(ops, Operator{Value: parseCall(e[s:i]), Operate: Mul})
|
||||
// } else {
|
||||
// ops = append(ops, Operator{Value: parseVar(e[s:i]), Operate: Mul})
|
||||
// }
|
||||
|
||||
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 {
|
||||
// ops = append(ops, Operator{Value: parseCall(e[s:i]), Operate: Div})
|
||||
// } else {
|
||||
// ops = append(ops, Operator{Value: parseVar(e[s:i]), Operate: Div})
|
||||
// }
|
||||
|
||||
s = i + 1
|
||||
isCall = false
|
||||
}
|
||||
}
|
||||
// s = i + 1
|
||||
// isCall = false
|
||||
|
||||
return nil
|
||||
}
|
||||
// case '>':
|
||||
|
||||
func parseCallParams(value string) []interface{} {
|
||||
// case '<':
|
||||
|
||||
i := 1
|
||||
s := 1
|
||||
// case '=':
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
// return ops
|
||||
// }
|
||||
|
||||
panic("can't find )")
|
||||
}
|
||||
// func parseCallParams(value string) []interface{} {
|
||||
|
||||
type Call struct {
|
||||
fname string
|
||||
params string
|
||||
}
|
||||
// i := 0
|
||||
// s := 0
|
||||
|
||||
func (call *Call) Execute() {
|
||||
parseFunc(call.fname)(parseCallParams(call.params)...)
|
||||
}
|
||||
// var params []interface{}
|
||||
|
||||
func parseCall(value string) *Call {
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
|
||||
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 params
|
||||
// // panic("can't find )")
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
// type Call struct {
|
||||
// fname string
|
||||
// params string
|
||||
// }
|
||||
|
||||
func parseCalls(expr string) []*Call {
|
||||
// func (call *Call) Execute() {
|
||||
// parseFunc(call.fname)(parseCallParams(call.params)...)
|
||||
// }
|
||||
|
||||
// var execList []string
|
||||
i := 0
|
||||
s := i
|
||||
// func parseCall(value string) *Call {
|
||||
|
||||
var calls []*Call
|
||||
FOR:
|
||||
for ; i < len(expr); i++ {
|
||||
switch expr[i] {
|
||||
case '}':
|
||||
break FOR
|
||||
case ';':
|
||||
calls = append(calls, parseCall(expr[s:i]))
|
||||
}
|
||||
}
|
||||
// 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 calls
|
||||
}
|
||||
// return nil
|
||||
// }
|
||||
|
||||
func parseDefine(v, method string) {
|
||||
// func parseCondCalls(expr string) []*Call {
|
||||
// // var execList []string
|
||||
// i := 0
|
||||
// s := 0
|
||||
|
||||
}
|
||||
// var calls []*Call
|
||||
|
||||
func parseConditionList(condExpr string) []string {
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
|
||||
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 s < i {
|
||||
// calls = append(calls, &Call{fname: "", params: expr[s:i]}) //可能存在问题
|
||||
// }
|
||||
// return calls
|
||||
|
||||
if cur < i {
|
||||
conds = append(conds, condExpr[cur:i])
|
||||
}
|
||||
// }
|
||||
|
||||
return conds
|
||||
}
|
||||
// func parseCalls(expr string) []*Call {
|
||||
|
||||
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)}"
|
||||
// // var execList []string
|
||||
// i := 0
|
||||
// s := 0
|
||||
|
||||
for _, expr := range parseConditionList(condExpr) {
|
||||
// condexpr
|
||||
log.Println(expr)
|
||||
ParseExpr:
|
||||
for i := 0; i < len(expr); i++ {
|
||||
// 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 expr[i] == '{' {
|
||||
s := i
|
||||
// if s < i {
|
||||
// calls = append(calls, parseCall(expr[s:i]))
|
||||
// }
|
||||
// return calls
|
||||
// }
|
||||
|
||||
for s > 0 {
|
||||
switch expr[s] {
|
||||
case ' ':
|
||||
s--
|
||||
case '=':
|
||||
// 判断 = 定义表达
|
||||
parseDefine(expr[0:s], expr[i+1:])
|
||||
break ParseExpr
|
||||
default:
|
||||
// 判断 = 条件表达
|
||||
exec := Execute{conds: parseCond(expr[0:s]), calls: parseCalls(expr[i+1:])}
|
||||
condition.execList = append(condition.execList, exec)
|
||||
// func parseDefine(v, method string) {
|
||||
|
||||
break ParseExpr
|
||||
}
|
||||
}
|
||||
// }
|
||||
|
||||
// 判断 = 执行表达
|
||||
exec := Execute{calls: parseCalls(expr)}
|
||||
condition.execList = append(condition.execList, exec)
|
||||
}
|
||||
// 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--
|
||||
// }
|
||||
// }
|
||||
|
||||
// 找不到{ 判断 = 执行表达
|
||||
exec := Execute{calls: parseCalls(expr)}
|
||||
condition.execList = append(condition.execList, exec)
|
||||
}
|
||||
// if cur < i {
|
||||
// conds = append(conds, condExpr[cur:i])
|
||||
// }
|
||||
|
||||
spew.Dump(condition)
|
||||
}
|
||||
// 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)
|
||||
// }
|
||||
|
|
108
new_test.go
Normal file
108
new_test.go
Normal file
|
@ -0,0 +1,108 @@
|
|||
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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user