Example #1
0
// n from beginning to m from end.
func (mt *MtType) EvalExpr(Context *eval.ContextType, n, m int) bool {
	m = len(mt.SVal) - m // Convert to Pos
	sv := mt.SVal[n:m]   // Slice of params to eval.
	fmt.Printf("mt.EvalExpr - TOP: ealuate sv=%+v ----------------------------------------------------- \n", sv)
	fmt.Printf("mt.EvalExpr - TOP: ealuate mt.SVal=%+v ----------------------------------------------------- \n", mt.SVal)
	// xyzzy -- temporary -- incomplete!!!!!!!!!!!!!!!!!!!!!!
	evalData := &eval.EvalType{
		Pos: 0,
		Ctx: Context,
		Mm:  mt.TokVal[n:m], // []tok.Token
	}
	fmt.Printf("INPUT m=%d n=%d, %s ----------------------------------------------------- \n", m, n, com.SVarI(evalData))
	tr := evalData.Pres2()
	fmt.Printf("BOTTOM: %s ----------------------------------------------------- \n", com.SVarI(tr))
	s := sv[0]
	v, t, _ := Context.GetFromContext(s)
	fmt.Printf("At: %s - in EvalExpr, v=%v t=%v for >%s<-\n", com.LF(), v, t, s)
	// xyzzy nil, 9 -- 9 is error, not found
	if t == eval.CtxType_Bool {
		fmt.Printf("Setting bool to true\n")
		mt.DataType = t
		mt.XValue = v
	}
	return true
}
Example #2
0
// ----------------------------------------------------------------------------------------------------------------------------------------
func FxReadJson(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("Fx_ReadJson Called, %d\n", callNo)
	// {% read_json ID "file_name.json" %} (config allows url:// not just file"

	if callNo == 0 {

		if !curTree.NOptions(2) {
			// xyzzy } else if !curTree.IsId(0) {		// -- implement to check that [0] is an ID
		} else {
			id := curTree.SVal[0]
			path := curTree.SVal[1]
			// path = path[0 : len(path)-1]
			err = nil
			// var jsonData map[string]SQLOne
			var file []byte
			file, err = ioutil.ReadFile(path)
			if err != nil {
				fmt.Printf("Error(10014): %v, %s, Config File:%s\n", err, com.LF(), path)
				return
			}
			file = []byte(strings.Replace(string(file), "\t", " ", -1)) // file = []byte(ReplaceString(string(file), "^[ \t][ \t]*//.*$", ""))

			// Check beginning of file if "{" then MapOf, if "[" Array, else look at single value
			if strings.HasPrefix(string(file), "{") {

				jsonData := make(map[string]interface{})

				err = json.Unmarshal(file, &jsonData)
				if err != nil {
					fmt.Printf("Error(10012): %v, %s, Config File:%s\n", err, com.LF(), path)
					return
				}

				Context.SetInContext(id, eval.CtxType_MapOf, jsonData)

			} else {

				jsonData := make([]interface{}, 0, 100)

				err = json.Unmarshal(file, &jsonData)
				if err != nil {
					fmt.Printf("Error(10012): %v, %s, Config File:%s\n", err, com.LF(), path)
					return
				}

				Context.SetInContext(id, eval.CtxType_ArrayOf, jsonData)

			}

		}
	}

	return
}
Example #3
0
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
func FxSet_context(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("SetContext Called, %d\n", callNo)
	if callNo == 10 {
		fmt.Printf("EvalExpr(context,0,0)=%v\n", curTree.EvalExpr(Context, 0, 0))
		if !curTree.MoreThan(2) {
		} else {
			id := curTree.SVal[0]
			val := curTree.SVal[1]
			if val == "true" || val == "TRUE" {
				Context.SetInContext(id, eval.CtxType_Bool, true) // xyzzy - type may not be correct
			} else {
				Context.SetInContext(id, eval.CtxType_Bool, false) // xyzzy - type may not be correct
			}
		}
	}
	return
}
Example #4
0
func FxCycle(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("Cycle Called, %d\n", callNo)
	if callNo == 0 {
		if !curTree.MoreThan(2) {
		} else {
			x := &FxCycleDataType{CurPos: 0}
			ne := 0                                                    // Number at end we have processed.
			no := len(curTree.SVal)                                    // Number of Options
			if curTree.MoreThan(1) && curTree.SVal[no-1] == "silent" { // extract silent
				x.Silent = true
				ne = 1
			}
			if curTree.MoreThan(2+ne) && curTree.SVal[no-1-ne] == "as" { // extract as <id>
				x.As = curTree.SVal[no-ne-1]
				ne += 2
			}
			if curTree.MoreThan(2 + ne) {
				x.Opts = curTree.EvalVars(curTree.SVal[0 : no-ne]) // eval options -> values
				x.CurPos = 0                                       // establish data and position
			}
			curTree.DataVal = x
		}
	}
	if callNo == 10 {
		x := curTree.DataVal.(*FxCycleDataType)
		if x.Silent {
			curTree.HTML_Output = ""
		} else {
			curTree.HTML_Output = x.Opts[x.CurPos] // Return Value
		}
		if x.As != "" {
			Context.SetInContext(x.As, eval.CtxType_Str, x.Opts[x.CurPos]) // xyzzy - type may not be correct
		}
		x.CurPos = (x.CurPos + 1) % len(x.Opts) // Increment Postion Mod Length
		curTree.DataVal = x
	}
	return
}
Example #5
0
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
func FxGet_context(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("GetContext Called, %d\n", callNo)
	if callNo == 10 {
		if !curTree.NOptions(1) {
		} else {
			id := curTree.SVal[0] // xyzzy - should be an EvalExpr of ...
			val, typ, fnd := Context.GetFromContext(id)
			if fnd {
				fmt.Printf("Found! id=%s typ=%d = ->%s<-\n", id, typ, val)
				if typ == eval.CtxType_Bool { // xyzzy - other types ...
					if val.(bool) {
						curTree.HTML_Output = "true"
					} else {
						curTree.HTML_Output = "false"
					}
				}
			} else {
				fmt.Printf("Not Found %s\n", id)
				curTree.HTML_Output = ""
			}
		}
	}
	return
}
Example #6
0
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
func FxDump_context(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("DumpContext Called, %d\n", callNo)
	Context.DumpContext()
	return
}
Example #7
0
func FxFor(callNo int, pt *Parse2Type, Context *eval.ContextType, curTree *MtType) (err error) {
	fmt.Printf("Fx_For Called, %d\n", callNo)
	fmt.Printf("---------------------------------------------------------------------------- for tree -------------------------------------------------------------------------\n")
	if false {
		fmt.Printf("%s\n\n", com.SVarI(curTree))
	} else {
		curTree.DumpMtType(os.Stdout, 0, 0)
	}

	tmpMt := func(ss []*MtType) (rv *MtType) {
		rv = &MtType{
			NodeType: gen.Tok_Tree_List,
			List:     make([]*MtType, 0, len(ss)),
			LineNo:   ss[0].LineNo,
			ColNo:    ss[0].ColNo,
			FileName: ss[0].FileName,
		}
		for _, vv := range ss {
			rv.List = append(rv.List, vv)
		}
		return
	}

	var walkTreeEmptyOutput func(mt *MtType, pos, depth int)
	walkTreeEmptyOutput = func(mt *MtType, pos, depth int) {
		mt.HTML_Output = ""
		for ii, vv := range mt.List {
			walkTreeEmptyOutput(vv, ii, depth+1)
		}
	}

	if callNo == 11 {
		if !curTree.MoreThan(1) {
		} else {
			ifp := FindTags(curTree.List[0], gen.Tok_Tree_Empty, gen.Tok_Tree_EndFor) // find parts of for loop
			fmt.Printf("ifp=%+v\n", ifp)
			if curTree.EvalExpr(Context, 0, 0) {
				x := tmpMt(curTree.List[0].List[0:ifp[0]])
				curTree.HTML_Output = ""
				// xyzzy - check type
				for ii, vv := range curTree.XValue.([]interface{}) {
					Context.SetInContext("$index", eval.CtxType_Int, ii) // xyzzy - conversion to string not correct -- needs to push $index - on endfor pop
					Context.SetInContext("$value", eval.CtxType_Str, vv) // xyzzy - conversion to string not correct
					//Context.SetInContext("key", fmt.Sprintf("%d", ii))    // xyzzy - conversion to string not correct	 -- key should be ID, Value too.
					//Context.SetInContext("value", fmt.Sprintf("%v", vv))  // xyzzy - conversion to string not correct
					pt.x_walk(x, pt.pos, pt.depth)
					curTree.HTML_Output += pt.CollectTree(x, 0) // Need to collect HTML_Output and append it to curTree.HTML_Output
				}
				mx := len(ifp)
				// xyzzy - check type
				if len(curTree.XValue.([]interface{})) == 0 && mx > 1 && curTree.List[0].List[ifp[mx-1]].NodeType == gen.Tok_Tree_Empty {
					i := mx - 1
					x := tmpMt(curTree.List[0].List[ifp[i]+1 : ifp[i+1]])
					pt.x_walk(x, pt.pos, pt.depth)
					curTree.HTML_Output += pt.CollectTree(x, 0) // Need to collect HTML_Output and append it to curTree.HTML_Output
				}
				walkTreeEmptyOutput(curTree.List[0], 0, 0) // set children's HTML_Output to ""
			}
		}
	}
	return
}