public Exp_Node FindParent(Exp_Node node, Exp_Node parent) { if (parent != null) { if (parent.left == node) { return(parent); } if (parent.right == node) { return(parent); } Exp_Node leftResult = null, rightResult = null; leftResult = FindParent(node, parent.left); rightResult = FindParent(node, parent.right); if (leftResult != null) { return(leftResult); } if (rightResult != null) { return(rightResult); } return(null); } else { return(null); } }
public static bool isOperation(Exp_Node piece) { if (piece.exp_Operator.init) { return(true); } return(false); }
public static IParameterType GetDataType(Exp_Node node) { if (node.exp_Type != Exp_Type.Constant) { return(ParameterType.GetParameterType("none")); } else if (!Exp_Node.isConstant(node.exp_Data)) { return(ParameterType.GetParameterType("none")); } else { if (node.exp_Data[0] == '\"' && node.exp_Data[node.exp_Data.Length - 1] == '\"') { return(ParameterType.GetParameterType("lpcstr")); } if (node.exp_Data.IndexOf(".") != -1) { Single valFloat32; if (Single.TryParse(node.exp_Data, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out valFloat32)) { return(ParameterType.GetParameterType("float32")); } //return ParameterType.GetParameterType("int32"); } Int32 valInt32 = 0; if (Int32.TryParse(node.exp_Data, NumberStyles.Integer, CultureInfo.InvariantCulture.NumberFormat, out valInt32)) { return(ParameterType.GetParameterType("int32")); } Int16 valInt16 = 0; if (Int16.TryParse(node.exp_Data, NumberStyles.Integer, CultureInfo.InvariantCulture.NumberFormat, out valInt16)) { return(ParameterType.GetParameterType("int16")); } byte valInt8 = 0; if (byte.TryParse(node.exp_Data, NumberStyles.Integer, CultureInfo.InvariantCulture.NumberFormat, out valInt8)) { return(ParameterType.GetParameterType("int8")); } bool valBool = false; if (bool.TryParse(node.exp_Data, out valBool)) { return(ParameterType.GetParameterType("bool")); } throw new NotImplementedException(node.exp_Data + " is not yet recognized"); } }
public Exp_Statement(List <string> list) { items = list; if (list == null) { throw new AST_EmptyInputException( "Expression is empty", new NullReferenceException("Expression piece list is null"), 0); } head = new Exp_Node_New(list, 0); while (Simplify(head)) { ; } }
public bool Simplify(Exp_Node node) { if (node.left != null /* && node.left.exp_Type == Exp_Type.Operator*/) { while (Simplify(node.left)) { ; } } if (node.right != null /* && node.right.exp_Type == Exp_Type.Operator*/) { while (Simplify(node.right)) { ; } } if (node.exp_Type == Exp_Type.Operator) { if (node.left != null && node.right != null && node.left.exp_Type == Exp_Type.Constant && node.right.exp_Type == Exp_Type.Constant) { var lType = GetDataType(node.left); var rType = GetDataType(node.right); var lValue = node.left.exp_Data; var rValue = node.right.exp_Data; if (!lType.Equals(rType)) { return(false); } //throw new NotImplementedException( // "Operations between " + lType.GetName() + " and " + rType.GetName() + // " are not suppported"); if (lType is PT_Void || lType is PT_None) { return(false); } var cl = ParameterType.Parse(lValue, (dynamic)lType); var cr = ParameterType.Parse(rValue, (dynamic)rType); if (node.exp_Operator.oper == "+") { node.SetLeft(null); node.SetRight(null); node.SetData((cl + cr).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == "-") { node.SetLeft(null); node.SetRight(null); node.SetData((cl - cr).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == "*") { node.SetLeft(null); node.SetRight(null); node.SetData((cl * cr).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == "/") { node.SetLeft(null); node.SetRight(null); node.SetData((cl / cr).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == "%") { node.SetLeft(null); node.SetRight(null); node.SetData((cl % cr).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == "==") { node.SetLeft(null); node.SetRight(null); node.SetData((cl == cr ? 1 : 0).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == "!=") { node.SetLeft(null); node.SetRight(null); node.SetData((cl != cr ? 1 : 0).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == ">") { node.SetLeft(null); node.SetRight(null); node.SetData((cl > cr ? 1 : 0).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == "<") { node.SetLeft(null); node.SetRight(null); node.SetData((cl < cr ? 1 : 0).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == ">=") { node.SetLeft(null); node.SetRight(null); node.SetData((cl >= cr ? 1 : 0).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } else if (node.exp_Operator.oper == "<=") { node.SetLeft(null); node.SetRight(null); node.SetData((cl <= cr ? 1 : 0).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } } else if (node.left == null && node.right != null && node.right.exp_Type == Exp_Type.Constant) { var rType = GetDataType(node.right); var rValue = node.right.exp_Data; var cr = ParameterType.Parse(rValue, (dynamic)rType); if (node.exp_Operator.oper == "-") { node.SetLeft(null); node.SetRight(null); node.SetData((-cr).ToString(CultureInfo.InvariantCulture)); node.SetType(Exp_Type.Constant); node.SetOperator(new AST_Operator()); return(true); } } } return(false); }
public Exp_Node FindParent(Exp_Node node) { return(FindParent(node, head)); }
public Exp_Node(List <string> input, int charInd) { int charIndex = charInd, leftIndex = 0, rightIndex = 0; //TODO - add support for different operations if (input == null || input.Count == 0) { throw new AST_EmptyInputException("Can't parse expression. Input list is empty.", charIndex); } this.input = input; List <string> l; List <string> r; List <string> temp = new List <string>(); int i = 0; //TODO - put this code in a separate place string bracketTypesStart = "", bracketTypesEnd = ""; foreach (var oper in AST_Expression.operators_ast) { if (oper.oper.IndexOf(' ') != -1) { string[] operParts = oper.oper.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (operParts.Length == 2) { bracketTypesStart += operParts[0]; bracketTypesEnd += operParts[1]; } } } var operPrefix = AST_Expression.operators_ast.Where((a) => a.isPrefix == true).ToList(); var operSuffix = AST_Expression.operators_ast.Where((a) => a.isPrefix == false).ToList(); var operInfix = AST_Expression.operators_ast.Where((a) => a.isPrefix == null).ToList(); //Get left child node //Check sequence: brackets, TODO - equals sign, prefix operations, infix operations, suffix operations if (bracketTypesStart.IndexOf(input[i]) != -1) { //Input expression starts with an opening bracket, find corresponding end int bracket_count = 1; ++charIndex; ++i; leftIndex = charIndex; //TODO - rewrite this code with a stack instead of an increment/decrement system while (i < input.Count) { if (bracketTypesStart.IndexOf(input[i]) != -1) { bracket_count++; } if (bracketTypesEnd.IndexOf(input[i]) != -1) { bracket_count--; } charIndex += input[i].Length; if (bracket_count == 0) { break; } temp.Add(input[i]); ++i; } ++i; if (bracket_count == 0) { l = temp; temp = new List <string>(); if (i >= input.Count) { //We reached the end of the expression //TODO - decide between "(" and "( )" exp_Data = input[0]; exp_Type = Exp_Type.Bracket; rightIndex = charInd; r = null; } else { if (isOperation(input[i])) { exp_Data = input[i]; exp_Type = Exp_Type.Operator; charIndex += input[i].Length; rightIndex = charIndex; if (i + 1 >= input.Count) { //Check for a suffix and infix operation if (operSuffix.FindIndex(a => a.oper == input[i]) != -1) { //It is a suffix operation r = null; } else { throw new AST_BadFormatException( input[i] + " is not a suffix operaiton", charIndex); } } else { if (operInfix.FindIndex(a => a.oper == input[i]) != -1) { //It is an infix operation for (i = i + 1; i < input.Count; ++i) { temp.Add(input[i]); charIndex += input[i].Length; } r = temp; } else { throw new AST_BadFormatException( input[i] + " is not an infix operaiton", charIndex); } } } else { throw new AST_BadFormatException("Didn\'t find an operation after brackets", charIndex); } } } else { throw new AST_BadFormatException( "Didn\'t find a corresponding closing bracket for " + input[0], charIndex); } } else { //It is not a compound expression. Check for function calls bool isVar = isVariable(input[i]), isCon = isConstant(input[i]); if ((isVar || isCon) && i == input.Count - 1) { //It is a leaf of the tree leftIndex = charInd; rightIndex = charInd; l = null; r = null; exp_Data = input[i]; exp_Type = isVar ? Exp_Type.Variable : Exp_Type.Constant; } else if (operInfix.FindIndex(a => a.oper == input[i + 1]) != -1 && i + 2 < input.Count) { //it is an expression leftIndex = charIndex; l = new List <string> { input[i] }; exp_Type = Exp_Type.Operator; exp_Data = input[i + 1]; charIndex += input[i].Length + input[i + 1].Length; rightIndex = charIndex; for (i = i + 2; i < input.Count; ++i) { temp.Add(input[i]); charIndex += input[i].Length; } r = temp; } else if (isVar && bracketTypesStart.IndexOf(input[i + 1]) != -1) { //It is a function call charIndex += input[i].Length + input[i + 1].Length; int funcIndex = i; i += 2; //Function call starts with an opening bracket, find corresponding end int bracket_count = 1; leftIndex = charIndex; //TODO - rewrite this code with a stack instead of an increment/decrement system while (i < input.Count) { if (bracketTypesStart.IndexOf(input[i]) != -1) { bracket_count++; } if (bracketTypesEnd.IndexOf(input[i]) != -1) { bracket_count--; } charIndex += input[i].Length; if (bracket_count == 0) { break; } temp.Add(input[i]); ++i; } ++i; if (bracket_count == 0) { if (i >= input.Count) { //Input is a function call l = temp; r = null; rightIndex = charInd; exp_Data = input[funcIndex]; exp_Type = Exp_Type.Function; } else if (isOperation(input[i])) { //Function call is only a child node of input temp.Insert(0, input[funcIndex + 1]); temp.Insert(0, input[funcIndex]); temp.Add(input[i - 1]); l = temp; bool isInfix = false, isSuffix = false; if (operInfix.FindIndex(a => a.oper == input[i]) != -1) { isInfix = true; } if (operSuffix.FindIndex(a => a.oper == input[i]) != -1) { isSuffix = true; } exp_Data = input[i]; exp_Type = Exp_Type.Operator; if (isInfix && i + 1 < input.Count) { //It is an infix operation, get right child node temp = new List <string>(); charIndex += input[i].Length; rightIndex = charIndex; for (i = i + 1; i < input.Count; ++i) { temp.Add(input[i]); charIndex += input[i].Length; } r = temp; } else if (isSuffix && i + 1 >= input.Count) { //It is a suffix operation r = null; } else { throw new AST_BadFormatException(input[i] + " is not a suffix operaiton", charIndex); } } else { throw new AST_BadFormatException("Unknown expression sequence", charIndex); } } else { throw new AST_BadFormatException( "Didn\'t find a corresponding closing bracket for " + input[funcIndex + 1], charIndex); } } else if (operPrefix.FindIndex(a => a.oper == input[i]) != -1) { //It is a prefix operation charIndex += input[i].Length; int operIndex = i; ++i; if (bracketTypesStart.IndexOf(input[i]) != -1) { //right child is a compound expression, we should find the right expression charIndex += input[i].Length; ++i; int bracket_count = 1; rightIndex = charIndex; //TODO - rewrite this code with a stack instead of an increment/decrement system while (i < input.Count) { if (bracketTypesStart.IndexOf(input[i]) != -1) { bracket_count++; } if (bracketTypesEnd.IndexOf(input[i]) != -1) { bracket_count--; } charIndex += input[i].Length; if (bracket_count == 0) { break; } temp.Add(input[i]); ++i; } ++i; if (bracket_count == 0) { if (i >= input.Count) { //Everything is good l = null; r = temp; leftIndex = charInd; exp_Data = input[operIndex]; exp_Type = Exp_Type.Operator; } else { throw new AST_BadFormatException("This operation is not supported", charIndex); } } else { throw new AST_BadFormatException( "Didn\'t find a corresponding closing bracket for " + input[operIndex + 1], charIndex); } } else { rightIndex = charIndex; for (; i < input.Count; ++i) { temp.Add(input[i]); charIndex += input[i].Length; } l = null; r = temp; exp_Data = input[operIndex]; exp_Type = Exp_Type.Operator; } } else { throw new AST_BadFormatException("Unknown expression sequence", charIndex); } } if (exp_Type == Exp_Type.None || exp_Data == null) { throw new AST_Exception("Failed to parse expression node", charInd); } if (l != null) { left = new Exp_Node(l, leftIndex); } if (r != null) { right = new Exp_Node(r, rightIndex); } }
public void SetRight(Exp_Node value) { right = value; }
public void SetLeft(Exp_Node value) { left = value; }