Example #1
0
func (exp *Expression) Execute(item *item.Item) bool {
	itemValue := reflect.ValueOf(*item)
	itemPointerValue := reflect.ValueOf(item)
	//Put in the appropriate Values
	for i := 0; i < len(exp.tokens); i++ {
		if exp.tokens[i].tokenType == type_VARBOOL {
			exp.tokens[i].boolValue = itemValue.Field(exp.tokens[i].index).Bool()
		} else if exp.tokens[i].tokenType == type_VARFLOAT {
			exp.tokens[i].floatValue = itemValue.Field(exp.tokens[i].index).Float()
		} else if exp.tokens[i].tokenType == type_MAPBOOL {
			exp.tokens[i].boolValue = item.GetBoolModValue(exp.tokens[i].mapKey)
		} else if exp.tokens[i].tokenType == type_MAPFLOAT {
			exp.tokens[i].floatValue = item.GetFloatModValue(exp.tokens[i].mapKey)
		} else if exp.tokens[i].tokenType == type_METHODBOOL {
			results := itemPointerValue.Method(exp.tokens[i].index).Call(nil)
			exp.tokens[i].boolValue = results[0].Bool()
		} else if exp.tokens[i].tokenType == type_METHODFLOAT {
			results := itemPointerValue.Method(exp.tokens[i].index).Call(nil)
			exp.tokens[i].floatValue = results[0].Float()
		} else if exp.tokens[i].tokenType == type_VARSTRING {
			exp.tokens[i].stringValue = itemValue.Field(exp.tokens[i].index).String()
		}
	}
	//Execute with real numbers
	stack := newStack()
	for i := 0; i < len(exp.tokens); i++ {
		tok := exp.tokens[i]
		if tok.tokenType == type_OPERATOR {
			if tok.operator == operator_NOT {
				stack.push(&token{tokenType: type_BOOL, boolValue: !stack.pop().boolValue})
			} else {
				stack.push(applyOperator(tok, stack.pop(), stack.pop()))
			}
		} else {
			stack.push(tok)
		}
	}
	/*fmt.Print("Executing: ")
	printTokens(exp.tokens)
	fmt.Print("Result: ")
	printTokens(stack.content)*/
	return stack.pop().boolValue
}
Example #2
0
func (filter *Filter) Execute(it *item.Item) string {
	boolValues := make([]bool, filter.boolIndex)
	floatValues := make([]float64, filter.floatIndex)
	stringValues := make([]string, filter.stringIndex)
	boolIndex := 0
	floatIndex := 0
	stringIndex := 0
	//***Plug in all the data from an item***
	itemValue := reflect.ValueOf(*it)
	numFields := itemValue.Type().NumField()
	//Variables
	for i := 0; i < numFields; i++ {
		if itemValue.Type().Field(i).Type.Name() == "bool" {
			boolValues[boolIndex] = itemValue.Field(i).Bool()
			boolIndex++
		} else if itemValue.Type().Field(i).Type.Name() == "float64" {
			floatValues[floatIndex] = itemValue.Field(i).Float()
			floatIndex++
		} else if itemValue.Type().Field(i).Type.Name() == "string" {
			stringValues[stringIndex] = itemValue.Field(i).String()
			stringIndex++
		}
	}
	//Methods
	itemValue = reflect.ValueOf(it)
	numMethods := itemValue.Type().NumMethod()
	for i := 0; i < numMethods; i++ {
		if itemValue.Type().Method(i).Type.String() == "func(*item.Item) bool" {
			results := itemValue.Method(i).Call(nil)
			boolValues[boolIndex] = results[0].Bool()
			boolIndex++
		} else if itemValue.Type().Method(i).Type.String() == "func(*item.Item) float64" {
			results := itemValue.Method(i).Call(nil)
			floatValues[floatIndex] = results[0].Float()
			floatIndex++
		} else if itemValue.Type().Method(i).Type.String() == "func(*item.Item) string" {
			results := itemValue.Method(i).Call(nil)
			stringValues[stringIndex] = results[0].String()
			stringIndex++
		}
	}
	//MapKeys
	for _, description := range item.ItemBoolMods { //The values in item.ItemMods are the keys to the item modifiers in the actual item
		index, _ := globalBoolIndices[description]
		boolValues[index] = it.GetBoolModValue(description)
	}
	for _, description := range item.ItemFloatMods { //The values in item.ItemMods are the keys to the item modifiers in the actual item
		index, _ := globalFloatIndices[description]
		floatValues[index] = it.GetFloatModValue(description)
	}
	// Functions
	for i := 0; i < len(filter.functions); i++ {
		f := filter.functions[i]
		tok := f.evaluate(boolValues, floatValues, stringValues, it.PossibleAffixes)
		//fmt.Println("Function: ", f.name, ", Values: ", tok.boolValue, " / ", tok.floatValue, " / ", tok.stringValue)
		if tok.tokenType == type_BOOL {
			index, _ := filter.boolIndices[f.name]
			boolValues[index] = tok.boolValue
		} else if tok.tokenType == type_FLOAT {
			index, _ := filter.floatIndices[f.name]
			floatValues[index] = tok.floatValue
		} else if tok.tokenType == type_STRING {
			index, _ := filter.stringIndices[f.name]
			stringValues[index] = tok.stringValue
		} else {
			panic("Unknown token type returned from function.")
		}
	}
	return filter.parentCondition.getWarning(boolValues, floatValues, stringValues, it.PossibleAffixes)

}