crontabex/new_test.go

109 lines
1.9 KiB
Go
Raw Normal View History

2018-12-26 11:10:53 +00:00
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)
}