// Assigns a value to a symbol name private void SetSymbol(FunctionParameter leftHand, FunctionParameter rightHand) { string symbolVal = ArithmeticOperationBase.GetResult(rightHand.Value, ref symbolTable); string symbolName = ""; // Is it an array index? string indexName = ""; string[] indexSplit = leftHand.Value.Split('['); if (indexSplit != null && indexSplit.Length == 2) { symbolName = indexSplit[0]; indexName = indexSplit[1].Substring(0, indexSplit[1].Length - 1); indexName = ArithmeticOperationBase.GetResult(indexName, ref symbolTable); Logger.Log($"\"{currentNode.Serialize()}\": index was {indexName}"); } else { symbolName = leftHand.Value; } bool isString = leftHand.IsReference ? (symbolTable.ContainsKey(symbolVal) && symbolTable[symbolVal].Value.Trim().StartsWith("\"") && symbolTable[symbolVal].Value.Trim().EndsWith("\"")) : (symbolVal.Trim().StartsWith("\"") && symbolVal.Trim().EndsWith("\"")); bool isReference = leftHand.IsReference ? true : !isString && symbolTable.ContainsKey(symbolVal); Logger.Log($"indexName={indexName}"); if (!string.IsNullOrWhiteSpace(indexName)) { if (symbolTable.ContainsKey(indexName)) { symbolName += $"[{symbolTable[indexName].Value}]"; } else { symbolName += $"[{indexName}]"; } } Logger.Log($"Assigning {symbolVal} to {symbolName}"); double tempNum = 0.0; string assignedType = (isString ? "String" : (symbolVal.Trim() == "True" || symbolVal.Trim() == "False" ? "Boolean" : (double.TryParse(symbolVal.Trim(), out tempNum) ? "Number" : ""))); if (!symbolTable.ContainsKey(symbolName)) { // TODO: Type = "Int" won't always work, we need a generic type like Number, however Reflection.ParameterInfo needs to be converted in that case symbolTable.Add(symbolName, new FunctionParameter { Name = rightHand.Name, Type = isReference ? symbolTable[symbolVal].Type : assignedType, Value = isReference ? symbolTable[symbolVal].Value : symbolVal });; } else { symbolTable[symbolName] = new FunctionParameter { Name = rightHand.Name, Type = isReference ? symbolTable[symbolVal].Type : assignedType, Value = isReference ? symbolTable[symbolVal].Value : symbolVal }; } }
public void ParamEditingFinished(string finalValue) { int paramIndex = -1; if (transform.name.Contains("Parameter") && name != "Parameter") { paramIndex = System.Convert.ToInt32(transform.name.Substring("Parameter".Length)) - 1; } if (paramIndex >= 0) { GetComponentInParent <FunctionCallBase>().parameters[paramIndex].Value = finalValue; GetComponentInParent <FunctionCallBase>().UpdateFunctionProperties(); ArithmeticOperationBase potentialArithmetic = GetComponentInParent <ArithmeticOperationBase>(); if (potentialArithmetic != null && potentialArithmetic.NextNodeObject != null && potentialArithmetic.NextNodeObject.GetComponent <FunctionCallBase>() && potentialArithmetic.NextNodeObject.GetComponent <FunctionCallBase>().prevArithmetic == potentialArithmetic) { potentialArithmetic.NextNodeObject.GetComponent <FunctionCallBase>().UpdateFunctionProperties(); } } }
public virtual void UpdateFunctionProperties() { if (functionNameText == null) { functionNameText = transform.Find("FuncName").transform.Find("Text").gameObject; } functionNameText.GetComponent <Text>().text = functionName; float margin = Mathf.Abs(firstParamOrigin.y - functionNameText.GetComponentInParent <RectTransform>().localPosition.y); // Remove any parameter UI objects List <Transform> paramsToDestroy = new List <Transform>(); foreach (Transform child in GetComponentsInChildren <Transform>()) { if (child.name.StartsWith("Parameter")) { paramsToDestroy.Add(child); } } foreach (Transform param in paramsToDestroy) { if (Convert.ToInt32(param.name.Replace("Parameter", "")) > parameters.Count) { Destroy(param.gameObject); GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, (initHeight == 0.0f ? GetComponent <RectTransform>().rect.height : initHeight) - firstParamHeight * (parameters.Count > 2 ? (float)parameters.Count : 1.5f) - margin); } } GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, (initHeight == 0.0f ? GetComponent <RectTransform>().rect.height : initHeight) + firstParamHeight * (parameters.Count > 2 ? (float)parameters.Count : 1.5f) + margin); for (ushort i = 0; i < paramCount && parameters != null && i < parameters.Count; i++) { int paramIdx = (parameters.Count - 1) - i; // TODO: type checking, pointing references to scene objects, etc. Transform param = transform.Find($"Parameter{paramIdx + 1}"); if (param == null) { GameObject paramObject = Instantiate(ParameterTemplate, transform); paramObject.SetActive(true); // some really weird maths? it works for arranging the parameter objects in the UI though... float height = paramObject.GetComponent <RectTransform>().rect.height; float origin = parameters.Count > 0 ? ((parameters.Count / 2.0f - parameters.Count) + i) * height : 0.0f; paramObject.transform.localPosition = new Vector3(0.0f, origin); paramObject.name = $"Parameter{paramIdx+1}"; if (paramObject.GetComponent <EditorDraggableNode>()) { paramObject.GetComponent <EditorDraggableNode>().enabled = true; } param = paramObject.transform; if (computer != null && computer.symbolTable != null) { ref Dictionary <string, FunctionParameter> symTable = ref computer.symbolTable; string resultStr = ArithmeticOperationBase.GetResult(parameters[paramIdx].Value, ref symTable); //double result = string.IsNullOrWhiteSpace(resultStr) ? 0 : double.Parse(resultStr); } } // Name is the parameter name as defined by the function. Not a variable name. If we haven't defined the parameter name, don't show the = character. param.GetComponentInChildren <Text>().text = $"{parameters[paramIdx].Name}{(!string.IsNullOrWhiteSpace(parameters[paramIdx].Name) ? "=" : "")}{(string.IsNullOrWhiteSpace(parameters[paramIdx].Expression) ? parameters[paramIdx].Value : parameters[paramIdx].Expression)}"; }
// Returns the result of the conditional at this frame public bool Evaluate(ref Dictionary <string, FunctionParameter> symbolTable) { bool ret = false; string lhVal = leftHand.Value; // Is lefthand an array index? string lhIndexName = ""; string[] lhIndexSplit = leftHand.Value.Split('['); if (lhIndexSplit != null && lhIndexSplit.Length == 2) { lhVal = lhIndexSplit[0]; lhIndexName = lhIndexSplit[1].Substring(0, lhIndexSplit[1].Length - 1); lhIndexName = ArithmeticOperationBase.GetResult(lhIndexName, ref symbolTable); if (!string.IsNullOrWhiteSpace(lhIndexName)) { lhVal += $"[{lhIndexName}]"; } } string rhVal = rightHand.Value; // Is righthand an array index? string rhIndexName = ""; string[] rhIndexSplit = rightHand.Value.Split('['); if (rhIndexSplit != null && rhIndexSplit.Length == 2) { rhVal = rhIndexSplit[0]; rhIndexName = rhIndexSplit[1].Substring(0, rhIndexSplit[1].Length - 1); rhIndexName = ArithmeticOperationBase.GetResult(rhIndexName, ref symbolTable); if (!string.IsNullOrWhiteSpace(rhIndexName)) { rhVal += $"[{rhIndexName}]"; } } Logger.Log($"lhIndexName = {lhIndexName}"); Logger.Log($"rhIndexName = {rhIndexName}"); string lh = ArithmeticOperationBase.GetResult(lhVal, ref symbolTable); string rh = ArithmeticOperationBase.GetResult(rhVal, ref symbolTable); bool isLeftString = leftHand.IsReference ? (symbolTable.ContainsKey(rh) && symbolTable[lh].Value.Trim().StartsWith("\"") && symbolTable[lh].Value.Trim().EndsWith("\"")) : (lh.Trim().StartsWith("\"") && lh.Trim().EndsWith("\"")); bool isLeftReference = leftHand.IsReference ? true : !isLeftString && symbolTable.ContainsKey(lh); bool isRightString = rightHand.IsReference ? (symbolTable.ContainsKey(rh) && symbolTable[rh].Value.Trim().StartsWith("\"") && symbolTable[rh].Value.Trim().EndsWith("\"")) : (rh.Trim().StartsWith("\"") && rh.Trim().EndsWith("\"")); bool isRightReference = rightHand.IsReference ? true : !isRightString && symbolTable.ContainsKey(rh); string leftValue = isLeftReference ? symbolTable[lh].Value : (isLeftString ? lh.Trim().Substring(1, lh.Trim().Length - 2) : lh.Trim()); string rightValue = isRightReference ? symbolTable[rh].Value : (isRightString ? rh.Trim().Substring(1, rh.Trim().Length - 2) : rh.Trim()); // failsafe for missing/invalid values if (string.IsNullOrWhiteSpace(leftValue) || string.IsNullOrWhiteSpace(rightValue)) { Logger.LogWarning("BoolCondition.Evaluate: invalid values!"); return(false); } double numL, numR = numL = 0; if (comparison == "==") { ret = leftValue == rightValue; } else if (comparison == "!=") { ret = leftValue != rightValue; } else if (comparison == ">=") { if (double.TryParse(leftValue, out numL) && double.TryParse(rightValue, out numR)) { ret = numL >= numR; } } else if (comparison == "<=") { if (double.TryParse(leftValue, out numL) && double.TryParse(rightValue, out numR)) { ret = numL <= numR; } } else if (comparison == ">") { if (double.TryParse(leftValue, out numL) && double.TryParse(rightValue, out numR)) { ret = numL > numR; } } else if (comparison == "<") { if (double.TryParse(leftValue, out numL) && double.TryParse(rightValue, out numR)) { ret = numL < numR; } } Logger.Log($"BoolCondition.Evaluate: Finished with result {ret.ToString().ToUpper()}. \nEvaluated '{leftHand.Value} {comparison} {rightHand.Value}' as '{leftValue} {comparison} {rightValue}'. \nNumerical: {numL} {comparison} {numR}"); return(ret); }