Пример #1
0
func libSubstr(th *eval.Thread, in []eval.Value, out []eval.Value) {
	s := in[0].String()
	start, err := strconv.ParseInt(in[1].String(), 10, 32)
	var res string
	if err != nil {
		panic(err)
	}
	end, err := strconv.ParseInt(in[2].String(), 10, 32)
	if err != nil {
		panic(err)
	}

	if int(start) < 0 {
		newStart := len(s) + int(start)
		res := s[newStart:len(s)]
		out[0] = eval.ToValue(res)
		return
	}

	if len(s) < int(end) {
		res = s[start:]
		out[0] = eval.ToValue(res)
		return
	}

	res = s[start:end]
	out[0] = eval.ToValue(res)
}
Пример #2
0
func libParseFloat(th *eval.Thread, in []eval.Value, out []eval.Value) {
	res, err := strconv.ParseFloat(in[0].String(), 64)
	if err != nil {
		panic(err)
	}
	out[0] = eval.ToValue(float64(res))
}
Пример #3
0
func libRandInt(th *eval.Thread, in []eval.Value, out []eval.Value) {
	res, err := strconv.ParseInt(in[0].String(), 10, 64)
	if err != nil {
		panic(err)
	}
	v := int(rand.Float64() * float64(res))
	out[0] = eval.ToValue(v)
}
Пример #4
0
func AbtabGrepMakeExpressionFilter(inp *AbtabURL, expr string) AbtabFilterFn {
	var fset = token.NewFileSet()
	w := eval.NewWorld()
	InstallStandardLibrary(w)

	var vars map[string]*eval.Variable = make(map[string]*eval.Variable, 1)

	var empty = ""
	var lnum int
	v, _ := w.DefineVar("__LINE__", eval.TypeOfNative(lnum), eval.ToValue(lnum))
	vars["__LINE__"] = v

	for _, fname := range inp.Header {
		v, _ := w.DefineVar(ScrubFieldNameForEval(fname), eval.TypeOfNative(empty), eval.ToValue(empty))
		vars[fname] = v
	}

	return func(rec *Rec) (bool, error) {
		// NB: this seems expensive, can we just re-assign to the internal var ref?
		for idx, fname := range inp.Header {
			var value = rec.Fields[idx]
			vars[fname].Init = eval.ToValue(value)
		}

		//fmt.Fprintf(os.Stderr, "setting __LINE__=%d\n", rec.LineNum)
		vars["__LINE__"].Init = eval.ToValue(int(rec.LineNum))

		// NB: this seems expensive, can we compile once?
		code, err := w.Compile(fset, expr)
		if err != nil {
			return false, err
		}

		v, err := code.Run()
		if err != nil {
			return false, err
		}

		if v.String() == "true" {
			return true, nil
		}

		return false, nil
	}
}
Пример #5
0
func libRandFloat(th *eval.Thread, in []eval.Value, out []eval.Value) {
	out[0] = eval.ToValue(rand.Float64())
}